-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheq3.exp
1267 lines (1070 loc) · 33.9 KB
/
eq3.exp
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
#!/usr/bin/expect
#
# Shell script in order to control eQ-3 radiator thermostat
#
# Version: 2020-12-29
#
# MIT License
#
# Copyright (c) 2017-2020 Martin Heckenbach
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# sets output to stdout and debug
log_user 0
set HELP(about) "\nFull-featured CLI for radiator thermostat eQ-3 CC-RT-BLE\n"
set HELP(usage) "Usage: \[<hciX>\] <mac/alias> <command> <parameters...>\n"
set HELP(sync) "\
sync - Syncs time and prints target temperature and mode"
set HELP(window) "\
window <temp> <h:mm> - Sets temperature in °C and period for open window mode\n\
\ temp: 5.0 to 29.5 in intervals of 0.5°C, e.g. 19.5 \n\
\ h:mm: time where minutes in intervals of 5 minutes but max. 1 hour, e.g. 1:00"
set HELP(auto) "\
auto - Sets auto mode and deactivates vacation mode if active."
set HELP(manual) "\
manual - Sets manual mode and deactivates vacation mode if active."
set HELP(comforteco) "\
comforteco <temp_comfort> <temp_eco> - Sets comfort and eco temperature in °C\n\
\ temp: 5.0 to 29.5 in intervals of 0.5°C, e.g. 19.5"
set HELP(offset) "\
offset <offset> - Sets the offset temperature in °C\n\
\ offset: temperature between -3.5 and 3.5 in intervals of 0.5°C, e.g. 1.5"
set HELP(comfort) "\
comfort - Sets target temperature to programmed comfort temperature"
set HELP(eco) "\
eco - Sets target temperature to programmed eco temperature"
set HELP(temperature) "\
temp <temp> - Sets target temperature to given value\n\
\ temp: 5.0 to 29.5 in intervals of 0.5°C, e.g. 19.5"
set HELP(vacation) "\
vacation <yy-mm-dd> <hh:mm> <temp> - Activates vacation mode until date and time and temperature in °C\n\
\ yy-mm-dd: until date, e.g. 17-03-31\n\
\ hh:mm: until time where minutes must be 00 or 30, e.g. 23:30\n\
\ temp: 5.0 to 29.5 in intervals of 0.5°C, e.g. 19.5
\ vacation <hh> <temp> - Activates vacation mode for given period in hours and temperature in °C\n\
\ hh: Period in hours\n\
\ temp: 5.0 to 29.5 in intervals of 0.5°C, e.g. 19.5"
set HELP(on) "\
on - Sets thermostat to permanent on (30°C)"
set HELP(off) "\
off - Sets thermostat to permanent off (4.5°C)"
set HELP(boost) "\
boost - Activates boost mode for 5 minutes"
set HELP(boost_off) "\
boost off - Deactivates boost mode"
set HELP(timer) "\
timers - Reads all timers and prints them human friendly\n\
timer-settings - Reads all timers and prints them ready for re-configuration\n\
timer <day> - Reads timer for given day\n\
timer <day> <base> <hh:mm> <temp> <hh:mm> ... - Sets timer for given day and up to 7 events with temperature and time\n\
\ day: mon, tue, wed, thu, fri, sat, sun, work, weekend, everyday, today, tomorrow\n\
\ base temperature before first and after last schedule: 5.0 to 29.5 in intervals of 0.5°C, e.g. 19.5 \n\
\ target temperature 5.0 to 29.5 in intervals of 0.5°C, e.g. 19.5 \n\
\ hh:mm: time where minutes must be in intervals of 10 minutes, e.g. 23:40"
set HELP(serial) "\
serial - Prints serial of thermostat (see little badge where batteries are)"
set HELP(lock) "\
lock - Locks thermostat (LOC)"
set HELP(unlock) "\
unlock - Unlocks thermostat"
set HELP(clear) "\
clear - Clear buffer of last request (will be printed in debug mode, set log_user to 1 in code!)"
set HELP(status) "\
status - Syncs time, Prints target temperature, mode and timers\n\
\ (in debug mode also last command even of official app, set log_user to 1 in code!)"
set HELP(json) "\
json - Same as status but in json format"
set HELP(reset) "\
reset - Factory reset"
set mac ""
set hci ""
set cmd ""
set timeout 10
set DELAY 100
set MAC_PATTERN "\[0-9A-F]{2}:\[0-9A-F]{2}:\[0-9A-F]{2}:\[0-9A-F]{2}:\[0-9A-F]{2}:\[0-9A-F]{2}"
set KNOWN_EQIVAS_FILE "~/.known_eqivas"
array set known_eqivas {}
set INIT(device) 1
set INIT(mode) 2
set INIT(timer) 4
set INIT(serial) 8
set HANDLES(name) "0321"
set HANDLES(vendor) "0311"
set HANDLES(request) "0411"
set HANDLES(notification) "0421"
set REQUESTS(setTime) "03"
set REQUESTS(setTimer) "10"
set REQUESTS(setComfortEco) "11"
set REQUESTS(setOffset) "13"
set REQUESTS(setWindowOpen) "14"
set REQUESTS(getTimer) "20"
set REQUESTS(setAutoMode) "4000"
set REQUESTS(setManualMode) "4040"
set REQUESTS(setVacationMode) "40"
set REQUESTS(startBoost) "45ff"
set REQUESTS(stopBoost) "4500"
set REQUESTS(setTemperature) "41"
set REQUESTS(setOn) "413c"
set REQUESTS(setOff) "4109"
set REQUESTS(setComfortTemperature) "43"
set REQUESTS(setEcoTemperature) "44"
set REQUESTS(lock) "8001"
set REQUESTS(unlock) "8000"
set REQUESTS(serial) "00"
set REQUESTS(reset) "f0"
set REQUESTS(clear) "00000000000000000000000000000000"
set MODE_BY_NAME(auto) 0
set MODE_BY_NAME(manual) 1
set MODE_BY_NAME(vacation) 2
set MODE_BY_NAME(boost) 4
set MODE_BY_NAME(dst) 8
set MODE_BY_NAME(window) 16
set MODE_BY_NAME(locked) 32
set MODE_BY_NAME(unknown) 64
set MODE_BY_NAME(battery) 128
set MODE(0) "auto"
set MODE(1) "manual"
set MODE(2) "vacation"
set MODE(4) "boost"
set MODE(8) "dst"
set MODE(16) "open window"
set MODE(32) "locked"
set MODE(64) "unknown"
set MODE(128) "low battery"
set thermostat(mac) ""
set thermostat(name) ""
set thermostat(alias) ""
set thermostat(vendor) ""
set thermostat(last_request) ""
set thermostat(serial) ""
set thermostat(firmware) ""
set thermostat(pin) ""
set thermostat(status) ""
set thermostat(mode) 0
set thermostat(valve) 0.0
set thermostat(temperature) 0.0
set thermostat(vacation_date) ""
set thermostat(vacation_time) ""
set thermostat(comfort_temperature) 0.0
set thermostat(eco_temperature) 0.0
set thermostat(window_temperature) 0.0
set thermostat(window_time) ""
set thermostat(offset_temperature) 0.0
set DAYS "Sat Sun Mon Tue Wed Thu Fri Weekend Work Everyday"
proc log {l} {
if {[log_user] != 0} {
puts $l
}
}
proc help {} {
global HELP
puts $HELP(about)
puts $HELP(usage)
puts "Sync:"
puts $HELP(sync)
puts "\nMode:"
puts $HELP(auto)
puts $HELP(manual)
puts "\nTemperature:"
puts $HELP(comfort)
puts $HELP(eco)
puts $HELP(boost)
puts $HELP(boost_off)
puts $HELP(temperature)
puts $HELP(on)
puts $HELP(off)
puts "\nTimers:"
puts $HELP(timer)
puts $HELP(vacation)
puts "\nConfiguration:"
puts $HELP(comforteco)
puts $HELP(window)
puts $HELP(offset)
puts "\nOthers:"
puts $HELP(lock)
puts $HELP(unlock)
puts $HELP(serial)
puts $HELP(status)
puts $HELP(json)
puts $HELP(clear)
puts $HELP(reset)
puts ""
}
proc readKnownEqivas {} {
global MAC_PATTERN
global KNOWN_EQIVAS_FILE
global known_eqivas
if {[info exists ::env(KNOWN_EQUIVAS)] == 1} {
set KNOWN_EQIVAS_FILE "$::env(KNOWN_EQUIVAS)"
}
if {[file exists "$KNOWN_EQIVAS_FILE"] == 0} {
return
}
set fp [open "$KNOWN_EQIVAS_FILE" r]
fconfigure $fp -buffering line
gets $fp data
while {$data != ""} {
regexp "($MAC_PATTERN) (\.+)" $data matched _mac _name
set known_eqivas($_mac) $_name
gets $fp data
}
close $fp
}
proc lookupKnownEqivaByName {pattern} {
global known_eqivas
global thermostat
foreach _mac [lsort [array names known_eqivas]] {
if { [string first "$pattern" $known_eqivas($_mac) ] != -1 } {
set thermostat(alias) $known_eqivas($_mac)
return $_mac
}
}
return ""
}
proc parseArgv {argv} {
global MAC_PATTERN
global KNOWN_EQIVAS_FILE
global mac
global hci
global cmd
if {[llength $argv] < 2} {
help
exit
}
if {[string match "hci?" [lindex $argv 0]]} {
set hci [lindex $argv 0]
set argv [lreplace $argv 0 0]
}
set id [lindex $argv 0]
set argv [lreplace $argv 0 0]
set is_mac [regexp -nocase "$MAC_PATTERN" $id]
if {$is_mac == 1 } {
set mac $id
} else {
readKnownEqivas
set mac [lookupKnownEqivaByName $id]
}
if { $mac == "" } {
puts "Device \"$id\" isn't known yet. Please check $KNOWN_EQIVAS_FILE file before using alias."
exit
}
set cmd [join $argv " "]
}
proc checkArgs {argv req command reg} {
global HELP
set check [regexp $reg [concat $argv]]
if {[llength $argv] <= $req || $check == 0} {
puts $HELP(usage)
puts $HELP($command)
puts ""
exit 1
}
}
proc connect {} {
global thermostat
global mac
expect "\[LE\]"
send "connect $mac\r"
expect "Connection successful" {
set thermostat(mac) $mac
return 0
}
return 1
}
proc disconnect {} {
global DELAY
send "disconnect\r"
after $DELAY
}
proc init {level} {
global INIT
set connected [connect]
if { $connected == 0} {
if {$level & $INIT(device)} {
readDeviceInfo
}
if {$level & $INIT(mode)} {
syncTime
}
if {$level & $INIT(timer)} {
readTimers
}
} else {
puts "Connection failed."
exit -1
}
return $connected
}
proc readHandle {handle} {
global DELAY
log "readHandle: char-read-hnd $handle"
send "char-read-hnd $handle\r"
expect {
"Characteristic value/descriptor: " {
log "ok"
}
}
expect -re "\[0-9a-f\ ]*" {
log "ok"
}
after $DELAY
set value $expect_out(buffer)
log "readHandle: $handle $value"
return $value
}
proc writeRequest {command value} {
global HANDLES
global REQUESTS
global DELAY
global thermostat
log "writeRequest: char-write-req $HANDLES(request) $command $value"
set thermostat(last_request) "$command $value"
send "char-write-req $HANDLES(request) $command$value\r"
expect "Characteristic value was written successfully" {
log "ok"
}
expect "Notification handle = 0x$HANDLES(notification) value: " {
log "ok"
}
expect -re "\[0-9a-f\ ]*" {
log "ok"
}
after $DELAY
set notification $expect_out(buffer)
log "notification: 0x$HANDLES(notification) $notification"
return $notification
}
proc writeHandle {handle value} {
global DELAY
log "writeHandle: char-write-req $handle $value"
send "char-write-req $handle $value\r"
expect "Characteristic value was written successfully" {
log "ok"
}
expect -re "\[0-9a-f\ ]*" {
log "ok"
}
after $DELAY
}
proc hexToDecimal {s} {
scan $s %x d
return $d
}
proc decimalToHex {d} {
scan $d %d d
set h [format %4.2x $d]
return [string trim $h]
}
proc hexToASCII {hex} {
set h [join $hex ""];
return [binary format H* $h]
}
proc asciiToHex {s} {
set s [string range $s 0 15]
set hex ""
foreach x [split $s ""] {
append hex [format %2.2X [scan $x %c]]
}
return $hex
}
proc hexToTemperature {hex} {
set value [hexToDecimal $hex]
return [expr $value / 2.0]
}
proc hexToDate {xyy xmm xdd} {
return "[format "%02d" [expr 2000 + [hexToDecimal $xyy]]]-[format "%02d" [hexToDecimal $xmm]]-[format "%02d" [hexToDecimal $xdd]]"
}
proc currentTime {} {
return [clock format [expr [clock seconds]] -format %H:%M:%S]
}
proc currentDate {} {
return [clock format [expr [clock seconds]] -format %y-%m-%d]
}
proc timeToHex {time} {
set part [split $time ":"]
scan [lindex $part 0] %d hours
scan [lindex $part 1] %d minutes
set hex "[decimalToHex $hours][decimalToHex $minutes]"
if {[llength $part] == 3} {
scan [lindex $part 2] %d seconds
append hex [decimalToHex $seconds]
}
return $hex
}
proc dateToHex {time} {
set part [split $time "-"]
scan [lindex $part 0] %d year
scan [lindex $part 1] %d month
scan [lindex $part 2] %d day
return "[decimalToHex $year][decimalToHex $month][decimalToHex $day]"
}
proc tempToHex {temp} {
return [decimalToHex [expr int($temp * 2)]]
}
proc minutesToTime {value multiplicator} {
set seconds [expr 60 * $multiplicator * $value]
set time ""
if {$seconds == 86400} {
set time "24:00"
} else {
set time [clock format [expr 60 * $multiplicator * $value] -format %H:%M -gmt true]
}
return $time
}
proc timeToMinutes {daytime devider} {
set part [split $daytime ":"]
scan [lindex $part 0] %d hours
scan [lindex $part 1] %d minutes
set enctime [expr ($hours * 60 + $minutes) / $devider]
return $enctime
}
proc getDayOrdinal {weekday} {
global DAYS
set todayOrdinal [clock format [clock seconds] -format %u]
if {$weekday == "today"} {
return [expr ($todayOrdinal + 1) % 7]
} elseif {$weekday == "tomorrow"} {
return [expr ($todayOrdinal + 2) % 7]
} elseif {$weekday == "weekend"} {
return 7
} elseif {$weekday == "work"} {
return 8
} elseif {$weekday == "everyday"} {
return 9
}
for {set day 0} {$day < 7} {incr day} {
if {[string tolower [lindex $DAYS $day]] == $weekday} {
break
}
}
return $day
}
proc getModeName {} {
global MODE
global MODE_BY_NAME
global thermostat
set name ""
if {$thermostat(temperature) == 30} {
set name "on"
return $name
} elseif {$thermostat(temperature) == 4.5} {
set name "off"
return $name
}
foreach key [lsort [array names MODE]] {
if {[expr $key == 0 && [hexToDecimal $thermostat(mode)] % 2 == 0] || [hexToDecimal $thermostat(mode)] & $key} {
append name $MODE($key)
append name " "
}
}
return [string trim $name]
}
proc getModeAsJson {} {
global MODE
global MODE_BY_NAME
global thermostat
set json "\{\n"
foreach key [lsort [array names MODE]] {
if {[expr $key == 0 && [hexToDecimal $thermostat(mode)] % 2 == 0] || [hexToDecimal $thermostat(mode)] & $key} {
set v "true"
} else {
set v "false"
}
append json " \"" $MODE($key) "\" : " $v ",\n"
}
append json " \"on\" : " [expr $thermostat(temperature) == 30 ? "true" : "false" ] ",\n"
append json " \"off\" : " [expr $thermostat(temperature) == 4.5 ? "true" : "false" ] "\n"
append json " \}"
return $json
}
proc readDeviceInfo {} {
global HANDLES
global thermostat
set thermostat(name) [hexToASCII [readHandle $HANDLES(name)]]
set thermostat(vendor) [hexToASCII [readHandle $HANDLES(vendor)]]
set thermostat(last_request) [readHandle $HANDLES(request)]
}
proc parseMode {raw_status} {
global MODE_BY_NAME
global thermostat
set thermostat(status) $raw_status
set thermostat(mode) [lindex $raw_status 2]
set thermostat(valve) [hexToDecimal [lindex $raw_status 3]]
set thermostat(temperature) [hexToTemperature [lindex $raw_status 5]]
if {[hexToDecimal $thermostat(mode)] & $MODE_BY_NAME(vacation)} {
set thermostat(vacation_date) [hexToDate [lindex $raw_status 7] [lindex $raw_status 9] [lindex $raw_status 6]]
set thermostat(vacation_time) [minutesToTime [hexToDecimal [lindex $raw_status 8]] 30]
} else {
set thermostat(vacation_date) ""
set thermostat(vacation_time) ""
}
if {[llength $raw_status] >= 15} {
set thermostat(window_temperature) [hexToTemperature [lindex $raw_status 10]]
set thermostat(window_time) [minutesToTime [hexToDecimal [lindex $raw_status 11]] 5]
set thermostat(comfort_temperature) [hexToTemperature [lindex $raw_status 12]]
set thermostat(eco_temperature) [hexToTemperature [lindex $raw_status 13]]
set thermostat(offset_temperature) [expr [hexToTemperature [lindex $raw_status 14]] - 3.5]
} else {
set thermostat(window_temperature) 0
set thermostat(window_time) ""
set thermostat(comfort_temperature) 0
set thermostat(eco_temperature) 0
set thermostat(offset_temperature) 0
}
}
proc readTimer {day} {
global REQUESTS
global thermostat
set raw_timer [writeRequest $REQUESTS(getTimer) [decimalToHex $day]]
set thermostat("timer_raw_$day") $raw_timer
set event 1
set start "00:00"
while {$event <= 7 && $start != "24:00"} {
set key "[lindex $day 0]_[lindex $event 0]"
set thermostat("timer_start_$key") $start
set end [minutesToTime [hexToDecimal [lindex $raw_timer [expr 2 * $event + 1 ]]] 10]
set thermostat("timer_end_$key") $end
set thermostat("timer_temperature_$key") [hexToTemperature [lindex $raw_timer [expr 2 * $event]]]
incr event
set start $end
}
}
proc readTimers {} {
global DAYS
for {set day 0} {$day < 9} {incr day} {
readTimer $day
}
}
proc printDevice {} {
global HANDLES
global thermostat
append out "Device mac:\t\t\t$thermostat(mac)\n"
if {[log_user] == 0} {
append out "Device name:\t\t\t$thermostat(name)\n"
append out "Device vendor:\t\t\t$thermostat(vendor)\n"
} else {
append out "Device name (0x$HANDLES(name)):\t\t$thermostat(name)\n"
append out "Device vendor (0x$HANDLES(vendor)):\t\t$thermostat(vendor)\n"
}
if {$thermostat(alias) != ""} {
append out "Alias:\t\t\t\t$thermostat(alias)"
}
append out "\n"
return $out
}
proc toJson {} {
global REQUESTS
global DAYS
global MODE_BY_NAME
global thermostat
set connected [connect]
if { $connected != 0} {
puts "\{ \"error\" : \"Connection failed.\" \}"
exit -1
}
readTimers
set currentTime [currentTime]
set currentDate [currentDate]
set raw_status [writeRequest $REQUESTS(setTime) "[dateToHex $currentDate][timeToHex $currentTime]"]
parseMode $raw_status
append out "\{\n"
append out " \"mac\" : \"$thermostat(mac)\",\n"
if {$thermostat(alias) != ""} {
append out " \"alias\" : \"$thermostat(alias)\",\n"
}
append out " \"temperature\" : $thermostat(temperature),\n"
append out " \"valve\" : $thermostat(valve),\n"
append out " \"mode\" : [getModeAsJson],\n"
if {[hexToDecimal $thermostat(mode)] & $MODE_BY_NAME(vacation)} {
append out " \"vacation\" : \"$thermostat(vacation_date) $thermostat(vacation_time)\",\n"
} else {
append out " \"vacation\" : null,\n"
}
if {[llength $thermostat(status)] >= 15} {
append out " \"comfort_temperature\" : $thermostat(comfort_temperature),\n"
append out " \"eco_temperature\" : $thermostat(eco_temperature),\n"
append out " \"open_window_temperature\" : $thermostat(window_temperature),\n"
append out " \"open_window_time\" : \"$thermostat(window_time)\",\n"
append out " \"offset_temperature\" : $thermostat(offset_temperature),\n"
}
append out " \"timers\" : \{\n"
for {set day 0} {$day < 9} {incr day} {
append out " \"[lindex $DAYS $day]\" : \[\n"
for {set event 1} {$event <= 7} {incr event} {
set key "[lindex $day 0]_[lindex $event 0]"
append out " \{\n"
append out " \"from\" : \"$thermostat("timer_start_$key")\",\n"
append out " \"to\" : \"$thermostat("timer_end_$key")\",\n"
append out " \"temperature\" : $thermostat("timer_temperature_$key")\n"
append out " \}"
if {"24:00" == "$thermostat("timer_end_$key")"} {
break
}
append out ",\n"
}
append out "\n ]"
if {$day < 8} {
append out ","
}
append out "\n"
}
append out " \}\n"
append out "\}"
puts $out
}
proc printStatus {} {
global HANDLES
global REQUESTS
global MODE_BY_NAME
global thermostat
if {[log_user] != 0} {
append out "Status (0x$HANDLES(request) $REQUESTS(setTime)):\t\t$thermostat(status)\n"
}
append out "Temperature:\t\t\t$thermostat(temperature)°C\n"
append out "Valve:\t\t\t\t$thermostat(valve)%\n"
append out "Mode:\t\t\t\t[getModeName]\n"
if {[hexToDecimal $thermostat(mode)] & $MODE_BY_NAME(vacation)} {
append out "Vacation mode:\t\t\ton\n"
append out "Vacation until:\t\t\t$thermostat(vacation_date) $thermostat(vacation_time)\n"
} else {
append out "Vacation mode:\t\t\toff\n"
}
if {[llength $thermostat(status)] >= 15} {
append out "\nComfort temperature:\t\t$thermostat(comfort_temperature)°C\n"
append out "Eco temperature:\t\t$thermostat(eco_temperature)°C\n"
append out "\nOpen window temperature:\t$thermostat(window_temperature)°C\n"
append out "Open window time:\t\t$thermostat(window_time)\n"
append out "\nOffset temperature:\t\t$thermostat(offset_temperature)°C\n"
}
if {[log_user] != 0} {
append out "\nLast request (0x$HANDLES(request)):\t\t$thermostat(last_request)\n"
}
return $out
}
proc printTimer {day} {
global DAYS
global HANDLES
global REQUESTS
global thermostat
append out "\n"
append out "Timer for [lindex $DAYS $day]"
if {[log_user] != 0} {
append out " (0x$HANDLES(request) $REQUESTS(getTimer)[decimalToHex $day]):\t$thermostat("timer_raw_$day")\n"
} else {
append out ":\n"
}
for {set event 1} {$event <= 7} {incr event} {
set key "[lindex $day 0]_[lindex $event 0]"
append out "\t$thermostat("timer_start_$key") - $thermostat("timer_end_$key"):\t$thermostat("timer_temperature_$key")°C\n"
if {"24:00" == "$thermostat("timer_end_$key")"} {
break
}
}
return $out
}
proc dumpTimer {day} {
global DAYS
global thermostat
set out "timer "
append out [string tolower [lindex $DAYS $day]]
append out " "
for {set event 1} {$event <= 7} {incr event} {
set key "[lindex $day 0]_[lindex $event 0]"
if {"00:00" == "$thermostat("timer_start_$key")"} {
append out "$thermostat("timer_temperature_$key") "
continue
}
append out "$thermostat("timer_start_$key") $thermostat("timer_temperature_$key") "
if {"24:00" == "$thermostat("timer_end_$key")"} {
append out "$thermostat("timer_end_$key")"
break
}
}
append out "\n"
return $out
}
proc dumpTimers {} {
set out ""
for {set day 0} {$day < 7} {incr day} {
append out [dumpTimer $day]
}
return $out
}
proc printTimers {} {
global DAYS
set todayOrdinal [clock format [clock seconds] -format %u]
set out ""
for {set day 0} {$day < 7} {incr day} {
append out [printTimer [expr ($day + $todayOrdinal + 1) % 7]]
}
return $out
}
proc dumpThermostat {} {
append out [printTimers]
append out "\n"
append out [printDevice]
puts $out
}
proc syncTime {} {
global REQUESTS
global timeout
set currentTime [currentTime]
set currentDate [currentDate]
set raw_status [writeRequest $REQUESTS(setTime) "[dateToHex $currentDate][timeToHex $currentTime]"]
if {$raw_status == ""} {
puts "ERROR: Thermostat hasn't responded after sync request in time ($timeout sec.)"
exit -1
}
parseMode $raw_status
puts "\n[printStatus]"
}
proc setVacationMode {date daytime temp} {
global REQUESTS
global timeout
set part [split $date "-"]
scan [lindex $part 0] %d year
scan [lindex $part 1] %d month
scan [lindex $part 2] %d day
set enctime [timeToMinutes $daytime 30]
set enctemp [expr ($temp * 2 + 128)]
set raw_status [writeRequest $REQUESTS(setVacationMode) "[decimalToHex $enctemp][decimalToHex $day][decimalToHex $year][decimalToHex $enctime][decimalToHex $month]"]
if {$raw_status == ""} {
puts "ERROR: Thermostat hasn't responded after vacation mode request in time ($timeout sec.)"
exit -1
}
puts "\nVacation mode successfully set"
parseMode $raw_status
puts "\n[printStatus]"
}
proc setAwayMode {hours temp} {
set target_time [expr (int([clock seconds] / 1800 + 1) * 1800 + $hours * 3600)]
set daytime [clock format $target_time -format %H:%M]
set date [clock format $target_time -format %y-%m-%d]
setVacationMode $date $daytime $temp
}
proc getTimer {weekday} {
global DAYS
set day [getDayOrdinal $weekday]
readTimer $day
puts [printTimer $day]
}
proc setTimer {params} {
global REQUESTS
global DAYS
global thermostat
set day [getDayOrdinal [lindex $params 0]]
set enctimer [decimalToHex $day]
set timer 1
set schedule ""
while {$timer < [llength $params] && $timer < 14} {
append enctimer [tempToHex [lindex $params $timer]]
set schedule [lindex $params [expr $timer + 1]]
append enctimer [decimalToHex [timeToMinutes $schedule 10]]
set timer [expr $timer + 2]
}
if {$schedule != "24:00"} {
append enctimer [tempToHex [lindex $params 1]]
append enctimer [decimalToHex [timeToMinutes "24:00" 10]]
}
append enctimer $REQUESTS(clear)
set enctimer [string range $enctimer [expr [llength $enctimer] - 1] 29]
writeRequest $REQUESTS(setTimer) $enctimer
puts "Timer set: $params"
}
proc setComfortEco {comfort eco} {
global REQUESTS
global timeout
set raw_status [writeRequest $REQUESTS(setComfortEco) "[tempToHex $comfort][tempToHex $eco]"]
if {$raw_status == ""} {
puts "ERROR: Thermostat hasn't responded to after comfort/eco request in time ($timeout sec.)"
exit -1
}
puts "\nComfort and eco temperature successfully set"
parseMode $raw_status
puts "\n[printStatus]"
}
proc setWindowMode {temp time} {
global REQUESTS
set part [split $time ":"]
scan [lindex $part 0] %d hours
scan [lindex $part 1] %d minutes
set period [expr ($minutes + $hours * 60) / 5]
if { $period > 12 } {
set period 12
}
set raw_status [writeRequest $REQUESTS(setWindowOpen) "[tempToHex $temp][decimalToHex $period]"]
puts "\nWindow open temperature and time successfully set"
parseMode $raw_status
puts "\n[printStatus]"
}
proc setOffset {temp} {
global REQUESTS
global timeout
set raw_status [writeRequest $REQUESTS(setOffset) "[decimalToHex [expr ($temp + 3.5) / 0.5]]"]
if {$raw_status == ""} {
puts "ERROR: Thermostat hasn't responded after offset request in time ($timeout sec.)"
exit -1
}
puts "\nOffset temperature successfully set"
parseMode $raw_status
puts "\n[printStatus]"
}
proc setMode {mode} {
global REQUESTS
global timeout
switch $mode {
"auto" {