forked from dirkx/makerspaceleiden-payment-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMqttlogStream.cpp
75 lines (60 loc) · 1.77 KB
/
MqttlogStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Simple 'tee' class - that sends all 'serial' port data also to the Syslog and/or MQTT bus -
// to the 'log' topic if such is possible/enabled.
//
// XXX should refactor in a generic buffered 'add a Stream' class and then
// make the various destinations classes in their own right you can 'add' to the T.
//
//
#include "global.h"
#include "log.h"
#include <PubSubClient.h>
#include "MqttlogStream.h"
void MqttStream::begin() {
if (!_mqttServer || !_mqttTopic || !_mqttPort) {
Log.printf("Missing%s for MQTT Logging\n",
_mqttServer ? "" : " server", _mqttTopic ? "" : " topic", _mqttPort ? "" : " port");
return;
}
PubSubClient * psc = new PubSubClient(*_client);
psc->setServer(_mqttServer, _mqttPort);
psc->connect(_mqttTopic);
psc->setBufferSize(sizeof(logbuff)+9+strlen(_mqttTopic));
_mqtt = psc;
loop();
}
void MqttStream::loop() {
static unsigned long lst = 0;
if (!_mqtt)
return;
_mqtt->loop();
if (_mqtt->connected())
return;
if (lst && millis() - lst < 15000)
return;
Log.printf("MQTT connection state: %d (not connected)\n", _mqtt->state());
if (_mqtt->connect(_mqttTopic))
Log.println("(re)connected to MQTT");
else
Log.println("MQTT (re)connection failed. Will retry");
}
size_t MqttStream::write(uint8_t c) {
if (at >= sizeof(logbuff) - 1) {
Log.println("Purged logbuffer (should never happen)");
at = 0;
};
if (c >= 32 && c < 128)
logbuff[ at++ ] = c;
if (c == '\n' || at >= sizeof(logbuff) - 1) {
logbuff[at++] = 0;
at = 0;
// perhaps we should buffer this - and do this in the main loop().
if (_mqtt) {
if (_mqttTopic == NULL)
_mqtt->publish("debug", logbuff);
else
_mqtt->publish(_mqttTopic, logbuff);
};
yield();
};
return 1;
}