Repository navigation
Expand file tree
/
Copy pathpy_mqtt.h
More file actions
116 lines (92 loc) · 3.16 KB
/
Copy pathpy_mqtt.h
File metadata and controls
116 lines (92 loc) · 3.16 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#pragma once
#include <Arduino.h>
#include <PubSubClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include "config.h"
// -------------------------------------------------------------
// MQTT Message (Topic + Payload)
// -------------------------------------------------------------
struct MqttMessage {
char topic[128];
char payload[512];
bool retain;
};
// -------------------------------------------------------------
// Discovery phases
// -------------------------------------------------------------
enum DiscoveryPhase {
DISC_IDLE,
DISC_STACK,
DISC_PWR,
DISC_BAT,
DISC_STAT,
DISC_DONE
};
extern DiscoveryPhase discoveryPhase;
extern size_t discPwrIndex;
extern size_t discBatModule;
extern size_t discStatModule;
// -------------------------------------------------------------
// Main MQTT class
// -------------------------------------------------------------
class PyMqtt {
public:
void begin();
void loop();
bool connect();
void disconnect();
void reconnectTask();
// Direktes Publish ohne Queue
bool publishRaw(const String& topic, const String& payload, bool retain = false);
// Status
bool isConnected() { return mqttClient.connected(); }
// Discovery publishers
void publishDiscoveryStack();
void publishDiscoveryPwrModule(int moduleIndex);
void publishDiscoveryBatModule(int moduleIndex);
void publishDiscoveryStatModule(int moduleIndex);
// Discovery state machine
void handleDiscoveryStep();
// Data publishers
void publishStack();
void publishPwrSingle();
void publishBatModule(int moduleIndex);
void publishStat(int moduleIndex);
void handlePwrBatch();
private:
// Core MQTT
WiFiClient wifiClient;
PubSubClient mqttClient = PubSubClient(wifiClient);
bool enabled = false;
bool discoveryActive = false;
bool mqttOk = false;
bool reconnectNeeded = false;
unsigned long lastReconnectAttempt = 0;
unsigned long lastMqttOk = 0;
unsigned long wifiConnectedSince = 0;
bool mqttStartAllowed = false;
// Reconnect timing
unsigned long reconnectIntervalMs;
const unsigned long reconnectIntervalMin = 5000;
const unsigned long reconnectIntervalMax = 60000;
// Discovery timing
unsigned long discoveryLastSend = 0;
unsigned long discoveryStartTime = 0;
int discoveryMessagesPerSecond = 5;
int discoveryDelayStartMs = 2000;
// Helpers
String normalizeName(const String& in);
int decimalsForUnit(const String& unit);
String deviceClassForUnit(const String& unit);
bool precisionDiffersFromDefault(const String& unit);
int precisionForUnit(const String& unit);
String computeValue(const String& raw, const FieldConfig& fc);
void addDiscoveryMeta(JsonDocument& doc, const FieldConfig& fc);
// PWR JSON builder
String buildPwrJson(int moduleIndex);
void sendPwrModule(int moduleIndex);
void logPublishFailure(const String& topic);
};
// Global instance
extern PyMqtt py_mqtt;