Moved TRMNL stuff to its own file
This commit is contained in:
73
TRMNL.h
Normal file
73
TRMNL.h
Normal file
@@ -0,0 +1,73 @@
|
||||
#ifndef TRMNL_H_
|
||||
#define TRMNL_H_
|
||||
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "json.hpp"
|
||||
|
||||
class TRMNL
|
||||
{
|
||||
private:
|
||||
std::string m_id;
|
||||
std::string m_api_key;
|
||||
std::string m_friendly_id;
|
||||
int m_refresh_rate;
|
||||
|
||||
std::function<void (const TRMNL& trmnl)> m_update_handler;
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
TRMNL(const std::string& id = "",
|
||||
const std::string& api_key = "",
|
||||
const std::string& friendly_id = "",
|
||||
int refresh_rate = 600);
|
||||
|
||||
// Getters
|
||||
const std::string& id() const;
|
||||
const std::string& api_key() const;
|
||||
const std::string& friendly_id() const;
|
||||
int refresh_rate() const;
|
||||
|
||||
// Setters
|
||||
void id(const std::string& id);
|
||||
void api_key(const std::string& api_key);
|
||||
void friendly_id(const std::string& friendly_id);
|
||||
void refresh_rate(int refresh_rate);
|
||||
|
||||
void set_update_handler(std::function<void (const TRMNL& trmnl)> handler);
|
||||
|
||||
static std::string friendly_from_id(std::string id);
|
||||
|
||||
friend void to_json(nlohmann::json& j, const TRMNL& trmnl);
|
||||
friend void from_json(const nlohmann::json& j, TRMNL& trmnl);
|
||||
friend class TRMNLContainer;
|
||||
};
|
||||
|
||||
class TRMNLContainer
|
||||
{
|
||||
private:
|
||||
std::list<TRMNL> m_devices;
|
||||
std::map<std::string, std::list<TRMNL>::iterator> m_by_id;
|
||||
std::map<std::string, std::list<TRMNL>::iterator> m_by_friendly;
|
||||
|
||||
void TRMNL_update_handler(const TRMNL& trmnl);
|
||||
|
||||
public:
|
||||
TRMNLContainer() = default;
|
||||
|
||||
void add_device(TRMNL trmnl);
|
||||
|
||||
TRMNL* get_device_by_id(const std::string& id);
|
||||
TRMNL* get_device_by_friendly(const std::string& friendly);
|
||||
|
||||
void clear();
|
||||
|
||||
friend void to_json(nlohmann::json& j, const TRMNLContainer& cont);
|
||||
friend void from_json(const nlohmann::json& j, TRMNLContainer& cont);
|
||||
};
|
||||
|
||||
#endif // TRMNL_H_
|
||||
Reference in New Issue
Block a user