48 std::lock_guard<std::mutex> lock(mutex_);
49 cache_filename_ = filename;
57 std::lock_guard<std::mutex> lock(mutex_);
59 cache_[unitID] = profileID;
67 auto it = cache_.find(unitID);
68 if (it != cache_.end()) {
78 std::lock_guard<std::mutex> lock(mutex_);
83 std::string cache_filename_;
84 std::unordered_map<size_t, size_t> cache_;
85 bool cache_updated =
false;
92 if (cache_filename_.empty()) {
93 throw std::runtime_error(
"HP cache filename is not set. Call HPProfileIDCache::setCacheFilename() before saving.");
97 for (
const auto& [unitID, profileID] : cache_) {
98 jarray.emplace_back(json::array{unitID, profileID});
101 std::ofstream ofs(cache_filename_);
103 ofs << json::serialize(jarray);
104 std::cout <<
"Heat pump profile allocation written to cache file '" << cache_filename_ <<
"'\n";
106 std::cout <<
"Error: Heat pump cache file cound not be written! Cannot write to " << cache_filename_ <<
"'\n";
110 void load_from_file() {
112 std::ifstream ifs(cache_filename_);
115 std::stringstream buffer;
116 buffer << ifs.rdbuf();
118 boost::system::error_code ec;
119 json::value jv = json::parse(buffer.str(), ec);
121 throw std::runtime_error(
"Failed to parse JSON cache file: " + ec.message());
123 const json::array& jarray = jv.as_array();
125 for (
const auto& item : jarray) {
126 const json::array& pair = item.as_array();
131 if (pair.size() != 2) {
134 if (!skip && pair[0].is_int64()) {
135 unitID =
static_cast<size_t>(pair[0].as_int64());
136 }
else if (!skip && pair[0].is_uint64()) {
137 unitID =
static_cast<size_t>(pair[0].as_uint64());
141 if (!skip && pair[1].is_int64()) {
142 profileID =
static_cast<size_t>(pair[1].as_int64());
143 }
else if (!skip && pair[1].is_uint64()) {
144 profileID =
static_cast<size_t>(pair[1].as_uint64());
150 std::cerr <<
"Skipping malformed cache entry: " << json::serialize(pair) <<
"\n";
154 cache_[unitID] = profileID;
157 std::cout <<
"Heat pump profile allocation read from cache file '" << cache_filename_ <<
"'\n";
188 std::lock_guard<std::mutex> lock(mutex_);
189 cache_filename_ = filename;
196 void updateCache(
size_t locationID,
const std::string& roof_orientation,
size_t profileID) {
197 std::lock_guard<std::mutex> lock(mutex_);
198 cache_updated =
true;
199 cache_[locationID][roof_orientation] = profileID;
206 std::optional<size_t>
readCache(
size_t locationID,
const std::string& roof_orientation) {
207 std::lock_guard<std::mutex> lock(mutex_);
208 auto loc_it = cache_.find(locationID);
209 if (loc_it != cache_.end()) {
210 auto orient_it = loc_it->second.find(roof_orientation);
211 if (orient_it != loc_it->second.end()) {
212 return orient_it->second;
222 std::lock_guard<std::mutex> lock(mutex_);
227 std::string cache_filename_;
228 std::unordered_map<size_t, std::unordered_map<std::string, size_t>> cache_;
229 bool cache_updated =
false;
232 void save_to_file() {
236 if (cache_filename_.empty()) {
237 throw std::runtime_error(
"PV cache filename is not set. Call PVProfileIDCache::setCacheFilename() before saving.");
241 for (
const auto& [locationID, orientation_map] : cache_) {
242 for (
const auto& [orientation, profileID] : orientation_map) {
243 jarray.emplace_back(json::array{locationID, orientation, profileID});
247 std::ofstream ofs(cache_filename_);
249 ofs << json::serialize(jarray);
250 std::cout <<
"PV profile allocation written to cache file '" << cache_filename_ <<
"'\n";
252 std::cout <<
"Error: PV cache file cound not be written! Cannot write to " << cache_filename_ <<
"'\n";
256 void load_from_file() {
258 std::ifstream ifs(cache_filename_);
261 std::stringstream buffer;
262 buffer << ifs.rdbuf();
264 boost::system::error_code ec;
265 json::value jv = json::parse(buffer.str(), ec);
267 throw std::runtime_error(
"Failed to parse JSON cache file: " + ec.message());
270 const json::array& jarray = jv.as_array();
271 for (
const auto& item : jarray) {
272 const json::array& entry = item.as_array();
274 std::string roof_orientation;
278 if (entry.size() != 3) {
281 if (!skip && entry[0].is_int64()) {
282 locationID =
static_cast<size_t>(entry[0].as_int64());
283 }
else if (!skip && entry[0].is_uint64()) {
284 locationID =
static_cast<size_t>(entry[0].as_uint64());
288 if (!skip && entry[1].is_string()) {
289 roof_orientation = entry[1].as_string().c_str();
293 if (!skip && entry[2].is_int64()) {
294 profileID =
static_cast<size_t>(entry[2].as_int64());
295 }
else if (!skip && entry[2].is_uint64()) {
296 profileID =
static_cast<size_t>(entry[2].as_uint64());
302 std::cerr <<
"Skipping malformed cache entry: " << json::serialize(entry) <<
"\n";
306 cache_[locationID][roof_orientation] = profileID;
309 std::cout <<
"PV profile allocation read from cache file '" << cache_filename_ <<
"'\n";