Uniot Core
0.8.1
Loading...
Searching...
No Matches
TopDevice.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2024 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <CBORObject.h>
22#include <Date.h>
23#include <MQTTDevice.h>
24#include <TaskScheduler.h>
25
26namespace uniot {
37class TopDevice : public MQTTDevice {
38 public:
45 : MQTTDevice(),
46 mpScheduler(nullptr) {}
47
57 virtual void syncSubscriptions() override {
58 mTopicTopAsk = MQTTDevice::subscribeDevice("debug/top/ask");
59 mTopicMemAsk = MQTTDevice::subscribeDevice("debug/mem/ask");
60 }
61
70 void setScheduler(const TaskScheduler& scheduler) {
71 mpScheduler = &scheduler;
72 }
73
83 virtual void handle(const String& topic, const Bytes& payload) override {
84 if (MQTTDevice::isTopicMatch(mTopicTopAsk, topic)) {
85 handleTop();
86 return;
87 }
88 if (MQTTDevice::isTopicMatch(mTopicMemAsk, topic)) {
89 handleMem();
90 return;
91 }
92 }
93
104 void handleTop() {
105 if (mpScheduler) {
106 CBORObject packet;
107 auto tasksObj = packet.putMap("tasks");
108 uint64_t tasksElapsedMs = 0;
109 mpScheduler->exportTasksInfo([&](const char* name, bool isAttached, uint64_t elapsedMs) {
110 tasksElapsedMs += elapsedMs;
111 tasksObj.putArray(name)
112 .append(isAttached) // Whether the task is currently attached to the scheduler
113 .append(elapsedMs); // Total execution time in milliseconds
114 });
115 auto idleMs = mpScheduler->getTotalElapsedMs() - tasksElapsedMs;
116 packet.put("idle", idleMs);
117 packet.put("timestamp", static_cast<int64_t>(Date::now()));
118 packet.put("uptime", static_cast<uint64_t>(millis()));
119
120 MQTTDevice::publishDevice("debug/top", packet.build());
121 }
122 }
123
130 void handleMem() {
131 CBORObject packet;
132 packet.put("available", static_cast<uint64_t>(ESP.getFreeHeap()));
133 MQTTDevice::publishDevice("debug/mem", packet.build());
134 }
135
136 private:
137 const TaskScheduler* mpScheduler;
138 String mTopicTopAsk;
139 String mTopicMemAsk;
140};
141
142} // namespace uniot
Definition Bytes.h:38
Definition CBORObject.h:40
CBORObject putMap(const char *key)
Put a new map at a specific string key, or get the existing one.
Definition CBORObject.h:338
CBORObject & put(int key, int value)
Put an integer value at a specific integer key.
Definition CBORObject.h:170
Bytes build() const
Build the CBOR data into binary format.
Definition CBORObject.h:499
static time_t now()
Returns the current Unix timestamp.
Definition Date.h:74
MQTTDevice()
Constructs a new MQTTDevice instance.
Definition MQTTDevice.h:44
bool isTopicMatch(const String &storedTopic, const String &incomingTopic) const
Determines if a stored topic matches an incoming topic string using MQTT wildcards.
Definition MQTTDevice.cpp:137
void publishDevice(const String &subTopic, const Bytes &payload, bool retained=false, bool sign=false)
Publishes a message to a device-specific subtopic.
Definition MQTTDevice.cpp:108
const String & subscribeDevice(const String &subTopic)
Subscribes to a device-specific subtopic.
Definition MQTTDevice.cpp:83
Definition TaskScheduler.h:164
void handleMem()
Handle request for memory usage information.
Definition TopDevice.h:130
TopDevice()
Construct a new TopDevice instance.
Definition TopDevice.h:44
virtual void handle(const String &topic, const Bytes &payload) override
Handle incoming MQTT messages.
Definition TopDevice.h:83
void setScheduler(const TaskScheduler &scheduler)
Associate a TaskScheduler with this monitoring device.
Definition TopDevice.h:70
virtual void syncSubscriptions() override
Set up MQTT topic subscriptions for device monitoring.
Definition TopDevice.h:57
void handleTop()
Handle request for task and system performance data.
Definition TopDevice.h:104
Contains all classes and functions related to the Uniot Core.