Unify Framework Lib 1.6.0
unify_node_state_monitor.hpp
Go to the documentation of this file.
1/******************************************************************************
2 * # License
3 * <b>Copyright 2022 Silicon Laboratories Inc. www.silabs.com</b>
4 ******************************************************************************
5 * The licensor of this software is Silicon Laboratories Inc. Your use of this
6 * software is governed by the terms of Silicon Labs Master Software License
7 * Agreement (MSLA) available at
8 * www.silabs.com/about-us/legal/master-software-license-agreement. This
9 * software is distributed to you in Source Code format and is governed by the
10 * sections of the MSLA applicable to Source Code.
11 *
12 *****************************************************************************/
13
27#ifndef UNIFY_NODE_STATE_MONITOR_H
28#define UNIFY_NODE_STATE_MONITOR_H
29#include "sys/clock.h"
30#include "ctimer.h"
31
32#include <string>
33#include <vector>
34#include <unordered_map>
35#include <unordered_set>
36#include <nlohmann/json.hpp>
38{
40using endpoint_id_t = uint16_t;
41
45 = 1 * CLOCK_SECOND;
49 = 1 * CLOCK_SECOND;
50
55{
56 public:
58 std::string cluster_name;
60 std::unordered_set<std::string> attributes;
62 std::unordered_set<std::string> supported_commands;
64 std::unordered_set<std::string> generated_commands;
70 explicit cluster(const std::string &name) :
71 cluster_name(name) {};
72
78 std::string to_string() const;
79};
80
85{
86 public:
91 std::unordered_map<std::string, cluster> clusters;
97 explicit endpoint(endpoint_id_t id) : endpoint_id(id) {};
106 cluster &emplace_cluster(const std::string &cluster_name);
107
113 std::string to_string() const;
114};
115
119class node
120{
121 public:
123 std::string unid;
125 uint32_t state = 4;
130 std::unordered_map<endpoint_id_t, endpoint> endpoints;
141 explicit node(const std::string &_unid) : unid(_unid)
142 {
144 };
145
155
161 std::string to_string() const;
162};
163
171{
172 public:
181 virtual void on_unify_node_added(const node &node) = 0;
182
189 virtual void on_unify_node_removed(const std::string &unid) = 0;
190
199 virtual void on_unify_node_state_changed(const node &node) = 0;
200
201 virtual ~node_state_monitor_interface() = default;
202};
203
222{
223 public:
230 {
231 // Singleton instance
232 static node_state_monitor instance;
233 return instance;
234 };
235
241 const std::unordered_map<std::string, node> &get_nodes() const
242 {
243 return nodes;
244 };
245
258
269
276 {
277 interface = _interface;
278 }
279
280 private:
281 // Map of all nodes <UNID, node>
282 std::unordered_map<std::string, node> nodes;
283 // Set of nodes pending a on_unify_node_added callback.
284 std::unordered_set<std::string> nodes_pending_node_added;
285 // Timer used for delaying on_unify_node_added callback.
287 // Threshold for delaying on_unify_node_added, the time a node shall have been
288 // unchanged before a on_unify_node_added callback is issued.
291 // Timeout for the timer that monitors node change thresholds.
294
303 static void on_mqtt_node_state_message(const char *topic,
304 const char *message,
305 const size_t message_length);
306
316 const char *topic, const char *message, const size_t message_length);
317
327 const char *topic, const char *message, const size_t message_length);
328
337 static void on_mqtt_cluster_attributes(const char *topic,
338 const char *message,
339 const size_t message_length);
340
345 node &emplace_node(const std::string &unid);
346
354 void on_node_state_changed(const std::string &unid,
355 const nlohmann::json &jsn);
362 void on_node_removed(const std::string &unid);
363
373 void on_cluster_attribute_added(const std::string &unid,
374 endpoint_id_t endpoint_id,
375 const std::string &cluster_name,
376 const std::string &attribute);
377
387 void on_cluster_attribute_removed(const std::string &unid,
388 endpoint_id_t endpoint_id,
389 const std::string &cluster_name,
390 const std::string &attribute);
391
402 void on_cluster_supported_commands_added(const std::string &unid,
403 endpoint_id_t endpoint_id,
404 const std::string &cluster_name,
405 const nlohmann::json &jsn);
406
416 void on_cluster_supported_commands_removed(const std::string &unid,
417 endpoint_id_t endpoint_id,
418 const std::string &cluster_name);
419
430 void on_cluster_generated_commands_added(const std::string &unid,
431 endpoint_id_t endpoint_id,
432 const std::string &cluster_name,
433 const nlohmann::json &jsn);
434
444 void on_cluster_generated_commands_removed(const std::string &unid,
445 endpoint_id_t endpoint_id,
446 const std::string &cluster_name);
447
449 static void on_node_state_functional_timeout(void *context);
450
451 // Delete copy constructor
458};
459
460} // namespace unify::node_state_monitor
461
462#endif //UNIFY_NODE_STATE_MONITOR_H
Unify Cluster.
Definition: unify_node_state_monitor.hpp:55
std::unordered_set< std::string > generated_commands
Definition: unify_node_state_monitor.hpp:64
std::unordered_set< std::string > supported_commands
Definition: unify_node_state_monitor.hpp:62
std::string cluster_name
Definition: unify_node_state_monitor.hpp:58
cluster(const std::string &name)
Construct a new cluster object.
Definition: unify_node_state_monitor.hpp:70
std::unordered_set< std::string > attributes
Definition: unify_node_state_monitor.hpp:60
std::string to_string() const
Get a string representation of the cluster.
Unify Endpoint.
Definition: unify_node_state_monitor.hpp:85
std::unordered_map< std::string, cluster > clusters
Definition: unify_node_state_monitor.hpp:91
endpoint_id_t endpoint_id
Definition: unify_node_state_monitor.hpp:88
cluster & emplace_cluster(const std::string &cluster_name)
Create a new cluster in the cluster map and return a reference to it. If the cluster_name already exi...
std::string to_string() const
Get a string representation of the endpoint.
endpoint(endpoint_id_t id)
Construct a new endpoint object.
Definition: unify_node_state_monitor.hpp:97
Interface class for the node_state_monitor.
Definition: unify_node_state_monitor.hpp:171
virtual void on_unify_node_state_changed(const node &node)=0
Unify Node state changed.
virtual void on_unify_node_removed(const std::string &unid)=0
Unify Node is removed.
virtual void on_unify_node_added(const node &node)=0
Unify Node is added.
Definition: unify_node_state_monitor.hpp:222
void on_cluster_supported_commands_removed(const std::string &unid, endpoint_id_t endpoint_id, const std::string &cluster_name)
Called from node_state_monitor::on_mqtt_cluster_supported_commands_message when supported commands ar...
void on_node_state_changed(const std::string &unid, const nlohmann::json &jsn)
Called from node_state_monitor::on_mqtt_node_state_message when a node state changes.
void set_node_state_functional_threshold(clock_time_t t)
Set the node state functional threshold.
clock_time_t node_state_functional_threshold
Definition: unify_node_state_monitor.hpp:290
node_state_monitor const & operator=(const node_state_monitor &)=delete
node_state_monitor(const node_state_monitor &)=delete
void on_cluster_attribute_removed(const std::string &unid, endpoint_id_t endpoint_id, const std::string &cluster_name, const std::string &attribute)
Called from node_state_monitor::on_mqtt_cluster_attributes when an attribute is removed from the clus...
static node_state_monitor & get_instance()
Get the instance object.
Definition: unify_node_state_monitor.hpp:229
void on_cluster_generated_commands_added(const std::string &unid, endpoint_id_t endpoint_id, const std::string &cluster_name, const nlohmann::json &jsn)
Called from node_state_monitor::on_mqtt_cluster_supported_generated_commands_message when supported g...
void set_node_state_functional_timeout_time(clock_time_t t)
Set the node state functional timeout time.
void on_cluster_attribute_added(const std::string &unid, endpoint_id_t endpoint_id, const std::string &cluster_name, const std::string &attribute)
Called from node_state_monitor::on_mqtt_cluster_attributes when an attribute is added to the cluster.
struct ctimer node_state_functional_timer
Definition: unify_node_state_monitor.hpp:286
const std::unordered_map< std::string, node > & get_nodes() const
Get a reference to the nodes object containing all the nodes.
Definition: unify_node_state_monitor.hpp:241
node_state_monitor_interface * interface
Definition: unify_node_state_monitor.hpp:295
static void on_mqtt_cluster_supported_generated_commands_message(const char *topic, const char *message, const size_t message_length)
Callback function for when cluster supported generated commands message is received from MQTT.
void on_cluster_supported_commands_added(const std::string &unid, endpoint_id_t endpoint_id, const std::string &cluster_name, const nlohmann::json &jsn)
Called from node_state_monitor::on_mqtt_cluster_supported_commands_message when supported commands ar...
std::unordered_map< std::string, node > nodes
Definition: unify_node_state_monitor.hpp:282
static void on_mqtt_cluster_supported_commands_message(const char *topic, const char *message, const size_t message_length)
Callback function for when cluster supported commands message is received from MQTT.
static void on_mqtt_cluster_attributes(const char *topic, const char *message, const size_t message_length)
Callback function for when cluster attributes message is received from MQTT.
static void on_node_state_functional_timeout(void *context)
void on_node_removed(const std::string &unid)
Called from node_state_monitor::on_mqtt_node_state_message when a node is removed.
std::unordered_set< std::string > nodes_pending_node_added
Definition: unify_node_state_monitor.hpp:284
node & emplace_node(const std::string &unid)
Create a new node in the node map and return a reference to it. If the unid alredy exist in the map r...
static void on_mqtt_node_state_message(const char *topic, const char *message, const size_t message_length)
Callback function for when state messages are received from MQTT.
clock_time_t node_state_functional_timeout_time
Definition: unify_node_state_monitor.hpp:293
void set_interface(node_state_monitor_interface *_interface)
Set the interface object.
Definition: unify_node_state_monitor.hpp:275
void on_cluster_generated_commands_removed(const std::string &unid, endpoint_id_t endpoint_id, const std::string &cluster_name)
Called from node_state_monitor::on_mqtt_cluster_supported_generated_commands_message when supported g...
Unify Node.
Definition: unify_node_state_monitor.hpp:120
std::string to_string() const
Get a string representation of the node.
node(const std::string &_unid)
Construct a new node object.
Definition: unify_node_state_monitor.hpp:141
std::unordered_map< endpoint_id_t, endpoint > endpoints
Definition: unify_node_state_monitor.hpp:130
endpoint & emplace_endpoint(endpoint_id_t endpoint_id)
Create a new endpoint in the endpoint map and return a reference to it. If the endpoint_id alredy exi...
uint32_t state
Definition: unify_node_state_monitor.hpp:125
clock_time_t last_update
Definition: unify_node_state_monitor.hpp:132
std::string unid
Definition: unify_node_state_monitor.hpp:123
uint32_t security_info
Definition: unify_node_state_monitor.hpp:127
uint32_t clock_time_t
Definition: contiki-conf.h:31
CCIF clock_time_t clock_time(void)
Definition: clock.c:75
#define CLOCK_SECOND
Definition: clock.h:131
Definition: unify_node_state_monitor.hpp:38
static constexpr clock_time_t DEFAULT_NODE_STATE_FUNCTIONAL_TIMEOUT
Definition: unify_node_state_monitor.hpp:49
uint16_t endpoint_id_t
Definition: unify_node_state_monitor.hpp:40
static constexpr clock_time_t DEFAULT_NODE_STATE_FUNCTIONAL_THRESHOLD
Definition: unify_node_state_monitor.hpp:45
Definition: ctimer.h:64