diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-01-06 15:29:26 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-01-06 15:29:26 +0100 |
commit | b155a5fa4b97f6f5f63f7079c671b5965db9d022 (patch) | |
tree | d089785ae1044077bb3f8e83a9dd233fbe3c2c9c | |
parent | 62fedb0f2992222e340b62dea7f976c9bff3063e (diff) |
send POST_LINK
-rw-r--r-- | confui/mesh_connector.cpp | 26 | ||||
-rw-r--r-- | confui/mesh_connector.h | 21 | ||||
-rw-r--r-- | confui/ui_automation.cpp | 4 | ||||
-rw-r--r-- | confui/ui_node.cpp | 2 | ||||
-rw-r--r-- | shared/pclient.c | 9 | ||||
-rw-r--r-- | shared/protocol.h | 8 |
6 files changed, 43 insertions, 27 deletions
diff --git a/confui/mesh_connector.cpp b/confui/mesh_connector.cpp index 2630da6..c77c159 100644 --- a/confui/mesh_connector.cpp +++ b/confui/mesh_connector.cpp @@ -4,6 +4,7 @@ #include <cstring> #include <stdio.h> +#include "../shared/pclient.h" #include "mesh_connector.h" using std::pair; @@ -99,7 +100,7 @@ map<cd_link_t, cd_s_automation *> CDMeshConnector::get_links(bool valid) { return links; } -void CDMeshConnector::update_link(cd_s_automation *automation) { +void CDMeshConnector::update_link(cd_s_automation *automation, bool publish) { printf("link[%d]", automation->id); if (automation->valid) { printf(" = %.*s %s %.*s", (int)automation->button->name_len, automation->button->name, @@ -111,9 +112,16 @@ void CDMeshConnector::update_link(cd_s_automation *automation) { printf(" (invalid)"); } printf("\n"); + + if (!publish) return; + if (!automation->valid) return; + + cd_s_bin* msg = cd_cmd_gen_post_link_add(automation->button->uuid, automation->light->uuid, automation->type); + cd_pclient_send(msg); + free(msg); } -cd_link_t CDMeshConnector::create_link(cd_uid_t button, cd_uid_t light, enum cd_e_automation_type type) { +cd_link_t CDMeshConnector::create_link(cd_uid_t button, cd_uid_t light, cd_e_automation_type type) { cd_link_t id = get_new_link_id(); cd_s_automation *automation = (cd_s_automation *)malloc(sizeof(cd_s_automation)); @@ -147,11 +155,17 @@ cd_link_t CDMeshConnector::create_link() { return id; } -void CDMeshConnector::remove_link(cd_link_t link_handle) { +void CDMeshConnector::remove_link(cd_link_t link_handle, bool publish) { printf("remove link[%d]\n", link_handle); - if (_links[link_handle] != nullptr) free(_links[link_handle]); + if (_links.count(link_handle) == 0) return; // invalid handle + if (_links[link_handle] == nullptr) return; // already removed link + + cd_s_bin* msg = cd_cmd_gen_post_link_rm(_links[link_handle]->button->uuid, _links[link_handle]->light->uuid); + cd_pclient_send(msg); + free(msg); + + free(_links[link_handle]); _links[link_handle] = nullptr; - return; } void CDMeshConnector::remove_node(cd_uid_t node_handle) { @@ -161,7 +175,7 @@ void CDMeshConnector::remove_node(cd_uid_t node_handle) { return; } -void CDMeshConnector::update_node(cd_s_node *node_ptr) { +void CDMeshConnector::update_node(cd_s_node *node_ptr, bool publish) { printf("turning %.*s %s\n", (int)node_ptr->name_len, node_ptr->name, node_ptr->light_on ? "on" : "off"); return; } diff --git a/confui/mesh_connector.h b/confui/mesh_connector.h index 64ab471..8c1fc91 100644 --- a/confui/mesh_connector.h +++ b/confui/mesh_connector.h @@ -18,12 +18,10 @@ typedef uint32_t cd_uid_t; /** @brief link/automation id type */ typedef uint32_t cd_link_t; -/** @brief automation types/actions */ -enum cd_e_automation_type { - CD_AUTOMATION_TYPE_TOGGLE, /** @brief button toggles light */ - CD_AUTOMATION_TYPE_TURN_ON, /** @brief button always turns on light (regardless of previous state) */ - CD_AUTOMATION_TYPE_TURN_OFF, /** @brief button always turns off light (regardless of previous state) */ -}; +typedef cd_e_cmd_link_type cd_e_automation_type; +#define CD_AUTOMATION_TYPE_TOGGLE CD_CMD_LINK_TYPE_TOGGLE +#define CD_AUTOMATION_TYPE_TURN_ON CD_CMD_LINK_TYPE_TURN_ON +#define CD_AUTOMATION_TYPE_TURN_OFF CD_CMD_LINK_TYPE_TURN_OFF /** @brief GUI node representation */ typedef struct { @@ -117,7 +115,7 @@ public: * @param light node id for node whose light will be used for this automation. * @param action action/automation type (toggle, on, off). */ - virtual cd_link_t create_link(cd_uid_t button, cd_uid_t light, enum cd_e_automation_type action); + virtual cd_link_t create_link(cd_uid_t button, cd_uid_t light, cd_e_automation_type action); /** * @brief overwrite link id with new automation and update on network. * @@ -126,14 +124,16 @@ public: * properties. * * @param automation pointer to automation struct (with new/modified values) + * @param publish `true` to send POST_LINK command */ - virtual void update_link(cd_s_automation *automation); + virtual void update_link(cd_s_automation *automation, bool publish = false); /** * @brief remove automation and update on network. * * @param link_handle automation id + * @param publish `true` to send POST_LINK command */ - virtual void remove_link(cd_link_t link_handle); + virtual void remove_link(cd_link_t link_handle, bool publish = false); /** * @brief overwrite node id with new node and update on network. @@ -142,8 +142,9 @@ public: * allocated using malloc()). used to update existing node properties. * * @param node_ptr pointer to node struct (with new/modified state) + * @param publish `true` to send POST_LED command */ - virtual void update_node(cd_s_node *node_ptr); + virtual void update_node(cd_s_node *node_ptr, bool publish = false); /** * @brief provision node into network * diff --git a/confui/ui_automation.cpp b/confui/ui_automation.cpp index eb6e1f6..2ade734 100644 --- a/confui/ui_automation.cpp +++ b/confui/ui_automation.cpp @@ -74,11 +74,11 @@ void CDAutomationWidget::apply() { if (!conf_valid()) return; _automation->button = g_cd_mesh_connector->get_node(dropdown_button->findData(dropdown_button->currentIndex())); - _automation->type = (enum cd_e_automation_type)dropdown_action->findData(dropdown_action->currentIndex()); + _automation->type = (cd_e_automation_type) dropdown_action->findData(dropdown_action->currentIndex()); _automation->light = g_cd_mesh_connector->get_node(dropdown_light->findData(dropdown_light->currentIndex())); _automation->valid = true; - g_cd_mesh_connector->update_link(_automation); + g_cd_mesh_connector->update_link(_automation, true); } void CDAutomationWidget::remove() { diff --git a/confui/ui_node.cpp b/confui/ui_node.cpp index 552b62a..56ff004 100644 --- a/confui/ui_node.cpp +++ b/confui/ui_node.cpp @@ -54,7 +54,7 @@ void CDNodeWidget::toggle_provision() { void CDNodeWidget::update_led(bool on) { _node->light_on = on; - g_cd_mesh_connector->update_node(_node); + g_cd_mesh_connector->update_node(_node, true); update(); } diff --git a/shared/pclient.c b/shared/pclient.c index 728bfa0..ff2315c 100644 --- a/shared/pclient.c +++ b/shared/pclient.c @@ -45,8 +45,8 @@ cd_s_bin* cd_cmd_gen_post_link_add(cd_uuid_t button, cd_uuid_t light, cd_e_cmd_l msg->opcode = CD_CMD_POST_LINK; msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); msg->add = true; - memcpy(&msg->button, &button, sizeof(cd_uuid_t)); - memcpy(&msg->led, &light, sizeof(cd_uuid_t)); + memcpy(msg->button, button, sizeof(cd_uuid_t)); + memcpy(msg->led, light, sizeof(cd_uuid_t)); msg->type = type; return bin; @@ -58,8 +58,9 @@ cd_s_bin* cd_cmd_gen_post_link_rm(cd_uuid_t button, cd_uuid_t light) { msg->opcode = CD_CMD_POST_LINK; msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); msg->add = false; - memcpy(&msg->button, &button, sizeof(cd_uuid_t)); - memcpy(&msg->led, &light, sizeof(cd_uuid_t)); + memcpy(msg->button, button, sizeof(cd_uuid_t)); + memcpy(msg->led, light, sizeof(cd_uuid_t)); + msg->type = 0; return bin; } diff --git a/shared/protocol.h b/shared/protocol.h index cad9a1c..b9a2c93 100644 --- a/shared/protocol.h +++ b/shared/protocol.h @@ -60,9 +60,9 @@ typedef struct { } cd_s_cmd_post_led; typedef enum { - CD_CMD_LINK_TYPE_TOGGLE = 0x00, - CD_CMD_LINK_TYPE_TURN_ON = 0x01, - CD_CMD_LINK_TYPE_TURN_OFF = 0x02, + CD_CMD_LINK_TYPE_TOGGLE = 0x00, /** @brief button toggles light */ + CD_CMD_LINK_TYPE_TURN_ON = 0x01, /** @brief button always turns on light (regardless of previous state) */ + CD_CMD_LINK_TYPE_TURN_OFF = 0x02, /** @brief button always turns off light (regardless of previous state) */ } cd_e_cmd_link_type; typedef struct { @@ -71,7 +71,7 @@ typedef struct { cd_uuid_t button; /** @brief uuid of button node */ cd_uuid_t led; /** @brief uuid of led node */ cd_cmd_bool_t add; /** @brief `true` to create/overwrite link, `false` to remove link */ - cd_e_cmd_link_type type; /** @brief link type */ + uint8_t type; /** @brief link type cd_e_cmd_link_type */ } cd_s_cmd_post_link; typedef struct { |