forked from sfeakes/AqualinkD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_services.c.old
1440 lines (1289 loc) · 63.1 KB
/
net_services.c.old
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017 Shaun Feakes - All rights reserved
*
* You may use redistribute and/or modify this code under the terms of
* the GNU General Public License version 2 as published by the
* Free Software Foundation. For the terms of this license,
* see <http://www.gnu.org/licenses/>.
*
* You are free to use this software under the terms of the GNU General
* Public License, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* https://github.com/sfeakes/aqualinkd
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <sys/time.h>
#include <syslog.h>
#include "mongoose.h"
#include "aqualink.h"
#include "config.h"
#include "aq_programmer.h"
#include "utils.h"
#include "net_services.h"
#include "json_messages.h"
#include "domoticz.h"
#include "aq_mqtt.h"
#include "devices_jandy.h"
#include "color_lights.h"
#ifdef AQ_PDA
#include "pda.h"
#endif
//static struct aqconfig *_aqconfig_;
static struct aqualinkdata *_aqualink_data;
static char *_web_root;
static int _mqtt_exit_flag = false;
/*
static const char *s_address = "trident:1883";
static const char *s_user_name = NULL;
static const char *s_password = NULL;
static const char *s_topic = "domoticz/out";
*/
//static struct mg_mqtt_topic_expression s_topic_expr = {NULL, 0};
#ifndef MG_DISABLE_MQTT
void start_mqtt(struct mg_mgr *mgr);
static struct aqualinkdata _last_mqtt_aqualinkdata;
void mqtt_broadcast_aqualinkstate(struct mg_connection *nc);
#endif
static sig_atomic_t s_signal_received = 0;
//static const char *s_http_port = "8080";
static struct mg_serve_http_opts s_http_server_opts;
static void signal_handler(int sig_num) {
signal(sig_num, signal_handler); // Reinstantiate signal handler
s_signal_received = sig_num;
}
static int is_websocket(const struct mg_connection *nc) {
return nc->flags & MG_F_IS_WEBSOCKET && !(nc->flags & MG_F_USER_2);
}
static void set_websocket_RSraw(struct mg_connection *nc) {
nc->flags |= MG_F_USER_2;
}
static int is_websocket_RSraw(const struct mg_connection *nc) {
return nc->flags & MG_F_USER_2;
}
static int is_mqtt(const struct mg_connection *nc) {
return nc->flags & MG_F_USER_1;
}
static void set_mqtt(struct mg_connection *nc) {
nc->flags |= MG_F_USER_1;
}
static void ws_send(struct mg_connection *nc, char *msg)
{
int size = strlen(msg);
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, msg, size);
//logMessage (LOG_DEBUG, "WS: Sent %d characters '%s'\n",size, msg);
}
void broadcast_aqualinkstate_error(struct mg_connection *nc, char *msg)
{
struct mg_connection *c;
char data[JSON_STATUS_SIZE];
build_aqualink_error_status_JSON(data, JSON_STATUS_SIZE, msg);
for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) {
if (is_websocket(c))
ws_send(c, data);
}
// Maybe enhacment in future to sent error messages to MQTT
}
void broadcast_aqualinkstate(struct mg_connection *nc)
{
static int mqtt_count=0;
struct mg_connection *c;
char data[JSON_STATUS_SIZE];
build_aqualink_status_JSON(_aqualink_data, data, JSON_STATUS_SIZE);
#ifndef MG_DISABLE_MQTT
if (_mqtt_exit_flag == true) {
mqtt_count++;
if (mqtt_count >= 10) {
start_mqtt(nc->mgr);
mqtt_count = 0;
}
}
#endif
for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) {
if (is_websocket(c))
ws_send(c, data);
#ifndef MG_DISABLE_MQTT
else if (is_mqtt(c))
mqtt_broadcast_aqualinkstate(c);
#endif
}
return;
}
void send_mqtt(struct mg_connection *nc, char *toppic, char *message)
{
static uint16_t msg_id = 0;
if (toppic == NULL)
return;
if (msg_id >= 65535){msg_id=1;}else{msg_id++;}
//mg_mqtt_publish(nc, toppic, msg_id, MG_MQTT_QOS(0), message, strlen(message));
mg_mqtt_publish(nc, toppic, msg_id, MG_MQTT_RETAIN | MG_MQTT_QOS(1), message, strlen(message));
logMessage(LOG_INFO, "MQTT: Published id=%d: %s %s\n", msg_id, toppic, message);
}
void send_domoticz_mqtt_state_msg(struct mg_connection *nc, int idx, int value)
{
if (idx <= 0)
return;
char mqtt_msg[JSON_MQTT_MSG_SIZE];
build_mqtt_status_JSON(mqtt_msg ,JSON_MQTT_MSG_SIZE, idx, value, TEMP_UNKNOWN);
send_mqtt(nc, _aqconfig_.mqtt_dz_pub_topic, mqtt_msg);
}
void send_domoticz_mqtt_temp_msg(struct mg_connection *nc, int idx, int value)
{
if (idx <= 0)
return;
char mqtt_msg[JSON_MQTT_MSG_SIZE];
build_mqtt_status_JSON(mqtt_msg ,JSON_MQTT_MSG_SIZE, idx, 0, (_aqualink_data->temp_units==FAHRENHEIT && _aqconfig_.convert_dz_temp)?roundf(degFtoC(value)):value);
send_mqtt(nc, _aqconfig_.mqtt_dz_pub_topic, mqtt_msg);
}
void send_domoticz_mqtt_numeric_msg(struct mg_connection *nc, int idx, int value)
{
if (idx <= 0)
return;
char mqtt_msg[JSON_MQTT_MSG_SIZE];
build_mqtt_status_JSON(mqtt_msg ,JSON_MQTT_MSG_SIZE, idx, 0, value);
send_mqtt(nc, _aqconfig_.mqtt_dz_pub_topic, mqtt_msg);
}
void send_domoticz_mqtt_status_message(struct mg_connection *nc, int idx, int value, char *svalue) {
if (idx <= 0)
return;
char mqtt_msg[JSON_MQTT_MSG_SIZE];
build_mqtt_status_message_JSON(mqtt_msg, JSON_MQTT_MSG_SIZE, idx, value, svalue);
send_mqtt(nc, _aqconfig_.mqtt_dz_pub_topic, mqtt_msg);
}
void send_mqtt_state_msg(struct mg_connection *nc, char *dev_name, aqledstate state)
{
static char mqtt_pub_topic[250];
sprintf(mqtt_pub_topic, "%s/%s/delay",_aqconfig_.mqtt_aq_topic, dev_name);
send_mqtt(nc, mqtt_pub_topic, (state==FLASH?MQTT_ON:MQTT_OFF));
sprintf(mqtt_pub_topic, "%s/%s",_aqconfig_.mqtt_aq_topic, dev_name);
send_mqtt(nc, mqtt_pub_topic, (state==OFF?MQTT_OFF:MQTT_ON));
}
//void send_mqtt_aux_msg(struct mg_connection *nc, char *root_topic, int dev_index, char *dev_topic, int value)
void send_mqtt_aux_msg(struct mg_connection *nc, char *dev_name, char *dev_topic, int value)
{
static char mqtt_pub_topic[250];
static char msg[10];
sprintf(msg, "%d", value);
//sprintf(mqtt_pub_topic, "%s/%s%d%s",_aqconfig_.mqtt_aq_topic, root_topic, dev_index, dev_topic);
sprintf(mqtt_pub_topic, "%s/%s%s",_aqconfig_.mqtt_aq_topic, dev_name, dev_topic);
send_mqtt(nc, mqtt_pub_topic, msg);
}
void send_mqtt_heater_state_msg(struct mg_connection *nc, char *dev_name, aqledstate state)
{
static char mqtt_pub_topic[250];
sprintf(mqtt_pub_topic, "%s/%s",_aqconfig_.mqtt_aq_topic, dev_name);
if (state == ENABLE) {
send_mqtt(nc, mqtt_pub_topic, MQTT_OFF);
sprintf(mqtt_pub_topic, "%s/%s%s",_aqconfig_.mqtt_aq_topic, dev_name, ENABELED_SUBT);
send_mqtt(nc, mqtt_pub_topic, MQTT_ON);
} else {
send_mqtt(nc, mqtt_pub_topic, (state==OFF?MQTT_OFF:MQTT_ON));
sprintf(mqtt_pub_topic, "%s/%s%s",_aqconfig_.mqtt_aq_topic, dev_name, ENABELED_SUBT);
send_mqtt(nc, mqtt_pub_topic, (state==OFF?MQTT_OFF:MQTT_ON));
}
}
// NSF need to change this function to the _new once finished.
void send_mqtt_temp_msg(struct mg_connection *nc, char *dev_name, long value)
{
static char mqtt_pub_topic[250];
static char degC[10];
sprintf(degC, "%.2f", (_aqualink_data->temp_units==FAHRENHEIT && _aqconfig_.convert_mqtt_temp)?degFtoC(value):value );
sprintf(mqtt_pub_topic, "%s/%s", _aqconfig_.mqtt_aq_topic, dev_name);
send_mqtt(nc, mqtt_pub_topic, degC);
}
/*
void send_mqtt_temp_msg_new(struct mg_connection *nc, char *dev_name, long value)
{
static char mqtt_pub_topic[250];
static char degC[5];
// NSF remove false below once we have finished.
sprintf(degC, "%.2f", (false && _aqualink_data->temp_units==FAHRENHEIT && _aqconfig_.convert_mqtt_temp)?degFtoC(value):value );
//sprintf(degC, "%d", value );
sprintf(mqtt_pub_topic, "%s/%s", _aqconfig_.mqtt_aq_topic, dev_name);
send_mqtt(nc, mqtt_pub_topic, degC);
}
*/
void send_mqtt_setpoint_msg(struct mg_connection *nc, char *dev_name, long value)
{
static char mqtt_pub_topic[250];
static char degC[10];
sprintf(degC, "%.2f", (_aqualink_data->temp_units==FAHRENHEIT && _aqconfig_.convert_mqtt_temp)?degFtoC(value):value );
sprintf(mqtt_pub_topic, "%s/%s/setpoint", _aqconfig_.mqtt_aq_topic, dev_name);
send_mqtt(nc, mqtt_pub_topic, degC);
}
void send_mqtt_numeric_msg(struct mg_connection *nc, char *dev_name, int value)
{
static char mqtt_pub_topic[250];
static char msg[10];
sprintf(msg, "%d", value);
sprintf(mqtt_pub_topic, "%s/%s", _aqconfig_.mqtt_aq_topic, dev_name);
send_mqtt(nc, mqtt_pub_topic, msg);
}
void send_mqtt_float_msg(struct mg_connection *nc, char *dev_name, float value) {
static char mqtt_pub_topic[250];
static char msg[10];
sprintf(msg, "%.2f", value);
sprintf(mqtt_pub_topic, "%s/%s", _aqconfig_.mqtt_aq_topic, dev_name);
send_mqtt(nc, mqtt_pub_topic, msg);
}
void send_mqtt_int_msg(struct mg_connection *nc, char *dev_name, int value) {
send_mqtt_numeric_msg(nc, dev_name, value);
/*
static char mqtt_pub_topic[250];
static char msg[10];
sprintf(msg, "%d", value);
sprintf(mqtt_pub_topic, "%s/%s", _aqconfig_.mqtt_aq_topic, dev_name);
send_mqtt(nc, mqtt_pub_topic, msg);
*/
}
void send_mqtt_string_msg(struct mg_connection *nc, char *dev_name, char *msg) {
static char mqtt_pub_topic[250];
sprintf(mqtt_pub_topic, "%s/%s", _aqconfig_.mqtt_aq_topic, dev_name);
send_mqtt(nc, mqtt_pub_topic, msg);
}
void mqtt_broadcast_aqualinkstate(struct mg_connection *nc)
{
static int cnt=0;
//static long int lastFlashTm = 0;
//static bool lastFlash = false;
bool force_update = false;
if (cnt > 300) { // 100 = about every 2 minutes.
force_update = true;
cnt = 0;
} else {
cnt++;
force_update = false;
}
int i;
//logMessage(LOG_INFO, "mqtt_broadcast_aqualinkstate: START\n");
if (_aqualink_data->service_mode_state != _last_mqtt_aqualinkdata.service_mode_state) {
_last_mqtt_aqualinkdata.service_mode_state = _aqualink_data->service_mode_state;
send_mqtt_string_msg(nc, SERVICE_MODE_TOPIC, _aqualink_data->service_mode_state==OFF?MQTT_OFF:(_aqualink_data->service_mode_state==FLASH?MQTT_FLASH:MQTT_ON));
}
if (_aqualink_data->air_temp != TEMP_UNKNOWN && _aqualink_data->air_temp != _last_mqtt_aqualinkdata.air_temp) {
_last_mqtt_aqualinkdata.air_temp = _aqualink_data->air_temp;
send_mqtt_temp_msg(nc, AIR_TEMP_TOPIC, _aqualink_data->air_temp);
//send_mqtt_temp_msg_new(nc, AIR_TEMPERATURE_TOPIC, _aqualink_data->air_temp);
send_domoticz_mqtt_temp_msg(nc, _aqconfig_.dzidx_air_temp, _aqualink_data->air_temp);
}
/*
if (_aqualink_data->pool_temp != TEMP_UNKNOWN && _aqualink_data->pool_temp != _last_mqtt_aqualinkdata.pool_temp) {
_last_mqtt_aqualinkdata.pool_temp = _aqualink_data->pool_temp;
send_mqtt_temp_msg(nc, POOL_TEMP_TOPIC, _aqualink_data->pool_temp);
send_domoticz_mqtt_temp_msg(nc, _aqconfig_.dzidx_pool_water_temp, _aqualink_data->pool_temp);
// IF spa is off, report pool water temp to Domoticz.
if (_aqualink_data->spa_temp == TEMP_UNKNOWN)
send_domoticz_mqtt_temp_msg(nc, _aqconfig_.dzidx_spa_water_temp, _aqualink_data->pool_temp);
}
*/
if (_aqualink_data->pool_temp != _last_mqtt_aqualinkdata.pool_temp) {
if (_aqualink_data->pool_temp == TEMP_UNKNOWN && _aqconfig_.report_zero_pool_temp) {
_last_mqtt_aqualinkdata.pool_temp = TEMP_UNKNOWN;
send_mqtt_temp_msg(nc, POOL_TEMP_TOPIC, (_aqconfig_.convert_mqtt_temp?-18:0));
} else if (_aqualink_data->pool_temp != TEMP_UNKNOWN) {
_last_mqtt_aqualinkdata.pool_temp = _aqualink_data->pool_temp;
send_mqtt_temp_msg(nc, POOL_TEMP_TOPIC, _aqualink_data->pool_temp);
send_domoticz_mqtt_temp_msg(nc, _aqconfig_.dzidx_pool_water_temp, _aqualink_data->pool_temp);
// IF spa is off, report pool water temp to Domoticz.
if (_aqualink_data->spa_temp == TEMP_UNKNOWN)
send_domoticz_mqtt_temp_msg(nc, _aqconfig_.dzidx_spa_water_temp, _aqualink_data->pool_temp);
}
}
if (_aqualink_data->spa_temp != _last_mqtt_aqualinkdata.spa_temp) {
if (_aqualink_data->spa_temp == TEMP_UNKNOWN && _aqconfig_.report_zero_spa_temp) {
_last_mqtt_aqualinkdata.spa_temp = TEMP_UNKNOWN;
send_mqtt_temp_msg(nc, SPA_TEMP_TOPIC, (_aqconfig_.convert_mqtt_temp?-18:0));
} else if (_aqualink_data->spa_temp != TEMP_UNKNOWN) {
_last_mqtt_aqualinkdata.spa_temp = _aqualink_data->spa_temp;
send_mqtt_temp_msg(nc, SPA_TEMP_TOPIC, _aqualink_data->spa_temp);
send_domoticz_mqtt_temp_msg(nc, _aqconfig_.dzidx_spa_water_temp, _aqualink_data->spa_temp);
}
}
if (_aqualink_data->pool_htr_set_point != TEMP_UNKNOWN && _aqualink_data->pool_htr_set_point != _last_mqtt_aqualinkdata.pool_htr_set_point) {
_last_mqtt_aqualinkdata.pool_htr_set_point = _aqualink_data->pool_htr_set_point;
send_mqtt_setpoint_msg(nc, BTN_POOL_HTR, _aqualink_data->pool_htr_set_point);
// removed until domoticz has a better virtuel thermostat
//send_domoticz_mqtt_temp_msg(nc, _aqconfig_.dzidx_pool_thermostat, _aqualink_data->pool_htr_set_point);
}
if (_aqualink_data->spa_htr_set_point != TEMP_UNKNOWN && _aqualink_data->spa_htr_set_point != _last_mqtt_aqualinkdata.spa_htr_set_point) {
_last_mqtt_aqualinkdata.spa_htr_set_point = _aqualink_data->spa_htr_set_point;
send_mqtt_setpoint_msg(nc, BTN_SPA_HTR, _aqualink_data->spa_htr_set_point);
}
/*
if (_aqualink_data->service_mode_state != _last_mqtt_aqualinkdata.service_mode_state) {
_last_mqtt_aqualinkdata.service_mode_state = _aqualink_data->service_mode_state;
send_mqtt_string_msg(nc, SERVICE_MODE_TOPIC, _aqualink_data->service_mode_state==ON?MQTT_ON:MQTT_OFF);
}
*/
if (_aqualink_data->frz_protect_set_point != TEMP_UNKNOWN && _aqualink_data->frz_protect_set_point != _last_mqtt_aqualinkdata.frz_protect_set_point) {
_last_mqtt_aqualinkdata.frz_protect_set_point = _aqualink_data->frz_protect_set_point;
send_mqtt_setpoint_msg(nc, FREEZE_PROTECT, _aqualink_data->frz_protect_set_point);
send_mqtt_string_msg(nc, FREEZE_PROTECT, _aqualink_data->frz_protect_state==ON?MQTT_ON:MQTT_OFF);
_last_mqtt_aqualinkdata.frz_protect_state = _aqualink_data->frz_protect_state;
send_mqtt_string_msg(nc, FREEZE_PROTECT_ENABELED, MQTT_ON);
/*
send_mqtt_string_msg(nc, FREEZE_PROTECT_ENABELED, MQTT_ON);
//send_domoticz_mqtt_temp_msg(nc, _aqconfig_.dzidx_rfz_protect, _aqualink_data->frz_protect_set_point);
} else if (_aqualink_data->frz_protect_set_point != _last_mqtt_aqualinkdata.frz_protect_set_point) {
_last_mqtt_aqualinkdata.frz_protect_set_point = _aqualink_data->frz_protect_set_point;
send_mqtt_string_msg(nc, FREEZE_PROTECT_ENABELED, MQTT_OFF);*/
}
if (_aqualink_data->frz_protect_state != _last_mqtt_aqualinkdata.frz_protect_state) {
_last_mqtt_aqualinkdata.frz_protect_state = _aqualink_data->frz_protect_state;
send_mqtt_string_msg(nc, FREEZE_PROTECT, _aqualink_data->frz_protect_state==ON?MQTT_ON:MQTT_OFF);
}
if (_aqualink_data->battery != _last_mqtt_aqualinkdata.battery) {
_last_mqtt_aqualinkdata.battery = _aqualink_data->battery;
send_mqtt_string_msg(nc, BATTERY_STATE, _aqualink_data->battery==OK?MQTT_ON:MQTT_OFF);
}
if (_aqualink_data->ph != TEMP_UNKNOWN && _aqualink_data->ph != _last_mqtt_aqualinkdata.ph) {
_last_mqtt_aqualinkdata.ph = _aqualink_data->ph;
send_mqtt_float_msg(nc, CHEM_PH_TOPIC, _aqualink_data->ph);
send_mqtt_float_msg(nc, CHRM_PH_F_TOPIC, roundf(degFtoC(_aqualink_data->ph)));
}
if (_aqualink_data->orp != TEMP_UNKNOWN && _aqualink_data->orp != _last_mqtt_aqualinkdata.orp) {
_last_mqtt_aqualinkdata.orp = _aqualink_data->orp;
send_mqtt_numeric_msg(nc, CHEM_ORP_TOPIC, _aqualink_data->orp);
send_mqtt_float_msg(nc, CHRM_ORP_F_TOPIC, roundf(degFtoC(_aqualink_data->orp)));
}
if (_aqualink_data->ar_swg_status != _last_mqtt_aqualinkdata.ar_swg_status ||
_aqualink_data->swg_percent != _last_mqtt_aqualinkdata.swg_percent) { // Percent can also effect status
char message[30];
int status;
int dzalert;
get_swg_status_mqtt(_aqualink_data, message, &status, &dzalert);
if (!_aqualink_data->simulate_panel)
sprintf(_aqualink_data->last_display_message, message);
send_domoticz_mqtt_status_message(nc, _aqconfig_.dzidx_swg_status, dzalert, &message[9]);
send_mqtt_int_msg(nc, SWG_TOPIC, status);
if (_aqualink_data->ar_swg_status == SWG_STATUS_OFF)
send_mqtt_int_msg(nc, SWG_ENABELED_TOPIC, SWG_OFF);
else
send_mqtt_int_msg(nc, SWG_ENABELED_TOPIC, SWG_ON);
send_mqtt_int_msg(nc, SWG_EXTENDED_TOPIC, (int)_aqualink_data->ar_swg_status);
_last_mqtt_aqualinkdata.ar_swg_status = _aqualink_data->ar_swg_status;
}
if (_aqualink_data->boost != _last_mqtt_aqualinkdata.boost) {
send_mqtt_int_msg(nc, SWG_BOOST_TOPIC, _aqualink_data->boost);
_last_mqtt_aqualinkdata.boost = _aqualink_data->boost;
}
//if (_aqualink_data->ar_swg_status == SWG_STATUS_ON) { // If the SWG is actually on
if (_aqualink_data->ar_swg_status != SWG_STATUS_OFF) { // If the SWG is actually on
if (_aqualink_data->swg_percent != TEMP_UNKNOWN && (force_update || _aqualink_data->swg_percent != _last_mqtt_aqualinkdata.swg_percent)) {
_last_mqtt_aqualinkdata.swg_percent = _aqualink_data->swg_percent;
send_mqtt_numeric_msg(nc, SWG_PERCENT_TOPIC, _aqualink_data->swg_percent);
send_mqtt_float_msg(nc, SWG_PERCENT_F_TOPIC, roundf(degFtoC(_aqualink_data->swg_percent)));
send_mqtt_float_msg(nc, SWG_SETPOINT_TOPIC, roundf(degFtoC(_aqualink_data->swg_percent)));
send_domoticz_mqtt_numeric_msg(nc, _aqconfig_.dzidx_swg_percent, _aqualink_data->swg_percent);
}
if (_aqualink_data->swg_ppm != TEMP_UNKNOWN && ( force_update || _aqualink_data->swg_ppm != _last_mqtt_aqualinkdata.swg_ppm)) {
_last_mqtt_aqualinkdata.swg_ppm = _aqualink_data->swg_ppm;
send_mqtt_numeric_msg(nc, SWG_PPM_TOPIC, _aqualink_data->swg_ppm);
send_mqtt_float_msg(nc, SWG_PPM_F_TOPIC, roundf(degFtoC(_aqualink_data->swg_ppm)));
send_domoticz_mqtt_numeric_msg(nc, _aqconfig_.dzidx_swg_ppm, _aqualink_data->swg_ppm);
}
}
//logMessage(LOG_INFO, "mqtt_broadcast_aqualinkstate: START LEDs\n");
//if (time(NULL) % 2) {} <-- use to determin odd/even second in time to make state flash on enabled.
// Loop over LED's and send any changes.
for (i=0; i < _aqualink_data->total_buttons; i++) {
/*
if ( _aqualink_data->aqbuttons[i].led->state == FLASH && _aqconfig_.flash_mqtt_buttons == true ) {
// Simply send on or off depending on if current second is odd or even.
// will send too many off and on messages as we get hit multiple times a second, but most effecient way to handle this
// considering flash is not very often and not for long.
time_t now;
now = time(NULL);
send_mqtt_state_msg(nc, _aqualink_data->aqbuttons[i].name, (now % 2)?OFF:ON);
logMessage(LOG_DEBUG, "Flash button : %s %s\n",_aqualink_data->aqbuttons[i].name,(now % 2)?"off":"on");
} else */if (_last_mqtt_aqualinkdata.aqualinkleds[i].state != _aqualink_data->aqbuttons[i].led->state) {
_last_mqtt_aqualinkdata.aqualinkleds[i].state = _aqualink_data->aqbuttons[i].led->state;
if (_aqualink_data->aqbuttons[i].code == KEY_POOL_HTR || _aqualink_data->aqbuttons[i].code == KEY_SPA_HTR) {
send_mqtt_heater_state_msg(nc, _aqualink_data->aqbuttons[i].name, _aqualink_data->aqbuttons[i].led->state);
} else {
send_mqtt_state_msg(nc, _aqualink_data->aqbuttons[i].name, _aqualink_data->aqbuttons[i].led->state);
}
if (_aqualink_data->aqbuttons[i].dz_idx != DZ_NULL_IDX)
send_domoticz_mqtt_state_msg(nc, _aqualink_data->aqbuttons[i].dz_idx, (_aqualink_data->aqbuttons[i].led->state==OFF?DZ_OFF:DZ_ON));
}
}
// Loop over Pumps
for (i=0; i < _aqualink_data->num_pumps; i++) {
//_aqualink_data->pumps[i].rpm = TEMP_UNKNOWN;
//_aqualink_data->pumps[i].gph = TEMP_UNKNOWN;
//_aqualink_data->pumps[i].watts = TEMP_UNKNOWN;
if (_aqualink_data->pumps[i].rpm != TEMP_UNKNOWN && _aqualink_data->pumps[i].rpm != _last_mqtt_aqualinkdata.pumps[i].rpm) {
_last_mqtt_aqualinkdata.pumps[i].rpm = _aqualink_data->pumps[i].rpm;
//send_mqtt_aux_msg(nc, PUMP_TOPIC, i+1, PUMP_RPM_TOPIC, _aqualink_data->pumps[i].rpm);
send_mqtt_aux_msg(nc, _aqualink_data->pumps[i].button->name, PUMP_RPM_TOPIC, _aqualink_data->pumps[i].rpm);
}
if (_aqualink_data->pumps[i].gpm != TEMP_UNKNOWN && _aqualink_data->pumps[i].gpm != _last_mqtt_aqualinkdata.pumps[i].gpm) {
_last_mqtt_aqualinkdata.pumps[i].gpm = _aqualink_data->pumps[i].gpm;
//send_mqtt_aux_msg(nc, PUMP_TOPIC, i+1, PUMP_GPH_TOPIC, _aqualink_data->pumps[i].gph);
send_mqtt_aux_msg(nc, _aqualink_data->pumps[i].button->name, PUMP_GPM_TOPIC, _aqualink_data->pumps[i].gpm);
}
if (_aqualink_data->pumps[i].watts != TEMP_UNKNOWN && _aqualink_data->pumps[i].watts != _last_mqtt_aqualinkdata.pumps[i].watts) {
_last_mqtt_aqualinkdata.pumps[i].watts = _aqualink_data->pumps[i].watts;
//send_mqtt_aux_msg(nc, PUMP_TOPIC, i+1, PUMP_WATTS_TOPIC, _aqualink_data->pumps[i].watts);
send_mqtt_aux_msg(nc, _aqualink_data->pumps[i].button->name, PUMP_WATTS_TOPIC, _aqualink_data->pumps[i].watts);
}
}
/*
// Loop over LED's and send any changes.
for (i=0; i < TOTAL_BUTTONS; i++) {
//logMessage(LOG_NOTICE, "%s = %d, last=%d", _aqualink_data->aqbuttons[i].name, _aqualink_data->aqbuttons[i].led->state, _last_mqtt_aqualinkdata.aqualinkleds[i].state);
//logMessage(LOG_INFO, "LED %d : new state %d | old state %d\n", i, _aqualink_data->aqbuttons[i].led->state, _last_mqtt_aqualinkdata.aqualinkleds[i].state);
if (_last_mqtt_aqualinkdata.aqualinkleds[i].state != _aqualink_data->aqbuttons[i].led->state){
_last_mqtt_aqualinkdata.aqualinkleds[i].state = _aqualink_data->aqbuttons[i].led->state;
//if (_aqualink_data->aqbuttons[i].dz_idx != DZ_NULL_IDX) {
if (_aqualink_data->aqbuttons[i].code == KEY_POOL_HTR || _aqualink_data->aqbuttons[i].code == KEY_SPA_HTR) {
send_mqtt_heater_state_msg(nc, _aqualink_data->aqbuttons[i].name, _aqualink_data->aqbuttons[i].led->state);
} else if (_aqualink_data->aqbuttons[i].led->state == FLASH && _aqconfig_.flash_mqtt_buttons == true) {
// This messed up the origional LED state, which means we send the flash to WEB UI as well.
time_t now;
now = time(NULL);
if ( now != lastFlashTm ) {
lastFlashTm = now;
_aqualink_data->aqbuttons[i].led->state = lastFlash;
_last_mqtt_aqualinkdata.aqualinkleds[i].state = !lastFlash;
send_mqtt_state_msg(nc, _aqualink_data->aqbuttons[i].name, _last_mqtt_aqualinkdata.aqualinkleds[i].state);
logMessage(LOG_DEBUG, "Flash button : %s\n",_aqualink_data->aqbuttons[i].name);
}
} else {
send_mqtt_state_msg(nc, _aqualink_data->aqbuttons[i].name, _aqualink_data->aqbuttons[i].led->state);
}
if (_aqualink_data->aqbuttons[i].dz_idx != DZ_NULL_IDX)
send_domoticz_mqtt_state_msg(nc, _aqualink_data->aqbuttons[i].dz_idx, (_aqualink_data->aqbuttons[i].led->state==OFF?DZ_OFF:DZ_ON));
//}
// Send mqtt
}
}*/
//logMessage(LOG_INFO, "mqtt_broadcast_aqualinkstate: END\n");
}
//
int getTempforMeteohub(char *buffer)
{
int length = 0;
if (_aqualink_data->air_temp != TEMP_UNKNOWN)
length += sprintf(buffer+length, "t0 %d\n",(int)degFtoC(_aqualink_data->air_temp)*10);
else
length += sprintf(buffer+length, "t0 \n");
if (_aqualink_data->pool_temp != TEMP_UNKNOWN)
length += sprintf(buffer+length, "t1 %d\n",(int)degFtoC(_aqualink_data->pool_temp)*10);
else
length += sprintf(buffer+length, "t1 \n");
return strlen(buffer);
}
void set_light_mode(char *value, int button)
{
int i;
clight_detail *light = NULL;
#ifdef AQ_PDA
if (_aqconfig_.pda_mode == true) {
logMessage(LOG_ERR, "Light mode control not supported in PDA mode\n");
return;
}
#endif
for (i=0; i < _aqualink_data->num_lights; i++) {
if (&_aqualink_data->aqbuttons[button] == _aqualink_data->lights[i].button) {
// Found the programmable light
light = &_aqualink_data->lights[i];
break;
}
}
if (light == NULL) {
logMessage(LOG_ERR, "Light mode control not configured for button %d\n",button);
return;
}
char buf[LIGHT_MODE_BUFER];
if (light->lightType == LC_PROGRAMABLE ) {
// 5 below is light index, need to look this up so it's not hard coded.
sprintf(buf, "%-5s%-5d%-5d%-5d%.2f",value,
button,
_aqconfig_.light_programming_initial_on,
_aqconfig_.light_programming_initial_off,
_aqconfig_.light_programming_mode );
//logMessage(LOG_NOTICE, "WEB: requset light mode %s\n", buf);
aq_programmer(AQ_SET_LIGHTPROGRAM_MODE, buf, _aqualink_data);
} else {
sprintf(buf, "%-5s%-5d%-5d",value, button, light->lightType);
aq_programmer(AQ_SET_LIGHTCOLOR_MODE, buf, _aqualink_data);
}
}
void action_web_request(struct mg_connection *nc, struct http_message *http_msg) {
// struct http_message *http_msg = (struct http_message *)ev_data;
if (getLogLevel() >= LOG_INFO) { // Simply for log message, check we are at
// this log level before running all this
// junk
char *uri = (char *)malloc(http_msg->uri.len + http_msg->query_string.len + 2);
strncpy(uri, http_msg->uri.p, http_msg->uri.len + http_msg->query_string.len + 1);
uri[http_msg->uri.len + http_msg->query_string.len + 1] = '\0';
logMessage(LOG_INFO, "URI request: '%s'\n", uri);
free(uri);
}
// If we have a get request, pass it
if (strstr(http_msg->method.p, "GET") && http_msg->query_string.len > 0) {
char command[20];
mg_get_http_var(&http_msg->query_string, "command", command, sizeof(command));
logMessage(LOG_INFO, "WEB: Message command='%s'\n", command);
if (strcmp(command, "dynamic_config") == 0) {
char data[JSON_BUFFER_SIZE];
int size = build_color_lights_js(_aqualink_data, data, JSON_BUFFER_SIZE);
mg_send_head(nc, 200, size, "Content-Type: text/javascript");
mg_send(nc, data, size);
} else
// if (strstr(http_msg->query_string.p, "command=status")) {
if (strcmp(command, "status") == 0) {
char data[JSON_STATUS_SIZE];
int size = build_aqualink_status_JSON(_aqualink_data, data, JSON_STATUS_SIZE);
mg_send_head(nc, 200, size, "Content-Type: application/json");
mg_send(nc, data, size);
//} else if (strstr(http_msg->query_string.p, "command=mhstatus")) {
} else if (strcmp(command, "mhstatus") == 0) {
char data[20];
int size = getTempforMeteohub(data);
mg_send_head(nc, 200, size, "Content-Type: text/plain");
mg_send(nc, data, size);
} else if (strcmp(command, "poollightmode") == 0) {
logMessage(LOG_ERR, "WEB: poollightmode taken out for update (forgot to put it back)\n");
/*
char value[20];
mg_get_http_var(&http_msg->query_string, "value", value, sizeof(value));
//aq_programmer(AQ_SET_COLORMODE, value, _aqualink_data);
set_light_mode(value, _aqconfig_.light_programming_button_pool);
mg_send_head(nc, 200, strlen(GET_RTN_OK), "Content-Type: text/plain");
mg_send(nc, GET_RTN_OK, strlen(GET_RTN_OK));
*/
} else if (strcmp(command, "spalightmode") == 0) {
logMessage(LOG_ERR, "WEB: spalightmode taken out for update (forgot to put it back)\n");
/*
char value[20];
mg_get_http_var(&http_msg->query_string, "value", value, sizeof(value));
//aq_programmer(AQ_SET_COLORMODE, value, _aqualink_data);
set_light_mode(value, _aqconfig_.light_programming_button_spa);
mg_send_head(nc, 200, strlen(GET_RTN_OK), "Content-Type: text/plain");
mg_send(nc, GET_RTN_OK, strlen(GET_RTN_OK));
*/
} else if (strcmp(command, "diag") == 0) {
aq_programmer(AQ_GET_DIAGNOSTICS_MODEL, NULL, _aqualink_data);
mg_send_head(nc, 200, strlen(GET_RTN_OK), "Content-Type: text/plain");
mg_send(nc, GET_RTN_OK, strlen(GET_RTN_OK));
} else if (strcmp(command, "swg_percent") == 0) {
char value[20];
mg_get_http_var(&http_msg->query_string, "value", value, sizeof(value));
aq_programmer(AQ_SET_SWG_PERCENT, value, _aqualink_data);
logMessage(LOG_INFO, "Web: request to set SWG to %s\n", value);
mg_send_head(nc, 200, strlen(GET_RTN_OK), "Content-Type: text/plain");
mg_send(nc, GET_RTN_OK, strlen(GET_RTN_OK));
} else if (strcmp(command, "pool_htr_set_pnt") == 0) {
char value[20];
mg_get_http_var(&http_msg->query_string, "value", value, sizeof(value));
aq_programmer(AQ_SET_POOL_HEATER_TEMP, value, _aqualink_data);
logMessage(LOG_INFO, "Web: request to set Pool heater to %s\n", value);
mg_send_head(nc, 200, strlen(GET_RTN_OK), "Content-Type: text/plain");
mg_send(nc, GET_RTN_OK, strlen(GET_RTN_OK));
} else if (strcmp(command, "spa_htr_set_pnt") == 0) {
char value[20];
mg_get_http_var(&http_msg->query_string, "value", value, sizeof(value));
aq_programmer(AQ_SET_SPA_HEATER_TEMP, value, _aqualink_data);
logMessage(LOG_INFO, "Web: request to set Spa heater to %s\n", value);
mg_send_head(nc, 200, strlen(GET_RTN_OK), "Content-Type: text/plain");
mg_send(nc, GET_RTN_OK, strlen(GET_RTN_OK));
} else if (strcmp(command, "frz_protect_set_pnt") == 0) {
char value[20];
mg_get_http_var(&http_msg->query_string, "value", value, sizeof(value));
aq_programmer(AQ_SET_FRZ_PROTECTION_TEMP, value, _aqualink_data);
logMessage(LOG_INFO, "Web: request to set Freeze protect to %s\n", value);
mg_send_head(nc, 200, strlen(GET_RTN_OK), "Content-Type: text/plain");
mg_send(nc, GET_RTN_OK, strlen(GET_RTN_OK));
} else if (strcmp(command, "extended_device_prg") == 0) {
char value[20];
char message[JSON_LABEL_SIZE];
mg_get_http_var(&http_msg->query_string, "value", value, sizeof(value));
bool prg = request2bool(value);
if (prg && !onetouch_enabled())
prg = false; // Ignore request if onetouch is not enabeled
set_extended_device_id_programming(prg);
sprintf(message,"{\"extended_device_prg\":\"%s\"}", bool2text(prg));
mg_send_head(nc, 200, strlen(message), "Content-Type: application/json");
mg_send(nc, message, strlen(message));
} else if (strcmp(command, "devices") == 0) {
char message[JSON_BUFFER_SIZE];
int size = build_device_JSON(_aqualink_data, message, JSON_BUFFER_SIZE, false);
mg_send_head(nc, 200, size, "Content-Type: application/json");
mg_send(nc, message, size);
} else if (strcmp(command, "homebridge") == 0) {
char message[JSON_BUFFER_SIZE];
int size = build_device_JSON(_aqualink_data, message, JSON_BUFFER_SIZE, true);
mg_send_head(nc, 200, size, "Content-Type: application/json");
mg_send(nc, message, size);
} else if (strcmp(command, "setconfigprm") == 0) {
char value[20];
char param[30];
char *webrtn;
mg_get_http_var(&http_msg->query_string, "param", param, sizeof(param));
mg_get_http_var(&http_msg->query_string, "value", value, sizeof(value));
if (setConfigValue(_aqualink_data, param, value)) {
webrtn = GET_RTN_OK;
logMessage(LOG_INFO, "Web: request to config %s to %s\n", param, value);
} else {
webrtn = GET_RTN_ERROR;
logMessage(LOG_ERR, "Web: request to config %s to %s failed\n", param, value);
}
mg_send_head(nc, 200, strlen(webrtn), "Content-Type: text/plain");
mg_send(nc, webrtn, strlen(webrtn));
} else if (strcmp(command, "writeconfig") == 0) {
if (writeCfg (_aqualink_data)) {
mg_send_head(nc, 200, strlen(GET_RTN_OK), "Content-Type: text/plain");
mg_send(nc, GET_RTN_OK, strlen(GET_RTN_OK));
} else {
mg_send_head(nc, 200, strlen(GET_RTN_ERROR), "Content-Type: text/plain");
mg_send(nc, GET_RTN_ERROR, strlen(GET_RTN_ERROR));
}
} else if (strcmp(command, "debug") == 0) {
char value[80];
char *rtn;
mg_get_http_var(&http_msg->query_string, "value", value, sizeof(value));
if (strcmp(value, "start") == 0) {
startInlineDebug();
rtn = GET_RTN_OK;
logMessage(LOG_DEBUG, "WEB: Started inline debug mode\n");
} else if (strcmp(value, "stop") == 0) {
logMessage(LOG_DEBUG, "WEB: Stoped inline debug mode\n");
stopInlineDebug();
rtn = GET_RTN_OK;
} else if (strcmp(value, "serialstart") == 0) {
startInlineSerialDebug();
logMessage(LOG_DEBUG, "WEB: Started inline debug mode\n");
rtn = GET_RTN_OK;
} else if (strcmp(value, "serialstop") == 0) {
logMessage(LOG_DEBUG, "WEB: Stoped inline debug mode\n");
stopInlineDebug();
rtn = GET_RTN_OK;
} else if (strcmp(value, "status") == 0) {
snprintf(value,80,"{\"sLevel\":\"%s\", \"iLevel\":%d, \"logReady\":\"%s\"}\n",elevel2text(getLogLevel()),getLogLevel(),islogFileReady()?"true":"false" );
mg_send_head(nc, 200, strlen(value), "Content-Type: text/json");
mg_send(nc, value, strlen(value));
return;
//rtn = value;
} else if (strcmp(value, "clean") == 0) {
cleanInlineDebug();
rtn = GET_RTN_OK;
} else if (strcmp(value, "download") == 0) {
mg_http_serve_file(nc, http_msg, getInlineLogFName(), mg_mk_str("text/plain"), mg_mk_str(""));
return;
} else {
rtn = GET_RTN_UNKNOWN;
}
mg_send_head(nc, 200, strlen(rtn), "Content-Type: text/plain");
mg_send(nc, rtn, strlen(rtn));
} else if (strncmp(command, "Pump_", 5) == 0) {
// Set Pump RPM
bool found = false;
int pumpIndex = atoi(command+5); // Check for 0
char value[10];
mg_get_http_var(&http_msg->query_string, "value", value, sizeof(value));
int rpm = atoi(value);
// Check for pumpIndex = 0 (BAD) and check RPM Value
//printf("******** ADD CHECK FOR PUMP & RPM HERE ********\n");
int pi;
for (pi=0; pi < _aqualink_data->num_pumps; pi++) {
if (_aqualink_data->pumps[pi].pumpIndex == pumpIndex) {
logMessage(LOG_NOTICE, "WEB: request to change pump %d to %d\n",pumpIndex, round(rpm));
_aqualink_data->unactioned.type = PUMP_RPM;
_aqualink_data->unactioned.value = round(rpm);
_aqualink_data->unactioned.id = pumpIndex;
found=true;
break;
}
}
if(!found)
logMessage(LOG_ERR, "WEB: Didn't find pump %d from command %s\n",pumpIndex,command);
} else {
int i;
for (i = 0; i < _aqualink_data->total_buttons; i++) {
if (strcmp(command, _aqualink_data->aqbuttons[i].name) == 0) {
char value[20];
char *rtn;
mg_get_http_var(&http_msg->query_string, "value", value, sizeof(value));
// logMessage (LOG_INFO, "Web Message command='%s'\n",command);
// aq_programmer(AQ_SEND_CMD, (char
// *)&_aqualink_data->aqbuttons[i].code, _aqualink_data);
logMessage(LOG_DEBUG, "WEB: Message request '%s' change state to '%s'\n", command, value);
#ifdef AQ_PDA
if (_aqconfig_.pda_mode == true) {
char msg[PTHREAD_ARG];
sprintf(msg, "%-5d%-5d",i, (strcmp(value, "on") == 0)?ON:OFF);
//printf("******* '%s' ********\n",msg);
aq_programmer(AQ_PDA_DEVICE_ON_OFF, msg, _aqualink_data);
rtn = GET_RTN_OK;
}
else
#endif
if (strcmp(value, "on") == 0) {
if (_aqualink_data->aqbuttons[i].led->state == OFF || _aqualink_data->aqbuttons[i].led->state == FLASH) {
//aq_programmer(AQ_SEND_CMD, (char *)&_aqualink_data->aqbuttons[i].code, _aqualink_data);
aq_send_cmd(_aqualink_data->aqbuttons[i].code);
rtn = GET_RTN_OK;
logMessage(LOG_INFO, "WEB: turn ON '%s' changed state to '%s'\n", command, value);
} else {
rtn = GET_RTN_NOT_CHANGED;
logMessage(LOG_INFO, "WEB: '%s' is already on '%s', current state %d\n", command, value, _aqualink_data->aqbuttons[i].led->state);
}
} else if (strcmp(value, "off") == 0) {
if (_aqualink_data->aqbuttons[i].led->state == ON ||
_aqualink_data->aqbuttons[i].led->state == ENABLE ||
_aqualink_data->aqbuttons[i].led->state == FLASH) {
//aq_programmer(AQ_SEND_CMD, (char *)&_aqualink_data->aqbuttons[i].code, _aqualink_data);
aq_send_cmd(_aqualink_data->aqbuttons[i].code);
rtn = GET_RTN_OK;
logMessage(LOG_INFO, "WEB: turn Off '%s' changed state to '%s'\n", command, value);
} else {
rtn = GET_RTN_NOT_CHANGED;
logMessage(LOG_INFO, "WEB: '%s' is already off '%s', current state %d\n", command, value, _aqualink_data->aqbuttons[i].led->state);
}
} else { // Blind switch
//aq_programmer(AQ_SEND_CMD, (char *)&_aqualink_data->aqbuttons[i].code, _aqualink_data);
aq_send_cmd(_aqualink_data->aqbuttons[i].code);
rtn = GET_RTN_OK;
logMessage(LOG_INFO, "WEB: '%s' blindly changed state\n", command, value);
}
logMessage(LOG_DEBUG, "WEB: On=%d, Off=%d, Enable=%d, Flash=%d\n", ON, OFF, ENABLE, FLASH);
// NSF change OK and 2 below to a constant
mg_send_head(nc, 200, strlen(rtn), "Content-Type: text/plain");
mg_send(nc, rtn, strlen(rtn));
// NSF place check we found command here
}
}
}
// If we get here, got a bad query
mg_send_head(nc, 200, strlen(GET_RTN_UNKNOWN), "Content-Type: text/plain");
mg_send(nc, GET_RTN_UNKNOWN, strlen(GET_RTN_UNKNOWN));
} else {
struct mg_serve_http_opts opts;
memset(&opts, 0, sizeof(opts)); // Reset all options to defaults
opts.document_root = _web_root; // Serve files from the current directory
// logMessage (LOG_DEBUG, "Doc root=%s\n",opts.document_root);
mg_serve_http(nc, http_msg, s_http_server_opts);
}
}
void action_websocket_request(struct mg_connection *nc, struct websocket_message *wm) {
char buffer[50];
struct JSONwebrequest request;
#ifdef AQ_PDA
// Any websocket request means UI is active, so don't let AqualinkD go to sleep if in PDA mode
if (pda_mode())
pda_reset_sleep();
#endif
strncpy(buffer, (char *)wm->data, wm->size);
buffer[wm->size] = '\0';
// logMessage (LOG_DEBUG, "buffer '%s'\n", buffer);
parseJSONwebrequest(buffer, &request);
logMessage(LOG_INFO, "WS: Message - Key '%s' Value '%s' | Key2 '%s' Value2 '%s'\n", request.first.key, request.first.value, request.second.key,
request.second.value);
/*
if (strcmp(request.first.key, "raw") == 0 || strcmp(request.first.key, "simulator") == 0 )
_aqualink_data->simulate_panel = true;
else
_aqualink_data->simulate_panel = false;
*/
if (strcmp(request.first.key, "raw") == 0) {
_aqualink_data->simulate_panel = true;
logMessage (LOG_NOTICE, "WS: Send raw command to controller %s\n",request.first.value);
unsigned int n;
sscanf(request.first.value, "0x%2x", &n);
//aq_programmer(AQ_SEND_CMD, (char *)&n, NULL);
aq_send_cmd((unsigned char)n);
//char message[JSON_LABEL_SIZE*10];
//build_device_JSON(_aqualink_data, _aqconfig_.light_programming_button, message, JSON_LABEL_SIZE*10);
//ws_send(nc, message);
} else if (strcmp(request.first.key, "mode") == 0) {
if (strcmp(request.first.value, "onetouchraw") == 0) {
set_websocket_RSraw(nc);
}
} else if (strcmp(request.first.key, "command") == 0) {
_aqualink_data->simulate_panel = false;
if (strcmp(request.first.value, "GET_AUX_LABELS") == 0) {
char labels[JSON_LABEL_SIZE];
build_aux_labels_JSON(_aqualink_data, labels, JSON_LABEL_SIZE);
ws_send(nc, labels);
} else if (strcmp(request.first.value, "GET_DEVICES") == 0) {
char message[JSON_BUFFER_SIZE];
build_device_JSON(_aqualink_data, message, JSON_BUFFER_SIZE, false);
ws_send(nc, message);
} else if ( strcmp(request.first.value, "simulator") == 0) {
_aqualink_data->simulate_panel = true;
logMessage(LOG_INFO, "WS: Set Simulator mode");
char labels[JSON_LABEL_SIZE];
build_aux_labels_JSON(_aqualink_data, labels, JSON_LABEL_SIZE);
ws_send(nc, labels);
} else if ( strcmp(request.first.value, SWG_BOOST_TOPIC) == 0) {
//logMessage(LOG_INFO, "Boost ");
if (request.second.value != NULL)
_aqualink_data->unactioned.value = request2bool(request.second.value);
else
_aqualink_data->unactioned.value = !_aqualink_data->boost;
_aqualink_data->unactioned.type = SWG_BOOST;
} else { // Search for value in command list
int i;
for (i = 0; i < _aqualink_data->total_buttons; i++) {
if (strcmp(request.first.value, _aqualink_data->aqbuttons[i].name) == 0) {
logMessage (LOG_INFO, "WS: button '%s' pressed\n",_aqualink_data->aqbuttons[i].name);
#ifdef AQ_PDA
if (_aqconfig_.pda_mode) {
char msg[PTHREAD_ARG];
sprintf(msg, "%-5d%-5d",i, (_aqualink_data->aqbuttons[i].led->state!=OFF)?OFF:ON);
aq_programmer(AQ_PDA_DEVICE_ON_OFF, msg, _aqualink_data);
} else
#endif
{
logMessage (LOG_DEBUG, "WS: request 0x%02hhx to be sent to controller\n",_aqualink_data->aqbuttons[i].code);
aq_send_cmd(_aqualink_data->aqbuttons[i].code);
}
break;
// NSF place check we found command here
}
}
}
} else if (strcmp(request.first.key, "parameter") == 0) {
_aqualink_data->simulate_panel = false;
if (strcmp(request.first.value, "FRZ_PROTECT") == 0 || strcmp(request.first.value, FREEZE_PROTECT) == 0) {
aq_programmer(AQ_SET_FRZ_PROTECTION_TEMP, request.second.value, _aqualink_data);
} else if (strcmp(request.first.value, "POOL_HTR") == 0 || strcmp(request.first.value, BTN_POOL_HTR) == 0 ) {
aq_programmer(AQ_SET_POOL_HEATER_TEMP, request.second.value, _aqualink_data);
} else if (strcmp(request.first.value, "SPA_HTR") == 0 || strcmp(request.first.value, BTN_SPA_HTR) == 0) {
aq_programmer(AQ_SET_SPA_HEATER_TEMP, request.second.value, _aqualink_data);
} else if (strcmp(request.first.value, SWG_TOPIC) == 0) {
//aq_programmer(AQ_SET_SWG_PERCENT, request.second.value, _aqualink_data);
int value = setpoint_check(SWG_SETPOINT, atoi(request.second.value), _aqualink_data);
if (_aqualink_data->ar_swg_status == SWG_STATUS_OFF ) {
// SWG is off, can't set %, so delay the set until it's on.
_aqualink_data->swg_delayed_percent = value;
} else {
aq_programmer(AQ_SET_SWG_PERCENT, request.second.value, _aqualink_data);
_aqualink_data->swg_percent = value; // Set the value as if it's already been set, just incase it's 0 as we won't get that message, or will update next time
}
//} else if (strcmp(request.first.value, "POOL_LIGHT_MODE") == 0) {
// set_light_mode(request.second.value, _aqconfig_.light_programming_button_pool);
} else if (strcmp(request.first.value, "LIGHT_MODE") == 0) {
// second is mode & third is button_id
int i;
for (i = 0; i < _aqualink_data->total_buttons; i++) {
// NSF I could pull the text here for the real light color name. 4th value in json
if (strcmp(request.third.value, _aqualink_data->aqbuttons[i].name) == 0) {
set_light_mode(request.second.value, i);
break;
}
}
//set_light_mode(request.second.value, 0);
} else {
int i;