-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
371 lines (324 loc) · 12.3 KB
/
__init__.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
"""Subscription manager for Tibber Han Solo."""
import asyncio
import base64
import logging
import struct
from datetime import datetime
from time import time
import aiohttp
import crcmod
_LOGGER = logging.getLogger(__name__)
STATE_STARTING = "starting"
STATE_RUNNING = "running"
STATE_STOPPED = "stopped"
PORT = 9876
FEND = 126 # 7e
class SubscriptionManager:
"""Subscription manager."""
# pylint: disable=too-many-instance-attributes
def __init__(self, loop, session, hostname):
"""Create resources for websocket communication."""
self.loop = loop
self._url = "ws://{}:{}".format(hostname, PORT)
self._session = session
self.subscriptions = []
self._state = None
self.websocket = None
self._retry_timer = None
self._client_task = None
self._wait_time_before_retry = 15
self._show_connection_error = True
self._is_running = False
self._crc = crcmod.mkCrcFun(0x11021, rev=True, initCrc=0xffff, xorOut=0x0000)
self._decoders = [decode_kaifa, decode_kamstrup, decode_aidon]
self._default_decoder = None
self._last_data_time = None
def start(self):
"""Start websocket."""
_LOGGER.debug("Start state %s.", self._state)
if self._state == STATE_RUNNING:
return
self._state = STATE_STARTING
self._cancel_client_task()
self._client_task = self.loop.create_task(self.running())
@property
def is_running(self):
"""Return if client is running or not."""
return self._is_running
async def running(self):
"""Start websocket connection."""
# pylint: disable=too-many-branches, too-many-statements
await self._close_websocket()
try:
_LOGGER.debug("Starting")
self.websocket = await self._session.ws_connect(self._url)
self._state = STATE_RUNNING
_LOGGER.debug("Running")
self._last_data_time = time()
while self._state == STATE_RUNNING:
try:
msg = await asyncio.wait_for(self.websocket.receive(), timeout=30)
except asyncio.TimeoutError:
if (time() - self._last_data_time) > 2 * 60:
if self._show_connection_error:
_LOGGER.error("No data, reconnecting.")
self._show_connection_error = False
self._is_running = False
return
_LOGGER.debug(
"No websocket data in 30 seconds, checking the connection."
)
try:
pong_waiter = self.websocket.ping()
await asyncio.wait_for(pong_waiter, timeout=10)
except asyncio.TimeoutError:
if self._show_connection_error:
_LOGGER.error(
"No response to ping in 10 seconds, reconnecting."
)
self._show_connection_error = False
self._is_running = False
return
continue
if msg.type in [aiohttp.WSMsgType.closed, aiohttp.WSMsgType.error]:
return
self._last_data_time = time()
self._is_running = True
await self._process_msg(msg)
self._show_connection_error = True
except Exception: # pylint: disable=broad-except
_LOGGER.error("Unexpected error", exc_info=True)
finally:
await self._close_websocket()
if self._state != STATE_STOPPED:
_LOGGER.debug("Reconnecting")
self._state = STATE_STOPPED
self.retry()
_LOGGER.debug("Closing running task.")
async def stop(self, timeout=10):
"""Close websocket connection."""
_LOGGER.debug("Stopping client.")
start_time = time()
self._cancel_retry_timer()
self._state = STATE_STOPPED
while (
timeout > 0
and self.websocket is not None
and not self.websocket.closed
and (time() - start_time) < timeout
):
await asyncio.sleep(0.1, loop=self.loop)
await self._close_websocket()
self._cancel_client_task()
_LOGGER.debug("Server connection is stopped")
def retry(self):
"""Retry to connect to websocket."""
_LOGGER.debug("Retry, state: %s", self._state)
if self._state in [STATE_STARTING, STATE_RUNNING]:
_LOGGER.debug("Skip retry since state: %s", self._state)
return
_LOGGER.debug("Cancel retry timer")
self._cancel_retry_timer()
self._state = STATE_STARTING
_LOGGER.debug("Restart")
self._retry_timer = self.loop.call_later(
self._wait_time_before_retry, self.start
)
_LOGGER.debug(
"Reconnecting to server in %i seconds.", self._wait_time_before_retry
)
async def subscribe(self, callback):
"""Add a new subscription."""
if callback in self.subscriptions:
return
self.subscriptions.append(callback)
async def unsubscribe(self, callback):
"""Unsubscribe."""
if callback not in self.subscriptions:
return
self.subscriptions.remove(callback)
async def _close_websocket(self):
if self.websocket is None:
return
try:
await self.websocket.close()
finally:
self.websocket = None
async def _process_msg(self, msg):
"""Process received msg."""
if msg.type != aiohttp.WSMsgType.BINARY:
return
decoded_data = self.decode(msg)
if decoded_data is None:
_LOGGER.warning("Failed to decode data %s", msg)
return
for callback in self.subscriptions:
await callback(decoded_data)
def decode(self, msg, check_time=True):
"""Decode received msg."""
data = msg.data
if data is None:
return None
if (len(data) < 9
or not data[0] == FEND
or not data[-1] == FEND):
_LOGGER.warning("Invalid data %s", data)
return None
data = data[1:-1]
crc = self._crc(data[:-2])
crc ^= 0xffff
if crc != struct.unpack("<H", data[-2:])[0]:
_LOGGER.warning("Invalid crc %s %s, %s", crc, struct.unpack("<H", data[-2:])[0],
''.join('{:02x}'.format(x).upper() for x in data))
return None
buf = ''.join('{:02x}'.format(x).upper() for x in data)
if self._default_decoder is not None:
decoded_data = self._default_decoder(buf, log=True)
if decoded_data is not None and (not check_time or valid_time(decoded_data.get('time_stamp'))):
return decoded_data
for decoder in self._decoders:
decoded_data = decoder(buf)
if decoded_data is not None and (not check_time or valid_time(decoded_data.get('time_stamp'))):
self._default_decoder = decoder
return decoded_data
_LOGGER.error("Unknown data %s", data)
return None
def _cancel_retry_timer(self):
if self._retry_timer is None:
return
try:
self._retry_timer.cancel()
finally:
self._retry_timer = None
def _cancel_client_task(self):
if self._client_task is None:
return
try:
self._client_task.cancel()
finally:
self._client_task = None
def valid_time(time_stamp):
"""Validate time stamp."""
if time_stamp is None:
return True
if not isinstance(time_stamp, datetime):
return False
return abs((time_stamp - datetime.now()).total_seconds()) < 3600 * 2
def decode_kaifa(buf, log=False):
"""Decode kaifa."""
# pylint: disable=too-many-instance-attributes, too-many-branches
if buf[10:12] != '10':
if log:
_LOGGER.error("Unknown control field %s", buf[10:12])
return None
try:
if int(buf[1:4], 16) * 2 != len(buf):
if log:
_LOGGER.error("Invalid length %s, %s", int(buf[1:4], 16) * 2, len(buf))
return None
buf = buf[32:]
txt_buf = buf[28:]
if txt_buf[:2] != '02':
if log:
_LOGGER.error("Unknown data %s", buf[0])
return None
year = int(buf[4:8], 16)
month = int(buf[8:10], 16)
day = int(buf[10:12], 16)
hour = int(buf[14:16], 16)
minute = int(buf[16:18], 16)
second = int(buf[18:20], 16)
date = "%02d%02d%02d_%02d%02d%02d" % (second, minute, hour, day, month, year)
res = {}
res['time_stamp'] = datetime.strptime(date, '%S%M%H_%d%m%Y')
pkt_type = txt_buf[2:4]
txt_buf = txt_buf[4:]
if pkt_type == '01':
res['Effect'] = int(txt_buf[2:10], 16)
elif pkt_type in ['09', '0D', '12', '0E']:
res['Version identifier'] = base64.b16decode(txt_buf[4:18]).decode("utf-8")
txt_buf = txt_buf[18:]
res['Meter-ID'] = base64.b16decode(txt_buf[4:36]).decode("utf-8")
txt_buf = txt_buf[36:]
res['Meter type'] = base64.b16decode(txt_buf[4:20]).decode("utf-8")
txt_buf = txt_buf[20:]
res['Effect'] = int(txt_buf[2:10], 16)
if pkt_type in ['12', '0E']:
txt_buf = txt_buf[10:]
txt_buf = txt_buf[78:]
res['Cumulative_hourly_active_import_energy'] = int(txt_buf[2:10], 16)
txt_buf = txt_buf[10:]
res['Cumulative_hourly_active_export_energy'] = int(txt_buf[2:10], 16)
txt_buf = txt_buf[10:]
res['Cumulative_hourly_reactive_import_energy'] = int(txt_buf[2:10], 16)
txt_buf = txt_buf[10:]
res['Cumulative_hourly_reactive_export_energy'] = int(txt_buf[2:10], 16)
else:
if log:
_LOGGER.warning("Unknown type %s", pkt_type)
return None
except ValueError:
if log:
_LOGGER.error("Failed", exc_info=True)
return None
return res
def decode_aidon(buf, log=False):
"""Decode Aidon."""
if buf[10:12] != '13':
if log:
_LOGGER.error("Unknown control field %s", buf[10:12])
return None
try:
if int(buf[1:4], 16) * 2 != len(buf):
if log:
_LOGGER.error("Invalid length %s, %s", int(buf[1:4], 16) * 2, len(buf))
return None
res = {}
res['time_stamp'] = None
pkt_type = buf[36:38]
if pkt_type == '01':
res['Effect'] = int(buf[60:68], 16)
elif pkt_type in ['09', '0C', '0D', '0E', '11', '12']:
res['Effect'] = int(buf[194:202], 16)
else:
if log:
_LOGGER.warning("Unknown type %s", pkt_type)
return None
except ValueError:
if log:
_LOGGER.error("Failed", exc_info=True)
return None
return res
def decode_kamstrup(buf, log=False):
"""Decode Kamstrup."""
if buf[8:10] != '13':
if log:
_LOGGER.error("Unknown control field %s", buf[10:12])
return None
if len(buf) < 176:
if log:
_LOGGER.error("Data length %s", len(buf))
return None
buf = buf[32:]
txt_buf = buf[26:]
pkt_type = txt_buf[0:2]
if pkt_type not in ['0F', '11', '1B', '17', '21', '19', '23']:
if log:
_LOGGER.warning("Unknown type %s", pkt_type)
return None
try:
year = int(buf[0:4], 16)
month = int(buf[4:6], 16)
day = int(buf[6:8], 16)
hour = int(buf[10:12], 16)
minute = int(buf[12:14], 16)
second = int(buf[14:16], 16)
date = "%02d%02d%02d_%02d%02d%02d" % (second, minute, hour, day, month, year)
res = {}
res['time_stamp'] = datetime.strptime(date, '%S%M%H_%d%m%Y')
res['Effect'] = int(txt_buf[160:168], 16)
except ValueError:
if log:
_LOGGER.error("Failed", exc_info=True)
return None
return res