Moved TRMNL stuff to its own file
This commit is contained in:
232
TRMNL.cpp
Normal file
232
TRMNL.cpp
Normal file
@@ -0,0 +1,232 @@
|
||||
#include "TRMNL.h"
|
||||
|
||||
using std::list;
|
||||
using std::map;
|
||||
using std::optional;
|
||||
using std::string;
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
TRMNL::TRMNL(const string& id,
|
||||
const string& api_key,
|
||||
const string& friendly_id,
|
||||
int refresh_rate)
|
||||
: m_id(id),
|
||||
m_api_key(api_key),
|
||||
m_friendly_id(friendly_id),
|
||||
m_refresh_rate(refresh_rate),
|
||||
m_update_handler()
|
||||
{}
|
||||
|
||||
const string& TRMNL::id() const
|
||||
{
|
||||
return m_id;
|
||||
}
|
||||
|
||||
const string& TRMNL::api_key() const
|
||||
{
|
||||
return m_api_key;
|
||||
}
|
||||
|
||||
const string& TRMNL::friendly_id() const
|
||||
{
|
||||
return m_friendly_id;
|
||||
}
|
||||
|
||||
int TRMNL::refresh_rate() const
|
||||
{
|
||||
return m_refresh_rate;
|
||||
}
|
||||
|
||||
void TRMNL::id(const std::string& id)
|
||||
{
|
||||
m_id = id;
|
||||
if (m_update_handler)
|
||||
{
|
||||
m_update_handler(*this);
|
||||
}
|
||||
}
|
||||
|
||||
void TRMNL::api_key(const std::string& api_key)
|
||||
{
|
||||
m_api_key = api_key;
|
||||
if (m_update_handler)
|
||||
{
|
||||
m_update_handler(*this);
|
||||
}
|
||||
}
|
||||
|
||||
void TRMNL::friendly_id(const std::string& friendly_id)
|
||||
{
|
||||
m_friendly_id = friendly_id;
|
||||
if (m_update_handler)
|
||||
{
|
||||
m_update_handler(*this);
|
||||
}
|
||||
}
|
||||
|
||||
void TRMNL::refresh_rate(int refresh_rate)
|
||||
{
|
||||
m_refresh_rate = refresh_rate;
|
||||
if (m_update_handler)
|
||||
{
|
||||
m_update_handler(*this);
|
||||
}
|
||||
}
|
||||
|
||||
void TRMNL::set_update_handler(std::function<void (const TRMNL& trmnl)> handler)
|
||||
{
|
||||
m_update_handler = handler;
|
||||
}
|
||||
|
||||
string TRMNL::friendly_from_id(string id)
|
||||
{
|
||||
size_t pos = id.find(':');
|
||||
while (pos != string::npos)
|
||||
{
|
||||
id.erase(pos, 1);
|
||||
pos = id.find(':');
|
||||
}
|
||||
|
||||
if (id.size() <= 6)
|
||||
{
|
||||
return id;
|
||||
}
|
||||
else
|
||||
{
|
||||
return id.substr(id.size() - 6);
|
||||
}
|
||||
}
|
||||
|
||||
void to_json(json& j, const TRMNL& trmnl)
|
||||
{
|
||||
j = json{
|
||||
{"ID", trmnl.m_id},
|
||||
{"api_key", trmnl.m_api_key},
|
||||
{"friendly_id", trmnl.m_friendly_id},
|
||||
{"refresh_rate", trmnl.m_refresh_rate},
|
||||
};
|
||||
}
|
||||
|
||||
void from_json(const json& j, TRMNL& trmnl)
|
||||
{
|
||||
bool updated = false;
|
||||
try
|
||||
{
|
||||
j.at("ID").get_to(trmnl.m_id);
|
||||
updated = true;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{}
|
||||
|
||||
try
|
||||
{
|
||||
j.at("api_key").get_to(trmnl.m_api_key);
|
||||
updated = true;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{}
|
||||
|
||||
try
|
||||
{
|
||||
j.at("friendly_id").get_to(trmnl.m_friendly_id);
|
||||
updated = true;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{}
|
||||
|
||||
try
|
||||
{
|
||||
j.at("refresh_rate").get_to(trmnl.m_refresh_rate);
|
||||
updated = true;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{}
|
||||
|
||||
if (updated && trmnl.m_update_handler)
|
||||
{
|
||||
trmnl.m_update_handler(trmnl);
|
||||
}
|
||||
}
|
||||
|
||||
void TRMNLContainer::TRMNL_update_handler(const TRMNL& trmnl)
|
||||
{
|
||||
auto it = m_by_id.find(trmnl.m_id);
|
||||
if (it == m_by_id.end())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// An update is needed only if friendly ID changes
|
||||
if (0 == m_by_friendly.count(trmnl.m_friendly_id))
|
||||
{
|
||||
m_by_friendly[trmnl.m_friendly_id] = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
void TRMNLContainer::add_device(TRMNL trmnl)
|
||||
{
|
||||
if (trmnl.m_id.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
trmnl.set_update_handler([this](const TRMNL& trmnl){ TRMNL_update_handler(trmnl); });
|
||||
auto it = m_devices.insert(m_devices.end(), trmnl);
|
||||
|
||||
m_by_id[trmnl.m_id] = it;
|
||||
|
||||
if (!trmnl.m_friendly_id.empty())
|
||||
{
|
||||
m_by_friendly[trmnl.m_friendly_id] = it;
|
||||
}
|
||||
}
|
||||
|
||||
TRMNL* TRMNLContainer::get_device_by_id(const string& id)
|
||||
{
|
||||
TRMNL* result = nullptr;
|
||||
|
||||
if (0 != m_by_id.count(id))
|
||||
{
|
||||
result = &(*m_by_id[id]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
TRMNL* TRMNLContainer::get_device_by_friendly(const string& friendly)
|
||||
{
|
||||
TRMNL* result = nullptr;
|
||||
|
||||
if (0 != m_by_friendly.count(friendly))
|
||||
{
|
||||
result = &(*m_by_friendly[friendly]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void TRMNLContainer::clear()
|
||||
{
|
||||
m_by_friendly.clear();
|
||||
m_by_id.clear();
|
||||
m_devices.clear();
|
||||
}
|
||||
|
||||
void to_json(json& j, const TRMNLContainer& cont)
|
||||
{
|
||||
j = cont.m_devices;
|
||||
}
|
||||
|
||||
void from_json(const json& j, TRMNLContainer& cont)
|
||||
{
|
||||
if (!j.is_array())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < j.size(); ++i)
|
||||
{
|
||||
cont.add_device(j[i]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user