forked from sfeakes/AqualinkD
-
Notifications
You must be signed in to change notification settings - Fork 2
/
net_services.c
2186 lines (1920 loc) · 82.9 KB
/
net_services.c
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>
#ifdef AQ_MANAGER
#include <systemd/sd-journal.h>
#endif
#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 "web_config.h"
#include "debug_timer.h"
#include "serialadapter.h"
#include "aq_timer.h"
#include "aq_scheduler.h"
#include "rs_msg_utils.h"
#include "simulator.h"
#include "hassio.h"
#include "version.h"
#ifdef AQ_PDA
#include "pda.h"
#endif
/*
#if defined AQ_DEBUG || defined AQ_TM_DEBUG
#include "timespec_subtract.h"
//#define AQ_TM_DEBUG
#endif
*/
//static struct aqconfig *_aqconfig_;
static struct aqualinkdata *_aqualink_data;
//static char *_web_root;
static pthread_t _net_thread_id = 0;
static bool _keepNetServicesRunning = false;
static struct mg_mgr _mgr;
static int _mqtt_exit_flag = false;
#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
void reset_last_mqtt_status();
//static const char *s_http_port = "8080";
static struct mg_serve_http_opts _http_server_opts;
#ifdef AQ_NO_THREAD_NETSERVICE
static sig_atomic_t s_signal_received = 0;
#endif
static void net_signal_handler(int sig_num) {
//printf("** net_signal_handler **\n");
#ifdef AQ_NO_THREAD_NETSERVICE
if (!_aqconfig_.thread_netservices) {
signal(sig_num, net_signal_handler); // Reinstantiate signal handler to aqualinkd.c
s_signal_received = sig_num;
} else {
intHandler(sig_num); // Force signal handler to aqualinkd.c
}
#else
intHandler(sig_num); // Force signal handler to aqualinkd.c
#endif
}
static int is_websocket(const struct mg_connection *nc) {
//return nc->flags & MG_F_IS_WEBSOCKET && !(nc->flags & MG_F_USER_2); // WS only, not WS simulator
return nc->flags & MG_F_IS_WEBSOCKET;
}
static void set_websocket_simulator(struct mg_connection *nc) {
nc->flags |= MG_F_USER_2;
}
static int is_websocket_simulator(const struct mg_connection *nc) {
return nc->flags & MG_F_USER_2;
}
static void set_websocket_aqmanager(struct mg_connection *nc) {
nc->flags |= MG_F_USER_3;
}
static int is_websocket_aqmanager(const struct mg_connection *nc) {
return nc->flags & MG_F_USER_3;
}
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);
//LOG(NET_LOG,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_simulator_message(struct mg_connection *nc) {
struct mg_connection *c;
char data[JSON_SIMULATOR_SIZE];
build_aqualink_simulator_packet_JSON(_aqualink_data, data, JSON_SIMULATOR_SIZE);
for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) {
if (is_websocket(c) && is_websocket_simulator(c)) {
ws_send(c, data);
}
}
//LOG(NET_LOG,LOG_DEBUG, "Sent to simulator '%s'\n",data);
_aqualink_data->simulator_packet_updated = false;
}
#ifdef AQ_MANAGER
#define WS_LOG_LENGTH 200
// Send log message to any aqManager websocket.
void ws_send_logmsg(struct mg_connection *nc, char *msg) {
struct mg_connection *c;
for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) {
if (is_websocket(c) && is_websocket_aqmanager(c)) {
ws_send(c, msg);
}
}
}
sd_journal *open_journal() {
sd_journal *journal;
char filter[51];
#ifndef AQ_CONTAINER
// Below works for local
if (sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY) < 0)
#else
// Container doesn't have local systemd_journal so use hosts through mapped filesystem
if (sd_journal_open_directory(&journal, "/var/log/journal", SD_JOURNAL_SYSTEM) < 0)
#endif
{
LOGSystemError(errno, NET_LOG, "Failed to open journal");
return journal;
}
snprintf(filter, 50, "SYSLOG_IDENTIFIER=%s",_aqualink_data->self );
if (sd_journal_add_match(journal, filter, 0) < 0)
{
LOGSystemError(errno, NET_LOG, "Failed to set journal syslog filter");
sd_journal_close(journal);
return journal;
}
/* Docker wll also have problem with this
// Daemon will change PID after printing startup message, so don't filter on current PID
if (_aqconfig_.deamonize != true) {
snprintf(filter, 50, "_PID=%d",getpid());
if (sd_journal_add_match(journal, filter, 0) < 0)
{
LOGSystemError(errno, NET_LOG, "Failed to set journal pid filter");
sd_journal_close(journal);
return journal;
}
}*/
if (sd_journal_set_data_threshold(journal, LOGBUFFER) < 0)
{
LOG(NET_LOG, LOG_WARNING, "Failed to set journal message size\n");
}
return journal;
}
void find_aqualinkd_startupmsg(sd_journal *journal)
{
static bool once=false;
const void *log;
size_t len;
// Only going to do this one time, incase re reset while reading.
if (once) {
return;
}
once=true;
sd_journal_previous_skip(journal, 200);
while ( sd_journal_next(journal) > 0) // need to capture return of this
{
if (sd_journal_get_data(journal, "MESSAGE", &log, &len) >= 0) {
if (rsm_strnstr((const char *)log+8, AQUALINKD_NAME, len-8) != NULL) {
// Go back one and return
sd_journal_previous_skip(journal, 1);
return;
}
}
}
// Blindly go back 100 messages since above didn;t find start
sd_journal_previous_skip(journal, 100);
}
#define BLANK_JOURNAL_READ_RESET 100
bool _broadcast_systemd_logmessages(bool aqMgrActive, bool reOpenStaleConnection);
bool broadcast_systemd_logmessages(bool aqMgrActive) {
return _broadcast_systemd_logmessages(aqMgrActive, false);
}
bool _broadcast_systemd_logmessages(bool aqMgrActive, bool reOpenStaleConnection) {
static sd_journal *journal;
static bool active = false;
char msg[WS_LOG_LENGTH];
static int cnt=0;
static char *cursor = NULL;
//char filter[51];
if (reOpenStaleConnection) {
sd_journal_close(journal);
active = false;
}
if (!aqMgrActive) {
if (!active) {
return true;
} else {
sd_journal_close(journal);
active = false;
return true;
cursor = NULL;
}
}
// aqManager is active
if (!active) {
if ( (journal = open_journal()) < 0) {
build_logmsg_JSON(msg, LOG_ERR, "Failed to open journal", WS_LOG_LENGTH,22);
ws_send_logmsg(_mgr.active_connections, msg);
return false;
}
if (sd_journal_seek_tail(journal) < 0) {
build_logmsg_JSON(msg, LOG_ERR, "Failed to seek to journal end", WS_LOG_LENGTH,29);
ws_send_logmsg(_mgr.active_connections, msg);
sd_journal_close(journal);
return false;
}
//if we have cusror go to it, otherwise jump back and try to find startup message
if (cursor != NULL) {
sd_journal_seek_cursor(journal, cursor);
sd_journal_next(journal);
} else
find_aqualinkd_startupmsg(journal);
active = true;
}
const void *log;
size_t len;
const void *pri;
size_t plen;
int rtn;
while ( (rtn = sd_journal_next(journal)) > 0) // need to capture return of this
{
if (sd_journal_get_data(journal, "MESSAGE", &log, &len) < 0) {
build_logmsg_JSON(msg, LOG_ERR, "Failed to get journal message", WS_LOG_LENGTH,29);
ws_send_logmsg(_mgr.active_connections, msg);
} else if (sd_journal_get_data(journal, "PRIORITY", &pri, &plen) < 0) {
build_logmsg_JSON(msg, LOG_ERR, "Failed to seek to journal message priority", WS_LOG_LENGTH,42);
ws_send_logmsg(_mgr.active_connections, msg);
} else {
build_logmsg_JSON(msg, atoi((const char *)pri+9), (const char *)log+8, WS_LOG_LENGTH,(int)len-8);
ws_send_logmsg(_mgr.active_connections, msg);
cnt=0;
sd_journal_get_cursor(journal, &cursor);
}
}
if (rtn < 0) {
build_logmsg_JSON(msg, LOG_ERR, "Failed to get seen to next journal message", WS_LOG_LENGTH,42);
ws_send_logmsg(_mgr.active_connections, msg);
sd_journal_close(journal);
active = false;
} else if (rtn == 0) {
// Sometimes we get no errors, and nothing to read, even when their is.
// So if we get too many, restart but don;t reset the cursor.
// Could test moving sd_journal_get_cursor(journal, &cursor); line to here from above.
// Quick way to times blank reads by log level, since less logs written higher number of blank reads
if ( cnt++ >= BLANK_JOURNAL_READ_RESET * ( (LOG_DEBUG_SERIAL+1) - getSystemLogLevel() ) ) {
/* Stale connection, call ourselves to reopen*/
if (!reOpenStaleConnection) {
//LOG(NET_LOG, LOG_WARNING, "**** %d Too many blank reads, resetting!! ****\n",cnt);
return _broadcast_systemd_logmessages(aqMgrActive, true);
}
//LOG(NET_LOG, LOG_WARNING, "**** Reset didn't work ****\n",cnt);
//return false;
}
}
return true;
}
#define USEC_PER_SEC 1000000L
bool write_systemd_logmessages_2file(char *fname, int lines)
{
FILE *fp = NULL;
sd_journal *journal;
const void *log;
size_t len;
const void *pri;
size_t plen;
char tsbuffer[20];
uint64_t realtime;
struct tm tm;
time_t sec;
fp = fopen (fname, "w");
if (fp == NULL) {
LOG(NET_LOG, LOG_WARNING, "Failed to open tmp log file '%s'\n",fname);
return false;
}
if ( (journal = open_journal()) < 0) {
fclose (fp);
return false;
}
if (sd_journal_seek_tail(journal) < 0)
{
LOG(NET_LOG, LOG_WARNING, "Failed to seek to journal end");
fclose (fp);
sd_journal_close(journal);
return false;
}
if (sd_journal_previous_skip(journal, lines) < 0)
{
LOG(NET_LOG, LOG_WARNING, "Failed to seek to journal start");
fclose (fp);
sd_journal_close(journal);
return false;
}
while ( sd_journal_next(journal) > 0) // need to capture return of this
{
if (sd_journal_get_data(journal, "MESSAGE", &log, &len) < 0) {
LOG(NET_LOG, LOG_WARNING, "Failed to get journal message");
} else if (sd_journal_get_data(journal, "PRIORITY", &pri, &plen) < 0) {
LOG(NET_LOG, LOG_WARNING, "Failed to get journal message priority");
} else if (sd_journal_get_realtime_usec(journal, &realtime) < 0) {
LOG(NET_LOG, LOG_WARNING, "Failed to get journal message timestamp");
} else {
sec = (time_t)(realtime/USEC_PER_SEC);
localtime_r(&sec, &tm);
strftime(tsbuffer, sizeof(tsbuffer), "%b %d %T", &tm); // need to capture return of this
fprintf(fp, "%-15s %-7s %.*s\n",tsbuffer,elevel2text(atoi((const char *)pri+9)), (int)len-8,(const char *)log+8);
}
}
sd_journal_close(journal);
fclose (fp);
return true;
}
#endif
void _broadcast_aqualinkstate(struct mg_connection *nc)
{
static int mqtt_count=0;
struct mg_connection *c;
char data[JSON_STATUS_SIZE];
#ifdef AQ_TM_DEBUG
int tid;
#endif
DEBUG_TIMER_START(&tid);
build_aqualink_status_JSON(_aqualink_data, data, JSON_STATUS_SIZE);
#ifdef AQ_MEMCMP
if ( memcmp(_aqualink_data, &_last_mqtt_aqualinkdata, sizeof(struct aqualinkdata) ) == 0) {
LOG(NET_LOG,LOG_NOTICE, "**********Structure buffs the same, ignoring request************\n");
DEBUG_TIMER_CLEAR(&tid);
return;
}
#endif
#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) && !is_websocket_simulator(c)) // No need to broadcast status messages to simulator.
if (is_websocket(c)) // All button simulator needs status messages
ws_send(c, data);
#ifndef MG_DISABLE_MQTT
else if (is_mqtt(c))
mqtt_broadcast_aqualinkstate(c);
#endif
}
#ifdef AQ_MEMCMP
memcpy(&_last_mqtt_aqualinkdata, _aqualink_data, sizeof(struct aqualinkdata));
#endif
DEBUG_TIMER_STOP(tid, NET_LOG, "broadcast_aqualinkstate() completed, took ");
return;
}
void send_mqtt(struct mg_connection *nc, const char *toppic, const 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));
LOG(NET_LOG,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_timer_duration_msg(struct mg_connection *nc, char *dev_name, aqkey *button)
{
static char mqtt_pub_topic[250];
sprintf(mqtt_pub_topic, "%s/%s/timer/duration",_aqconfig_.mqtt_aq_topic, dev_name);
if ((button->special_mask & TIMER_ACTIVE) == TIMER_ACTIVE) {
char val[10];
sprintf(val, "%d", get_timer_left(button));
send_mqtt(nc, mqtt_pub_topic, val);
} else {
send_mqtt(nc, mqtt_pub_topic, "0");
}
}
void send_mqtt_timer_state_msg(struct mg_connection *nc, char *dev_name, aqkey *button)
{
static char mqtt_pub_topic[250];
sprintf(mqtt_pub_topic, "%s/%s/timer",_aqconfig_.mqtt_aq_topic, dev_name);
send_mqtt(nc, mqtt_pub_topic, ( ((button->special_mask & TIMER_ACTIVE) == TIMER_ACTIVE) && (button->led->state != OFF) )?MQTT_ON:MQTT_OFF );
send_mqtt_timer_duration_msg(nc, dev_name, button);
}
//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_led_state_msg(struct mg_connection *nc, char *dev_name, aqledstate state, char *onS, char *offS)
{
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, offS);
sprintf(mqtt_pub_topic, "%s/%s%s",_aqconfig_.mqtt_aq_topic, dev_name, ENABELED_SUBT);
send_mqtt(nc, mqtt_pub_topic, onS);
} else {
send_mqtt(nc, mqtt_pub_topic, (state==OFF?offS:onS));
sprintf(mqtt_pub_topic, "%s/%s%s",_aqconfig_.mqtt_aq_topic, dev_name, ENABELED_SUBT);
send_mqtt(nc, mqtt_pub_topic, (state==OFF?offS:onS));
}
}
void send_mqtt_swg_state_msg(struct mg_connection *nc, char *dev_name, aqledstate state)
{
//send_mqtt_led_state_msg(nc, dev_name, state, SWG_ON, SWG_OFF);
send_mqtt_led_state_msg(nc, dev_name, state, "2", "0");
}
void send_mqtt_heater_state_msg(struct mg_connection *nc, char *dev_name, aqledstate state)
{
send_mqtt_led_state_msg(nc, dev_name, state, MQTT_ON, MQTT_OFF);
/*
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];
// Use "not CELS" over "equal FAHR" so we default to FAHR for unknown units
//sprintf(degC, "%.2f", (_aqualink_data->temp_units==FAHRENHEIT && _aqconfig_.convert_mqtt_temp)?degFtoC(value):value );
sprintf(degC, "%.2f", (_aqualink_data->temp_units!=CELSIUS && _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];
// Use "not CELS" over "equal FAHR" so we default to FAHR for unknown units
//sprintf(degC, "%.2f", (_aqualink_data->temp_units==FAHRENHEIT && _aqconfig_.convert_mqtt_temp)?degFtoC(value):value );
sprintf(degC, "%.2f", (_aqualink_data->temp_units!=CELSIUS && _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, const char *dev_name, const 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;
int i;
const char *status;
if (_aqconfig_.mqtt_timed_update) {
#ifdef AQ_NO_THREAD_NETSERVICE
if (cnt > 300) { // 100 = about every 2 minutes.
#else
if (cnt > 30) { // 30 = about every 2 minutes.
#endif
reset_last_mqtt_status();
cnt = 0;
} else {
cnt++;
}
}
//LOG(NET_LOG,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));
}
// Only send to display messag topic if not in simulator mode
//if (!_aqualink_data->simulate_panel) {
status = getAqualinkDStatusMessage(_aqualink_data);
if (strcmp(status, _last_mqtt_aqualinkdata.last_display_message) != 0) {
strcpy(_last_mqtt_aqualinkdata.last_display_message, status);
send_mqtt_string_msg(nc, DISPLAY_MSG_TOPIC, status);
}
//}
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 != _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, 0);
} if (_aqualink_data->pool_temp == TEMP_UNKNOWN && ! _aqconfig_.report_zero_pool_temp) {
// Don't post anything in this case, ie leave last posted value alone
} 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, 0);
} if (_aqualink_data->spa_temp == TEMP_UNKNOWN && ! _aqconfig_.report_zero_spa_temp && _aqualink_data->pool_temp != TEMP_UNKNOWN ) {
// Use Pool Temp as spa temp
if (_last_mqtt_aqualinkdata.spa_temp != _aqualink_data->pool_temp) {
_last_mqtt_aqualinkdata.spa_temp = _aqualink_data->pool_temp;
send_mqtt_temp_msg(nc, SPA_TEMP_TOPIC, _aqualink_data->pool_temp);
}
} 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);
}
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->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_ENABELED, MQTT_ON);
// Duplicate of below if statment. NSF come back and check if necessary for startup.
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;
}
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);
//send_mqtt_string_msg(nc, FREEZE_PROTECT_ENABELED, MQTT_ON);
}
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)));
}
// Salt Water Generator
if (_aqualink_data->swg_led_state != LED_S_UNKNOWN) {
//LOG(NET_LOG,LOG_DEBUG, "Sending MQTT SWG MEssages\n");
if (_aqualink_data->swg_led_state != _last_mqtt_aqualinkdata.swg_led_state) {
send_mqtt_swg_state_msg(nc, SWG_TOPIC, _aqualink_data->swg_led_state);
_last_mqtt_aqualinkdata.swg_led_state = _aqualink_data->swg_led_state;
}
if (_aqualink_data->swg_percent != TEMP_UNKNOWN && (_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 && (_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);
}
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->boost_duration != _last_mqtt_aqualinkdata.boost_duration ) {
send_mqtt_int_msg(nc, SWG_BOOST_DURATION_TOPIC, _aqualink_data->boost_duration);
_last_mqtt_aqualinkdata.boost_duration = _aqualink_data->boost_duration;
}
} else {
//LOG(NET_LOG,LOG_DEBUG, "SWG status unknown\n");
}
if (_aqualink_data->ar_swg_device_status != SWG_STATUS_UNKNOWN) {
//LOG(NET_LOG,LOG_DEBUG, "Sending MQTT SWG Extended %d\n",_aqualink_data->ar_swg_device_status);
if (_aqualink_data->ar_swg_device_status != _last_mqtt_aqualinkdata.ar_swg_device_status) {
char message[30];
int status;
int dzalert;
get_swg_status_mqtt(_aqualink_data, message, &status, &dzalert);
send_domoticz_mqtt_status_message(nc, _aqconfig_.dzidx_swg_status, dzalert, &message[9]);
send_mqtt_int_msg(nc, SWG_EXTENDED_TOPIC, (int)_aqualink_data->ar_swg_device_status);
send_mqtt_string_msg(nc, SWG_STATUS_MSG_TOPIC, message);
_last_mqtt_aqualinkdata.ar_swg_device_status = _aqualink_data->ar_swg_device_status;
//LOG(NET_LOG,LOG_DEBUG, "SWG Extended sending cur=%d sent=%d\n",_aqualink_data->ar_swg_device_status,_last_mqtt_aqualinkdata.ar_swg_device_status);
} else {
//LOG(NET_LOG,LOG_DEBUG, "SWG Extended already sent cur=%d sent=%d\n",_aqualink_data->ar_swg_device_status,_last_mqtt_aqualinkdata.ar_swg_device_status);
}
} else {
//LOG(NET_LOG,LOG_DEBUG, "SWG Extended unknown\n");
}
if (READ_RSDEV_JXI && _aqualink_data->heater_err_status != _last_mqtt_aqualinkdata.heater_err_status) {
char message[30];
if (_aqualink_data->heater_err_status == NUL) {
send_mqtt_int_msg(nc, LXI_ERROR_CODE, (int)_aqualink_data->heater_err_status);
send_mqtt_string_msg(nc, LXI_ERROR_MESSAGE, "");
} else {
//send_mqtt_int_msg(nc, LXI_STATUS, (int)_aqualink_data->heater_err_status);
send_mqtt_int_msg(nc, LXI_ERROR_CODE, (int)_aqualink_data->heater_err_status);
getJandyHeaterErrorMQTT(_aqualink_data, message);
send_mqtt_string_msg(nc, LXI_ERROR_MESSAGE, status);
}
_last_mqtt_aqualinkdata.heater_err_status = _aqualink_data->heater_err_status;
}
// LOG(NET_LOG,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 (_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);
}
send_mqtt_timer_state_msg(nc, _aqualink_data->aqbuttons[i].name, &_aqualink_data->aqbuttons[i]);
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));
}
} else if ((_aqualink_data->aqbuttons[i].special_mask & TIMER_ACTIVE) == TIMER_ACTIVE) {
//send_mqtt_timer_duration_msg(nc, _aqualink_data->aqbuttons[i].name, &_aqualink_data->aqbuttons[i]);
// send_mqtt_timer_state_msg will call send_mqtt_timer_duration_msg so no need to do it here.
// Have to use send_mqtt_timer_state_msg due to a timer being set on a device that's already on, (ir no state change so above code does't get hit)
send_mqtt_timer_state_msg(nc, _aqualink_data->aqbuttons[i].name, &_aqualink_data->aqbuttons[i]);
}
}
// 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);
}
if (_aqualink_data->pumps[i].mode != TEMP_UNKNOWN && _aqualink_data->pumps[i].mode != _last_mqtt_aqualinkdata.pumps[i].mode) {
_last_mqtt_aqualinkdata.pumps[i].mode = _aqualink_data->pumps[i].mode;
send_mqtt_aux_msg(nc, _aqualink_data->pumps[i].button->name, PUMP_MODE_TOPIC, _aqualink_data->pumps[i].mode);
}
if (_aqualink_data->pumps[i].status != TEMP_UNKNOWN && _aqualink_data->pumps[i].status != _last_mqtt_aqualinkdata.pumps[i].status) {
_last_mqtt_aqualinkdata.pumps[i].status = _aqualink_data->pumps[i].status;
send_mqtt_aux_msg(nc, _aqualink_data->pumps[i].button->name, PUMP_STATUS_TOPIC, _aqualink_data->pumps[i].status);
}
if (_aqualink_data->pumps[i].pressureCurve != TEMP_UNKNOWN && _aqualink_data->pumps[i].pressureCurve != _last_mqtt_aqualinkdata.pumps[i].pressureCurve) {
_last_mqtt_aqualinkdata.pumps[i].pressureCurve = _aqualink_data->pumps[i].pressureCurve;
send_mqtt_aux_msg(nc, _aqualink_data->pumps[i].button->name, PUMP_PPC_TOPIC, _aqualink_data->pumps[i].pressureCurve);
}
}
}
typedef enum {uActioned, uBad, uDevices, uStatus, uHomebridge, uDynamicconf, uDebugStatus, uDebugDownload, uSimulator, uSchedules, uSetSchedules, uAQmanager, uLogDownload, uNotAvailable} uriAtype;
//typedef enum {NET_MQTT=0, NET_API, NET_WS, DZ_MQTT} netRequest;
const char actionName[][5] = {"MQTT", "API", "WS", "DZ"};
#define BAD_SETPOINT "No device for setpoint found"
#define NO_PLIGHT_DEVICE "No programable light found"
#define NO_VSP_SUPPORT "Pump VS programs not supported yet"
#define PUMP_NOT_FOUND "No matching Pump found"
#define NO_DEVICE "No matching Device found"
#define INVALID_VALUE "Invalid value"
#define NOCHANGE_IGNORING "No change, device is already in that state"
#define UNKNOWN_REQUEST "Didn't understand request"
#ifdef AQ_PDA
void create_PDA_on_off_request(aqkey *button, bool isON)
{
int i;
char msg[PTHREAD_ARG];
for (i=0; i < _aqualink_data->total_buttons; i++) {
if (_aqualink_data->aqbuttons[i].code == button->code) {
sprintf(msg, "%-5d%-5d", i, (isON? ON : OFF));
aq_programmer(AQ_PDA_DEVICE_ON_OFF, msg, _aqualink_data);
break;
}
}
}
#endif
//uriAtype action_URI(char *from, const char *URI, int uri_length, float value, bool convertTemp) {
//uriAtype action_URI(netRequest from, const char *URI, int uri_length, float value, bool convertTemp) {
uriAtype action_URI(request_source from, const char *URI, int uri_length, float value, bool convertTemp, char **rtnmsg) {
/* Example URI ()
* Note URI is NOT terminated
* devices
* status
* Freeze/setpoint/set
* Filter_Pump/set
* Pool_Heater/setpoint/set
* Pool_Heater/set
* SWG/Percent_f/set
* Filter_Pump/RPM/set
* Pump_1/RPM/set
* Pool Light/color/set
* Pool Light/program/set
*/
uriAtype rtn = uBad;
bool found = false;
int i;
char *ri1 = (char *)URI;
char *ri2 = NULL;
char *ri3 = NULL;
//bool charvalue=false;
//char *ri4 = NULL;
LOG(NET_LOG,LOG_DEBUG, "%s: URI Request '%.*s': value %.2f\n", actionName[from], uri_length, URI, value);
// Split up the URI into parts.
for (i=1; i < uri_length; i++) {