118 lines
3.9 KiB
C++
118 lines
3.9 KiB
C++
// if filename is changes, remove old .o file from \\TRUENAS\esphome\config\.esphome\build\sthome-ut8\.pioenvs\sthome-ut8\src\source\solar
|
|
#include "source/solar/cbf_cache_item.h"
|
|
|
|
namespace solar
|
|
{
|
|
cbf_cache_item::cbf_cache_item()
|
|
{
|
|
this->store0 = std::make_shared<cbf_store>(0, 0, 1, byte_vector(), false, 0, 0); // make generic empty store
|
|
this->store1 = std::make_shared<cbf_store>(0, 0, 2, byte_vector(), false, 0, 0); // make generic empty store
|
|
ESP_LOGI( this->store0->tag("cache_item CTOR0A ").c_str(), "%-20s %s", "Created cache item", this->store0->to_string().c_str());
|
|
ESP_LOGI( this->store1->tag("cache_item CTOR0B ").c_str(), "%-20s %s", "Created cache item", this->store1->to_string().c_str());
|
|
}
|
|
cbf_cache_item::cbf_cache_item(const cbf_store& store0)
|
|
{
|
|
this->store0 = store0.clone();
|
|
this->store1 = store0.clone();
|
|
this->store0->id = 1;
|
|
this->store1->id = 2;
|
|
}
|
|
void cbf_cache_item::clear()
|
|
{
|
|
store0 = nullptr;
|
|
store1 = nullptr;
|
|
}
|
|
void cbf_cache_item::swap(cbf_cache_item &s)
|
|
{
|
|
std::swap(this->store0, s.store0);
|
|
std::swap(this->store1, s.store1);
|
|
}
|
|
bool cbf_cache_item::update(const cbf_store& newitem)
|
|
{
|
|
if(!this->store0->is_valid()) {
|
|
store0 = newitem.clone();
|
|
store0->id = 1;
|
|
return true;
|
|
}
|
|
if(!this->store1->is_valid()) {
|
|
store1 = newitem.clone();
|
|
store1->id = 2;
|
|
return true;
|
|
}
|
|
bool result = store1->last_timestamp > store0->last_timestamp;
|
|
if(result) {
|
|
store0 = newitem.clone();
|
|
this->store0->id = 1;
|
|
}
|
|
else {
|
|
store1 = newitem.clone();
|
|
this->store1->id = 2;
|
|
}
|
|
return result;
|
|
}
|
|
cbf_store::cbf_updateresult cbf_cache_item::update(const cbf_store& newitem, const int *comparecolumns, int store_selector)
|
|
{
|
|
cbf_store::cbf_updateresult result = cbf_store::cbf_updateresult::stu_NONE;
|
|
switch(store_selector) {
|
|
case 2:
|
|
result = this->store1->update(newitem, comparecolumns);
|
|
break;
|
|
default:
|
|
result = this->store0->update(newitem, comparecolumns);
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
const cb_frame& cbf_cache_item::get_frame() const
|
|
{
|
|
return get_store(); // return most recent valid store's frame
|
|
}
|
|
const cbf_store& cbf_cache_item::get_store() const
|
|
{
|
|
if(this->store0->is_valid() && !this->store1->is_valid()) {
|
|
return *store0;
|
|
}
|
|
if(!this->store0->is_valid() && this->store1->is_valid()) {
|
|
return *store1;
|
|
}
|
|
if(this->store0->is_valid() && this->store1->is_valid()) {
|
|
if(this->store0->last_timestamp >= this->store1->last_timestamp) {
|
|
return *this->store0;
|
|
} else {
|
|
return *this->store1;
|
|
}
|
|
}
|
|
return *this->store0; // no valid stores, but return store0 which is always present
|
|
}
|
|
std::string cbf_cache_item::tag(const std::string& prefix) const
|
|
{
|
|
return get_store().tag(prefix, prefix.length());
|
|
}
|
|
std::string cbf_cache_item::to_string() const
|
|
{
|
|
return get_store().to_string();
|
|
}
|
|
std::string cbf_cache_item::tag0(const std::string& prefix) const
|
|
{
|
|
return store0->tag(prefix);
|
|
}
|
|
std::string cbf_cache_item::tag1(const std::string& prefix) const
|
|
{
|
|
return store1->tag(prefix);
|
|
}
|
|
std::string cbf_cache_item::st0_tostring() const
|
|
{
|
|
char buffer[150];
|
|
snprintf(buffer, sizeof(buffer), "ST1: [%s] %s", trim(tag0()).c_str(), store0->to_string().c_str());
|
|
return std::string(buffer);
|
|
}
|
|
std::string cbf_cache_item::st1_tostring() const
|
|
{
|
|
char buffer[150];
|
|
snprintf(buffer, sizeof(buffer), "ST2: [%s] %s", trim(tag1()).c_str(), store1->to_string().c_str());
|
|
return std::string(buffer);
|
|
}
|
|
|
|
} // namespace solar
|
|
|