forked from ekmmetering/ekmmeters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ekmmeters.py
3921 lines (3292 loc) · 158 KB
/
ekmmeters.py
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
""" ekmmeters.py
(c) 2015, 2016. 2017, 2018, 2019 EKM Metering.
The ekmmeters library API for v3 and v4 EKM Omnimeters.
This software is provided under an MIT license:
https://opensource.org/licenses/MIT
"""
import struct
import time
from collections import OrderedDict
from collections import namedtuple
from datetime import date
import sqlite3
import binascii
import serial
import traceback
import sys
import json
import datetime
import codecs
def hex2str(string):
return codecs.decode(codecs.decode(string,"hex"),"ascii")
def str2hex(string):
return codecs.decode(codecs.encode(string.encode(),"hex"),"ascii")
def ekm_no_log(output_string):
""" No-op predefined module level logging callback.
Args:
output_string (str): string to output.
"""
pass
def ekm_print_log(output_string):
""" Simple print predefined module level logging callback.
Args:
output_string (str): string to output.
Returns:
"""
print(output_string)
pass
global ekmmeters_log_func #: Module level log or diagnostic print
ekmmeters_log_func = ekm_no_log
global ekmmeters_log_level
ekmmeters_log_level = 3
global __EKMMETERS_VERSION
__EKMMETERS_VERSION = "0.2.6"
def ekm_set_log(function_name):
""" Set predefined or user-defined module level log output function.
Args:
function_name (function): function taking 1 string returning nothing.
"""
global ekmmeters_log_func
ekmmeters_log_func = function_name
pass
def ekm_log(logstr, priority=3):
""" Send string to module level log
Args:
logstr (str): string to print.
priority (int): priority, supports 3 (default) and 4 (special).
"""
if priority <= ekmmeters_log_level:
dt = datetime.datetime
stamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M.%f")
ekmmeters_log_func("[EKM Meter Debug Message: " + stamp + "] -> " + logstr)
pass
def ekm_set_log_level(level=3):
""" Set the logging level.
Args:
level (int): cutoff level (print at level and below).
"""
global ekmmeters_log_level
ekmmeters_log_level = level
pass
class MeterData():
""" Each :class:`~ekmmeters.SerialBlock` value is an array with these offsets. All Omnimeter versions.
=============== =
SizeValue 0
TypeValue 1
ScaleValue 2
StringValue 3
NativeValue 4
CalculatedFlag 5
EventFlag 6
=============== =
"""
def __init__(self):
pass
SizeValue = 0
TypeValue = 1
ScaleValue = 2
StringValue = 3
NativeValue = 4
CalculatedFlag = 5
EventFlag = 6
class MaxDemandResetInterval():
""" As passed in :func:`~ekmmeters.Meter.setMaxDemandResetInterval`. V4 Omnimeters.
======= =
Off 0
Monthly 1
Weekly 2
Daily 3
Hourly 4
======= =
"""
def __init__(self):
pass
Off = 0
Monthly = 1
Weekly = 2
Daily = 3
Hourly = 4
class MaxDemandPeriod():
"""As passed in :func:`~ekmmeters.Meter.setMaxDemandPeriod`. V3 and V4 Omnimeters.
============= =
At_15_Minutes 1
At_30_Minutes 2
At_60_Minutes 3
============= =
"""
def __init__(self):
pass
At_15_Minutes = 1
At_30_Minutes = 2
At_60_Minutes = 3
class LCDItems():
""" As passed in :func:`~ekmmeters.V4Meter.addLcdItem`. V4 Omnimeters.
=================== ==
kWh_Tot 1
Rev_kWh_Tot 2
RMS_Volts_Ln_1 3
RMS_Volts_Ln_2 4
RMS_Volts_Ln_3 5
Amps_Ln_1 6
Amps_Ln_2 7
Amps_Ln_3 8
RMS_Watts_Ln_1 9
RMS_Watts_Ln_2 10
RMS_Watts_Ln_3 11
RMS_Watts_Tot 12
Power_Factor_Ln_1 13
Power_Factor_Ln_2 14
Power_Factor_Ln_3 15
kWh_Tariff_1 16
kWh_Tariff_2 17
kWh_Tariff_3 18
kWh_Tariff_4 19
Rev_kWh_Tariff_1 20
Rev_kWh_Tariff_2 21
Rev_kWh_Tariff_3 22
Rev_kWh_Tariff_4 23
Reactive_Pwr_Ln_1 24
Reactive_Pwr_Ln_2 25
Reactive_Pwr_Ln_3 26
Reactive_Pwr_Tot 27
Line_Freq 28
Pulse_Cnt_1 29
Pulse_Cnt_2 30
Pulse_Cnt_3 31
kWh_Ln_1 32
Rev_kWh_Ln_1 33
kWh_Ln_2 34
Rev_kWh_Ln_2 35
kWh_Ln_3 36
Rev_kWh_Ln_3 37
Reactive_Energy_Tot 38
Max_Demand_Rst 39
Rev_kWh_Rst 40
State_Inputs 41
Max_Demand 42
Raw_Pulse_1 43
Raw_Pulse_2 44
Raw_Pulse_3 45
=================== ==
"""
def __init__(self):
pass
kWh_Tot = 1
Rev_kWh_Tot = 2
RMS_Volts_Ln_1 = 3
RMS_Volts_Ln_2 = 4
RMS_Volts_Ln_3 = 5
Amps_Ln_1 = 6
Amps_Ln_2 = 7
Amps_Ln_3 = 8
RMS_Watts_Ln_1 = 9
RMS_Watts_Ln_2 = 10
RMS_Watts_Ln_3 = 11
RMS_Watts_Tot = 12
Power_Factor_Ln_1 = 13
Power_Factor_Ln_2 = 14
Power_Factor_Ln_3 = 15
kWh_Tariff_1 = 16
kWh_Tariff_2 = 17
kWh_Tariff_3 = 18
kWh_Tariff_4 = 19
Rev_kWh_Tariff_1 = 20
Rev_kWh_Tariff_2 = 21
Rev_kWh_Tariff_3 = 22
Rev_kWh_Tariff_4 = 23
Reactive_Pwr_Ln_1 = 24
Reactive_Pwr_Ln_2 = 25
Reactive_Pwr_Ln_3 = 26
Reactive_Pwr_Tot = 27
Line_Freq = 28
Pulse_Cnt_1 = 29
Pulse_Cnt_2 = 30
Pulse_Cnt_3 = 31
kWh_Ln_1 = 32
Rev_kWh_Ln_1 = 33
kWh_Ln_2 = 34
Rev_kWh_Ln_2 = 35
kWh_Ln_3 = 36
Rev_kWh_Ln_3 = 37
Reactive_Energy_Tot = 38
Max_Demand_Rst = 39
Rev_kWh_Rst = 40
State_Inputs = 41
Max_Demand = 42
Raw_Pulse_1 = 43
Raw_Pulse_2 = 44
Raw_Pulse_3 = 45
class CTRatio():
""" As passed in :func:`~ekmmeters.Meter.setCTRatio`. V3 and V4 Omnimeters.
========= ====
Amps_100 100
Amps_200 200
Amps_400 400
Amps_600 600
Amps_800 800
Amps_1000 1000
Amps_1200 1200
Amps_1500 1500
Amps_2000 2000
Amps_3000 3000
Amps_4000 4000
Amps_5000 5000
========= ====
"""
def __init__(self):
pass
Amps_100 = 100
Amps_200 = 200
Amps_400 = 400
Amps_600 = 600
Amps_800 = 800
Amps_1000 = 1000
Amps_1200 = 1200
Amps_1500 = 1500
Amps_2000 = 2000
Amps_3000 = 3000
Amps_4000 = 4000
Amps_5000 = 5000
class Field():
""" Union of all V3A and V4AB Fields Returned.
Use these values to directy get read data with
Meter::getField() or in directy traversal of
:class:`~ekmmeters.SerialBlock`.
========================= =======================
Meter_Address 12 character Mfr ID'
Time_Stamp Epoch in ms at read
Model Meter model
Firmware Meter firmware
kWh_Tot Meter power total
kWh_Tariff_1 kWh for Tariff 1
kWh_Tariff_2 kWh for Tariff 2
kWh_Tariff_3 kWh for Tariff 3
kWh_Tariff_4 kWh for Tariff 4
Rev_kWh_Tot Meter rev. total
Rev_kWh_Tariff_1 Rev kWh for Tariff 1
Rev_kWh_Tariff_2 Rev kWh for Tariff 2
Rev_kWh_Tariff_3 Rev kWh for Tariff 3
Rev_kWh_Tariff_4 Rev kWh for Tariff 4
RMS_Volts_Ln_1 Volts line 1
RMS_Volts_Ln_2 Volts line 2
RMS_Volts_Ln_3 Volts line 3
Amps_Ln_1 Current line 1
Amps_Ln_2 Current line 2
Amps_Ln_3 Current line 3
RMS_Watts_Ln_1 Instantaneous watts line 1
RMS_Watts_Ln_2 Instantaneous watts line 2
RMS_Watts_Ln_3 Instantaneous watts line 3
RMS_Watts_Tot Instantaneous watts 1 + 2 + 3
Cos_Theta_Ln_1 Prefix in :class:`~ekmmeters.CosTheta`
Cos_Theta_Ln_2 Prefix in :class:`~ekmmeters.CosTheta`
Cos_Theta_Ln_3 Prefix in :class:`~ekmmeters.CosTheta`
Max_Demand Demand in period
Max_Demand_Period :class:`~ekmmeters.MaxDemandPeriod`
Meter_Time :func:`~ekmmeters.Meter.setTime` and :func:`~ekmmeters.Meter.splitEkmDate`
CT_Ratio :class:`~ekmmeters.Meter.setCTRatio`
Pulse_Cnt_1 Pulse Count Line 1
Pulse_Cnt_2 Pulse Count Line 2
Pulse_Cnt_3 Pulse Count Line 3
Pulse_Ratio_1 :func:`~ekmmeters.V4Meter.setPulseInputRatio`
Pulse_Ratio_2 :func:`~ekmmeters.V4Meter.setPulseInputRatio`
Pulse_Ratio_3 :func:`~ekmmeters.V4Meter.setPulseInputRatio`
State_Inputs :class:`~ekmmeters.StateIn`
Power_Factor_Ln_1 EKM Power Factor
Power_Factor_Ln_2 EKM Power Factor
Power_Factor_Ln_3 EKM Power Factor
Reactive_Energy_Tot Total VAR
kWh_Ln_1 Line 1 power
kWh_Ln_2 Line 2 power
kWh_Ln_3 Line 3 power
Rev_kWh_Ln_1 Line 1 reverse power
Rev_kWh_Ln_2 Line 2 reverse power
Rev_kWh_Ln_3 Line 3 revers power
Resettable_kWh_Tot :func:`~ekmmeters.V4Meter.setZeroResettableKWH`
Resettable_Rev_kWh_Tot :func:`~ekmmeters.V4Meter.setZeroResettableKWH`
Reactive_Pwr_Ln_1 VAR Line 1
Reactive_Pwr_Ln_2 VAR Line 2
Reactive_Pwr_Ln_3 VAR Line 3
Reactive_Pwr_Tot VAR Total
Line_Freq Freq. Hz.
State_Watts_Dir :class:`~ekmmeters.DirectionFlag`
State_Out :class:`~ekmmeters.StateOut`
kWh_Scale :class:`~ekmmeters.ScaleKWH`
RMS_Watts_Max_Demand Power peak in period
Pulse_Output_Ratio :class:`~ekmmeters.PulseOutput`
Max_Demand_Interval_Reset Forced reset interval period
Net_Calc_Watts_Ln_1 RMS_Watts with Direction
Net_Calc_Watts_Ln_2 RMS_Watts with Direction
Net_Calc_Watts_Ln_3 RMS_Watts with Direction
Net_Calc_Watts_Tot RMS_Watts with Direction
Status_A Reserved diagnostic.
Status_B Reserved diagnostic.
Status_C Reserved diagnostic.
========================= =======================
Power_Factor is the only power factor measurement supported by
upstring EKM products. The original Cos Theta value
is provided as an API-only feature.
"""
def __init__(self):
pass
Meter_Address = 'Meter_Address'
Time_Stamp = 'Time_Stamp'
Model = 'Model'
Firmware = 'Firmware'
kWh_Tot = 'kWh_Tot'
kWh_Tariff_1 = 'kWh_Tariff_1'
kWh_Tariff_2 = 'kWh_Tariff_2'
kWh_Tariff_3 = 'kWh_Tariff_3'
kWh_Tariff_4 = 'kWh_Tariff_4'
Rev_kWh_Tot = 'Rev_kWh_Tot'
Rev_kWh_Tariff_1 = 'Rev_kWh_Tariff_1'
Rev_kWh_Tariff_2 = 'Rev_kWh_Tariff_2'
Rev_kWh_Tariff_3 = 'Rev_kWh_Tariff_3'
Rev_kWh_Tariff_4 = 'Rev_kWh_Tariff_4'
RMS_Volts_Ln_1 = 'RMS_Volts_Ln_1'
RMS_Volts_Ln_2 = 'RMS_Volts_Ln_2'
RMS_Volts_Ln_3 = 'RMS_Volts_Ln_3'
Amps_Ln_1 = 'Amps_Ln_1'
Amps_Ln_2 = 'Amps_Ln_2'
Amps_Ln_3 = 'Amps_Ln_3'
RMS_Watts_Ln_1 = 'RMS_Watts_Ln_1'
RMS_Watts_Ln_2 = 'RMS_Watts_Ln_2'
RMS_Watts_Ln_3 = 'RMS_Watts_Ln_3'
RMS_Watts_Tot = 'RMS_Watts_Tot'
Cos_Theta_Ln_1 = 'Cos_Theta_Ln_1'
Cos_Theta_Ln_2 = 'Cos_Theta_Ln_2'
Cos_Theta_Ln_3 = 'Cos_Theta_Ln_3'
Max_Demand = 'Max_Demand'
Max_Demand_Period = 'Max_Demand_Period'
Meter_Time = 'Meter_Time'
CT_Ratio = 'CT_Ratio'
Pulse_Cnt_1 = 'Pulse_Cnt_1'
Pulse_Cnt_2 = 'Pulse_Cnt_2'
Pulse_Cnt_3 = 'Pulse_Cnt_3'
Pulse_Ratio_1 = 'Pulse_Ratio_1'
Pulse_Ratio_2 = 'Pulse_Ratio_2'
Pulse_Ratio_3 = 'Pulse_Ratio_3'
State_Inputs = 'State_Inputs'
Power_Factor_Ln_1 = 'Power_Factor_Ln_1'
Power_Factor_Ln_2 = 'Power_Factor_Ln_2'
Power_Factor_Ln_3 = 'Power_Factor_Ln_3'
Reactive_Energy_Tot = 'Reactive_Energy_Tot'
kWh_Ln_1 = 'kWh_Ln_1'
kWh_Ln_2 = 'kWh_Ln_2'
kWh_Ln_3 = 'kWh_Ln_3'
Rev_kWh_Ln_1 = 'Rev_kWh_Ln_1'
Rev_kWh_Ln_2 = 'Rev_kWh_Ln_2'
Rev_kWh_Ln_3 = 'Rev_kWh_Ln_3'
Resettable_kWh_Tot = 'Resettable_kWh_Tot'
Resettable_Rev_kWh_Tot = 'Resettable_Rev_kWh_Tot'
Reactive_Pwr_Ln_1 = 'Reactive_Pwr_Ln_1'
Reactive_Pwr_Ln_2 = 'Reactive_Pwr_Ln_2'
Reactive_Pwr_Ln_3 = 'Reactive_Pwr_Ln_3'
Reactive_Pwr_Tot = 'Reactive_Pwr_Tot'
Line_Freq = 'Line_Freq'
State_Watts_Dir = 'State_Watts_Dir'
State_Out = 'State_Out'
kWh_Scale = 'kWh_Scale'
RMS_Watts_Max_Demand = 'RMS_Watts_Max_Demand'
Max_Demand_Interval_Reset = 'Max_Demand_Interval_Reset'
Pulse_Output_Ratio = 'Pulse_Output_Ratio'
Net_Calc_Watts_Ln_1 = 'Net_Calc_Watts_Ln_1'
Net_Calc_Watts_Ln_2 = 'Net_Calc_Watts_Ln_2'
Net_Calc_Watts_Ln_3 = 'Net_Calc_Watts_Ln_3'
Net_Calc_Watts_Tot = 'Net_Calc_Watts_Tot'
Status_A = 'Status_A'
Status_B = 'Status_B'
Status_C = 'Status_C'
class Seasons():
""" As passed to :func:`~ekmmeters.Meter.assignSeasonSchedule`. V3 and V4 Omnimeters.
assign* methods use a zero based index for seasons.
You may set a season using one of these constants
or fill and iterate over range(Extents.Seaons).
======== =
Season_1 0
Season_2 1
Season_3 2
Season_4 3
======== =
"""
def __init__(self):
pass
Season_1 = 0
Season_2 = 1
Season_3 = 2
Season_4 = 3
class Months():
""" As passed to :func:`~ekmmeters.Meter.extractMonthTariff`. V3 and V4 Omnimeters.
======== =
Month_1 0
Month_2 1
Month_3 2
Month_4 3
Month_3 4
Month_4 5
======== =
"""
def __init__(self):
pass
Month_1 = 0
Month_2 = 1
Month_3 = 2
Month_4 = 3
Month_4 = 4
Month_5 = 5
class Tariffs():
""" As passed to :func:`~ekmmeters.Meter.assignSchedule`. V3 and V4 Omnimeters.
======== =
Tariff_1 0
Tariff_2 1
Tariff_3 2
Tariff_4 3
======== =
"""
def __init__(self):
pass
Tariff_1 = 0
Tariff_2 = 1
Tariff_3 = 2
Tariff_4 = 3
class Extents():
""" Traversal extents to use with for range(Extent) idiom. V3 and V4 Omnimeters.
Use of range(Extent.Entity) as an iterator insures safe
assignnment without off by one errors. Note that the meter serial api and
the actual supported month tariffs and schedules differ. Use of more than 4
month tariffs or six schedules results in onboard meter issues, so these are limited
in the extents.
========== ==
Seasons 4
Holidays 20
Periods 4
Schedules 6
Months 4
========== ==
"""
def __init__(self):
pass
Seasons = 4
Holidays = 20
Periods = 4
Tariffs = 4
Schedules = 6
Months = 6
class PulseOutput():
""" As passed to :func:`~ekmmeters.V4Meter.setPulseOutputRatio`. V4 Omnimeters.
========== ==========
Ratio_1 Ratio_40
Ratio_2 Ratio_50
Ratio_4 Ratio_80
Ratio_5 Ratio_100
Ratio_8 Ratio_200
Ratio_10 Ratio_400
Ratio_16 Ratio_800
Ratio_20 Ratio_1600
Ratio_25
========== ==========
"""
def __init__(self):
pass
Ratio_1 = 1
Ratio_2 = 2
Ratio_4 = 4
Ratio_5 = 5
Ratio_8 = 8
Ratio_10 = 10
Ratio_16 = 16
Ratio_20 = 20
Ratio_25 = 25
Ratio_40 = 40
Ratio_50 = 50
Ratio_80 = 80
Ratio_100 = 100
Ratio_200 = 200
Ratio_400 = 400
Ratio_800 = 800
Ratio_1600 = 1600
class Pulse():
""" As passed to :func:`~ekmmeters.V4Meter.setPulseInputRatio`. V4 Omnimeters.
Simple constant to clarify call.
=== =
In1 1
In2 2
In3 3
=== =
"""
def __init__(self):
pass
In1 = 1
In2 = 2
In3 = 3
class Schedules():
""" Allowed schedules. V3 and V4 Omnimeters.
Schedules on the meter are zero based, these apply to most passed
schedule parameters.
========== =
Schedule_1 0
Schedule_2 1
Schedule_3 2
Schedule_4 3
Schedule_5 4
Schedule_6 5
========== =
"""
def __init__(self):
pass
Schedule_1 = 0
Schedule_2 = 1
Schedule_3 = 2
Schedule_4 = 3
Schedule_5 = 4
Schedule_6 = 5
class ReadSchedules():
""" For :func:`~ekmmeters.Meter.readSchedules` and :func:`~ekmmeters.Meter.getSchedulesBuffer`. V3 and V4.
================ ==================================
Schedules_1_To_4 1st 4 blocks of tariff schedules
Schedules_5_To_6 2nd 4 blocks of tariff schedules
================ ==================================
"""
def __init__(self):
pass
Schedules_1_To_4 = 0
Schedules_5_To_6 = 1
class ReadMonths():
""" As passed to :func:`~ekmmeters.Meter.readMonthTariffs` and :func:`~ekmmeters.Meter.getMonthsBuffer`. V3 and V4.
Use to select the forward or reverse six month tariff data.
========== ================================
kWh Select forward month tariff data
kWhReverse Select reverse month tariff data
========== ================================
"""
def __init__(self):
pass
kWh = 1
kWhReverse = 2
class DirectionFlag():
""" On V4, State_Watts_Dir mask shows RMS_Watts direction on line 1-3.
The Direction flag is used to generate Calc_Net_Watts field on every
read. Each word in constant is the direction of the corresponding at
the moment of read. Ex ForwardReverseForward means RMS_Watts lines one
and three are positive, and line two is negtive.
===================== =
ForwardForwardForward 1
ForwardForwardReverse 2
ForwardReverseForward 3
ReverseForwardForward 4
ForwardReverseReverse 5
ReverseForwardReverse 6
ReverseReverseForward 7
ReverseReverseReverse 8
===================== =
"""
def __init__(self):
pass
ForwardForwardForward = 1
ForwardForwardReverse = 2
ForwardReverseForward = 3
ReverseForwardForward = 4
ForwardReverseReverse = 5
ReverseForwardReverse = 6
ReverseReverseForward = 7
ReverseReverseReverse = 8
class ScaleKWH():
""" Scaling or kWh values controlled by Fields.kWh. V4 Omnimeters.
If MeterData.ScaleValue is ScaleType.KWH, Fields.kWh_Scale one of these.
=========== == ===========
NoScale 0 no scaling
Scale10 1 scale 10^-1
Scale100 2 scale 10^-2
EmptyScale -1 Reserved
=========== == ===========
"""
def __init__(self):
pass
NoScale = 0
Scale10 = 1
Scale100 = 2
EmptyScale = -1
class ScaleType():
""" Scale type defined in SerialBlock. V4 Omnimeters.
These values are set when a field is defined a SerialBlock.
A Div10 or Div100 results in immediate scaling, otherwise
the scaling is perfformed per the value in Field.kWh_Scale
as described in ScaleKWH.
====== ==============================
KWH :class:`~ekmmeters.ScaleKWH`
No Do not scale
Div10 Scale 10^-1
Div100 Scale 10^-2
====== ==============================
"""
def __init__(self):
pass
KWH = "kwh"
No = "None"
Div10 = "10"
Div100 = "100"
class FieldType():
""" Every SerialBlock element has a field type. V3 and V4 Omnimeters.
Data arrives as ascii. Field type determines disposition.
The destination type is Python.
============ ==========================
NoType Not type assigned, invalid
Hex Implicit hex string
Int Implicit int
Float Implicit float
String Leave as string, terminate
PowerFactor EKM L or C prefixed pf
============ ==========================
"""
def __init__(self):
pass
NoType = "None" #: no type assigned
Hex = "hex" #: leave as hexified string
Int = "int" #: int in python
Float = "float" #: float in python
String = "string" #: string in python
PowerFactor = "pf" #: do power factor conversion
class Relay():
""" Relay specified in :func:`~ekmmeters.V4Meter.setRelay`. V4 Omnimeters.
====== ================
Relay1 OUT1 on V4 Meter
Relay2 OUT2 on V4 Meter
====== ================
"""
def __init__(self):
pass
Relay1 = 1 #: Relay 1 Selection code for v4 meter
Relay2 = 2 #: Relay 2 Selection code for v4 meter
class RelayState():
""" Relay state in :func:`~ekmmeters.V4Meter.setRelay`. V4 Omnimeters.
=========== =
RelayOpen 0
RelayClosed 1
=========== =
"""
def __init__(self):
pass
RelayOpen = 0 #: Relay Open command code for v4 meter
RelayClose = 1 #: Relay Close command code for v4 meter
class RelayInterval():
""" Relay interval in :func:`~ekmmeters.V4Meter.setRelay`. V4 Omnimeters.
===== ======================
Max 9999 seconds
Min 0, parameter limit
Hold 0 (lock relay state)
===== ======================
"""
def __init__(self):
pass
Max = 9999 #: Maximum wait
Min = 0 #: Lowest legal value
Hold = Min #: Hold is just zero
class StateOut():
""" Pulse output state at time of read. V4 Omnimeters.
======= =
OffOff 1
OffOn 2
OnOff 3
OnOn 4
======= =
"""
def __init__(self):
pass
OffOff = 1
OffOn = 2
OnOff = 3
OnOn = 4
class StateIn():
""" State of each pulse line at time of read. V4 Omnimeters.
================= =
HighHighHigh 0
HighHighLow 1
HighLowHigh 2
HighLowLow 3
LowHighHigh 4
LowHighLow 5
LowLowHigh 6
LowLowLow 7
================= =
"""
def __init__(self):
pass
HighHighHigh = 0
HighHighLow = 1
HighLowHigh = 2
HighLowLow = 3
LowHighHigh = 4
LowHighLow = 5
LowLowHigh = 6
LowLowLow = 7
class CosTheta():
""" Prefix characters returned in power factor. Note a cos of zero has one space. V3 and V4 Omnimeters.
"""
def __init__(self):
pass
InductiveLag = "L"
CapacitiveLead = "C"
NoLeadOrLag = (" ")
class SerialBlock(OrderedDict):
""" Simple subclass of collections.OrderedDict.
Key is a :class:`~ekmmeters.Field` and value is :class:`~ekmmeters.MeterData` indexed array.
The :class:`~ekmmeters.MeterData` points to one of the following:
============== ==============================================
SizeValue Integer. Equivalent to struct char[SizeValue]
TypeValue A :class:`~ekmmeters.FieldType` value.
ScaleValue A :class:`~ekmmeters.ScaleType` value.
StringValue Printable, scaled and formatted content.
NativeValue Converted, scaled value of field native type.
CalculatedFlag If True, not part of serial read, calculated.
EventFlag If True, state value
============== ==============================================
"""
def __init__(self):
super(SerialBlock, self).__init__()
class SerialPort(object):
""" Wrapper for serial port commands.
It should only be necessary to create one SerialPort per real port.
Object construction sets the class variables. The port is opened with
initPort(), and any serial exceptions will thrown at that point.
The standard serial settings for v3 and v4 EKM meters are 9600 baud,
7 bits, 1 stop bit, no parity. The baud rate may be reset but all timings
and test in this library are at 9600 baud. Bits, stop and parity may not
be changed.
"""
def __init__(self, ttyport, baudrate=9600, force_wait = 0.2):
"""
Args:
ttyport (str): port name, ex 'COM3' '/dev/ttyUSB0'
baudrate (int): optional, 9600 default and recommended
force_wait(float) : optional post commnd sleep, if required
"""
self.m_ttyport = ttyport
self.m_baudrate = baudrate
self.m_ser = None
self.m_fd = None
self.m_max_waits = 20
self.m_wait_sleep = 0.02
self.m_force_wait = force_wait
self.m_init_wait = 0.1
pass
def initPort(self):
""" Required initialization call, wraps pyserial constructor. """
try:
self.m_ser = serial.Serial(port=self.m_ttyport,
baudrate=self.m_baudrate,
timeout=0,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.SEVENBITS,
rtscts=False)
ekm_log("Pyserial version = " + serial.VERSION)
ekm_log("Port = " + self.m_ttyport)
ekm_log("Rate = " + str(self.m_baudrate))
time.sleep(self.m_init_wait)
return True
except:
ekm_log(traceback.format_exc(sys.exc_info()))
return False
def getName(self):
""" Getter for serial port name
Returns:
string: name of serial port (ex: 'COM3', '/dev/ttyS0')
"""
return self.m_ttyport
def closePort(self):
""" Passthrough for pyserial port close()."""
self.m_ser.close()
pass
def write(self, output):