forked from sfeakes/AqualinkD
-
Notifications
You must be signed in to change notification settings - Fork 2
/
iaqtouch_aq_programmer.c
1434 lines (1152 loc) · 46.7 KB
/
iaqtouch_aq_programmer.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 <string.h>
#include <stdbool.h>
#include "iaqtouch_aq_programmer.h"
#include "aq_serial.h"
#include "utils.h"
#include "aq_programmer.h"
#include "aqualink.h"
//#include "packetLogger.h"
#include "iaqtouch.h"
#include "rs_msg_utils.h"
#include "aq_panel.h"
#include "config.h"
#include "devices_jandy.h"
#include "packetLogger.h"
#include "color_lights.h"
// System Page is obfiously fixed and not dynamic loaded, so set buttons to stop confustion.
#define KEY_IAQTCH_SCHEDULE KEY_IAQTCH_KEY01
#define KEY_IAQTCH_CUSTOMIZE_HOME KEY_IAQTCH_KEY02
#define KEY_IAQTCH_PROGRAM_GROUP KEY_IAQTCH_KEY03
#define KEY_IAQTCH_SET_TEMP KEY_IAQTCH_KEY04
#define KEY_IAQTCH_SYSTEM_SETUP KEY_IAQTCH_KEY05
#define KEY_IAQTCH_TOUCH_SETUP KEY_IAQTCH_KEY06
#define KEY_IAQTCH_SET_DATETIME KEY_IAQTCH_KEY07
#define KEY_IAQTCH_LOCKOUT_PASSWD KEY_IAQTCH_KEY08
#define KEY_IAQTCH_SET_ACQUAPURE KEY_IAQTCH_KEY09
bool _cansend = false;
unsigned char _iaqt_pgm_command = NUL;
/**************
*
* Command queue stuff
*
*/
// External command
bool iaqt_queue_cmd(unsigned char cmd) {
if (_iaqt_pgm_command == NUL) {
_iaqt_pgm_command = cmd;
return true;
}
return false;
}
void set_iaq_cansend(bool cansend){
_cansend = cansend;
}
unsigned char pop_iaqt_cmd(unsigned char receive_type)
{
unsigned char cmd = NUL;
if (!_cansend)
return cmd;
if (receive_type == CMD_IAQ_POLL) {
cmd = _iaqt_pgm_command;
_iaqt_pgm_command = NUL;
}
if (cmd != NUL)
LOG(IAQT_LOG,LOG_DEBUG, "Sending '0x%02hhx' to controller\n", cmd);
return cmd;
}
void waitfor_iaqt_queue2empty()
{
int i=0;
while ( (_iaqt_pgm_command != NUL) && ( i++ < PROGRAMMING_POLL_COUNTER) ) {
delay(PROGRAMMING_POLL_DELAY_TIME);
}
// Initial startup can take some time, _cansend should be false during this time.
// If we start programming before we receive the first status page, nothing works, this forces that wait
while(_cansend == false) {
delay(PROGRAMMING_POLL_DELAY_TIME * 2);
}
if (_iaqt_pgm_command != NUL) {
// Wait for longer interval
while ( (_iaqt_pgm_command != NUL) && ( i++ < PROGRAMMING_POLL_COUNTER * 2 ) ) {
delay(PROGRAMMING_POLL_DELAY_TIME * 2);
}
}
if (_iaqt_pgm_command != NUL) {
LOG(IAQT_LOG,LOG_WARNING, "Send command Queue did not empty, timeout\n");
}
}
void send_aqt_cmd(unsigned char cmd)
{
waitfor_iaqt_queue2empty();
iaqt_queue_cmd(cmd);
LOG(IAQT_LOG,LOG_DEBUG, "Queue send '0x%02hhx' to controller (programming)\n", _iaqt_pgm_command);
}
/**************
*
* Control Command queue stuff
*
*/
unsigned char _iaqt_control_cmd[AQ_MAXPKTLEN];
int _iaqt_control_cmd_len;
int ref_iaqt_control_cmd(unsigned char **cmd)
{
//printf("*********** GET READY SENDING CONTROL ****************\n");
*cmd = _iaqt_control_cmd;
if ( getLogLevel(IAQT_LOG) >= LOG_DEBUG ) {
char buff[1000];
//sprintf("Sending control command:")
beautifyPacket(buff, _iaqt_control_cmd, _iaqt_control_cmd_len, false);
LOG(IAQT_LOG,LOG_DEBUG, "Sending commandsed : %s\n", buff);
}
return _iaqt_control_cmd_len;
}
void rem_iaqt_control_cmd(unsigned char *cmd)
{
memset(_iaqt_control_cmd, 0, AQ_MAXPKTLEN * sizeof(unsigned char));
_iaqt_control_cmd_len = 0;
}
bool waitfor_iaqt_ctrl_queue2empty()
{
int i=0;
while ( (_iaqt_control_cmd_len >0 ) && ( i++ < 100) ) {
LOG(IAQT_LOG,LOG_DEBUG, "Waiting for commandset to send\n");
delay(50);
}
LOG(IAQT_LOG,LOG_DEBUG, "Wait for commandset over!\n");
if (_iaqt_control_cmd_len > 0 ) {
LOG(IAQT_LOG,LOG_WARNING, "Send control command Queue did not empty, timeout\n");
return false;
}
return true;
}
/*
unsigned const char _waitfor_iaqt_nextPage(struct aqualinkdata *aq_data, int numMessageReceived) {
waitfor_iaqt_queue2empty();
int i=0;
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
while( ++i <= numMessageReceived)
{
//LOG(IAQT_LOG,LOG_DEBUG, "waitfor_iaqt_nextPage (%d of %d)\n",i,numMessageReceived);
pthread_cond_wait(&aq_data->active_thread.thread_cond, &aq_data->active_thread.thread_mutex);
if(wasiaqtThreadKickTypePage()) break;
}
LOG(IAQT_LOG,LOG_DEBUG, "waitfor_iaqt_nextPage finished in (%d of %d)\n",i,numMessageReceived);
pthread_mutex_unlock(&aq_data->active_thread.thread_mutex);
if(wasiaqtThreadKickTypePage())
return iaqtCurrentPage();
else
return NUL;
}
unsigned const char shortwaitfor_iaqt_nextPage(struct aqualinkdata *aq_data) {
return _waitfor_iaqt_nextPage(aq_data, 3);
}
*/
unsigned const char waitfor_iaqt_messages(struct aqualinkdata *aq_data, int numMessageReceived) {
//return _waitfor_iaqt_nextPage(aq_data, 30);
waitfor_iaqt_queue2empty();
int i=0;
LOG(IAQT_LOG,LOG_DEBUG, "waitfor_iaqt_messages (%d of %d)\n",i,numMessageReceived);
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
while( ++i <= numMessageReceived)
{
//LOG(IAQT_LOG,LOG_DEBUG, "waitfor_iaqt_nextPage (%d of %d)\n",i,numMessageReceived);
pthread_cond_wait(&aq_data->active_thread.thread_cond, &aq_data->active_thread.thread_mutex);
}
LOG(IAQT_LOG,LOG_DEBUG, "waitfor_iaqt_messages finished in (%d of %d)\n",i,numMessageReceived);
pthread_mutex_unlock(&aq_data->active_thread.thread_mutex);
return iaqtLastMsg();
}
unsigned const char waitfor_iaqt_nextPage(struct aqualinkdata *aq_data) {
//return _waitfor_iaqt_nextPage(aq_data, 30);
waitfor_iaqt_queue2empty();
int i=0;
const int numMessageReceived = 30;
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
while( ++i <= numMessageReceived)
{
//LOG(IAQT_LOG,LOG_DEBUG, "waitfor_iaqt_nextPage (%d of %d)\n",i,numMessageReceived);
pthread_cond_wait(&aq_data->active_thread.thread_cond, &aq_data->active_thread.thread_mutex);
if(wasiaqtThreadKickTypePage()) break;
}
LOG(IAQT_LOG,LOG_DEBUG, "waitfor_iaqt_nextPage finished in (%d of %d)\n",i,numMessageReceived);
pthread_mutex_unlock(&aq_data->active_thread.thread_mutex);
if(wasiaqtThreadKickTypePage())
return iaqtCurrentPage();
else
return NUL;
}
unsigned const char waitfor_iaqt_nextMessage(struct aqualinkdata *aq_data, const unsigned char msg_type) {
waitfor_iaqt_queue2empty();
int i=0;
const int numMessageReceived = 30;
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
while( ++i <= numMessageReceived)
{
LOG(IAQT_LOG,LOG_DEBUG, "waitfor_iaqt_nextMessage 0x%02hhx (%d of %d)\n",msg_type,i,numMessageReceived);
pthread_cond_wait(&aq_data->active_thread.thread_cond, &aq_data->active_thread.thread_mutex);
if( msg_type == NUL || iaqtLastMsg() == msg_type) break;
}
pthread_mutex_unlock(&aq_data->active_thread.thread_mutex);
return iaqtLastMsg();
}
//typedef enum iaqt_control_command_type()
typedef enum {icct_setrpm, icct_settime, icct_setdate} iaqtControlCmdYype;
// Type is always 0 at the moment, haven't found any
void queue_iaqt_control_command(iaqtControlCmdYype type, int num) {
//unsigned char packets[AQ_MAXPKTLEN];
//int cnt;
if (waitfor_iaqt_ctrl_queue2empty() == false)
return;
_iaqt_control_cmd[0] = DEV_MASTER;
_iaqt_control_cmd[1] = 0x24;
_iaqt_control_cmd[2] = 0x31;
_iaqt_control_cmd_len = num2iaqtRSset(&_iaqt_control_cmd[3], num, true);
// Pad with 0xcd for some reason.
for(_iaqt_control_cmd_len = _iaqt_control_cmd_len+3; _iaqt_control_cmd_len <= 18; _iaqt_control_cmd_len++)
_iaqt_control_cmd[_iaqt_control_cmd_len] = 0xcd;
// Tell the control panel we are ready to send this shit.
send_aqt_cmd(ACK_CMD_READY_CTRL);
LOG(IAQT_LOG,LOG_DEBUG, "Queued extended commandsed of length %d\n",_iaqt_control_cmd_len);
//printHex(packets, 19);
//printf("\n");
//send_jandy_command(NULL, packets, cnt);
}
bool queue_iaqt_control_command_str(iaqtControlCmdYype type, char *str) {
if (waitfor_iaqt_ctrl_queue2empty() == false)
return false;
_iaqt_control_cmd[0] = DEV_MASTER;
_iaqt_control_cmd[1] = 0x24;
_iaqt_control_cmd[2] = 0x31;
_iaqt_control_cmd_len = char2iaqtRSset(&_iaqt_control_cmd[3], str, strlen(str));
// Need to bad time for some reason not yet known
if (type == icct_settime) {
//Debug: RS Serial: To 0x00 of type iAq pBut | HEX: 0x10|0x02|0x00|0x24|0x31|0x30|0x31|0x3a|0x30|0x31|0x00|0x30|0x32|0x00|0xcd|0xcd|0xcd|0xcd|0xcd|0xcd|0xcd|0x60|0x10|0x03|
// From position 11 (8 without pre) add 0x30|0x32|0x00
_iaqt_control_cmd_len += 3;
_iaqt_control_cmd[9] = 0x30;
_iaqt_control_cmd[10] = 0x32;
_iaqt_control_cmd[11] = 0x00;
}
// Pad with 0xcd for some reason.
for(_iaqt_control_cmd_len = _iaqt_control_cmd_len+3; _iaqt_control_cmd_len <= 18; _iaqt_control_cmd_len++)
_iaqt_control_cmd[_iaqt_control_cmd_len] = 0xcd;
// Tell the control panel we are ready to send this shit.
send_aqt_cmd(ACK_CMD_READY_CTRL);
return true;
//debuglogPacket()
}
bool goto_iaqt_page(const unsigned char pageID, struct aqualinkdata *aq_data) {
if (iaqtCurrentPage() == pageID)
return true;
// If we go to Other Status Page, do that so we can quit
if (pageID == IAQ_PAGE_STATUS) {
send_aqt_cmd(KEY_IAQTCH_STATUS);
unsigned char rtn = waitfor_iaqt_nextPage(aq_data);
if (rtn != IAQ_PAGE_STATUS && rtn != IAQ_PAGE_STATUS2) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find Status page\n");
return false;
}
LOG(IAQT_LOG, LOG_DEBUG, "IAQ Touch got to Status page\n");
return true;
}
if (pageID == IAQ_PAGE_HOME || pageID == IAQ_PAGE_DEVICES) {
if (iaqtCurrentPage() != IAQ_PAGE_HOME) {
send_aqt_cmd(KEY_IAQTCH_HOME);
if (waitfor_iaqt_nextPage(aq_data) != IAQ_PAGE_HOME) {
//LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find Home page\n");
// We reset to home page after every programming cycle and error really don't care, so don't throw the error.
return false;
}
LOG(IAQT_LOG, LOG_DEBUG, "IAQ Touch got to Home page\n");
}
if (pageID == IAQ_PAGE_DEVICES) {
send_aqt_cmd(KEY_IAQTCH_HOMEP_KEY08);
if (waitfor_iaqt_nextPage(aq_data) != IAQ_PAGE_DEVICES) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find Device page\n");
return false;
}
}
LOG(IAQT_LOG, LOG_DEBUG, "IAQ Touch got to Device page\n");
return true;
} else if (pageID == IAQ_PAGE_MENU || pageID == IAQ_PAGE_SET_TEMP || pageID == IAQ_PAGE_SET_TIME || pageID == IAQ_PAGE_SET_SWG ||
pageID == IAQ_PAGE_SYSTEM_SETUP || pageID == IAQ_PAGE_FREEZE_PROTECT || pageID == IAQ_PAGE_LABEL_AUX ||
pageID == IAQ_PAGE_VSP_SETUP) {
// All other pages require us to go to Menu page
send_aqt_cmd(KEY_IAQTCH_MENU);
if (waitfor_iaqt_nextPage(aq_data) != IAQ_PAGE_MENU) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find Menu page\n");
return false;
} else
LOG(IAQT_LOG, LOG_INFO, "IAQ Touch got to Menu page\n");
// There are several pages from the MENU page that have no other pages,
// So hit those first
if (pageID == IAQ_PAGE_SET_TEMP) {
send_aqt_cmd(KEY_IAQTCH_SET_TEMP);
if (waitfor_iaqt_nextPage(aq_data) != IAQ_PAGE_SET_TEMP) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find Set Temp page\n");
return false;
}
LOG(IAQT_LOG, LOG_INFO, "IAQ Touch got to Set Temp page\n");
return true;
}
if (pageID == IAQ_PAGE_SET_TIME) {
send_aqt_cmd(KEY_IAQTCH_SET_DATETIME);
if (waitfor_iaqt_nextPage(aq_data) != IAQ_PAGE_SET_TIME) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find Set Time page\n");
return false;
}
LOG(IAQT_LOG, LOG_INFO, "IAQ Touch got to Set Time page\n");
return true;
}
if (pageID == IAQ_PAGE_SET_SWG) {
send_aqt_cmd(KEY_IAQTCH_SET_ACQUAPURE);
if (waitfor_iaqt_nextPage(aq_data) != IAQ_PAGE_SET_SWG) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find Aquapure page\n");
return false;
}
LOG(IAQT_LOG, LOG_INFO, "IAQ Touch got to Aquapure page\n");
return true;
}
// All pages now require us to goto System Setup
send_aqt_cmd(KEY_IAQTCH_SYSTEM_SETUP);
if (waitfor_iaqt_nextPage(aq_data) != IAQ_PAGE_SYSTEM_SETUP) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find System Setup page\n");
return false;
}
LOG(IAQT_LOG, LOG_INFO, "IAQ Touch got to System Setup page\n");
// Look for menu items for the next pages as they can change
char *menuText;
struct iaqt_page_button *button;
switch (pageID) {
case IAQ_PAGE_FREEZE_PROTECT:
menuText = "Freeze Protection";
break;
case IAQ_PAGE_LABEL_AUX:
menuText = "Label Aux";
break;
case IAQ_PAGE_VSP_SETUP:
menuText = "VSP Setup";
break;
default:
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch unknown menu '0x%02hhx'\n", pageID);
return false;
break;
}
button = iaqtFindButtonByLabel(menuText);
if (button == NULL) {
//send_aqt_cmd(KEY_IAQTCH_NEXT_PAGE);
// Try Next Page
//unsigned char page = waitfor_iaqt_nextPage(aq_data);
//LOG(IAQT_LOG, LOG_ERR, "PAGE RETURN IS 0x%02hhx\n",page);
//if (waitfor_iaqt_nextPage(aq_data) != pageID) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find '%s' button on page setup\n", menuText);
return false;
//}
}
// send_aqt_cmd(KEY_IAQTCH_KEY01);
send_aqt_cmd(button->keycode);
if (waitfor_iaqt_nextPage(aq_data) != pageID) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find %s page\n", menuText);
return false;
} else
LOG(IAQT_LOG, LOG_INFO, "IAQ Touch got to %s page\n", menuText);
return true;
}
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch unknown menu '0x%02hhx'\n", pageID);
return false;
}
void *set_aqualink_iaqtouch_device_on_off( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
char *buf = (char*)threadCtrl->thread_args;
//char device_name[15];
struct iaqt_page_button *button;
unsigned int device = atoi(&buf[0]);
unsigned int state = atoi(&buf[5]);
waitForSingleThreadOrTerminate(threadCtrl, AQ_SET_IAQTOUCH_DEVICE_ON_OFF);
if (device > aq_data->total_buttons) {
LOG(IAQT_LOG,LOG_ERR, "(PDA mode) Device On/Off :- bad device number '%d'\n",device);
cleanAndTerminateThread(threadCtrl);
return ptr;
}
LOG(IAQT_LOG,LOG_INFO, "PDA Device On/Off, device '%s', state %d\n",aq_data->aqbuttons[device].label,state);
// See if it's on the current page
button = iaqtFindButtonByLabel(aq_data->aqbuttons[device].label);
if (button == NULL) {
// No luck, go to the device page
if ( goto_iaqt_page(IAQ_PAGE_DEVICES, aq_data) == false )
goto f_end;
button = iaqtFindButtonByLabel(aq_data->aqbuttons[device].label);
// If not found see if page has next
if (button == NULL && iaqtFindButtonByIndex(16)->type == 0x03 ) {
iaqt_queue_cmd(KEY_IAQTCH_NEXT_PAGE);
waitfor_iaqt_nextPage(aq_data);
// This will fail, since not looking at device page 2 buttons
button = iaqtFindButtonByLabel(aq_data->aqbuttons[device].label);
}
}
if (button == NULL) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find '%s' button on device list\n", aq_data->aqbuttons[device].label);
goto f_end;
}
send_aqt_cmd(button->keycode);
//LOG(IAQT_LOG, LOG_ERR, "******* CURRENT MENU '0x%02hhx' *****\n",iaqtCurrentPage());
// NSF NEED TO CHECK FOR POPUP MESSAGE, AND KILL IT.
//page IAQ_PAGE_SET_TEMP hit button 0
//page IAQ_PAGE_COLOR_LIGHT hit button ???????
// Heater popup can be cleared with a home button and still turn on.
// Color light can be cleared with a home button, but won;t turn on.
waitfor_iaqt_queue2empty();
//waitfor_iaqt_messages(aq_data,1);
// OR maybe use waitfor_iaqt_messages(aq_data,1)
// Turn spa on when pump off, ned to remove message "spa will turn on after safty delay", home doesn't clear.
send_aqt_cmd(KEY_IAQTCH_HOME);
// Turn spa off need to read message if heater is on AND hit ok......
f_end:
goto_iaqt_page(IAQ_PAGE_HOME, aq_data);
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
void *set_aqualink_iaqtouch_light_colormode( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
char *buf = (char*)threadCtrl->thread_args;
//char device_name[15];
struct iaqt_page_button *button;
const char *mode_name;
int val = atoi(&buf[0]);
int btn = atoi(&buf[5]);
int typ = atoi(&buf[10]);
bool use_current_mode = false;
bool turn_off = false;
waitForSingleThreadOrTerminate(threadCtrl, AQ_SET_IAQTOUCH_LIGHTCOLOR_MODE);
//char *buf = (char*)threadCtrl->thread_args;
if (btn < 0 || btn >= aq_data->total_buttons ) {
LOG(IAQT_LOG, LOG_ERR, "Can't program light mode on button %d\n", btn);
cleanAndTerminateThread(threadCtrl);
return ptr;
}
aqkey *key = &aq_data->aqbuttons[btn];
//unsigned char code = key->code;
// We also need to cater for light being ON AND changing the color mode. we have extra OK to hit.
if (val == 0) {
use_current_mode = true;
LOG(IAQT_LOG, LOG_INFO, "Light Programming #: %d, button: %s, color light type: %d, using current mode\n", val, key->label, typ);
// NOT SURE WHAT TO DO HERE..... No color mode and iaatouch doesn;t support last color in PDA mode.
} else if (val == -1) {
turn_off = true;
LOG(IAQT_LOG, LOG_INFO, "Light Programming #: %d, button: %s, color light type: %d, Turning off\n", val, key->label, typ);
} else {
mode_name = light_mode_name(typ, val-1, IAQTOUCH);
use_current_mode = false;
if (mode_name == NULL) {
LOG(IAQT_LOG, LOG_ERR, "Light Programming #: %d, button: %s, color light type: %d, couldn't find mode name '%s'\n", val, key->label, typ, mode_name);
cleanAndTerminateThread(threadCtrl);
return ptr;
} else {
LOG(IAQT_LOG, LOG_INFO, "Light Programming #: %d, button: %s, color light type: %d, name '%s'\n", val, key->label, typ, mode_name);
}
}
// See if it's on the current page
button = iaqtFindButtonByLabel(key->label);
if (button == NULL) {
// No luck, go to the device page
if ( goto_iaqt_page(IAQ_PAGE_DEVICES, aq_data) == false )
goto f_end;
button = iaqtFindButtonByLabel(key->label);
// If not found see if page has next
if (button == NULL && iaqtFindButtonByIndex(16)->type == 0x03 ) {
iaqt_queue_cmd(KEY_IAQTCH_NEXT_PAGE);
waitfor_iaqt_nextPage(aq_data);
// This will fail, since not looking at device page 2 buttons
button = iaqtFindButtonByLabel(key->label);
}
}
if (button == NULL) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find '%s' button on device list\n", key->label);
goto f_end;
}
// WE have a iaqualink button, press it.
send_aqt_cmd(button->keycode);
// See if we want to use the last color, or turn it off
if (use_current_mode || turn_off) {
// After pressing the button, Just need to wait for 5 seconds and it will :-
// a) if off turn on and default to last color.
// b) if on, turn off. (pain that we need to wait 5 seconds.)
waitfor_iaqt_queue2empty();
waitfor_iaqt_nextPage(aq_data);
if (use_current_mode) {
// Their is no message for this, so give one.
sprintf(aq_data->last_display_message, "Light will turn on in 5 seconds");
aq_data->is_display_message_programming = true;
aq_data->updated = true;
}
// Wait for next page maybe?
// Below needs a timeout.
while (waitfor_iaqt_nextPage(aq_data) == IAQ_PAGE_COLOR_LIGHT);
goto f_end;
}
// BELOW WE NEED TO CATER FOR OK POPUP IF LIGHT IS ALREADY ON
if (button->state == 0x01) { // Light is on, need to select the BUTTON
waitfor_iaqt_queue2empty();
// We Should wait for popup message, before sending code
send_aqt_cmd(KEY_IAQTCH_OK);
}
if (waitfor_iaqt_nextPage(aq_data) != IAQ_PAGE_COLOR_LIGHT) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not color light page\n");
goto f_end;
}
button = iaqtFindButtonByLabel(mode_name);
if (button == NULL) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did find color '%s' in color light page\n",mode_name);
goto f_end;
}
//LOG(IAQT_LOG, LOG_ERR, "IAQ Touch WAIYING FOR 1 MESSAGES\n");
//waitfor_iaqt_messages(aq_data, 1);
// Finally found the color. select it
send_aqt_cmd(button->keycode);
waitfor_iaqt_nextPage(aq_data);
f_end:
goto_iaqt_page(IAQ_PAGE_HOME, aq_data);
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
void *set_aqualink_iaqtouch_pump_rpm( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
char *buf = (char*)threadCtrl->thread_args;
char VSPstr[20];
int structIndex;
struct iaqt_page_button *button;
//printf("**** program string '%s'\n",buf);
int pumpIndex = atoi(&buf[0]);
int pumpRPM = -1;
//int pumpRPM = atoi(&buf[2]);
for (structIndex=0; structIndex < aq_data->num_pumps; structIndex++) {
if (aq_data->pumps[structIndex].pumpIndex == pumpIndex) {
if (aq_data->pumps[structIndex].pumpType == PT_UNKNOWN) {
LOG(IAQT_LOG,LOG_ERR, "Can't set Pump RPM/GPM until type is known\n");
cleanAndTerminateThread(threadCtrl);
return ptr;
}
pumpRPM = RPM_check(aq_data->pumps[structIndex].pumpType, atoi(&buf[2]), aq_data);
break;
}
}
//int pumpRPM = atoi(&buf[2]);
//int pumpIndex = 1;
// Just force to pump 1 for testing
sprintf(VSPstr, "VSP%1d Spd ADJ",pumpIndex);
// NSF Should probably check pumpRPM is not -1 here
waitForSingleThreadOrTerminate(threadCtrl, AQ_SET_IAQTOUCH_PUMP_RPM);
LOG(IAQT_LOG,LOG_NOTICE, "IAQ Touch Set Pump %d to RPM %d\n",pumpIndex,pumpRPM);
if ( goto_iaqt_page(IAQ_PAGE_DEVICES, aq_data) == false )
goto f_end;
button = iaqtFindButtonByLabel(VSPstr);
if (button == NULL) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find '%s' button on page setup\n", VSPstr);
goto f_end;
}
send_aqt_cmd(button->keycode);
if (waitfor_iaqt_nextPage(aq_data) != IAQ_PAGE_SET_VSP) {
LOG(IAQT_LOG, LOG_ERR, "IAQ Touch did not find %s page\n", VSPstr);
goto f_end;
}
LOG(IAQT_LOG, LOG_INFO, "IAQ Touch got to %s page\n", VSPstr);
//send_aqt_cmd(ACK_CMD_READY_CTRL);
queue_iaqt_control_command(0, pumpRPM);
waitfor_iaqt_ctrl_queue2empty();
LOG(IAQT_LOG, LOG_INFO, "IAQ Touch got to %s page\n", VSPstr);
// We should get the device page back.
waitfor_iaqt_nextPage(aq_data);
//goto_iaqt_page(IAQ_PAGE_STATUS, aq_data);
/*
// Send Devices button.
send_aqt_cmd(KEY_IAQTCH_HOME);
waitfor_iaqt_queue2empty();
sleep(1);
send_aqt_cmd(KEY_IAQTCH_HOMEP_KEY08);
waitfor_iaqt_queue2empty();
sleep(1);
send_aqt_cmd(KEY_IAQTCH_KEY03);
*/
//send_aqt_cmd(0x80);
// Go to status page on startup to read devices
// This is too soon
//goto_iaqt_page(IAQ_PAGE_STATUS, aq_data);
//waitfor_iaqt_nextPage(aq_data);
f_end:
goto_iaqt_page(IAQ_PAGE_HOME, aq_data);
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
void *set_aqualink_iaqtouch_vsp_assignments( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
waitForSingleThreadOrTerminate(threadCtrl, AQ_GET_IAQTOUCH_VSP_ASSIGNMENT);
if ( goto_iaqt_page(IAQ_PAGE_VSP_SETUP, aq_data) == false )
goto f_end;
/* Info: Button 00| ePump | type=0xff | state=0x00 | unknown=0xff
* Info: Button 01| Intelliflo VF | type=0xff | state=0x00 | unknown=0xff
* Info: Button 02| Intelliflo VS | type=0xff | state=0x00 | unknown=0xff
* Info: Button 03| ePump | type=0xff | state=0x00 | unknown=0xff
* ---- This is next column
* Info: Button 04| Filtration | type=0xff | state=0x00 | unknown=0xff
* Info: Button 05| Filtration | type=0xff | state=0x00 | unknown=0xff
* Info: Button 06| Filtration | type=0xff | state=0x00 | unknown=0xff
* Info: Button 07| Not Installed | type=0xff | state=0x00 | unknown=0xff
* ----- Next column
* Info: Button 08| 600 RPM | type=0xff | state=0x00 | unknown=0xff
* Info: Button 09| 15 GPM | type=0xff | state=0x00 | unknown=0xff
* Info: Button 10| 600 RPM | type=0xff | state=0x00 | unknown=0xff
* Info: Button 11| 600 RPM | type=0xff | state=0x00 | unknown=0xff
* ----- Next colums
* Info: Button 12| 3450 RPM | type=0xff | state=0x00 | unknown=0xff
* Info: Button 13| 130 GPM | type=0xff | state=0x00 | unknown=0xff
* Info: Button 14| 3450 RPM | type=0xff | state=0x00 | unknown=0xff
* Info: Button 15| 3450 RPM | type=0xff | state=0x00 | unknown=0xff
* ----- Next column
* Info: Button 16| 2500 | type=0xff | state=0x00 | unknown=0xff
* Info: Button 17| N/A | type=0xff | state=0x00 | unknown=0xff
* Info: Button 18| 2500 | type=0xff | state=0x00 | unknown=0xff
* Info: Button 19| 2500 | type=0xff | state=0x00 | unknown=0xff
* ----- Next column
* Info: Button 20| :03 | type=0xff | state=0x00 | unknown=0xff
* Info: Button 21| N/A | type=0xff | state=0x00 | unknown=0xff
* Info: Button 22| :03 | type=0xff | state=0x00 | unknown=0xff
* Info: Button 23| :03 | type=0xff | state=0x00 | unknown=0xff
*
*/
f_end:
goto_iaqt_page(IAQ_PAGE_HOME, aq_data);
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
void *get_aqualink_iaqtouch_freezeprotect( void *ptr )
{
LOG(IAQT_LOG,LOG_ERR, "IAQ Touch get_aqualink_iaqtouch_freezeprotect has not beed tested\n");
LOG(IAQT_LOG,LOG_ERR, "**** We should not be here ****\n");
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
waitForSingleThreadOrTerminate(threadCtrl, AQ_GET_IAQTOUCH_FREEZEPROTECT);
if ( goto_iaqt_page(IAQ_PAGE_FREEZE_PROTECT, aq_data) == false )
goto f_end;
// The Message at index 0 is the deg that freeze protect is set to.
int frz = rsm_atoi(iaqtGetMessageLine(0));
if (frz >= 0) {
aq_data->frz_protect_set_point = frz;
LOG(IAQT_LOG,LOG_NOTICE, "IAQ Touch Freeze Protection setpoint %d\n",frz);
}
// Need to run over table messages and check ens with X for on off.
// Go to status page on startup to read devices
goto_iaqt_page(IAQ_PAGE_STATUS, aq_data);
f_end:
goto_iaqt_page(IAQ_PAGE_HOME, aq_data);
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
void *get_aqualink_iaqtouch_setpoints( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
struct iaqt_page_button *button;
waitForSingleThreadOrTerminate(threadCtrl, AQ_GET_IAQTOUCH_SETPOINTS);
// Get product info.
send_aqt_cmd(KEY_IAQTCH_HELP);
waitfor_iaqt_nextPage(aq_data);
if ( goto_iaqt_page(IAQ_PAGE_SET_TEMP, aq_data) == false )
goto f_end;
// Button 0 is "Pool Heat 50"
// Button 2 is "Spa Heat 100"
button = iaqtFindButtonByLabel("Pool Heat");
if (button != NULL) {
aq_data->pool_htr_set_point = rsm_atoi((char *)&button->name + 9);
LOG(IAQT_LOG,LOG_DEBUG, "IAQ Touch got to Pool heater setpoint %d\n",aq_data->pool_htr_set_point);
} else {
button = iaqtFindButtonByLabel("Temp1");
if (button != NULL) {
aq_data->pool_htr_set_point = rsm_atoi((char *)&button->name + 5);
LOG(IAQT_LOG,LOG_DEBUG, "IAQ Touch got to Temp1 setpoint %d\n",aq_data->pool_htr_set_point);
if (isSINGLE_DEV_PANEL != true)
{
changePanelToMode_Only();
LOG(IAQT_LOG,LOG_ERR, "AqualinkD set to 'Combo Pool & Spa' but detected 'Only Pool OR Spa' panel, please change config\n");
}
}
}
button = iaqtFindButtonByLabel("Spa Heat");
if (button != NULL) {
aq_data->spa_htr_set_point = rsm_atoi((char *)&button->name + 8);
LOG(IAQT_LOG,LOG_DEBUG, "IAQ Touch got to Spa heater setpoint %d\n",aq_data->spa_htr_set_point);
} else {
button = iaqtFindButtonByLabel("Temp2");
if (button != NULL) {
aq_data->spa_htr_set_point = rsm_atoi((char *)&button->name + 5);
LOG(IAQT_LOG,LOG_DEBUG, "IAQ Touch got to Temp2 setpoint %d\n",aq_data->spa_htr_set_point);
if (isSINGLE_DEV_PANEL != true)
{
changePanelToMode_Only();
LOG(IAQT_LOG,LOG_ERR, "AqualinkD set to 'Combo Pool & Spa' but detected 'Only Pool OR Spa' panel, please change config\n");
}
}
}
if ( goto_iaqt_page(IAQ_PAGE_FREEZE_PROTECT, aq_data) == false )
goto f_end;
// The Message at index 0 is the deg that freeze protect is set to.
int frz = rsm_atoi(iaqtGetMessageLine(0));
if (frz >= 0) {
aq_data->frz_protect_set_point = frz;
LOG(IAQT_LOG,LOG_NOTICE, "IAQ Touch Freeze Protection setpoint %d\n",frz);
}
// Get the temperature units if we are in iaq touch PDA mode
if (isPDA_PANEL) {
// If we are here, hit back then next button to get button with degrees on it.
// Only if in PDA mode
send_aqt_cmd(KEY_IAQTCH_BACK); // Clear the feeze protect menu and go back to system setup
if ( waitfor_iaqt_nextPage(aq_data) != IAQ_PAGE_SYSTEM_SETUP )
{
LOG(IAQT_LOG,LOG_ERR, "Couldn't get back to setup page, Temperature units unknown, default to DegF\n");
aq_data->temp_units = FAHRENHEIT;
goto f_end;
}
send_aqt_cmd(KEY_IAQTCH_NEXT_PAGE_ALTERNATE); // Go to page 2
if ( waitfor_iaqt_nextPage(aq_data) != IAQ_PAGE_SYSTEM_SETUP2 )
{
LOG(IAQT_LOG,LOG_ERR, "Couldn't get back to setup page, Temperature units unknown, default to DegF\n");
aq_data->temp_units = FAHRENHEIT;
goto f_end;
}
button = iaqtFindButtonByLabel("Degrees");
if (button != NULL) {
LOG(IAQT_LOG,LOG_NOTICE, "Temperature units are '%s'\n",button->name);
if (button->name[8] == 'C') {
LOG(IAQT_LOG,LOG_NOTICE, "Temperature unit message is '%s' set to degC\n",button->name);
aq_data->temp_units = CELSIUS;
} else {
LOG(IAQT_LOG,LOG_NOTICE, "Temperature unit message is '%s' set to degF\n",button->name);
aq_data->temp_units = FAHRENHEIT;
}
/*
printf("************** DEG IS '%c'\n", (uintptr_t)button->name[8]);
if ( (uintptr_t)button->name[8] == 'C') { // NSF THIS DOES NOT WORK, LEAVE COMPILER WARNING TO COME BACK AND FIX
aq_data->temp_units = CELSIUS;
} else {
aq_data->temp_units = FAHRENHEIT;
}
*/
}
}
// Need to run over table messages and check ens with X for on off.
// Go to status page on startup to read devices
goto_iaqt_page(IAQ_PAGE_STATUS, aq_data);
f_end:
goto_iaqt_page(IAQ_PAGE_HOME, aq_data);
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
// THIS IS NOT FINISHED.
void *get_aqualink_iaqtouch_aux_labels( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
int i;
waitForSingleThreadOrTerminate(threadCtrl, AQ_GET_IAQTOUCH_AUX_LABELS);
if ( goto_iaqt_page(IAQ_PAGE_LABEL_AUX, aq_data) == false )
goto f_end;
// Need to loop over messages. Tab 0x09 is next in each message
/*
* Info: Table Messages 00| Circuit Circuit Name Freeze
* Info: Table Messages 01| Aux1 Aux1 No
* Info: Table Messages 02| Aux2 Aux2 No
* Info: Table Messages 03| Aux3 Pool Light No
* Info: Table Messages 04| Aux4 Aux4 No
* Info: Table Messages 05| Aux5 Aux5 No
* Info: Table Messages 06| Aux6 Aux6 No
* Info: Table Messages 07| Aux7 Aux7 No
*
* Info: Table Messages ??| Aux B7 Aux B7 No
*/