aboutsummaryrefslogtreecommitdiff
path: root/src/diller/client.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/diller/client.h')
-rw-r--r--src/diller/client.h209
1 files changed, 209 insertions, 0 deletions
diff --git a/src/diller/client.h b/src/diller/client.h
new file mode 100644
index 0000000..d10add9
--- /dev/null
+++ b/src/diller/client.h
@@ -0,0 +1,209 @@
+#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 &params) = 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 &params) {
+ 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 &params) {
+ print_diller_event(params);
+ }
+};
+
+class noop_diller_event_listener : public diller_event_listener {
+ public:
+ void on_diller_event(const key_value_map &params) {
+ 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 &params) {
+ static_cast<void>(params);
+ }
+
+ virtual void on_status(const key_value_map &params) {
+ static_cast<void>(params);
+ }
+
+ virtual void on_property(const key_value_map &params) {
+ static_cast<void>(params);
+ }
+
+ virtual void on_properties(const key_value_map &params) {
+ static_cast<void>(params);
+ }
+
+ virtual void on_unknown(const key_value_map &params) {
+ static_cast<void>(params);
+ }
+
+ virtual void on_debug(const key_value_map &params) {
+ static_cast<void>(params);
+ }
+};
+
+} // namespace client
+} // namespace diller
+