-
Notifications
You must be signed in to change notification settings - Fork 22
/
sem-6000.exp
executable file
·3609 lines (2249 loc) · 74 KB
/
sem-6000.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 Voltcraft SEM 6000
#
# Version: 2022-01-12
#
# MIT License
#
# Copyright (c) 2019-2021 heckie75
#
# 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, kWHETHER 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 verbose 0
set DELAY 100
set MTU 160
set timeout 3
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 WEEKDAYS "SMTWTFS"
set KNOWN_SEMS_FILE "~/.known_sem6"
set HELP(usage) "Usage: <mac/alias> <pin> --<command1> <parameters...> --<command2>\n\
\ <mac>: bluetooth mac address of smart energy meter\n\
\ <alias>: you can use alias instead of mac address, see ~/.known_sem6 \n\
\ <pin>: pin of smart energy meter if not stored in alias file\n\
\ <command>: For command and parameters"
set HELP(sync) "\
--sync - synchronize time"
set HELP(on) "\
--on - turn on smart energy meter"
set HELP(off) "\
--off - turn off smart energy meter"
set HELP(toggle) "\
--toggle - toggle smart energy meter"
set HELP(status) "\
--status - get status incl. voltage, ampere, watts, power factor and frequency"
set HELP(scheduler) "\
--scheduler <on|off> <hh:mm|+mm> \[<smtwtfs>\] \[<YYYY-MM-DD>]\n\
\ on|off - action of new scheduler\n\
\ hh:mm|+mm - start time or minutes from now\n\
\ smtwtfs - (optional) weekdays, use capital letters to set, e.g. sMTWTFs\n\
\ use the word \"sameday\" for same weekdays like today\n\
\ if weekdays are missing then scheduler runs only once\n\
\ YYYY-MM-DD - (optional) Date from when it should run\n\
\n\
--scheduler <n> <set|unset|reset> - activate, deactivate or reset given scheduler\n\
\n\
--scheduler reset - resets all schedulers, i.e. 1 - 12\n\
\n\
--scheduler - query status of all schedulers"
set HELP(countdown) "\
--countdown <on|off> <YYYY-MM-DD hh:mm:ss|hh:mm|+mm>\n\
\ - set countdown action and runtime\n\
\ on|off - action of countdown\n\
\ YYYY-MM-DD hh:mm:ss - given ETA\n\
\ hh:mm - given ETA within next 24h\n\
\ +mm - given runtime in minutes\n\
\n\
--countdown <reset> - reset countdown\n\
\n\
--countdown - query status of countdown"
set HELP(randommode) "\
--randommode <hh:mm> <hh:mm> <smtwtfs>\n\
\ - set randommode\n\
\ hh:mm - Starting time\n\
\ hh:mm - Ending time\n\
\ smtwtfs - weekdays, use capital letters to set, e.g. sMTWTFs\n\
\n\
--randommode off - turn randommode off\n\
\n\
--randommode - query status of randommode"
set HELP(measure) "\
--measure header - print header line for measurements\n\
--measure \[<s>\] - take measurements, optional duration in seconds (use 0 for single), otherwise forever"
set HELP(data) "\
--data <day|month|year> - request measured power consumption from enery meter\n\
--data reset - reset all data of measured power consumption\n\
--data header - print header line for measurements or data"
set HELP(standby) "\
--standby <watts> - turn off device if consumption is less than given watts\n\
\ watts - low-water-mark, max. 3680, e.g. 2.654 for 2.654W"
set HELP(settings) "\
--settings - request settings from device, i.e. overload, led, price"
set HELP(led) "\
--led <on|off> - turn led ring on / off"
set HELP(overload) "\
--overload <watts>\n\
\ - set overload with high-water-mark and actions\n\
\ watts - high-water-mark, max. 3680, e.g. 3680 for 3680W"
set HELP(price) "\
--price <normal price> \[<reduced price> <hh:mm> <hh:mm>]\n\
\ - set price and optionally a reduced price with start and end time\n\
\ Note: You can either use reduced price mode or statistics but not both"
set HELP(statistics) "\
--statistics reset - reset all captured data and set starting time to now\n\
\ Note: You can either use reduced price mode or statistics but not both\n\
--statistics set <mm-dd hh:mm> - set starting time for statistics explicitly\n\
--statistics - capture and calculate statistics, i.e. average and projections consumption"
set HELP(name) "\
--name <name> - give smart meter a new name, max. 18 characters"
set HELP(pin) "\
--pin <1234|reset> - change PIN or reset to default PIN, i.e. 0000"
set HELP(device) "\
--device - read device meta information, i.e. name and serial no."
set HELP(reset) "\
--reset - factory reset"
set HELP(dump) "\
--dump - request all information from device"
set HELP(print) "\
--print - print gathered information"
set HELP(json) "\
--json - print some gathered information in JSON format"
set HELP(sleep) "\
--sleep <n> - pause processing for n seconds"
set HELP(verbose) "\
--verbose - print information about processing"
set HELP(debug) "\
--debug - print actions in gatttool"
set HELP(help) "\
--help \[<command>\] - print general help or help for specific command"
set INIT(connect) 1
set INIT(auth) 2
set INIT(sync) 4
set INIT(settings) 8
set INIT(measurement) 16
set INIT(device) 32
set INIT(countdown) 64
set INIT(scheduler) 128
set INIT(randommode) 256
set INIT(statistics) 512
set hndfile ""
set BASIC_HANDLES 3
set CHARACTERISTIC(2a00) "devname"
set CHARACTERISTIC(fff1) "device"
set CHARACTERISTIC(fff3) "command"
set CHARACTERISTIC(fff4) "notification"
array set HANDLES {}
set CMD(SCHEDULER_ADD) 0
set CMD(SCHEDULER_EDIT) 1
set CMD(SCHEDULER_DEL) 2
set CMD(RESET_ALL) 0
set CMD(RESET_DATA) 2
set CMD(DATA_DAY) 10
set CMD(DATA_MONTH) 11
set CMD(DATA_YEAR) 12
array set aliases {}
set sem(header_measure) ""
set sem(header_data) ""
set sem(init) 0
set sem(mac) ""
set sem(pin) ""
set sem(name) ""
set sem(serial) ""
set sem(vendor) ""
set sem(firmware) ""
set sem(hardware) ""
set sem(mtu) 0
set sem(alias) ""
set sem(power) -1
set sem(watt) -1
set sem(voltage) -1
set sem(ampere) -1
set sem(frequency) -1
set sem(power_factor) -1
set sem(total) -1
set sem(total_day) -1
set sem(total_month) -1
set sem(total_year) -1
set sem(led) -1
set sem(over_power) -1
set sem(normal_price) -1
set sem(reduced_mode) -1
set sem(reduced_start) 0
set sem(reduced_end) 0
set sem(reduced_price) -1
set sem(countdown) -1
set sem(schedulers) -1
set sem(randommode) -1
set sem(statistics_start) ""
set sem(statistics_runtime) -1
set sem(avg_consumption) -1
set sem(projected_consumption) -1
set sem(projected_price) -1
array set data_per_hour {}
array set data_per_day {}
array set data_per_month {}
# prints help output
proc help { argv } {
global HELP
# print help for given command
if { [llength $argv] == 2 && [lindex $argv 0] == "--help" } {
set command [lindex $argv 1]
puts stderr $HELP(usage)
if { [ info exists HELP($command) ] == 1 } {
puts stderr "\n$HELP($command)\n"
} else {
puts stderr "\nUnknown command <$command>.\n"
}
exit 1
} elseif { [llength $argv ] < 1 || [ lindex $argv 0 ] == "--help" } {
puts stderr "$HELP(usage)"
puts stderr "\nBasic commands:\n"
puts stderr "$HELP(on)"
puts stderr "$HELP(off)"
puts stderr "$HELP(toggle)"
puts stderr "$HELP(standby)\n"
puts stderr "\nScheduler commands:\n"
puts stderr "$HELP(scheduler)"
puts stderr "\nCountdown commands:\n"
puts stderr "$HELP(countdown)"
puts stderr "\nMeasurement commands:\n"
puts stderr "$HELP(status)"
puts stderr "$HELP(measure)\n"
puts stderr "$HELP(data)"
puts stderr "\nDevice commands:\n"
puts stderr "$HELP(device)"
puts stderr "$HELP(sync)"
puts stderr "$HELP(name)"
puts stderr "$HELP(pin)"
puts stderr "\nDevice settings:\n"
puts stderr "$HELP(settings)"
puts stderr "$HELP(led)"
puts stderr "$HELP(overload)\n"
puts stderr "$HELP(price)"
puts stderr "$HELP(reset)"
puts stderr "\nStatistics:\n"
puts stderr "$HELP(statistics)"
puts stderr "\nOther commands:\n"
puts stderr "$HELP(print)"
puts stderr "$HELP(json)"
puts stderr "$HELP(dump)"
puts stderr "$HELP(sleep)"
puts stderr "$HELP(verbose)"
puts stderr "$HELP(debug)"
puts stderr "$HELP(help)"
puts stderr ""
exit 1
}
}
proc print_sem {} {
global sem
global data_per_hour
global data_per_day
global data_per_month
set out ""
append out [ print_device ]
append out [ print_settings ]
append out [ print_schedulers ]
append out [ print_countdown ]
append out [ print_randommode ]
append out [ print_measurement ]
append out [ print_statistics ]
append out $sem(header_data)
append out [ print_data [ array get data_per_hour ] ]
append out [ print_data [ array get data_per_day ] ]
append out [ print_data [ array get data_per_month ] ]
puts -nonewline $out
}
proc print_device {} {
global INIT
global sem
set out ""
if { ( $sem(init) & $INIT(device) ) == $INIT(device) } {
append out "Device:\n"
append out " Mac: $sem(mac)\n"
append out " Device PIN: $sem(pin)\n"
append out " Name: $sem(name)\n"
append out " Vendor: $sem(vendor)\n"
append out " Serial: $sem(serial)\n"
append out " Firmware: $sem(firmware)\n"
append out " Hardware: $sem(hardware)\n"
append out " MTU: $sem(mtu)\n"
append out " Alias: $sem(alias)\n"
append out "\n"
}
return $out
}
proc calc_power_factor { power voltage ampere watt } {
if { $power == 1 && $voltage > 0 && $ampere > 0 } {
return [ format_decimal [ expr $watt / $voltage / $ampere ] ]
} else {
return 1
}
}
proc print_measurement {} {
global INIT
global sem
if { ( $sem(init) & $INIT(measurement) ) != $INIT(measurement) } {
return ""
}
append out "Status:\n"
append out " Power: [ expr $sem(power) == 1 ? "on" : "off" ]\n"
append out " Voltage: $sem(voltage) VAC\n"
append out " Ampere: $sem(ampere) A\n"
append out " Watts: $sem(watt) W\n"
append out " Frequency: $sem(frequency) Hz\n"
append out " Power factor: $sem(power_factor)\n"
# append out " Total consumption: $sem(total) kWh\n"
if { $sem(total_day) != -1} {
append out " Total last 24h: [expr $sem(total_day) / 1000.0 ] kWh\n"
}
if { $sem(total_month) != -1} {
append out " Total last 31 days: [expr $sem(total_month) / 1000.0 ] kWh\n"
}
if { $sem(total_year) != -1} {
append out " Total last 12 months: [expr $sem(total_year) / 1000.0 ] kWh\n"
}
append out "\n"
return $out
}
proc print_data { _data } {
array set data $_data
set out ""
foreach d [ lsort [ array names data ] ] {
append out "$d\t$data($d)\n"
}
return $out
}
proc print_statistics {} {
global INIT
global sem
if { ( $sem(init) & $INIT(statistics) ) != $INIT(statistics) } {
return ""
}
append out "Statistics:\n"
append out " Log statistics since: $sem(statistics_start)\n"
append out " Runtime: [seconds_to_time_w_days $sem(statistics_runtime)]\n"
append out " Current consumption: [expr $sem(total_year) / 1000.0 ] kWh\n"
append out " Avg. consumption: $sem(avg_consumption) Wh\n"
append out " Projected consumption: [expr $sem(projected_consumption) / 1000.0 ] kWh per year\n"
append out " Projected price: $sem(projected_price) $ per year\n\n"
}
proc print_measurement_as_csv {} {
global sem
append out "[ current_datetime_str ]\t"
append out "$sem(power)\t$sem(voltage)\t$sem(ampere)\t"
append out "$sem(watt)\t$sem(frequency)\t$sem(power_factor)"
return $out
}
proc print_settings {} {
global INIT
global sem
set out ""
if { ( $sem(init) & $INIT(settings) ) == $INIT(settings) } {
append out "Settings:\n"
append out " LED ring: [ expr $sem(led) == 1 ? "on" : "off" ]\n"
append out " Overload limit: $sem(over_power) W\n"
append out "\n"
append out " Price: [ format_decimal [ expr $sem(normal_price) / 100.0 ] ] $ per kWh\n"
append out "\n"
append out " Reduced mode: [ expr $sem(reduced_mode) == 1 ? "on" : "off" ]\n"
if { $sem(reduced_mode) == 1 } {
append out " Reduced price: [ format_decimal [ expr $sem(reduced_price) / 100.0 ] ]\n"
append out " Reduced start: [ minutes_to_time $sem(reduced_start) ]\n"
append out " Reduced end: [ minutes_to_time $sem(reduced_end) ]\n"
}
append out "\n"
}
return $out
}
proc print_countdown {} {
global sem
set out ""
if { $sem(countdown) > 0 } {
set runtime [ clock format [ expr $sem(countdown_schedule) - $sem(countdown_origin) ] -format "%H:%M:%S" -timezone :GMT ]
set runtime "[ expr ( $sem(countdown_schedule) - $sem(countdown_origin) ) / 86400 ] days, $runtime"
set remain [ clock format [ expr $sem(countdown_schedule) - [ clock seconds ] + [ time_offset ] ] -format "%H:%M:%S" -timezone :GMT ]
set remain "[ expr ( $sem(countdown_schedule) - [ clock seconds ] + [ time_offset ] ) / 86400 ] days, $remain"
append out "Countdown: on\n"
append out " Action: [ expr $sem(countdown) == 1 ? "on" : "off" ]\n"
append out " Started at: [ datetime_str $sem(countdown_origin) ]\n"
append out " ETA: [ datetime_str $sem(countdown_schedule) ]\n"
append out " Runtime: $runtime\n"
append out " Remaining: $remain\n"
append out "\n"
} elseif { $sem(countdown) == 0 } {
append out "Countdown: off\n"
}
return $out
}
proc print_schedulers {} {
global sem
set out ""
if {$sem(schedulers) >= 0 } {
append out "Schedulers: $sem(schedulers)\n\n"
}
for { set i 0 } { $i < $sem(schedulers) } { incr i } {
append out "Scheduler [ expr $i + 1 ]:\n"
append out " Slot: $sem("scheduler_slot_$i")\n"
append out " Active: [ expr $sem("scheduler_active_$i") == 1 ? "yes" : "no" ]\n"
append out " Action: turn [ expr $sem("scheduler_action_$i") == 1 ? "on" : "off" ]\n"
append out " Date: $sem("scheduler_date_$i")\n"
append out " Time: $sem("scheduler_time_$i")\n"
append out " Weekdays: $sem("scheduler_daymask_$i")\n"
append out "\n"
}
return $out
}
proc print_randommode {} {
global sem
set out ""
if { $sem(randommode) > 0 } {
append out "Randommode: on\n"
append out " Weekdays: $sem(randommode_daymask)\n"
append out " Start time: $sem(randommode_start)\n"
append out " End time: $sem(randommode_end)\n"
append out "\n"
} elseif { $sem(randommode) == 0 } {
append out "Randommode: off\n"
}
return $out
}
proc json_sem {} {
global sem
global data_per_hour
global data_per_day
global data_per_month
set out "{\n"
append out "[ json_device ],\n"
append out "[ json_settings ],\n"
append out "[ json_schedulers ],\n"
append out "[ json_countdown ],\n"
append out "[ json_randommode ],\n"
append out "[ json_measurement ],\n"
append out "[ json_data "data_per_hour" [ array get data_per_hour ] ],\n"
append out "[ json_data "data_per_day" [ array get data_per_day ] ],\n"
append out "[ json_data "data_per_month" [ array get data_per_month ] ],\n"
append out "[ json_statistics ]\n"
append out "}"
puts $out
}
proc json_device {} {
global INIT
global sem
set out ""
if { ( $sem(init) & $INIT(device) ) == $INIT(device) } {
append out " \"device\" : \{\n"
append out " \"mac\" : \"$sem(mac)\",\n"
append out " \"pin\" : \"$sem(pin)\",\n"
append out " \"name\" : \"$sem(name)\",\n"
append out " \"vendor\" : \"$sem(vendor)\",\n"
append out " \"serial\" : \"$sem(serial)\",\n"
append out " \"firmware\" : \"$sem(firmware)\",\n"
append out " \"hardware\" : \"$sem(hardware)\",\n"
append out " \"mtu\" : $sem(mtu),\n"
append out " \"alias\" : \"$sem(alias)\"\n"
append out " \}"
} else {
append out " \"device\" : null"
}
return $out
}
proc json_measurement {} {
global INIT
global sem
if { ( $sem(init) & $INIT(measurement) ) != $INIT(measurement) } {
return " \"status\" : null"
}
append out " \"status\" : \{\n"
append out " \"power\" : $sem(power),\n"
append out " \"voltage\" : $sem(voltage),\n"
append out " \"ampere\" : $sem(ampere),\n"
append out " \"watts\" : $sem(watt),\n"
append out " \"frequency\" : $sem(frequency),\n"
append out " \"power_factor\" : $sem(power_factor),\n"
if { $sem(total_day) != -1} {
append out " \"total_day\" : $sem(total_day),\n"
}
if { $sem(total_month) != -1} {
append out " \"total_month\" : $sem(total_month),\n"
}
if { $sem(total_year) != -1} {
append out " \"total_year\" : $sem(total_year),\n"
}
# append out " \"total\" : $sem(total)\n"
append out " \"total\" : null\n"
append out " \}"
return $out
}
proc json_settings {} {
global sem
global INIT
if { ( $sem(init) & $INIT(settings) ) == $INIT(settings) } {
append out " \"settings\" : \{\n"
append out " \"led\" : $sem(led),\n"
append out " \"overload\" : $sem(over_power),\n"
append out " \"price\" : [ format_decimal [ expr $sem(normal_price) / 100.0 ] ],\n"
if { $sem(reduced_mode) == 1 } {
append out " \"reduced\" : \{\n"
append out " \"price\" : [ format_decimal [ expr $sem(reduced_price) / 100.0 ] ],\n"
append out " \"start\" : \"[ minutes_to_time $sem(reduced_start) ]\",\n"
append out " \"end\" : \"[ minutes_to_time $sem(reduced_end) ]\"\n"
append out " \}\n"
} else {
append out " \"reduced\" : null\n"
}
append out " \}"
} else {
return " \"settings\" : null"
}
}
proc json_schedulers {} {
global sem
if { $sem(schedulers) < 0 } {
return " \"schedulers\" : null"
}
append out " \"schedulers\" : \["
for { set i 0 } { $i < $sem(schedulers) } { incr i } {
append out " \{\n"
append out " \"id\" : [ expr $i + 1 ],\n"
append out " \"slot\" : $sem("scheduler_slot_$i"),\n"
append out " \"active\" : $sem("scheduler_active_$i"),\n"
append out " \"action\" : $sem("scheduler_action_$i"),\n"
append out " \"date\" : \"$sem("scheduler_date_$i")\",\n"
append out " \"time\" : \"$sem("scheduler_time_$i")\",\n"
append out " \"days\" : \"$sem("scheduler_daymask_$i")\"\n"
if { $i < [ expr $sem(schedulers) - 1 ] } {
append out " \},\n"
} else {
append out " \}\n"
}
}
append out " ]"
return $out
}
proc json_countdown {} {
global sem
if { $sem(countdown) < 0 } {
return " \"countdown\" : null"
} elseif { $sem(countdown) > 0 } {
set runtime [ expr ( $sem(countdown_schedule) - $sem(countdown_origin) ) ]
set remain [ expr ( $sem(countdown_schedule) - [ clock seconds ] + [ time_offset ] ) ]
append out " \"countdown\" : \{\n"
append out " \"action\" : $sem(countdown),\n"
append out " \"started\" : \"[ datetime_str $sem(countdown_origin) ]\",\n"
append out " \"eta\" : \"[ datetime_str $sem(countdown_schedule) ]\",\n"
append out " \"runtime\" : $runtime,\n"
append out " \"remaining\" : $remain\n"
append out " \}"
} else {
append out " \"countdown\" : \{\}"
}
return $out
}
proc json_randommode {} {
global sem
if { $sem(randommode) > 0 } {
append out " \"randommode\" : \{\n"
append out " \"weekdays\" :\"$sem(randommode_daymask)\",\n"
append out " \"start\" : \"$sem(randommode_start)\",\n"
append out " \"end\" : \"$sem(randommode_end)\"\n"
append out " \}"
} elseif { $sem(randommode) == 0 } {
append out " \"randommode\" : \{\}"
} else {
append out " \"randommode\" : null"
}
return $out
}
proc json_data { _obj _data } {
array set data $_data
set out " \"$_obj\" : \[\n"
set size [ array size data ]
set i 0
foreach d [ lsort [ array names data ] ] {
incr i
append out " \{\n"
append out " \"period\" : \"$d\",\n"
append out " \"consumption\" : $data($d)\n"
if { $i < $size } {
append out " \},\n"
} else {
append out " \}\n"
}
}
append out " \]"
return $out
}
proc json_statistics {} {
global sem
global INIT
if { ( $sem(init) & $INIT(statistics) ) == $INIT(statistics) } {
append out " \"statistics\" : \{\n"
append out " \"start\" : \"$sem(statistics_start)\",\n"
append out " \"runtime\" : $sem(statistics_runtime),\n"
append out " \"total\" : $sem(total_year),\n"
append out " \"avg\" : $sem(avg_consumption),\n"
append out " \"projected_consumption\" : $sem(projected_consumption),\n"
append out " \"projected_price\" : $sem(projected_price)\n"
append out " \}"
} else {
return " \"statistics\" : null"
}
}
proc get_total_from_data { _data } {
array set data $_data
set total 0
foreach d [ array names data ] {
set total [ expr $total + $data($d) ]
}
return $total
}
proc read_aliases {} {
global MAC_PATTERN
global KNOWN_SEMS_FILE
global aliases
if { [ file exists "$KNOWN_SEMS_FILE" ] == 0 } {
return
}
set fp [ open "$KNOWN_SEMS_FILE" r ]
fconfigure $fp -buffering line
gets $fp data
while { $data != "" } {
regexp "($MAC_PATTERN) (\[0-9]{4}) (\.+)" $data matched _mac _pin _alias
set aliases($_mac) "$_pin $_alias"
gets $fp data
}
close $fp
}
proc lookup_alias { pattern } {
global aliases
global sem
set mac ""
foreach _mac [ lsort [ array names aliases ] ] {
if { [ string first "$pattern" $aliases($_mac) ] != -1 } {
regexp "(\[0-9]{4}) (\.+)" $aliases($_mac) matched _pin _alias
set mac $_mac
set sem(pin) $_pin
set sem(alias) $_alias
}
}
return $mac
}
proc get_mac { id } {
global MAC_PATTERN
global hndfile
set mac ""
set is_mac [ regexp "$MAC_PATTERN" $id ]
if { $is_mac == 1 } {
set mac $id
} else {
read_aliases
set mac [ lookup_alias $id ]
}
append hndfile "/tmp/sem6-" $mac ".hnd"
return $mac
}
proc hex_to_decimal { s } {