111 lines
4.6 KiB
C++
111 lines
4.6 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->clear();
|
|
this->store1->id = 2;
|
|
//ESP_LOGI( this->store0->tag("cache_item CTOR1A ").c_str(), "%-20s %s", "Created cache item", this->store0->to_string().c_str());
|
|
//ESP_LOGI( this->store1->tag("cache_item CTOR1B ").c_str(), "%-20s %s", "Created cache item", this->store1->to_string().c_str());
|
|
}
|
|
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)
|
|
{
|
|
//std::string posttag0 = std::string("cache_item UPD ST1");
|
|
//std::string posttag1 = std::string("cache_item UPD ST2");
|
|
// ESP_LOGI(store0->tag(posttag0).c_str(), "VALID: %s %s", store0->is_valid() ? "Y" : "N", store0->to_string().c_str());
|
|
// ESP_LOGI(store1->tag(posttag1).c_str(), "VALID: %s %s", store1->is_valid() ? "Y" : "N", store1->to_string().c_str());
|
|
if(!this->store0->is_valid()) {
|
|
store0 = newitem.clone();
|
|
store0->id = 1;
|
|
// ESP_LOGI(store0->tag("== ST1 INV NEW ==").c_str(), store0->to_string().c_str());
|
|
return true;
|
|
}
|
|
if(!this->store1->is_valid()) {
|
|
store1 = newitem.clone();
|
|
store1->id = 2;
|
|
// ESP_LOGI(store1->tag("== ST2 INV NEW ==").c_str(), store1->to_string().c_str());
|
|
return true;
|
|
}
|
|
bool result = store1->last_timestamp > store0->last_timestamp;
|
|
if(result) {
|
|
store0 = newitem.clone();
|
|
this->store0->id = 1;
|
|
// ESP_LOGI(store0->tag("== ST1 OLD NEW ==").c_str(), store0->to_string().c_str());
|
|
}
|
|
else {
|
|
store1 = newitem.clone();
|
|
this->store1->id = 2;
|
|
// ESP_LOGI(store1->tag("== OLD ST2 NEW ==").c_str(), store1->to_string().c_str());
|
|
}
|
|
return result;
|
|
}
|
|
bool cbf_cache_item::update(const cbf_store& newitem, bool& publish, const int *comparecolumns, int store_selector)
|
|
{
|
|
bool result = false;
|
|
switch(store_selector) {
|
|
case 2:
|
|
result = this->store1->update(newitem, publish, comparecolumns);
|
|
break;
|
|
default:
|
|
result = this->store0->update(newitem, publish, comparecolumns);
|
|
break;
|
|
}
|
|
//std::string posttag = std::string("cache_item ") + (result ? "UPD" : "NUP") + " ST" + std::to_string(store_selector) + " ";
|
|
//if(store_selector == 1 ) {
|
|
// ESP_LOGI(store1->tag(posttag).c_str(), store1->to_string().c_str());
|
|
//} else {
|
|
// ESP_LOGI(store0->tag(posttag).c_str(), store0->to_string().c_str());
|
|
//}
|
|
return result;
|
|
}
|
|
std::string cbf_cache_item::tag(const std::string& prefix) const
|
|
{
|
|
char tag[24];
|
|
snprintf(tag, sizeof(tag), "%-18s ", prefix.c_str());
|
|
return std::string(tag);
|
|
}
|
|
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::to_string() const
|
|
{
|
|
// Be extra careful with the placement of printf format specifiers and their corresponding arguments.
|
|
// Incorrect placement / misaligned arguments can lead to undefined behavior and processor crashes.
|
|
char buffer[180];
|
|
// trim
|
|
auto trimmedtag0 = trim(tag0());
|
|
auto trimmedtag1 = trim(tag1());
|
|
snprintf(buffer, sizeof(buffer), "ST1: [%s] %s | ST2: [%s] %s", trimmedtag0.c_str(), store0->to_string().c_str(), trimmedtag1.c_str(), store1->to_string().c_str());
|
|
return std::string(buffer);
|
|
}
|
|
|
|
} // namespace solar
|
|
|