Solar/cbf_cache.cpp
2025-09-03 22:38:18 +02:00

83 lines
3.3 KiB
C++

#include "source/solar/cbf_cache.h"
namespace solar
{
static const int comparecolumns[] = {cbf_store::sisortframe, cbf_store::sisortrtr, 0};
void cbf_cache::clear()
{
cache_map.clear();
}
int cbf_cache::size() const
{
return cache_map.size();
}
bool cbf_cache::hasitem(uint32_t can_id) const
{
return cache_map.find(can_id) != cache_map.end();
}
cbf_cache_item& cbf_cache::getitem(uint32_t can_id)
{
return cache_map.at(can_id);
}
const cbf_cache_item& cbf_cache::getitem(uint32_t can_id) const
{
return cache_map.at(can_id);
}
bool cbf_cache::additem(const cbf_store& storeitem)
{
const auto& ret = cache_map.emplace(storeitem.can_id, cbf_cache_item(storeitem));
if(ret.second) {
return false; // new item inserted, no publish
}
auto& kvp = *ret.first;
auto& item = kvp.second;
auto updateresult = item.update(storeitem, comparecolumns, 1);
if(updateresult & cbf_store::cbf_updateresult::stu_DUPLICATE) {
// ESP_LOGI(item.store0.tag("== ST1 DUP == ").c_str(), item.store0.to_string().c_str());
return updateresult & cbf_store::cbf_updateresult::stu_PUBLISH;
}
// try next store to see if it has a duplicate of new item
updateresult = item.update(storeitem, comparecolumns, 2);
if(updateresult & cbf_store::cbf_updateresult::stu_DUPLICATE) {
// ESP_LOGI(item.store1.tag("== ST2 DUP == ").c_str(), item.store1.to_string().c_str());
return updateresult & cbf_store::cbf_updateresult::stu_PUBLISH;
}
item.update(storeitem);
//cache_map.erase(kvp.first);
//ret = cache_map.emplace(storeitem.can_id, item);
//if(!ret.second) {
// ESP_LOGE(item.store0.tag("== ST1 ERR == ").c_str(), "Error re-inserting item into cache_map");
// ESP_LOGE(item.store1.tag("== ST2 ERR == ").c_str(), "Error re-inserting item into cache_map");
//}
return false;
}
const cb_frame& cbf_cache::get_frame(uint32_t can_id) const
{
auto it = cache_map.find(can_id);
if(it == cache_map.end())
return emptyframe; // return reference to static empty frame if not found
const auto& item = it->second;
return item.get_frame();
}
bool cbf_cache::send_frame(esphome::canbus::Canbus *canbus, uint32_t can_id, bool extended_id, bool remote_transmission_request)
{
if(!this->hasitem(can_id))
return false;
const auto& cbf = get_frame(can_id);
if(cbf.getpublish()) {
return cbf.send_frame(canbus, extended_id, remote_transmission_request);
}
return false;
}
// static version to send arbitrary frame
bool cbf_cache::send_frame(esphome::canbus::Canbus *canbus, uint32_t can_id, const byte_vector& frame, bool extended_id, bool remote_transmission_request)
{
return cb_frame::send_frame(canbus, can_id, frame, extended_id, remote_transmission_request);
}
bool cbf_cache::send_request(esphome::canbus::Canbus *canbus, uint32_t can_id, bool extended_id)
{
return cb_frame::send_frame(canbus, can_id, {}, extended_id, true);
}
} // namespace solar