-
-
Notifications
You must be signed in to change notification settings - Fork 225
/
app.h
486 lines (404 loc) · 13.1 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//-----------------------------------------------------------------------------
// 2023 Ahoy, https://ahoydtu.de
// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed
//-----------------------------------------------------------------------------
#ifndef __APP_H__
#define __APP_H__
#include <Arduino.h>
#include <ArduinoJson.h>
#if defined(ESP32)
#include <esp_task_wdt.h>
#define WDT_TIMEOUT_SECONDS 8 // Watchdog Timeout 8s
#endif
#include "config/settings.h"
#include "defines.h"
#include "appInterface.h"
#include "hm/hmSystem.h"
#include "hm/NrfRadio.h"
#if defined(ESP32)
#include "hms/CmtRadio.h"
#endif
#if defined(ENABLE_MQTT)
#include "publisher/pubMqtt.h"
#endif /*ENABLE_MQTT*/
#include "publisher/pubSerial.h"
#include "utils/crc.h"
#include "utils/dbg.h"
#include "utils/scheduler.h"
#include "utils/syslog.h"
#include "web/RestApi.h"
#include "web/Protection.h"
#include "plugins/MaxPower.h"
#if defined(ENABLE_HISTORY)
#include "plugins/history.h"
#endif /*ENABLE_HISTORY*/
#include "web/web.h"
#include "hm/Communication.h"
#if defined(ETHERNET)
#include "network/AhoyEthernet.h"
#else /* defined(ETHERNET) */
#if defined(ESP32)
#include "network/AhoyWifiEsp32.h"
#else
#include "network/AhoyWifiEsp8266.h"
#endif
#endif /* defined(ETHERNET) */
#include "utils/improv.h"
#if defined(ENABLE_SIMULATOR)
#include "hm/simulator.h"
#endif /*ENABLE_SIMULATOR*/
#include <RF24.h> // position is relevant since version 1.4.7 of this library
// convert degrees and radians for sun calculation
#define SIN(x) (sin(radians(x)))
#define COS(x) (cos(radians(x)))
#define ASIN(x) (degrees(asin(x)))
#define ACOS(x) (degrees(acos(x)))
typedef HmSystem<MAX_NUM_INVERTERS> HmSystemType;
typedef Web<HmSystemType> WebType;
typedef RestApi<HmSystemType> RestApiType;
#if defined(ENABLE_MQTT)
typedef PubMqtt<HmSystemType> PubMqttType;
#endif /*ENABLE_MQTT*/
typedef PubSerial<HmSystemType> PubSerialType;
#if defined(ENABLE_HISTORY)
typedef HistoryData<HmSystemType> HistoryType;
#endif /*ENABLE_HISTORY*/
#if defined (ENABLE_SIMULATOR)
typedef Simulator<HmSystemType> SimulatorType;
#endif /*ENABLE_SIMULATOR*/
// PLUGINS
#if defined(PLUGIN_DISPLAY)
#include "plugins/Display/Display.h"
#include "plugins/Display/Display_data.h"
typedef Display<HmSystemType, Radio> DisplayType;
#endif
class app : public IApp, public ah::Scheduler {
public:
app();
~app() {}
void setup(void);
void loop(void) override;
void onNetwork(bool gotIp);
void regularTickers(void);
void handleIntr(void) {
mNrfRadio.handleIntr();
}
void* getRadioObj(bool nrf) override {
if(nrf)
return (void*)&mNrfRadio;
else {
#ifdef ESP32
return (void*)&mCmtRadio;
#else
return NULL;
#endif
}
}
#ifdef ESP32
void handleHmsIntr(void) {
mCmtRadio.handleIntr();
}
#endif
uint32_t getUptime() override {
return Scheduler::getUptime();
}
uint32_t getTimestamp() override {
return Scheduler::mTimestamp;
}
uint64_t getTimestampMs() override {
return ((uint64_t)Scheduler::mTimestamp * 1000) + ((uint64_t)millis() - (uint64_t)Scheduler::mTsMillis) % 1000;
}
bool saveSettings(bool reboot) override {
mShowRebootRequest = true; // only message on index, no reboot
mSavePending = true;
mSaveReboot = reboot;
if(reboot) {
ah::Scheduler::resetTicker();
}
once(std::bind(&app::tickSave, this), 3, "save");
return true;
}
void initInverter(uint8_t id) override {
mSys.addInverter(id, [this](Inverter<> *iv) {
if((IV_MI == iv->ivGen) || (IV_HM == iv->ivGen))
iv->radio = &mNrfRadio;
#if defined(ESP32)
else if((IV_HMS == iv->ivGen) || (IV_HMT == iv->ivGen))
iv->radio = &mCmtRadio;
#endif
});
}
bool readSettings(const char *path) override {
return mSettings.readSettings(path);
}
bool eraseSettings(bool eraseWifi = false) {
return mSettings.eraseSettings(eraseWifi);
}
bool getSavePending() override {
return mSavePending;
}
bool getLastSaveSucceed() override {
return mSettings.getLastSaveSucceed();
}
bool getShouldReboot() override {
return mSaveReboot;
}
bool getAvailNetworks(JsonObject obj) override {
return mNetwork->getAvailNetworks(obj, this);
}
void setupStation(void) override {
mNetwork->begin();
}
bool getWasInCh12to14(void) const override {
#if defined(ESP8266)
return mNetwork->getWasInCh12to14();
#else
return false;
#endif
}
String getIp(void) override {
return mNetwork->getIp();
}
String getMac(void) override {
return mNetwork->getMac();
}
bool isApActive(void) override {
return mNetwork->isApActive();
}
void setRebootFlag() override {
once(std::bind(&app::tickReboot, this), 3, "rboot");
}
const char *getVersion() override {
return mVersion;
}
const char *getVersionModules() override {
return mVersionModules;
}
void addOnce(ah::scdCb c, uint32_t timeout, const char *name) override {
once(c, timeout, name);
}
uint32_t getSunrise() override {
return mSunrise;
}
uint32_t getSunset() override {
return mSunset;
}
bool getSettingsValid() override {
return mConfig->valid;
}
bool getRebootRequestState() override {
return mShowRebootRequest;
}
void setMqttDiscoveryFlag() override {
#if defined(ENABLE_MQTT)
once(std::bind(&PubMqttType::sendDiscoveryConfig, &mMqtt), 1, "disCf");
#endif
}
bool getMqttIsConnected() override {
#if defined(ENABLE_MQTT)
return mMqtt.isConnected();
#else
return false;
#endif
}
uint32_t getMqttTxCnt() override {
#if defined(ENABLE_MQTT)
return mMqtt.getTxCnt();
#else
return 0;
#endif
}
uint32_t getMqttRxCnt() override {
#if defined(ENABLE_MQTT)
return mMqtt.getRxCnt();
#else
return 0;
#endif
}
#if defined(ETHERNET)
bool isWiredConnection() override {
return mNetwork->isWiredConnection();
}
#endif
void lock(bool fromWeb) override {
mProtection->lock(fromWeb);
}
char *unlock(const char *clientIp, bool loginFromWeb) override {
return mProtection->unlock(clientIp, loginFromWeb);
}
void resetLockTimeout(void) override {
mProtection->resetLockTimeout();
}
bool isProtected(const char *clientIp, const char *token, bool askedFromWeb) const override {
return mProtection->isProtected(clientIp, token, askedFromWeb);
}
bool getNrfEnabled(void) override {
return mConfig->nrf.enabled;
}
bool getCmtEnabled(void) override {
return mConfig->cmt.enabled;
}
uint8_t getNrfIrqPin(void) {
return mConfig->nrf.pinIrq;
}
uint8_t getCmtIrqPin(void) {
return mConfig->cmt.pinIrq;
}
uint32_t getTimezoneOffset() override {
return mApi.getTimezoneOffset();
}
void getSchedulerInfo(uint8_t *max) override {
getStat(max);
}
void getSchedulerNames(void) override {
printSchedulers();
}
void setTimestamp(uint32_t newTime) override {
DPRINT(DBG_DEBUG, F("setTimestamp: "));
DBGPRINTLN(String(newTime));
if(0 == newTime)
mNetwork->updateNtpTime();
else {
Scheduler::setTimestamp(newTime);
onNtpUpdate(false);
}
}
float getTotalMaxPower(void) override {
return mMaxPower.getTotalMaxPower();
}
uint16_t getHistoryValue(uint8_t type, uint16_t i) override {
#if defined(ENABLE_HISTORY)
return mHistory.valueAt((HistoryStorageType)type, i);
#else
return 0;
#endif
}
uint32_t getHistoryPeriod(uint8_t type) override {
#if defined(ENABLE_HISTORY)
return mHistory.getPeriod((HistoryStorageType)type);
#else
return 0;
#endif
}
uint16_t getHistoryMaxDay() override {
#if defined(ENABLE_HISTORY)
return mHistory.getMaximumDay();
#else
return 0;
#endif
}
uint32_t getHistoryLastValueTs(uint8_t type) override {
#if defined(ENABLE_HISTORY)
return mHistory.getLastValueTs((HistoryStorageType)type);
#else
return 0;
#endif
}
#if defined(ENABLE_HISTORY_LOAD_DATA)
void addValueToHistory(uint8_t historyType, uint8_t valueType, uint32_t value) override {
#if defined(ENABLE_HISTORY)
return mHistory.addValue((HistoryStorageType)historyType, valueType, value);
#endif
}
#endif
private:
#define CHECK_AVAIL true
#define SKIP_YIELD_DAY true
void resetSystem(void);
void zeroIvValues(bool checkAvail = false, bool skipYieldDay = true);
void payloadEventListener(uint8_t cmd, Inverter<> *iv) {
mMaxPower.payloadEvent(cmd, iv);
#if defined(ENABLE_MQTT)
if (mMqttEnabled)
mMqtt.payloadEventListener(cmd, iv);
#endif
#if defined(PLUGIN_DISPLAY)
if(DISP_TYPE_T0_NONE != mConfig->plugin.display.type)
mDisplay.payloadEventListener(cmd);
#endif
updateLed();
}
void mqttSubRxCb(JsonObject obj);
void setupLed();
void updateLed();
void tickReboot(void) {
DPRINTLN(DBG_INFO, F("Rebooting..."));
ah::Scheduler::resetTicker();
WiFi.disconnect();
delay(200);
ESP.restart();
}
void tickSave(void) {
if(!mSettings.saveSettings())
mSaveReboot = false;
mSavePending = false;
if(mSaveReboot)
setRebootFlag();
}
void tickNtpUpdate(void);
void onNtpUpdate(bool gotTime);
bool mNtpReceived = false;
void updateNtp(void);
void triggerTickSend(uint8_t id) override {
once([this, id]() {
sendIv(mSys.getInverterByPos(id));
}, 0, "devct");
}
void tickCalcSunrise(void);
void tickIVCommunication(void);
void tickSun(void);
void tickSunrise(void);
void tickComm(void);
void tickSend(void);
bool sendIv(Inverter<> *iv);
void tickMinute(void);
void tickZeroValues(void);
void tickMidnight(void);
void notAvailChanged(void);
HmSystemType mSys;
NrfRadio<> mNrfRadio;
Communication mCommunication;
bool mShowRebootRequest = false;
AhoyNetwork *mNetwork = nullptr;
WebType mWeb;
RestApiType mApi;
Protection *mProtection = nullptr;
#ifdef ENABLE_SYSLOG
DbgSyslog mDbgSyslog;
#endif
PubSerialType mPubSerial;
//Improv mImprov;
#ifdef ESP32
CmtRadio<> mCmtRadio;
#endif
char mVersion[12];
char mVersionModules[12];
settings mSettings;
settings_t *mConfig = nullptr;
bool mSavePending = false;
bool mSaveReboot = false;
uint8_t mSendLastIvId = 0;
bool mAllIvNotAvail = false;
bool mNetworkConnected = false;
#if defined(ENABLE_MQTT)
PubMqttType mMqtt;
#endif
bool mTickerInstallOnce = false;
bool mMqttEnabled = false;
// sun
int32_t mCalculatedTimezoneOffset = 0;
uint32_t mSunrise = 0, mSunset = 0;
// plugins
MaxPower<float> mMaxPower;
#if defined(PLUGIN_DISPLAY)
DisplayType mDisplay;
DisplayData mDispData;
#endif
#if defined(ENABLE_HISTORY)
HistoryType mHistory;
#endif /*ENABLE_HISTORY*/
#if defined(ENABLE_SIMULATOR)
SimulatorType mSimulator;
#endif /*ENABLE_SIMULATOR*/
};
#endif /*__APP_H__*/