93 lines
3.8 KiB
C++
93 lines
3.8 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, bool remote_transmission_request) const
|
|
{
|
|
return hasitem(canid_rtr_t(can_id, remote_transmission_request));
|
|
}
|
|
bool cbf_cache::hasitem(canid_rtr_t key) const
|
|
{
|
|
return cache_map.find(key) != cache_map.end();
|
|
}
|
|
cbf_cache_item& cbf_cache::getitem(uint32_t can_id, bool remote_transmission_request)
|
|
{
|
|
return cache_map.at(canid_rtr_t(can_id, remote_transmission_request));
|
|
}
|
|
const cbf_cache_item& cbf_cache::getitem(uint32_t can_id, bool remote_transmission_request) const
|
|
{
|
|
return cache_map.at(canid_rtr_t(can_id, remote_transmission_request));
|
|
}
|
|
bool cbf_cache::additem(const cbf_store& storeitem)
|
|
{
|
|
const auto& ret = cache_map.emplace(canid_rtr_t(storeitem.can_id, storeitem.rtr), 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);
|
|
return false;
|
|
}
|
|
const cb_frame& cbf_cache::get_frame(uint32_t can_id, bool remote_transmission_request) const
|
|
{
|
|
return get_frame(canid_rtr_t(can_id, remote_transmission_request));
|
|
}
|
|
const cb_frame& cbf_cache::get_frame(canid_rtr_t key) const
|
|
{
|
|
auto it = cache_map.find(key);
|
|
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)
|
|
{
|
|
auto key = canid_rtr_t(can_id, remote_transmission_request);
|
|
if(!this->hasitem(key))
|
|
return false;
|
|
const auto& cbf = get_frame(key);
|
|
if(cbf.getpublish()) {
|
|
return cbf.send_frame(canbus, extended_id, remote_transmission_request);
|
|
}
|
|
return false;
|
|
for(const auto& kvp : cache_map) {
|
|
const auto& item = kvp.second;
|
|
const auto& cbf = item.get_frame();
|
|
if(cbf.getpublish()) {
|
|
cbf.send_frame(canbus, extended_id, remote_transmission_request);
|
|
}
|
|
}
|
|
}
|
|
// 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
|