AlteriomPainlessMesh v2.1.1
Easy ESP32/ESP8266 mesh networking with Alteriom IoT extensions
Loading...
Searching...
No Matches
remote.hpp
Go to the documentation of this file.
1#pragma once
2
4
7
9
10namespace painlessmesh {
11namespace plugin {
16namespace remote {
18 public:
19 std::list<std::pair<uint, TSTRING>> log;
20 RemotePackage() : plugin::SinglePackage(14) {}
21 RemotePackage(JsonObject jsonObj) : plugin::SinglePackage(jsonObj) {
22 for (JsonObject doc : jsonObj["log"].as<JsonArray>()) {
23 std::pair<uint, TSTRING> pr(doc["first"], doc["second"]);
24 log.push_back(pr);
25 }
26 }
27
28 JsonObject addTo(JsonObject&& jsonObj) const {
29 jsonObj = SinglePackage::addTo(std::move(jsonObj));
30 JsonArray arr = jsonObj["log"].to<JsonArray>();
31 for (auto&& pr : log) {
32 JsonDocument obj;
33 obj["first"] = pr.first;
34 obj["second"] = pr.second;
35 arr.add(obj);
36 }
37 return jsonObj;
38 }
39};
40
41template <class T>
42void begin(T& mesh, uint32_t destination, double frequency = 120,
43 std::function<bool(protocol::Variant&)> callback = nullptr) {
44 auto sendPkg = std::make_shared<RemotePackage>();
45 sendPkg->from = mesh.getNodeId();
46 sendPkg->dest = destination;
47 mesh.addTask(frequency * TASK_SECOND, TASK_FOREVER, [sendPkg, &mesh]() {
48 Log.remote("Memory %u and stability %u\n", ESP.getFreeHeap(),
49 mesh.stability);
50 // Note that we could clear the queue afterwards, but we
51 // actually want to keep resending it in case something just
52 // went wrong and we lost connection
53 sendPkg->log = Log.get_remote_queue();
54 if (!sendPkg->log.empty()) mesh.sendPackage(sendPkg.get());
55 });
56
57 if (callback) {
58 mesh.onPackage(sendPkg->type, callback);
59 }
60}
61
62} // namespace remote
63} // namespace plugin
64} // namespace painlessmesh
Definition logger.hpp:46
void remote(const char *format...)
Definition logger.hpp:160
std::list< std::pair< uint32_t, TSTRING > > & get_remote_queue()
Definition logger.hpp:177
Definition plugin.hpp:40
JsonObject addTo(JsonObject &&jsonObj) const
Definition plugin.hpp:60
std::list< std::pair< uint, TSTRING > > log
Definition remote.hpp:19
RemotePackage(JsonObject jsonObj)
Definition remote.hpp:21
RemotePackage()
Definition remote.hpp:20
JsonObject addTo(JsonObject &&jsonObj) const
Definition remote.hpp:28
Definition protocol.hpp:481
void begin(T &mesh, uint32_t destination, double frequency=120, std::function< bool(protocol::Variant &)> callback=nullptr)
Definition remote.hpp:42
Definition wifi.hpp:34
painlessmesh::logger::LogClass Log
Definition wifi.cpp:2
painlessmesh::logger::LogClass Log
Definition wifi.cpp:2