forked from lumapu/ahoy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.h
267 lines (218 loc) · 8.22 KB
/
app.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#ifndef __APP_H__
#define __APP_H__
#include "dbg.h"
#include "Arduino.h"
#include <RF24.h>
#include <RF24_config.h>
#include <ArduinoJson.h>
#include "eep.h"
#include "defines.h"
#include "crc.h"
#include "CircularBuffer.h"
#include "hmSystem.h"
#include "mqtt.h"
#include "ahoywifi.h"
#include "web.h"
// hier läst sich das Verhalten der app in Bezug auf MQTT
// durch PER-Conpiler defines anpassen
//
// #define __MQTT_TEST__ // MQTT Interval wird auf 10 Sekunden verkürzt ( nur für testzwecke )
#define __MQTT_AFTER_RX__ // versendet die MQTT Daten sobald die WR daten Aufbereitet wurden ( gehört eigentlich ins Setup )
// #define __MQTT_NO_DISCOVERCONFIG__ // das versenden der MQTTDiscoveryConfig abschalten ( gehört eigentlich ins Setup )
typedef CircularBuffer<packet_t, PACKET_BUFFER_SIZE> BufferType;
typedef HmRadio<DEF_RF24_CE_PIN, DEF_RF24_CS_PIN, BufferType> RadioType;
typedef Inverter<float> InverterType;
typedef HmSystem<RadioType, BufferType, MAX_NUM_INVERTERS, InverterType> HmSystemType;
const char* const wemosPins[] = {"D3 (GPIO0)", "TX (GPIO1)", "D4 (GPIO2)", "RX (GPIO3)",
"D2 (GPIO4)", "D1 (GPIO5)", "GPIO6", "GPIO7", "GPIO8",
"GPIO9", "GPIO10", "GPIO11", "D6 (GPIO12)", "D7 (GPIO13)",
"D5 (GPIO14)", "D8 (GPIO15)", "D0 (GPIO16 - no IRQ!)"};
const char* const pinNames[] = {"CS", "CE", "IRQ"};
const char* const pinArgNames[] = {"pinCs", "pinCe", "pinIrq"};
typedef struct {
uint8_t invId;
uint32_t ts;
uint8_t data[MAX_PAYLOAD_ENTRIES][MAX_RF_PAYLOAD_SIZE];
uint8_t len[MAX_PAYLOAD_ENTRIES];
bool complete;
uint8_t maxPackId;
uint8_t retransmits;
bool requested;
} invPayload_t;
class ahoywifi;
class web;
class app {
public:
app();
~app() {}
void setup(uint32_t timeout);
void loop(void);
void handleIntr(void);
void cbMqtt(char* topic, byte* payload, unsigned int length);
void saveValues(void);
void resetPayload(Inverter<>* iv);
String getStatistics(void);
String getLiveData(void);
String getJson(void);
bool getWifiApActive(void);
uint8_t getIrqPin(void) {
return mConfig.pinIrq;
}
uint64_t Serial2u64(const char *val) {
char tmp[3] = {0};
uint64_t ret = 0ULL;
uint64_t u64;
for(uint8_t i = 0; i < 6; i++) {
tmp[0] = val[i*2];
tmp[1] = val[i*2 + 1];
if((tmp[0] == '\0') || (tmp[1] == '\0'))
break;
u64 = strtol(tmp, NULL, 16);
ret |= (u64 << ((5-i) << 3));
}
return ret;
}
String getDateTimeStr(time_t t) {
char str[20] = {0};
if(0 == t)
sprintf(str, "n/a");
else
sprintf(str, "%04d-%02d-%02d %02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
return String(str);
}
inline uint32_t getUptime(void) {
return mUptimeSecs;
}
inline uint32_t getTimestamp(void) {
return mTimestamp;
}
void eraseSettings(bool all = false) {
//DPRINTLN(DBG_VERBOSE, F("main.h:eraseSettings"));
uint8_t buf[64] = {0};
uint16_t addr = (all) ? ADDR_START : ADDR_START_SETTINGS;
uint16_t end;
do {
end = addr + 64;
if(end > (ADDR_SETTINGS_CRC + 2))
end = (ADDR_SETTINGS_CRC + 2);
DPRINTLN(DBG_DEBUG, F("erase: 0x") + String(addr, HEX) + " - 0x" + String(end, HEX));
mEep->write(addr, buf, (end-addr));
addr = end;
} while(addr < (ADDR_SETTINGS_CRC + 2));
mEep->commit();
}
inline bool checkTicker(uint32_t *ticker, uint32_t interval) {
uint32_t mil = millis();
if(mil >= *ticker) {
*ticker = mil + interval;
return true;
}
else if(mil < (*ticker - interval)) {
*ticker = mil + interval;
return true;
}
return false;
}
HmSystemType *mSys;
private:
void resetSystem(void);
void loadDefaultConfig(void);
void loadEEpconfig(void);
void setupMqtt(void);
bool buildPayload(uint8_t id);
void processPayload(bool retransmit);
void processPayload(bool retransmit, uint8_t cmd);
void sendMqttDiscoveryConfig(void);
const char* getFieldDeviceClass(uint8_t fieldId);
const char* getFieldStateClass(uint8_t fieldId);
inline uint16_t buildEEpCrc(uint32_t start, uint32_t length) {
DPRINTLN(DBG_VERBOSE, F("main.h:buildEEpCrc"));
uint8_t buf[32];
uint16_t crc = 0xffff;
uint8_t len;
while(length > 0) {
len = (length < 32) ? length : 32;
mEep->read(start, buf, len);
crc = Hoymiles::crc16(buf, len, crc);
start += len;
length -= len;
}
return crc;
}
void updateCrc(void) {
DPRINTLN(DBG_VERBOSE, F("app::updateCrc"));
uint16_t crc;
crc = buildEEpCrc(ADDR_START, ADDR_WIFI_CRC);
DPRINTLN(DBG_DEBUG, F("new Wifi CRC: ") + String(crc, HEX));
mEep->write(ADDR_WIFI_CRC, crc);
crc = buildEEpCrc(ADDR_START_SETTINGS, ((ADDR_NEXT) - (ADDR_START_SETTINGS)));
DPRINTLN(DBG_DEBUG, F("new Settings CRC: ") + String(crc, HEX));
mEep->write(ADDR_SETTINGS_CRC, crc);
mEep->commit();
}
bool checkEEpCrc(uint32_t start, uint32_t length, uint32_t crcPos) {
DPRINTLN(DBG_VERBOSE, F("main.h:checkEEpCrc"));
DPRINTLN(DBG_DEBUG, F("start: ") + String(start) + F(", length: ") + String(length));
uint16_t crcRd, crcCheck;
crcCheck = buildEEpCrc(start, length);
mEep->read(crcPos, &crcRd);
DPRINTLN(DBG_DEBUG, "CRC RD: " + String(crcRd, HEX) + " CRC CALC: " + String(crcCheck, HEX));
return (crcCheck == crcRd);
}
void stats(void) {
DPRINTLN(DBG_VERBOSE, F("main.h:stats"));
#ifdef ESP8266
uint32_t free;
uint16_t max;
uint8_t frag;
ESP.getHeapStats(&free, &max, &frag);
#elif defined(ESP32)
uint32_t free;
uint32_t max;
uint8_t frag;
free = ESP.getFreeHeap();
max = ESP.getMaxAllocHeap();
frag = 0;
#endif
DPRINT(DBG_VERBOSE, F("free: ") + String(free));
DPRINT(DBG_VERBOSE, F(" - max: ") + String(max) + "%");
DPRINTLN(DBG_VERBOSE, F(" - frag: ") + String(frag));
}
uint32_t mUptimeTicker;
uint16_t mUptimeInterval;
uint32_t mUptimeSecs;
uint8_t mHeapStatCnt;
bool mWifiSettingsValid;
bool mSettingsValid;
eep *mEep;
uint32_t mTimestamp;
bool mShowRebootRequest;
ahoywifi *mWifi;
web *mWebInst;
sysConfig_t mSysConfig;
config_t mConfig;
char mVersion[12];
uint16_t mSendTicker;
uint8_t mSendLastIvId;
invPayload_t mPayload[MAX_NUM_INVERTERS];
uint32_t mRxFailed;
uint32_t mRxSuccess;
uint32_t mFrameCnt;
uint8_t mLastPacketId;
// timer
uint32_t mTicker;
uint32_t mRxTicker;
// mqtt
mqtt mMqtt;
uint16_t mMqttTicker;
uint16_t mMqttInterval;
bool mMqttActive;
bool mMqttConfigSendState[MAX_NUM_INVERTERS];
// serial
uint16_t mSerialTicker;
};
#endif /*__APP_H__*/