forked from sfeakes/AqualinkD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pda_aq_programmer.c
1260 lines (1074 loc) · 40.4 KB
/
pda_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 <stdarg.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include "aqualink.h"
#include "utils.h"
#include "aq_programmer.h"
#include "aq_serial.h"
#include "pda.h"
#include "pda_menu.h"
#include "pda_aq_programmer.h"
#include "config.h"
#include "aq_panel.h"
#ifdef AQ_DEBUG
#include <time.h>
#include "timespec_subtract.h"
#endif
bool waitForPDAMessageHighlight(struct aqualinkdata *aq_data, int highlighIndex, int numMessageReceived);
bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype, int numMessageReceived);
bool waitForPDAMessageTypes(struct aqualinkdata *aq_data, unsigned char mtype1, unsigned char mtype2, int numMessageReceived);
bool waitForPDAMessageTypesOrMenu(struct aqualinkdata *aq_data, unsigned char mtype1, unsigned char mtype2, int numMessageReceived, char *text, int line);
bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu);
bool wait_pda_selected_item(struct aqualinkdata *aq_data);
bool waitForPDAnextMenu(struct aqualinkdata *aq_data);
bool loopover_devices(struct aqualinkdata *aq_data);
bool find_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, int charlimit);
bool select_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, bool waitForNextMenu);
static pda_type _PDA_Type;
/*
// Each RS message / call to this function is around 0.2 seconds apart
//#define MAX_ACK_FOR_THREAD 200 // ~40 seconds (Init takes 30)
#define MAX_ACK_FOR_THREAD 60 // ~12 seconds (testing, will stop every thread)
// *** DELETE THIS WHEN PDA IS OUT OF BETA ****
void pda_programming_thread_check(struct aqualinkdata *aq_data)
{
static pthread_t thread_id = 0;
static int ack_count = 0;
#ifdef AQ_DEBUG
static struct timespec start;
static struct timespec now;
struct timespec elapsed;
#endif
// Check for long lasting threads
if (aq_data->active_thread.thread_id != 0) {
if (thread_id != *aq_data->active_thread.thread_id) {
printf ("**************** LAST POINTER SET %ld , %p ****************************\n",thread_id,&thread_id);
thread_id = *aq_data->active_thread.thread_id;
#ifdef AQ_DEBUG
clock_gettime(CLOCK_REALTIME, &start);
#endif
printf ("**************** NEW POINTER SET %d, %ld %ld , %p %p ****************************\n",aq_data->active_thread.ptype,thread_id,*aq_data->active_thread.thread_id,&thread_id,aq_data->active_thread.thread_id);
ack_count = 0;
} else if (ack_count > MAX_ACK_FOR_THREAD) {
#ifdef AQ_DEBUG
clock_gettime(CLOCK_REALTIME, &now);
timespec_subtract(&elapsed, &now, &start);
LOG(PDA_LOG,LOG_ERR, "Thread %d,%p FAILED to finished in reasonable time, %d.%03ld sec, killing it.\n",
aq_data->active_thread.ptype,
aq_data->active_thread.thread_id,
elapsed.tv_sec, elapsed.tv_nsec / 1000000L);
#else
LOG(PDA_LOG,LOG_ERR, "Thread %d,%p FAILED to finished in reasonable time, killing it!\n", aq_data->active_thread.ptype, aq_data->active_thread.thread_id)
#endif
if (pthread_cancel(*aq_data->active_thread.thread_id) != 0)
LOG(PDA_LOG,LOG_ERR, "Thread kill failed\n");
else {
}
aq_data->active_thread.thread_id = 0;
aq_data->active_thread.ptype = AQP_NULL;
ack_count = 0;
thread_id = 0;
} else {
ack_count++;
}
} else {
ack_count = 0;
thread_id = 0;
}
}
*/
bool wait_pda_selected_item(struct aqualinkdata *aq_data)
{
int i=0;
while (pda_m_hlightindex() == -1 && i < 5){
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,10);
i++;
}
if (pda_m_hlightindex() == -1)
return false;
else
return true;
}
bool waitForPDAnextMenu(struct aqualinkdata *aq_data) {
waitForPDAMessageType(aq_data,CMD_PDA_CLEAR,10);
return waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,15);
}
bool loopover_devices(struct aqualinkdata *aq_data) {
int i;
int index = -1;
if (! goto_pda_menu(aq_data, PM_EQUIPTMENT_CONTROL)) {
LOG(PDA_LOG,LOG_ERR, "loopover_devices :- can't goto PM_EQUIPTMENT_CONTROL menu\n");
//cleanAndTerminateThread(threadCtrl);
return false;
}
// Should look for message "ALL OFF", that's end of device list.
for (i=0; i < 18 && (index = pda_find_m_index("ALL OFF")) == -1 ; i++) {
send_cmd(KEY_PDA_DOWN);
//waitForMessage(aq_data, NULL, 1);
waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_MSG_LONG,8);
}
if (index == -1) {
LOG(PDA_LOG,LOG_ERR, "loopover_devices :- can't find ALL OFF\n");
return false;
}
return true;
}
/*
if charlimit is set, use case insensitive match and limit chars.
if charlimit is -1, use VERY loose matching.
*/
bool find_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, int charlimit) {
int i=pda_m_hlightindex();
int min_index = -1;
int max_index = -1;
int index = -1;
int cnt = 0;
LOG(PDA_LOG,LOG_DEBUG, "PDA Device programmer looking for menu text '%s' (limit=%d)\n",menuText,charlimit);
if (charlimit == 0)
index = pda_find_m_index(menuText);
else if (charlimit > 0)
index = pda_find_m_index_case(menuText, charlimit);
else if (charlimit == -1)
index = pda_find_m_index_loose(menuText);
//int index = (charlimit == 0)?pda_find_m_index(menuText):pda_find_m_index_case(menuText, charlimit);
if (index < 0) { // No menu, is there a page down. "PDA Line 9 = ^^ MORE __"
if (strncasecmp(pda_m_line(9)," ^^ MORE", 10) == 0) {
int j;
for(j=0; j < 20; j++) {
send_cmd(KEY_PDA_DOWN);
//delay(500);
//wait_for_empty_cmd_buffer();
//waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,2);
waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_MSG_LONG,8);
//waitForMessage(aq_data, NULL, 1);
index = (charlimit == 0)?pda_find_m_index(menuText):pda_find_m_index_case(menuText, charlimit);
if (index >= 0) {
i=pda_m_hlightindex();
break;
}
}
if (index < 0) {
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer couldn't find menu item on any page '%s'\n",menuText);
return false;
}
} else {
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer couldn't find menu item '%s' in menu %d index %d\n", menuText, pda_m_type(), index);
return false;
}
}
if (strncasecmp(pda_m_line(9)," ^^ MORE", 10) != 0) {
if (pda_m_type() == PM_HOME) {
min_index = 4;
max_index = 9;
} else if (pda_m_type() == PM_EQUIPTMENT_CONTROL) {
min_index = 1;
max_index = 9;
} else if (pda_m_type() == PM_MAIN) {
// Line 0 = MAIN MENU
// Line 1 =
// Line 2 = HELP >
// Line 3 = PROGRAM >
// Line 4 = SET TEMP >
// Line 5 = SET TIME >
// Line 6 = PDA OPTIONS >
// Line 7 = SYSTEM SETUP >
// Line 8 =
// Line 9 =
// Line 0 = MAIN MENU
// Line 1 = HELP >
// Line 2 = PROGRAM >
// Line 3 = SET TEMP >
// Line 4 = SET TIME >
// Line 5 = SET AquaPure >
// Line 6 = PDA OPTIONS >
// Line 7 = SYSTEM SETUP >
// Line 8 =
// Line 9 = BOOST
// "SET AquaPure" and "BOOST" are only present when filter pump is running
if (strncasecmp(pda_m_line(9)," BOOST ", 16) == 0) {
min_index = 1;
max_index = 8; // to account for 8 missing
if (index == 9) { // looking for boost
index = 8;
}
} else {
min_index = 2;
max_index = 7;
}
}
}
LOG(PDA_LOG,LOG_DEBUG, "find_pda_menu_item i=%d idx=%d min=%d max=%d\n",
i, index, min_index, max_index);
if (i < index) {
if ((min_index != -1) && ((index - i) > (i - min_index + max_index - index + 1))) {
cnt = i - min_index + max_index - index + 1;
for (i=0; i < cnt; i++) {
send_cmd(KEY_PDA_UP);
}
} else {
for (i=pda_m_hlightindex(); i < index; i++) {
send_cmd(KEY_PDA_DOWN);
}
}
} else if (i > index) {
if ((min_index != -1) && ((i - index) > (index - min_index + max_index - i + 1))) {
cnt = i - min_index + max_index - index + 1;
for (i=0; i < cnt; i++) {
send_cmd(KEY_PDA_UP);
}
} else {
for (i=pda_m_hlightindex(); i > index; i--) {
send_cmd(KEY_PDA_UP);
}
}
}
return waitForPDAMessageHighlight(aq_data, index, 10);
}
bool _select_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, bool waitForNextMenu, bool loose);
bool select_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, bool waitForNextMenu){
return _select_pda_menu_item(aq_data, menuText, waitForNextMenu, false);
}
bool select_pda_menu_item_loose(struct aqualinkdata *aq_data, char *menuText, bool waitForNextMenu){
return _select_pda_menu_item(aq_data, menuText, waitForNextMenu, true);
}
bool _select_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, bool waitForNextMenu, bool loose) {
//int matchType = loose?-1:0; // NSF release 2.1.0 was this and it worked. Need to re-check why I did this.
//int matchType = loose?-1:1;
int matchType = loose?-1:strlen(menuText); // NSF Not way to check this. (release 2.2.0 introduced this with the line above)
if ( find_pda_menu_item(aq_data, menuText, matchType) ) {
send_cmd(KEY_PDA_SELECT);
LOG(PDA_LOG,LOG_DEBUG, "PDA Device programmer selected menu item '%s'\n",menuText);
if (waitForNextMenu)
waitForPDAnextMenu(aq_data);
return true;
}
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer couldn't select menu item '%s' menu %d\n",menuText, pda_m_type());
return false;
}
// for reference see H0572300 - AquaLink PDA I/O Manual
// https://www.jandy.com/-/media/zodiac/global/downloads/h/h0572300.pdf
// and H0574200 - AquaPalm Wireless Handheld Remote Installation and Operation Manual
// https://www.jandy.com/-/media/zodiac/global/downloads/h/h0574200.pdf
// and 6594 - AquaLink RS Control Panel Installation Manual
// https://www.jandy.com/-/media/zodiac/global/downloads/0748-91071/6594.pdf
bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu) {
bool ret = true;
int cnt = 0;
LOG(PDA_LOG,LOG_DEBUG, "PDA Device programmer request for menu %d\n",menu);
if (pda_m_type() == PM_FW_VERSION) {
LOG(PDA_LOG,LOG_DEBUG, "goto_pda_menu at FW version menu\n");
send_cmd(KEY_PDA_BACK);
if (! waitForPDAnextMenu(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer wait for next menu failed\n");
}
} else if (pda_m_type() == PM_BUILDING_HOME) {
LOG(PDA_LOG,LOG_DEBUG, "goto_pda_menu building home menu\n");
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,15);
}
while (ret && (pda_m_type() != menu) && cnt <= 5) {
switch (menu) {
case PM_HOME:
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
break;
case PM_EQUIPTMENT_CONTROL:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "EQUIPMENT ON/OFF", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
break;
case PM_PALM_OPTIONS:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "PALM OPTIONS", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
break;
case PM_AUX_LABEL:
if ( _PDA_Type == PDA) {
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "SYSTEM SETUP", true);
} else if (pda_m_type() == PM_SYSTEM_SETUP) {
ret = select_pda_menu_item(aq_data, "LABEL AUX", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
} else {
LOG(PDA_LOG,LOG_ERR, "PDA in AquaPlalm mode, there is no SYSTEM SETUP / LABEL AUX menu\n");
}
break;
case PM_SYSTEM_SETUP:
if ( _PDA_Type == PDA) {
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "SYSTEM SETUP", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
} else {
LOG(PDA_LOG,LOG_ERR, "PDA in AquaPlalm mode, there is no SYSTEM SETUP menu\n");
}
break;
case PM_FREEZE_PROTECT:
if ( _PDA_Type == PDA) {
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "SYSTEM SETUP", true);
} else if (pda_m_type() == PM_SYSTEM_SETUP) {
ret = select_pda_menu_item(aq_data, "FREEZE PROTECT", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
} else {
LOG(PDA_LOG,LOG_ERR, "PDA in AquaPlalm mode, there is no SYSTEM SETUP / FREEZE PROTECT menu\n");
}
break;
case PM_AQUAPURE:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "SET AquaPure", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
break;
case PM_BOOST:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item_loose(aq_data, "BOOST", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
//printf("****MENU SELECT RETURN %d*****\n",ret);
break;
case PM_SET_TEMP:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
if (isCOMBO_PANEL) {
ret = select_pda_menu_item(aq_data, "SET TEMP", true);
} else {
// Depending on control panel config, may get an extra menu asking to press any key
LOG(PDA_LOG,LOG_DEBUG, "PDA in single device mode, \n");
ret = select_pda_menu_item(aq_data, "SET TEMP", false);
// We could press enter here, but I can't test it, so just wait for message to dissapear.
ret = waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,25);
//waitForPDAMessageType(aq_data,CMD_PDA_CLEAR,10);
//waitForPDAMessageTypesOrMenu(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,20,"press ANY key",8);
}
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
break;
case PM_SET_TIME:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "SET TIME", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
break;
default:
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer didn't understand requested menu\n");
return false;
break;
}
LOG(PDA_LOG,LOG_DEBUG, "PDA Device programmer request for menu %d, current %d\n", menu, pda_m_type());
cnt++;
}
if (pda_m_type() != menu) {
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer didn't find a requested menu %d, current %d\n", menu, pda_m_type());
return false;
}
return true;
}
void *set_aqualink_PDA_device_on_off( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
//int i=0;
//int found;
char device_name[15];
waitForSingleThreadOrTerminate(threadCtrl, AQ_PDA_DEVICE_ON_OFF);
char *buf = (char*)threadCtrl->thread_args;
unsigned int device = atoi(&buf[0]);
unsigned int state = atoi(&buf[5]);
if (device > aq_data->total_buttons) {
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off :- bad device number '%d'\n",device);
cleanAndTerminateThread(threadCtrl);
return ptr;
}
LOG(PDA_LOG,LOG_INFO, "PDA Device On/Off, device '%s', state %d\n",aq_data->aqbuttons[device].label,state);
if (! goto_pda_menu(aq_data, PM_EQUIPTMENT_CONTROL)) {
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off :- can't find EQUIPTMENT CONTROL menu\n");
cleanAndTerminateThread(threadCtrl);
return ptr;
}
// If single config (Spa OR pool) rather than (Spa AND pool) heater is TEMP1 and TEMP2
if (isSINGLE_DEV_PANEL && device == aq_data->pool_heater_index) { // rename Heater and Spa
sprintf(device_name,"%-13s\n","TEMP1");
} else if (isSINGLE_DEV_PANEL && device == aq_data->spa_heater_index) {// rename Heater and Spa
sprintf(device_name,"%-13s\n","TEMP2");
} else {
//Pad name with spaces so something like "SPA" doesn't match "SPA BLOWER"
sprintf(device_name,"%-13s\n",aq_data->aqbuttons[device].label);
}
if ( find_pda_menu_item(aq_data, device_name, 13) ) {
if (aq_data->aqbuttons[device].led->state != state) {
//printf("*** Select State ***\n");
LOG(PDA_LOG,LOG_INFO, "PDA Device On/Off, found device '%s', changing state\n",aq_data->aqbuttons[device].label,state);
send_cmd(KEY_PDA_SELECT);
while (get_aq_cmd_length() > 0) { delay(500); }
// If you are turning on a heater there will be a sub menu to set temp
if ((state == ON) && ((device == aq_data->pool_heater_index) || (device == aq_data->spa_heater_index))) {
if (! waitForPDAnextMenu(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off: %s on - waitForPDAnextMenu\n",
aq_data->aqbuttons[device].label);
} else {
send_cmd(KEY_PDA_SELECT);
while (get_aq_cmd_length() > 0) { delay(500); }
if (!waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,20)) {
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off: %s on - wait for CMD_PDA_HIGHLIGHT\n",
aq_data->aqbuttons[device].label);
}
}
} else { // not turning on heater wait for line update
// worst case spa when pool is running
if (!waitForPDAMessageType(aq_data,CMD_MSG_LONG,2)) {
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off: %s on - wait for CMD_MSG_LONG\n",
aq_data->aqbuttons[device].label);
}
}
} else {
LOG(PDA_LOG,LOG_INFO, "PDA Device On/Off, found device '%s', not changing state, is same\n",aq_data->aqbuttons[device].label,state);
}
} else {
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off, device '%s' not found\n",aq_data->aqbuttons[device].label);
}
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
void *get_aqualink_PDA_device_status( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
//int i;
waitForSingleThreadOrTerminate(threadCtrl, AQ_PDA_DEVICE_STATUS);
goto_pda_menu(aq_data, PM_HOME);
if (! loopover_devices(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Device Status :- failed\n");
}
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
void *set_aqualink_PDA_init( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
//int i=0;
waitForSingleThreadOrTerminate(threadCtrl, AQ_PDA_INIT);
//int val = atoi((char*)threadCtrl->thread_args);
//LOG(PDA_LOG,LOG_DEBUG, "PDA Init\n", val);
LOG(PDA_LOG,LOG_DEBUG, "PDA Init\n");
if (pda_m_type() == PM_FW_VERSION) {
// check pda_m_line(1) to "AquaPalm"
if (strstr(pda_m_line(1), "AquaPalm") != NULL) {
_PDA_Type = AQUAPALM;
} else {
_PDA_Type = PDA;
}
char *ptr1 = pda_m_line(1);
char *ptr2 = pda_m_line(5);
ptr1[AQ_MSGLEN+1] = '\0';
ptr2[AQ_MSGLEN+1] = '\0';
//strcpy(aq_data->version, stripwhitespace(ptr));
snprintf(aq_data->version, (AQ_MSGLEN*2)-1, "%s %s",stripwhitespace(ptr1),stripwhitespace(ptr2));
//printf("****** Version '%s' ********\n",aq_data->version);
LOG(PDA_LOG,LOG_DEBUG, "PDA type=%d, version=%s\n", _PDA_Type, aq_data->version);
// don't wait for version menu to time out press back to get to home menu faster
send_cmd(KEY_PDA_BACK);
if (! waitForPDAnextMenu(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Init :- wait for next menu failed\n");
}
}
else {
LOG(PDA_LOG,LOG_ERR, "PDA Init :- should be called when on FW VERSION menu.\n");
}
/*
// Get status of all devices
if (! loopover_devices(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Init :- can't find menu\n");
}
*/
// Get heater setpoints
if (! get_PDA_aqualink_pool_spa_heater_temps(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Init :- Error getting heater setpoints\n");
}
// Get freeze protect setpoint, AquaPalm doesn't have freeze protect in menu.
if (_PDA_Type != AQUAPALM && ! get_PDA_freeze_protect_temp(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Init :- Error getting freeze setpoints\n");
}
pda_reset_sleep();
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
void *set_aqualink_PDA_wakeinit( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
//int i=0;
// At this point, we should probably just exit if there is a thread already going as
// it means the wake was called due to changing a device.
waitForSingleThreadOrTerminate(threadCtrl, AQ_PDA_WAKE_INIT);
LOG(PDA_LOG,LOG_DEBUG, "PDA Wake Init\n");
// Get status of all devices
if (! loopover_devices(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Init :- can't find menu\n");
}
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
bool get_PDA_freeze_protect_temp(struct aqualinkdata *aq_data) {
if ( _PDA_Type == PDA) {
if (! goto_pda_menu(aq_data, PM_FREEZE_PROTECT)) {
return false;
}
/* select the freeze protect temp to see which devices are enabled by freeze
protect */
send_cmd(KEY_PDA_SELECT);
return waitForPDAnextMenu(aq_data);
} else {
LOG(PDA_LOG,LOG_INFO, "In PDA AquaPalm mode, freezepoints not supported\n");
return false;
}
}
bool get_PDA_aqualink_pool_spa_heater_temps(struct aqualinkdata *aq_data) {
// Get heater setpoints
if (! goto_pda_menu(aq_data, PM_SET_TEMP)) {
return false;
}
return true;
}
bool waitForPDAMessageHighlight(struct aqualinkdata *aq_data, int highlighIndex, int numMessageReceived)
{
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageHighlight index %d\n",highlighIndex);
if(pda_m_hlightindex() == highlighIndex) return true;
int i=0;
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
while( ++i <= numMessageReceived)
{
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageHighlight last = 0x%02hhx : index %d : (%d of %d)\n",aq_data->last_packet_type,pda_m_hlightindex(),i,numMessageReceived);
if (aq_data->last_packet_type == CMD_PDA_HIGHLIGHT && pda_m_hlightindex() == highlighIndex) break;
pthread_cond_wait(&aq_data->active_thread.thread_cond, &aq_data->active_thread.thread_mutex);
}
pthread_mutex_unlock(&aq_data->active_thread.thread_mutex);
if (pda_m_hlightindex() != highlighIndex) {
//LOG(PDA_LOG,LOG_ERR, "Could not select MENU of Aqualink control panel\n");
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageHighlight: did not receive index '%d'\n",highlighIndex);
return false;
} else
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageHighlight: received index '%d'\n",highlighIndex);
return true;
}
bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype, int numMessageReceived)
{
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageType 0x%02hhx\n",mtype);
int i=0;
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
while( ++i <= numMessageReceived)
{
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageType 0x%02hhx, last message type was 0x%02hhx (%d of %d)\n",mtype,aq_data->last_packet_type,i,numMessageReceived);
if (aq_data->last_packet_type == mtype) break;
pthread_cond_wait(&aq_data->active_thread.thread_cond, &aq_data->active_thread.thread_mutex);
}
pthread_mutex_unlock(&aq_data->active_thread.thread_mutex);
if (aq_data->last_packet_type != mtype) {
//LOG(PDA_LOG,LOG_ERR, "Could not select MENU of Aqualink control panel\n");
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageType: did not receive 0x%02hhx\n",mtype);
return false;
} else
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageType: received 0x%02hhx\n",mtype);
return true;
}
// Wait for Message, hit return on particular menu.
bool waitForPDAMessageTypesOrMenu(struct aqualinkdata *aq_data, unsigned char mtype1, unsigned char mtype2, int numMessageReceived, char *text, int line)
{
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageTypes 0x%02hhx or 0x%02hhx\n",mtype1,mtype2);
int i=0;
bool gotmenu = false;
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
while( ++i <= numMessageReceived)
{
if (gotmenu == false && line > 0 && text != NULL) {
if (stristr(pda_m_line(line), text) != NULL) {
send_cmd(KEY_PDA_SELECT);
gotmenu = true;
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageTypesOrMenu saw '%s' and line %d\n",text,line);
}
}
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageTypes 0x%02hhx | 0x%02hhx, last message type was 0x%02hhx (%d of %d)\n",mtype1,mtype2,aq_data->last_packet_type,i,numMessageReceived);
if (aq_data->last_packet_type == mtype1 || aq_data->last_packet_type == mtype2) break;
pthread_cond_wait(&aq_data->active_thread.thread_cond, &aq_data->active_thread.thread_mutex);
}
pthread_mutex_unlock(&aq_data->active_thread.thread_mutex);
if (aq_data->last_packet_type != mtype1 && aq_data->last_packet_type != mtype2) {
//LOG(PDA_LOG,LOG_ERR, "Could not select MENU of Aqualink control panel\n");
LOG(PDA_LOG,LOG_ERR, "waitForPDAMessageTypes: did not receive 0x%02hhx or 0x%02hhx\n",mtype1,mtype2);
return false;
} else
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageTypes: received 0x%02hhx\n",aq_data->last_packet_type);
return true;
}
bool waitForPDAMessageTypes(struct aqualinkdata *aq_data, unsigned char mtype1, unsigned char mtype2, int numMessageReceived)
{
return waitForPDAMessageTypesOrMenu(aq_data, mtype1, mtype2, numMessageReceived, NULL, 0);
}
bool set_PDA_numeric_field_value(struct aqualinkdata *aq_data, int val, int *cur_val, char *select_label, int step) {
int i=0;
if (val == *cur_val) {
LOG(PDA_LOG,LOG_INFO, "PDA %s value : already at %d\n", select_label, val);
return true;
}
if (select_label != NULL) {
// :TODO: Should probably change below to call find_pda_menu_item(), rather than doing it here
// If we lease this, need to limit on the number of loops
//while ( strncasecmp(pda_m_hlight(), select_label, 8) != 0 ) {
while ( strncasecmp(pda_m_hlight(), select_label, strlen(select_label)) != 0 ) {
send_cmd(KEY_PDA_DOWN);
delay(500); // Last message probably was CMD_PDA_HIGHLIGHT, so wait before checking.
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,2);
if (i > 10) {
LOG(PDA_LOG,LOG_ERR, "PDA numeric selector could not find string '%s'\n", select_label);
return false;
}
i++;
}
send_cmd(KEY_PDA_SELECT);
}
if (val < *cur_val) {
LOG(PDA_LOG,LOG_DEBUG, "PDA %s value : lower from %d to %d\n", select_label, *cur_val, val);
for (i = *cur_val; i > val; i=i-step) {
send_cmd(KEY_PDA_DOWN);
}
} else if (val > *cur_val) {
LOG(PDA_LOG,LOG_DEBUG, "PDA %s value : raise from %d to %d\n", select_label, *cur_val, val);
for (i = *cur_val; i < val; i=i+step) {
send_cmd(KEY_PDA_UP);
}
} else {
LOG(PDA_LOG,LOG_INFO, "PDA %s value : already at %d\n", select_label, val);
send_cmd(KEY_PDA_BACK);
return true;
}
send_cmd(KEY_PDA_SELECT);
LOG(PDA_LOG,LOG_DEBUG, "PDA %s value : set to %d\n", select_label, *cur_val);
return true;
}
bool set_PDA_aqualink_SWG_setpoint(struct aqualinkdata *aq_data, int val) {
if (! goto_pda_menu(aq_data, PM_AQUAPURE)) {
LOG(PDA_LOG,LOG_ERR, "Error finding SWG setpoints menu\n");
}
if (aq_data->aqbuttons[SPA_INDEX].led->state != OFF)
return set_PDA_numeric_field_value(aq_data, val, &aq_data->swg_percent, "SET SPA", 5);
else
return set_PDA_numeric_field_value(aq_data, val, &aq_data->swg_percent, "SET POOL", 5);
//return true;
}
bool set_PDA_aqualink_boost(struct aqualinkdata *aq_data, bool val)
{
if (! goto_pda_menu(aq_data, PM_BOOST)) {
LOG(PDA_LOG,LOG_ERR, "Error finding BOOST menu\n");
return false;
}
// Should be on the START menu item
if (val == true) { // Turn on should just be enter
if (strstr(pda_m_hlight(), "START") != NULL)
send_cmd(KEY_PDA_SELECT);
else {
LOG(PDA_LOG,LOG_ERR, "Error finding BOOST START menu\n");
return false;
}
} else {
/*
// Should be select options PAUSE | RESTART | STOP
int i=0;
for (i=0; i < 6; i++) {
send_cmd(KEY_PDA_DOWN);
waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,10);
if (strstr(pda_m_hlight(), "STOP") != NULL) {
send_cmd(KEY_PDA_SELECT);
break;
}
}
if (i >= 6)
LOG(PDA_LOG,LOG_ERR, "Error finding BOOST STOP menu\n");
// Should be select options PAUSE | RESTART | STOP
// so press down twice then select
*/
// NSF This is really crap, but can't get above to work, need to come back and check menu items against selections.
send_cmd(KEY_PDA_DOWN);
send_cmd(KEY_PDA_DOWN);
send_cmd(KEY_PDA_SELECT);
}
return true;
}
bool set_PDA_aqualink_heater_setpoint(struct aqualinkdata *aq_data, int val, bool isPool) {
char label[10];
int *cur_val;
if ( isCOMBO_PANEL ) {
if (isPool) {
sprintf(label, "POOL HEAT");
cur_val = &aq_data->pool_htr_set_point;
} else {
sprintf(label, "SPA HEAT");
cur_val = &aq_data->spa_htr_set_point;
}
} else {
if (isPool) {
sprintf(label, "TEMP1");
cur_val = &aq_data->pool_htr_set_point;
} else {
sprintf(label, "TEMP2");
cur_val = &aq_data->spa_htr_set_point;
}
}
if (val == *cur_val) {
LOG(PDA_LOG,LOG_INFO, "PDA %s setpoint : temp already %d\n", label, val);
send_cmd(KEY_PDA_BACK);
return true;
}
if (! goto_pda_menu(aq_data, PM_SET_TEMP)) {
LOG(PDA_LOG,LOG_ERR, "Error finding heater setpoints menu\n");
return false;
}
set_PDA_numeric_field_value(aq_data, val, cur_val, label, 1);
return true;
}
bool set_PDA_aqualink_freezeprotect_setpoint(struct aqualinkdata *aq_data, int val) {
if (_PDA_Type != PDA) {
LOG(PDA_LOG,LOG_INFO, "In PDA AquaPalm mode, freezepoints not supported\n");
return false;
} else if (! goto_pda_menu(aq_data, PM_FREEZE_PROTECT)) {
LOG(PDA_LOG,LOG_ERR, "Error finding freeze protect setpoints menu\n");
return false;
} else if (! set_PDA_numeric_field_value(aq_data, val, &aq_data->frz_protect_set_point, NULL, 1)) {
LOG(PDA_LOG,LOG_ERR, "Error failed to set freeze protect temp value\n");
return false;
} else {
return waitForPDAnextMenu(aq_data);
}
}
// Test ine this.
bool get_PDA_aqualink_aux_labels(struct aqualinkdata *aq_data) {
#ifdef BETA_PDA_AUTOLABEL
int i=0;
char label[10];
LOG(PDA_LOG,LOG_INFO, "Finding PDA labels, (BETA ONLY)\n");
if (! goto_pda_menu(aq_data, PM_AUX_LABEL)) {
LOG(PDA_LOG,LOG_ERR, "Error finding aux label menu\n");
return false;
}
for (i=1;i<8;i++) {
sprintf(label, "AUX%d",i);
select_pda_menu_item(aq_data, label, true);
send_cmd(KEY_PDA_BACK);
waitForPDAnextMenu(aq_data);
}
// Read first page of devices and make some assumptions.
#endif
return true;
}
/*
bool waitForPDAMessage(struct aqualinkdata *aq_data, int numMessageReceived, unsigned char packettype)
{
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessage %s %d\n",message,numMessageReceived);
int i=0;
pthread_mutex_init(&aq_data->active_thread.thread_mutex, NULL);
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
char* msgS;
char* ptr;
if (message != NULL) {
if (message[0] == '^')
msgS = &message[1];
else
msgS = message;
}
while( ++i <= numMessageReceived)
{
if (message != NULL)
LOG(PDA_LOG,LOG_DEBUG, "Programming mode: loop %d of %d looking for '%s' received message '%s'\n",i,numMessageReceived,message,aq_data->last_message);
else
LOG(PDA_LOG,LOG_DEBUG, "Programming mode: loop %d of %d waiting for next message, received '%s'\n",i,numMessageReceived,aq_data->last_message);
if (message != NULL) {
ptr = stristr(aq_data->last_message, msgS);
if (ptr != NULL) { // match
LOG(PDA_LOG,LOG_DEBUG, "Programming mode: String MATCH\n");
if (msgS == message) // match & don't care if first char
break;
else if (ptr == aq_data->last_message) // match & do care if first char
break;