diff options
Diffstat (limited to 'src/diller_client.h')
-rw-r--r-- | src/diller_client.h | 209 |
1 files changed, 0 insertions, 209 deletions
diff --git a/src/diller_client.h b/src/diller_client.h deleted file mode 100644 index 988db98..0000000 --- a/src/diller_client.h +++ /dev/null @@ -1,209 +0,0 @@ -#pragma once - -#include <SoftwareSerial.h> -#include <Arduino.h> -#include "diller_utils.h" - -namespace diller { -namespace client { - -using namespace diller::utils; - -enum class diller_cmd : uint8_t { - UNKNOWN, - STATUS, - NETWORK, - WLAN, - PROPERTY, - PROPERTIES -}; - -class diller_event_listener { - public: - virtual void on_diller_event(const key_value_map ¶ms) = 0; -}; - -class client { - public: - client() : listener(nullptr) { - } - - virtual ~client() { - } - - virtual void property(const char *property, const char *value, const char *name) = 0; - virtual void property_value(const char *property, const char *value) = 0; - virtual void property_name(const char *property, const char *name) = 0; - virtual void property_description(const char *property, const char *description) = 0; - - void set_diller_event_listener(diller_event_listener *l) { - listener = l; - } - - protected: - diller_event_listener *listener; -}; - -template<typename io_t, typename key_value_map_t = diller::utils::fixed_size_key_value_map<10>> -class client_software_serial : public client { - public: - client_software_serial() : client(), params(), parser(params) { - } - - void wlan(const char *ssid, const char *password) { - io_t::print("wlan ssid="); - escape(ssid); - io_t::print(" password="); - escape(password); - } - - void property(const char *property, const char *value, const char *name) { - io_t::print("property id="); - escape(property); - io_t::print(" value="); - escape(value); - io_t::print(" name="); - escape(name); - } - - void property_value(const char *property, const char *value) { - io_t::print("property id="); - escape(property); - io_t::print(" value="); - escape(value); - } - - void property_name(const char *property, const char *name) { - io_t::print("property id="); - escape(property); - io_t::print(" name="); - escape(name); - } - - void property_description(const char *property, const char *description) { - io_t::print("property id="); - escape(property); - io_t::print(" description="); - escape(description); - } - - void loop() { - auto x = tty.readline(); - // Serial.print("x="); - // Serial.println(static_cast<int>(x)); - if (x == tty_status::FULL_LINE) { - parser.parse(tty.line); - - process_command(); - } - } - - protected: - void escape(const char *s) { - char c; - while ((c = *s++) != '\0') { - if (c == ' ') { - io_t::print('\\'); - } - io_t::print(c); - } - } - - void process_command() { - if (params.is_empty()) { - return; - } - - if (!listener) { - return; - } - - listener->on_diller_event(params); - params.clear(); - } - - private: - // SoftwareSerial serial; - key_value_map_t params; - diller::utils::diller_parser parser; - diller::utils::tty<io_t, 100, 3000, false> tty; -}; - -void print_diller_event(const key_value_map ¶ms) { - if (!params.size()) { - return; - } - - Serial.print("Diller: "); - Serial.print(params.key(0)); - Serial.print(' '); - - for (uint8_t i = 1; i < params.size(); i++) { - auto key = params.key(i); - auto value = params.value(i); - - Serial.print(key); - if (value) { - Serial.print("="); - Serial.print(value); - } - Serial.print(" "); - } - Serial.println(); -} - -class printing_diller_event_listener : public diller_event_listener { - public: - void on_diller_event(const key_value_map ¶ms) { - print_diller_event(params); - } -}; - -class noop_diller_event_listener : public diller_event_listener { - public: - void on_diller_event(const key_value_map ¶ms) { - auto key = params.key(0); - if (strcmp("network", key) == 0) { - on_network(params); - } else if (strcmp("status", key) == 0) { - on_status(params); - } else if (strcmp("property", key) == 0) { - on_property(params); - } else if (strcmp("properties", key) == 0) { - on_properties(params); - } else if (strcmp("debug", key) == 0) { - on_debug(params); - } else { - on_unknown(params); - } - } - - protected: - virtual void on_network(const key_value_map ¶ms) { - static_cast<void>(params); - } - - virtual void on_status(const key_value_map ¶ms) { - static_cast<void>(params); - } - - virtual void on_property(const key_value_map ¶ms) { - static_cast<void>(params); - } - - virtual void on_properties(const key_value_map ¶ms) { - static_cast<void>(params); - } - - virtual void on_unknown(const key_value_map ¶ms) { - static_cast<void>(params); - } - - virtual void on_debug(const key_value_map ¶ms) { - static_cast<void>(params); - } -}; - -} // namespace client -} // namespace diller - |