]> mj.ucw.cz Git - home-hw.git/commitdiff
Clock host: Decoded keys are passed to MQTT
authorMartin Mares <mj@ucw.cz>
Sun, 14 May 2023 12:59:38 +0000 (14:59 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 14 May 2023 12:59:38 +0000 (14:59 +0200)
MQTT
clock/host/burrow-clock.c

diff --git a/MQTT b/MQTT
index 96f1af87bd8e3ff50996ba2d97d892a99ba37b81..3847b224ead6e48fd714685d16e2dd7b3d51c494 100644 (file)
--- a/MQTT
+++ b/MQTT
@@ -56,6 +56,8 @@ burrow/lights/catarium/bottom 0-1 [intensity] 0-1 [warm-cold]
 burrow/lights/rainbow/<led>    0-1 [R] 0-1 [G] 0-1 [B] optional-owner
 burrow/lights/rainbow/brightness       0-1
 
+burrow/control/catarium-ir     key-name
+
 bsb/stats/*
 bsb/frame                      hex dump of raw frames received
 
@@ -68,5 +70,4 @@ status/influx                 ok/dead
 status/iris                    ok/dead
 status/loft-ssr                        ok/dead
 status/power-meter             ok/dead
-status/prometheus              ok/dead         # Obsolete
 status/rainbow                 ok/dead
index d0a99de5f9714591c89d3c3a22bed78f9f541eaf..311bd3336f5442dbbe2b85fafa58aec97cfcc732 100644 (file)
@@ -49,6 +49,22 @@ static void mqtt_publish(const char *topic, const char *fmt, ...)
        va_end(args);
 }
 
+static void mqtt_publish_ephemeral(const char *topic, const char *fmt, ...)
+{
+       va_list args;
+       va_start(args, fmt);
+       char m[256];
+       int l = vsnprintf(m, sizeof(m), fmt, args);
+       if (mosquitto_publish(mosq, NULL, topic, l, m, 0, false) != MOSQ_ERR_SUCCESS)
+               msg(L_ERROR, "Mosquitto: publish failed");
+       va_end(args);
+}
+
+static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message)
+{
+       msg(L_DEBUG, "MQTT(%d): %s", level, message);
+}
+
 static void mqtt_conn_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int status)
 {
        if (!status) {
@@ -65,17 +81,89 @@ static void mqtt_init(void)
                die("Mosquitto: initialization failed");
 
        mosquitto_connect_callback_set(mosq, mqtt_conn_callback);
+       mosquitto_log_callback_set(mosq, mqtt_log_callback);
 
        if (mosquitto_will_set(mosq, "status/clock", 4, "dead", 0, true) != MOSQ_ERR_SUCCESS)
                die("Mosquitto: unable to set will");
 
-       if (mosquitto_connect_async(mosq, "10.32.184.5", 1883, 60) != MOSQ_ERR_SUCCESS)
-               die("Mosquitto: connect failed");
+       if (mosquitto_tls_set(mosq, "/etc/burrow-mqtt/ca.crt", NULL, "/etc/burrow-mqtt/client.crt", "/etc/burrow-mqtt/client.key", NULL) != MOSQ_ERR_SUCCESS)
+               die("Mosquitto: Unable to set TLS parameters");
+
+       if (mosquitto_connect_async(mosq, "burrow-mqtt", 8883, 60) != MOSQ_ERR_SUCCESS)
+               die("Mosquitto: Unable to connect");
 
        if (mosquitto_loop_start(mosq))
                die("Mosquitto: cannot start service thread");
 }
 
+/*** IR remote ***/
+
+static const struct {
+       u32 code;
+       const char * const name;
+} key_table[] = {
+       { 0x4bc0ab54, "1" },
+       { 0x4bc06b94, "2" },
+       { 0x4bc0eb14, "3" },
+       { 0x4bc01be4, "4" },
+       { 0x4bc09b64, "5" },
+       { 0x4bc05ba4, "6" },
+       { 0x4bc0db24, "7" },
+       { 0x4bc03bc4, "8" },
+       { 0x4bc0bb44, "9" },
+       { 0x4bc0fb04, ">10" },
+       { 0x4bc07b84, "10/0" },
+       { 0x4bc07986, "clr" },
+       { 0x4b2010ef, "play-mode" },
+       { 0x4b20817e, "group/search" },
+       { 0x4bc0926d, "memory" },
+       { 0x4b20629d, "enter" },
+       { 0x4b2051ae, "band" },
+       { 0x4b90cb34, "fm-mode" },
+       { 0x4bc000ff, "preset+" },
+       { 0x4bc0807f, "preset-" },
+       { 0x4b9002fd, "tuning-up" },
+       { 0x4b90827d, "tuning-down" },
+       { 0x4b207887, "input-up" },
+       { 0x4b20f807, "input-down" },
+       { 0x4bc0a956, "dimmer" },
+       { 0x4bc040bf, "volume-up" },
+       { 0x4bc0c03f, "volume-down" },
+       { 0x4b20aa55, "display" },
+       { 0x4bc0a05f, "muting" },
+       { 0x4bc0f807, "pause" },
+       { 0x4bc0d827, "play" },
+       { 0x4bc038c7, "stop" },
+       { 0x4bc0d12e, "rewind" },
+       { 0x4bc051ae, "ffwd" },
+       { 0x4bc07887, "prev-song" },
+       { 0x4bc0b847, "next-song" },
+       { 0x4b20ea15, "random" },
+       { 0x4b206a95, "repeat" },
+       { 0x4b2048b7, "scroll" },
+       { 0x4b904ab5, "playlist-up" },
+       { 0x4b90ca35, "playlist-down" },
+       { 0x4b900af5, "album-up" },
+       { 0x4b908a75, "album-down" },
+       { 0x4b905aa5, "menu" },
+       { 0x4b90da25, "select" },
+       { 0x4b9041be, "list-up" },
+       { 0x4b90c13e, "list-down" },
+};
+
+static void report_ir_key(u32 key_code)
+{
+       for (uint i = 0; i < (uint) ARRAY_SIZE(key_table); i++) {
+               if (key_table[i].code == key_code) {
+                       msg(L_DEBUG, "IR key %08x: %s", key_code, key_table[i].name);
+                       mqtt_publish_ephemeral("burrow/control/catarium-ir", key_table[i].name);
+                       return;
+               }
+       }
+
+       msg(L_WARN, "IR key %08x not recognized", key_code);
+}
+
 /*** USB ***/
 
 static void clock_timer_handler(struct main_timer *timer);
@@ -203,7 +291,7 @@ static void rx_callback(struct libusb_transfer *xfer)
                msg(L_ERROR, "Short RX transfer");
        } else {
                u32 key = get_u32_be(dev->rx_buffer);
-               msg(L_INFO, "IR key %08x", key);
+               report_ir_key(key);
        }
 
        rx_submit(dev);