#pragma once #include #include #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> 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(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 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(params); } virtual void on_status(const key_value_map ¶ms) { static_cast(params); } virtual void on_property(const key_value_map ¶ms) { static_cast(params); } virtual void on_properties(const key_value_map ¶ms) { static_cast(params); } virtual void on_unknown(const key_value_map ¶ms) { static_cast(params); } virtual void on_debug(const key_value_map ¶ms) { static_cast(params); } }; } // namespace client } // namespace diller