-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathexample.py
167 lines (141 loc) · 5.26 KB
/
example.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
#!/usr/bin/env python
#
# Copyright (c) 2020 Poul-Henning Kamp <phk@phk.freebsd.dk>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
import time
import random
import todolist
import tcp_connection
import hdlc_connection
import dlms_connection
from blue_book_units import UNITS
from blue_book_electricity import electricity_names
import parameters
class Cosem():
def __init__(
self,
todo,
dlms,
cls,
obj,
freq=10,
):
self.todo = todo
self.dlms = dlms
self.cls = cls
self.obj = obj
self.freq = freq
self.units = None
self.next = None
self.scale = 1.0
self.dlms.get_cosem(self, 3, self.cb_units)
self.name = electricity_names(self.obj)
self.unit = None
self.fmt = None
def poll(self):
self.dlms.get_cosem(self, 2, self.cb_val)
def cb_units(self, val):
print("COSEM_UNITS", self, val)
self.units = val
self.unit = UNITS[val[1]]
self.next = time.time() + self.freq
self.todo.schedule(self.next, self.poll)
self.scale = 10 ** val[0]
self.fmt = "%" + ".%df" % (-min(0, val[0]))
def cb_val(self, val):
r = self.fmt % (val * self.scale)
print("COSEM_DATA", "%30s" % str(self), "%12.2f" % val, "%12s %-4s" % (r, self.unit), "%7.3f" % time.time(), self.name)
self.next += self.freq
self.todo.schedule(self.next, self.poll)
def __repr__(self):
return "<COSEM " + ".".join("%d" % x for x in self.obj) + ">"
def main(points):
def cb(*args, **kwargs):
print("CB", *args, **kwargs)
todo = todolist.ToDo()
dlms = dlms_connection.DlmsSession(todo)
for i,j in POINTS.items():
freq = j[0]
assert freq
if freq == 1:
freq = 3
cp = Cosem(todo, dlms, 3, i, freq=freq)
dlms.add_app(cp)
hdlc = hdlc_connection.HdlcConnection(
todo=todo,
app=dlms,
src_adr=parameters.LLC_SRC,
dst_adr=parameters.LLC_DST,
)
conn = tcp_connection.TcpConnection(
todo=todo,
app=hdlc,
host=parameters.HOST,
port=parameters.PORT,
)
RATE_FAST = 3
RATE_SLOW = 10
POINTS = {
(1, 0, 1, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 1, 8, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 2, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 2, 8, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 14, 7, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 15, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 15, 8, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 16, 8, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 21, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 21, 8, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 22, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 22, 8, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 23, 7, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 24, 7, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 31, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 32, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 41, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 41, 8, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 42, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 42, 8, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 43, 7, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 44, 7, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 51, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 52, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 61, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 61, 8, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 62, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 62, 8, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 63, 7, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 64, 7, 0, 255, ): ( RATE_SLOW, ),
(1, 0, 71, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 72, 7, 0, 255, ): ( RATE_FAST, ),
(1, 0, 81, 7, 10, 255, ): ( RATE_SLOW, ),
(1, 0, 81, 7, 20, 255, ): ( RATE_SLOW, ),
(1, 0, 81, 7, 21, 255, ): ( RATE_SLOW, ),
(1, 0, 81, 7, 40, 255, ): ( RATE_SLOW, ),
(1, 0, 81, 7, 51, 255, ): ( RATE_SLOW, ),
(1, 0, 81, 7, 62, 255, ): ( RATE_SLOW, ),
(1, 0, 91, 7, 0, 255, ): ( 1, ),
}
if __name__ == "__main__":
main(POINTS)