-
Notifications
You must be signed in to change notification settings - Fork 33
/
task_syscontrol.cpp
892 lines (778 loc) · 20.7 KB
/
task_syscontrol.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
#include "task_syscontrol.h"
#define TASK_SYS_CONTROL_PRIO 5
// globals
uint32_t TaskSysControlHWmark = 0;
DNSServer dnsServer;
bool SHT3x_original = false;
bool SHT3x_alternative = false;
bool runscan = false;
bool dontSaveConfig = false;
bool saveSystemConfigflag = false;
bool saveWifiConfigflag = false;
bool resetWifiConfigflag = false;
bool resetSystemConfigflag = false;
bool clearQueue = false;
bool shouldReboot = false;
int8_t ithoInitResult = 0;
bool IthoInit = false;
bool wifiModeAP = false;
// locals
StaticTask_t xTaskSysControlBuffer;
StackType_t xTaskSysControlStack[STACK_SIZE];
TaskHandle_t xTaskSysControlHandle = NULL;
SemaphoreHandle_t mutexI2Ctask;
unsigned long lastI2CinitRequest = 0;
bool ithoInitResultLogEntry = true;
Ticker scan;
Ticker getSettingsHack;
unsigned long lastWIFIReconnectAttempt = 0;
unsigned long APmodeTimeout = 0;
unsigned long wifiLedUpdate = 0;
unsigned long SHT3x_readout = 0;
unsigned long query2401tim = 0;
unsigned long mqttUpdatetim = 0;
unsigned long lastVersionCheck = 0;
bool i2cStartCommands = false;
bool joinSend = false;
SHTSensor sht_org(SHTSensor::SHT3X);
SHTSensor sht_alt(SHTSensor::SHT3X_ALT);
void startTaskSysControl()
{
xTaskSysControlHandle = xTaskCreateStaticPinnedToCore(
TaskSysControl,
"TaskSysControl",
STACK_SIZE,
(void *)1,
TASK_SYS_CONTROL_PRIO,
xTaskSysControlStack,
&xTaskSysControlBuffer,
CONFIG_ARDUINO_RUNNING_CORE);
}
void TaskSysControl(void *pvParameters)
{
configASSERT((uint32_t)pvParameters == 1UL);
Ticker TaskTimeout;
Ticker queueUpdater;
mutexI2Ctask = xSemaphoreCreateMutex();
wifiInit();
init_vRemote();
initSensor();
startTaskCC1101();
esp_task_wdt_add(NULL);
queueUpdater.attach_ms(QUEUE_UPDATE_MS, update_queue);
for (;;)
{
yield();
esp_task_wdt_reset();
TaskTimeout.once_ms(35000, []()
{ logInput("Warning: Task SysControl timed out!"); });
execSystemControlTasks();
if (shouldReboot)
{
TaskTimeout.detach();
logInput("Reboot requested");
if (!dontSaveConfig)
{
saveSystemConfig();
}
delay(1000);
ACTIVE_FS.end();
esp_task_wdt_init(1, true);
esp_task_wdt_add(NULL);
while (true)
;
}
TaskSysControlHWmark = uxTaskGetStackHighWaterMark(NULL);
vTaskDelay(25 / portTICK_PERIOD_MS);
}
// else delete task
vTaskDelete(NULL);
}
void execSystemControlTasks()
{
if (IthoInit && millis() > 250)
{
IthoInit = ithoInitCheck();
}
if (!i2cStartCommands && millis() > 15000 && (millis() - lastI2CinitRequest > 5000))
{
lastI2CinitRequest = millis();
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)1000 / portTICK_PERIOD_MS) == pdTRUE)
{
sendQueryDevicetype(false);
xSemaphoreGive(mutexI2Ctask);
}
if (currentItho_fwversion() > 0)
{
char logBuff[LOG_BUF_SIZE]{};
sprintf(logBuff, "I2C init: QueryDevicetype - fw:%d hw:%d", currentItho_fwversion(), currentIthoDeviceID());
logInput(logBuff);
ithoInitResult = 1;
i2cStartCommands = true;
#if defined(CVE)
digitalWrite(ITHOSTATUS, HIGH);
#elif defined(NON_CVE)
digitalWrite(STATUSPIN, HIGH);
#endif
if (systemConfig.syssht30 > 0)
{
if (currentIthoDeviceID() == 0x1B)
{
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)1000 / portTICK_PERIOD_MS) == pdTRUE)
{
i2c_result_updateweb = false;
index2410 = 0;
if (systemConfig.syssht30 == 1)
{
/*
switch itho hum setting off on every boot because
hum sensor setting gets restored in itho firmware after power cycle
*/
value2410 = 0;
}
else if (systemConfig.syssht30 == 2)
{
/*
if value == 2 setting changed from 1 -> 0
then restore itho hum setting back to "on"
*/
value2410 = 1;
}
if (currentItho_fwversion() == 25)
{
index2410 = 63;
}
else if (currentItho_fwversion() == 26 || currentItho_fwversion() == 27)
{
index2410 = 71;
}
if (index2410 > 0)
{
sendQuery2410(i2c_result_updateweb);
setSetting2410(i2c_result_updateweb);
char logBuff[LOG_BUF_SIZE]{};
sprintf(logBuff, "I2C init: set hum sensor in itho firmware to: %s", value2410 ? "on" : "off");
logInput(logBuff);
}
xSemaphoreGive(mutexI2Ctask);
}
}
if (systemConfig.syssht30 == 2)
{
systemConfig.syssht30 = 0;
saveSystemConfig();
}
}
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)1000 / portTICK_PERIOD_MS) == pdTRUE)
{
sendQueryStatusFormat(false);
char logBuff[LOG_BUF_SIZE]{};
sprintf(logBuff, "I2C init: QueryStatusFormat - items:%d", ithoStatus.size());
logInput(logBuff);
xSemaphoreGive(mutexI2Ctask);
}
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)1000 / portTICK_PERIOD_MS) == pdTRUE)
{
sendQueryStatus(false);
logInput("I2C init: QueryStatus");
xSemaphoreGive(mutexI2Ctask);
}
sendHomeAssistantDiscovery = true;
}
else
{
ithoInitResult = -1;
if (ithoInitResultLogEntry)
{
ithoInitResultLogEntry = false;
logInput("I2C init: QueryDevicetype - failed");
}
}
}
if (systemConfig.itho_sendjoin > 0 && !joinSend && ithoInitResult == 1)
{
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)500 / portTICK_PERIOD_MS) == pdTRUE)
{
joinSend = true;
sendRemoteCmd(0, IthoJoin, virtualRemotes);
xSemaphoreGive(mutexI2Ctask);
logInput("I2C init: Virtual remote join command send");
if (systemConfig.itho_sendjoin == 1)
{
systemConfig.itho_sendjoin = 0;
saveSystemConfig();
}
}
}
// Itho queue
if (clearQueue)
{
clearQueue = false;
ithoQueue.clear_queue();
}
if (ithoQueue.ithoSpeedUpdated)
{
ithoQueue.ithoSpeedUpdated = false;
uint16_t speed = ithoQueue.get_itho_speed();
char buf[32]{};
sprintf(buf, "speed:%d", speed);
//#if defined (CVE)
if (writeIthoVal(speed, &ithoCurrentVal, &updateIthoMQTT))
{
if (lastCmd.source == nullptr)
logLastCommand(buf, "ithoQueue");
sysStatReq = true;
}
else
{
logLastCommand(buf, "ithoQueue_error");
}
//#endif
}
// System control tasks
if ((WiFi.status() != WL_CONNECTED) && !wifiModeAP)
{
if (millis() - lastWIFIReconnectAttempt > 60000)
{
logInput("Attempt to reconnect WiFi");
lastWIFIReconnectAttempt = millis();
// Attempt to reconnect
if (connectWiFiSTA())
{
logInput("Reconnect WiFi successful");
lastWIFIReconnectAttempt = 0;
}
else
{
logInput("Reconnect WiFi failed!");
}
}
}
if (runscan)
{
runscan = false;
scan.once_ms(10, wifiScan);
}
if (wifiModeAP)
{
if (millis() - APmodeTimeout > 900000)
{ // reboot after 15 min in AP mode
shouldReboot = true;
}
dnsServer.processNextRequest();
if (millis() - wifiLedUpdate >= 500)
{
wifiLedUpdate = millis();
if (digitalRead(WIFILED) == LOW)
{
digitalWrite(WIFILED, HIGH);
}
else
{
digitalWrite(WIFILED, LOW);
}
}
}
if (!(currentItho_fwversion() > 0) && millis() - lastVersionCheck > 60000)
{
lastVersionCheck = millis();
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)1000 / portTICK_PERIOD_MS) == pdTRUE)
{
sendQueryDevicetype(false);
xSemaphoreGive(mutexI2Ctask);
}
}
// request itho status every systemConfig.itho_updatefreq sec.
if (millis() - query2401tim >= systemConfig.itho_updatefreq * 1000UL && i2cStartCommands)
{
query2401tim = millis();
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)500 / portTICK_PERIOD_MS) == pdTRUE)
{
sendQueryStatus(false);
xSemaphoreGive(mutexI2Ctask);
}
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)500 / portTICK_PERIOD_MS) == pdTRUE)
{
sendQuery31DA(false);
xSemaphoreGive(mutexI2Ctask);
}
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)500 / portTICK_PERIOD_MS) == pdTRUE)
{
sendQuery31D9(false);
xSemaphoreGive(mutexI2Ctask);
}
updateMQTTihtoStatus = true;
}
if (ithoInitResult == -1)
{
if (millis() - mqttUpdatetim >= systemConfig.itho_updatefreq * 1000UL)
{
mqttUpdatetim = millis();
updateMQTTihtoStatus = true;
}
}
if (get2410)
{
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)500 / portTICK_PERIOD_MS) == pdTRUE)
{
get2410 = false;
resultPtr2410 = sendQuery2410(i2c_result_updateweb);
xSemaphoreGive(mutexI2Ctask);
}
}
if (set2410)
{
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)500 / portTICK_PERIOD_MS) == pdTRUE)
{
setSetting2410(i2c_result_updateweb);
set2410 = false;
xSemaphoreGive(mutexI2Ctask);
}
getSettingsHack.once_ms(1, []()
{ getSetting(index2410, true, false, false); });
}
if (systemConfig.syssht30 == 1)
{
if (millis() - SHT3x_readout >= systemConfig.itho_updatefreq * 1000UL && (SHT3x_original || SHT3x_alternative))
{
SHT3x_readout = millis();
if (SHT3x_original || SHT3x_alternative)
{
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)500 / portTICK_PERIOD_MS) == pdTRUE)
{
if (SHT3x_original)
{
if (sht_org.readSample())
{
ithoHum = sht_org.getHumidity();
ithoTemp = sht_org.getTemperature();
}
}
if (SHT3x_alternative)
{
if (sht_alt.readSample())
{
ithoHum = sht_alt.getHumidity();
ithoTemp = sht_alt.getTemperature();
}
}
xSemaphoreGive(mutexI2Ctask);
}
}
}
}
}
void wifiInit()
{
if (!loadWifiConfig())
{
logInput("Setup: Wifi config load failed");
setupWiFiAP();
}
else if (!connectWiFiSTA(true))
{
logInput("Setup: Wifi connect STA failed");
setupWiFiAP();
}
configTime(0, 0, wifiConfig.ntpserver);
WiFi.scanDelete();
if (WiFi.scanComplete() == -2)
{
WiFi.scanNetworks(true);
}
if (!wifiModeAP)
{
logWifiInfo();
}
else
{
logInput("Setup: AP mode active");
}
}
void setupWiFiAP()
{
/* Soft AP network parameters */
IPAddress apIP(192, 168, 4, 1);
IPAddress netMsk(255, 255, 255, 0);
WiFi.persistent(false); // Do not use SDK storage of SSID/WPA parameters
esp_wifi_set_storage(WIFI_STORAGE_RAM);
WiFi.disconnect(true);
WiFi.setAutoReconnect(false);
delay(200);
WiFi.mode(WIFI_AP);
esp_wifi_set_ps(WIFI_PS_NONE);
delay(100);
WiFi.softAPConfig(apIP, apIP, netMsk);
WiFi.softAP(hostName(), WiFiAPPSK);
delay(500);
/* Setup the DNS server redirecting all the domains to the apIP */
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(53, "*", apIP);
wifiModeAP = true;
APmodeTimeout = millis();
logInput("wifi AP mode started");
}
bool connectWiFiSTA(bool restore)
{
wifiModeAP = false;
D_LOG("Connecting to wireless network...\n");
// Do not use SDK storage of SSID/WPA parameters
WiFi.persistent(false);
#ifdef ESPRESSIF32_3_5_0
//
// Do not use flash storage for wifi settings
esp_err_t wifi_set_storage = esp_wifi_set_storage(WIFI_STORAGE_RAM);
D_LOG("esp_wifi_set_storage: %s\n", esp_err_to_name(wifi_set_storage));
// Disconnect any existing connections and clear
if (!WiFi.disconnect(true))
D_LOG("Unable to set wifi disconnect\n");
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
// Set hostname
bool setHostname_result = WiFi.setHostname(hostName());
D_LOG("WiFi.setHostname: %s\n", setHostname_result ? "OK" : "NOK");
// No AutoReconnect
if (!WiFi.setAutoReconnect(false))
D_LOG("Unable to set auto reconnect\n");
delay(200);
// Set correct mode
if (!WiFi.mode(WIFI_STA))
D_LOG("Unable to set WiFi mode\n");
// No power saving
esp_err_t wifi_set_ps = esp_wifi_set_ps(WIFI_PS_NONE);
D_LOG("esp_wifi_set_ps: %s\n", esp_err_to_name(wifi_set_ps));
//
#else
// Set wifi mode to STA for next step (clear config)
if (!WiFi.mode(WIFI_STA))
D_LOG("Unable to set WiFi mode to STA\n");
// Clear any saved wifi config
if (restore)
{
esp_err_t wifi_restore = esp_wifi_restore();
D_LOG("esp_wifi_restore: %s\n", esp_err_to_name(wifi_restore));
}
// Disconnect any existing connections and clear
if (!WiFi.disconnect(true, true))
D_LOG("Unable to set wifi disconnect\n");
// Reset wifi mode to NULL
if (!WiFi.mode(WIFI_MODE_NULL))
D_LOG("Unable to set WiFi mode to NULL\n");
// Set hostname
bool setHostname_result = WiFi.setHostname(hostName());
D_LOG("WiFi.setHostname: %s\n", setHostname_result ? "OK" : "NOK");
// No AutoReconnect
if (!WiFi.setAutoReconnect(false))
D_LOG("Unable to set auto reconnect\n");
delay(200);
// Begin init of actual wifi connection
// Set correct mode
if (!WiFi.mode(WIFI_STA))
D_LOG("Unable to set WiFi mode\n");
esp_err_t esp_wifi_set_max_tx_power(int8_t power);
esp_err_t wifi_set_max_tx_power = esp_wifi_set_max_tx_power(78);
D_LOG("esp_wifi_set_max_tx_power: %s\n", esp_err_to_name(wifi_set_max_tx_power));
int8_t wifi_power_level = -1;
esp_err_t wifi_get_max_tx_power = esp_wifi_get_max_tx_power(&wifi_power_level);
D_LOG("esp_wifi_get_max_tx_power: %s - level:%d\n", esp_err_to_name(wifi_get_max_tx_power), wifi_power_level);
// Do not use flash storage for wifi settings
esp_err_t wifi_set_storage = esp_wifi_set_storage(WIFI_STORAGE_RAM);
D_LOG("esp_wifi_set_storage: %s\n", esp_err_to_name(wifi_set_storage));
// No power saving
esp_err_t wifi_set_ps = esp_wifi_set_ps(WIFI_PS_NONE);
D_LOG("esp_wifi_set_ps: %s\n", esp_err_to_name(wifi_set_ps));
#endif
if (strcmp(wifiConfig.dhcp, "off") == 0)
{
set_static_ip_config();
}
delay(2000);
wl_status_t wifi_begin = WiFi.begin(wifiConfig.ssid, wifiConfig.passwd);
auto timeoutmillis = millis() + 30000;
wl_status_t status = WiFi.status();
while (millis() < timeoutmillis)
{
esp_task_wdt_reset();
status = WiFi.status();
if (status == WL_CONNECTED)
{
digitalWrite(WIFILED, LOW);
return true;
}
else if (status != WL_DISCONNECTED) // fix for issue #108
{
logInput("WiFi: status != WL_DISCONNECTED, reinit setup");
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
if (strcmp(wifiConfig.dhcp, "off") == 0)
{
set_static_ip_config();
}
WiFi.mode(WIFI_STA);
delay(1000);
wifi_begin = WiFi.begin(wifiConfig.ssid, wifiConfig.passwd);
delay(2000);
}
if (digitalRead(WIFILED) == LOW)
{
digitalWrite(WIFILED, HIGH);
}
else
{
digitalWrite(WIFILED, LOW);
}
delay(100);
}
digitalWrite(WIFILED, HIGH);
char buf[64]{};
sprintf(buf, "Setup: wifi not connected - %s", wl_status_to_name(status));
logInput(buf);
return false;
}
void set_static_ip_config()
{
bool configOK = true;
IPAddress staticIP;
IPAddress gateway;
IPAddress subnet;
IPAddress dns1;
IPAddress dns2;
if (!staticIP.fromString(wifiConfig.ip))
{
configOK = false;
}
if (!gateway.fromString(wifiConfig.gateway))
{
configOK = false;
}
if (!subnet.fromString(wifiConfig.subnet))
{
configOK = false;
}
if (!dns1.fromString(wifiConfig.dns1))
{
configOK = false;
}
if (!dns2.fromString(wifiConfig.dns2))
{
configOK = false;
}
if (configOK)
{
if (!WiFi.config(staticIP, gateway, subnet, dns1, dns2))
{
logInput("Static IP config NOK");
}
else
{
logInput("Static IP config OK");
}
}
}
void initSensor()
{
if (systemConfig.syssht30 == 1)
{
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)500 / portTICK_PERIOD_MS) == pdTRUE)
{
}
else
{
logInput("Error: SHT i2c semaphore not available");
return;
}
if (sht_org.init() && sht_org.readSample())
{
SHT3x_original = true;
}
else if (sht_alt.init() && sht_alt.readSample())
{
SHT3x_alternative = true;
}
if (SHT3x_original)
{
logInput("Setup: Original SHT30 sensor found");
}
else if (SHT3x_alternative)
{
logInput("Setup: Alternative SHT30 sensor found");
}
if (SHT3x_original || SHT3x_alternative)
{
xSemaphoreGive(mutexI2Ctask);
return;
}
delay(200);
if (sht_org.init() && sht_org.readSample())
{
SHT3x_original = true;
}
else if (sht_alt.init() && sht_alt.readSample())
{
SHT3x_alternative = true;
}
if (SHT3x_original)
{
logInput("Setup: Original SHT30 sensor found (2nd try)");
}
else if (SHT3x_alternative)
{
logInput("Setup: Alternative SHT30 sensor found (2nd try)");
}
else
{
systemConfig.syssht30 = 0;
logInput("Setup: SHT30 sensor not present");
}
xSemaphoreGive(mutexI2Ctask);
}
}
void init_vRemote()
{
// setup virtual remote
char buff[128]{};
sprintf(buff, "Setup: Virtual remotes, start ID: %02X,%02X,%02X - No.: %d", sys.getMac(3), sys.getMac(4), sys.getMac(5), systemConfig.itho_numvrem);
logInput(buff);
virtualRemotes.setMaxRemotes(systemConfig.itho_numvrem);
loadVirtualRemotesConfig();
}
bool ithoInitCheck()
{
#if defined(CVE)
if (digitalRead(STATUSPIN) == LOW)
{
return false;
}
sendI2CPWMinit();
#endif
return false;
}
void update_queue()
{
ithoQueue.update_queue();
}
// Update itho Value
bool writeIthoVal(uint16_t value, volatile uint16_t *ithoCurrentVal, bool *updateIthoMQTT)
{
if (value > 254)
return false;
if (*ithoCurrentVal != value)
{
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)500 / portTICK_PERIOD_MS) == pdTRUE)
{
}
else
{
return false;
}
IthoPWMcommand(value, ithoCurrentVal, updateIthoMQTT);
xSemaphoreGive(mutexI2Ctask);
return true;
}
return false;
}
bool ithoI2CCommand(uint8_t remoteIndex, const char *command, cmdOrigin origin)
{
D_LOG("EXEC VREMOTE BUTTON COMMAND:%s remote:%d\n", command, remoteIndex);
if (xSemaphoreTake(mutexI2Ctask, (TickType_t)500 / portTICK_PERIOD_MS) == pdTRUE)
{
}
else
{
return false;
}
bool updateweb = false;
if (origin == WEB)
updateweb = true;
if (strcmp(command, "away") == 0)
{
sendRemoteCmd(remoteIndex, IthoAway, virtualRemotes);
}
else if (strcmp(command, "low") == 0)
{
sendRemoteCmd(remoteIndex, IthoLow, virtualRemotes);
}
else if (strcmp(command, "medium") == 0)
{
sendRemoteCmd(remoteIndex, IthoMedium, virtualRemotes);
}
else if (strcmp(command, "high") == 0)
{
sendRemoteCmd(remoteIndex, IthoHigh, virtualRemotes);
}
else if (strcmp(command, "timer1") == 0)
{
sendRemoteCmd(remoteIndex, IthoTimer1, virtualRemotes);
}
else if (strcmp(command, "timer2") == 0)
{
sendRemoteCmd(remoteIndex, IthoTimer2, virtualRemotes);
}
else if (strcmp(command, "timer3") == 0)
{
sendRemoteCmd(remoteIndex, IthoTimer3, virtualRemotes);
}
else if (strcmp(command, "cook30") == 0)
{
sendRemoteCmd(remoteIndex, IthoCook30, virtualRemotes);
}
else if (strcmp(command, "cook60") == 0)
{
sendRemoteCmd(remoteIndex, IthoCook60, virtualRemotes);
}
else if (strcmp(command, "auto") == 0)
{
sendRemoteCmd(remoteIndex, IthoAuto, virtualRemotes);
}
else if (strcmp(command, "autonight") == 0)
{
sendRemoteCmd(remoteIndex, IthoAutoNight, virtualRemotes);
}
else if (strcmp(command, "join") == 0)
{
sendRemoteCmd(remoteIndex, IthoJoin, virtualRemotes);
}
else if (strcmp(command, "leave") == 0)
{
sendRemoteCmd(remoteIndex, IthoLeave, virtualRemotes);
}
else if (strcmp(command, "type") == 0)
{
sendQueryDevicetype(updateweb);
}
else if (strcmp(command, "status") == 0)
{
sendQueryStatus(updateweb);
}
else if (strcmp(command, "statusformat") == 0)
{
sendQueryStatusFormat(updateweb);
}
else if (strcmp(command, "31DA") == 0)
{
sendQuery31DA(updateweb);
}
else if (strcmp(command, "31D9") == 0)
{
sendQuery31D9(updateweb);
}
else if (strcmp(command, "10D0") == 0)
{
filterReset(0, virtualRemotes);
}
else
{
xSemaphoreGive(mutexI2Ctask);
return false;
}
const char *source;
auto it = cmdOriginMap.find(origin);
if (it != cmdOriginMap.end())
source = it->second;
else
source = cmdOriginMap.rbegin()->second;
char originchar[30]{};
sprintf(originchar, "%s-vremote-%d", source, remoteIndex);
logLastCommand(command, originchar);
xSemaphoreGive(mutexI2Ctask);
return true;
}