diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-01-06 13:54:15 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-01-06 13:54:15 +0100 |
commit | a2fe55b5c31e08ca4df165172269e7eb58d75bf6 (patch) | |
tree | 19d2229618e8e6c9bdddda2756d1d11c1bb6843e | |
parent | c3dcee6a94032904c03a4f7daec98fdc81ea2357 (diff) |
add node to gui on get node response
-rw-r--r-- | confui/main.cpp | 6 | ||||
-rw-r--r-- | confui/mainwindow.h | 3 | ||||
-rw-r--r-- | confui/mesh_connector.cpp | 9 | ||||
-rw-r--r-- | confui/mesh_connector.h | 3 | ||||
-rw-r--r-- | confui/serial.cpp | 22 |
5 files changed, 35 insertions, 8 deletions
diff --git a/confui/main.cpp b/confui/main.cpp index a860f28..ae52a61 100644 --- a/confui/main.cpp +++ b/confui/main.cpp @@ -7,10 +7,12 @@ static const uint8_t *_ptest = (uint8_t *)&_test; uint8_t g_cd_endianness; } +CDMainWindow* g_cd_main_window = nullptr; + int main(int argc, char *argv[]) { g_cd_endianness = *_ptest; g_cd_app = new QApplication(argc, argv); - CDMainWindow w; - w.show(); + g_cd_main_window = new CDMainWindow(); + g_cd_main_window->show(); return g_cd_app->exec(); } diff --git a/confui/mainwindow.h b/confui/mainwindow.h index 587c447..4093cd1 100644 --- a/confui/mainwindow.h +++ b/confui/mainwindow.h @@ -10,6 +10,9 @@ class CDAutomationsTabWidget; class CDNodeOverviewTabWidget; +class CDMainWindow; + +extern CDMainWindow *g_cd_main_window; /** * @brief main window diff --git a/confui/mesh_connector.cpp b/confui/mesh_connector.cpp index b7991b7..fb6ac1d 100644 --- a/confui/mesh_connector.cpp +++ b/confui/mesh_connector.cpp @@ -41,6 +41,7 @@ CDMeshConnector::CDMeshConnector() { cd_uid_t CDMeshConnector::create_node(cd_s_node node) { cd_s_node *_node = (cd_s_node *)malloc(sizeof(cd_s_node)); + // TODO: handle empty structs // id cd_uid_t id = get_new_node_id(); @@ -187,7 +188,7 @@ string CDMeshConnector::cd_mac_to_string(cd_mac_addr_t mac) { string CDMeshConnector::cd_uuid_to_string(cd_uuid_t uuid) { char *addr = nullptr; - asprintf(&addr, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", uuid[15], uuid[14], uuid[13], uuid[12], uuid[11], uuid[10], uuid[9], uuid[8], uuid[7], uuid[6], uuid[5], uuid[4], uuid[3], uuid[2], uuid[1], uuid[0]); + asprintf(&addr, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]); string ret = addr; free(addr); return ret; @@ -196,3 +197,9 @@ string CDMeshConnector::cd_uuid_to_string(cd_uuid_t uuid) { cd_s_automation *CDMeshConnector::get_link(cd_link_t id) { return _links[id]; } cd_s_node *CDMeshConnector::get_node(cd_uid_t id) { return _nodes[id]; } + +cd_uid_t CDMeshConnector::get_or_create_node_by_uuid(cd_uuid_t uuid) { + for (pair<cd_uid_t, cd_s_node *> node : _nodes) + if (memcmp(node.second->uuid, uuid, sizeof(cd_uuid_t)) == 0) return node.first; + return create_node({}); +} diff --git a/confui/mesh_connector.h b/confui/mesh_connector.h index 43f78d5..1c9f938 100644 --- a/confui/mesh_connector.h +++ b/confui/mesh_connector.h @@ -29,6 +29,7 @@ enum cd_e_automation_type { typedef struct { cd_uid_t id; /** @brief GUI-specific id (used as handle) */ cd_mac_addr_t address; /** @brief node bluetooth mac address */ + cd_uuid_t uuid; /** @brief node uuid */ size_t name_len; /** @brief name length in bytes */ const char *name; /** @brief user-friendly node name */ bool light_on; /** @brief state of light on node */ @@ -102,6 +103,7 @@ public: virtual cd_s_automation *get_link(cd_link_t id); /** @brief get node pointer by node id */ virtual cd_s_node *get_node(cd_uid_t id); + virtual cd_uid_t get_or_create_node_by_uuid(cd_uuid_t uuid); // network modification functions /** @brief create empty automation */ @@ -159,6 +161,7 @@ public: static string cd_mac_to_string(cd_mac_addr_t mac); /** @brief convert `cd_uuid_t` to `std::string` for printing/GUI */ static string cd_uuid_to_string(cd_uuid_t uuid); + }; /** @brief global pointer to mesh connector, initialized in CDMainWindow */ diff --git a/confui/serial.cpp b/confui/serial.cpp index c972ffb..bfb809e 100644 --- a/confui/serial.cpp +++ b/confui/serial.cpp @@ -2,6 +2,8 @@ #include "../shared/serial_parse.h" #include "../shared/bin.h" #include "../shared/pclient.h" +#include "mainwindow.h" +#include "mesh_connector.h" #include <iostream> #include <QDebug> @@ -122,11 +124,21 @@ void cd_cmd_ping(cd_s_bin* data) { void cd_cmd_response_get_node_parse_node(cd_s_cmd_node* node) { printf("yes i am node with name '%.*s'\n", node->name_len, node->remaining_data); printf("my light is %s and i am%s provisioned\n", node->light_on ? "on" : "off", node->provisioned ? "" : " not"); - // WIP - // cd_gui_node_id_t id = get_or_create_id(node->uuid); - // node* gui_node = g_cd_mesh_connector->get_node(id); - // (fill gui_node) - // (refresh gui) + + cd_uid_t node_id = g_cd_mesh_connector->get_or_create_node_by_uuid(node->uuid); + cd_s_node* gui_node = g_cd_mesh_connector->get_node(node_id); + + memcpy(gui_node->address, node->address, sizeof(cd_mac_addr_t)); + memcpy(gui_node->uuid, node->uuid, sizeof(cd_uuid_t)); + gui_node->name_len = node->name_len; + if (gui_node->name != nullptr) free((char*) gui_node->name); // TODO: set name to non-const pointer + char* name = (char*) malloc(node->name_len); + memcpy(name, node->remaining_data, node->name_len); + gui_node->name = name; + gui_node->light_on = !!node->light_on; + gui_node->provisioned = !!node->provisioned; + + g_cd_main_window->update(); } void cd_cmd_response_get_node(cd_s_bin* data) { |