-
Notifications
You must be signed in to change notification settings - Fork 15
/
sterownik.py
410 lines (348 loc) · 16.2 KB
/
sterownik.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Licencja GNU/GPL
# Uwaga ! Firma Elektro-System s.c. nie ponosi odpowiedzialności z tytułu ewentualnych szkód powstałych w wyniku działania biblioteki i/lub algorytmów powstałych przy jej użyciu.
__author__ = "Maciej Komur (elektro-system s.c.)"
__date__ = "01-10-2014"
__version__ = "0.2"
# modyfikacje: Bartłomiej Zimoń
import urllib
import httplib2
import struct
import sys
import threading
import time
from base64 import b64encode
#httplib2.debuglevel=4
class sterownik:
s_address = '127.0.0.';
s_user = 'admin';
s_password = 'admin';
s_statusdata = None;
s_typ_kotla = None
h = None;
version = None
last_res = None
last_content = None
testuj = False
ile_razy_testuj = 20
__temp=lambda self,hi,lo:((hi<<8|lo)-(hi>>7<<16))/10.0
def __init__(self, address, user, password):
"""Konstruktor. Podajemy adres sterownika (adres ip), login i hasło"""
self.s_address = address;
self.s_user = user;
self.s_password = password;
self.crcTable = [0, 49, 98, 83, 196, 245,166, 151,185, 136,219, 234,125, 76, 31, 46, 67, 114,33, 16,
135, 182,229, 212,250, 203,152, 169,62, 15, 92, 109,134, 183,228, 213,66, 115,32, 17,
63, 14, 93, 108,251, 202,153, 168,197, 244,167, 150,1, 48, 99, 82, 124, 77, 30, 47,
184, 137,218, 235,61, 12, 95, 110,249, 200,155, 170,132, 181,230, 215,64, 113,34, 19,
126, 79, 28, 45, 186, 139,216, 233,199, 246,165, 148,3, 50, 97, 80, 187, 138,217, 232,
127, 78, 29, 44, 2, 51, 96, 81, 198, 247,164, 149,248, 201,154, 171,60, 13, 94, 111,
65, 112,35, 18, 133, 180,231, 214,122, 75, 24, 41, 190, 143,220, 237,195, 242,161, 144,
7, 54, 101, 84, 57, 8, 91, 106,253, 204,159, 174,128, 177,226, 211,68, 117,38, 23,
252, 205,158, 175,56, 9, 90, 107,69, 116,39, 22, 129, 176,227, 210,191, 142,221, 236,
123, 74, 25, 40, 6, 55, 100, 85, 194, 243,160, 145,71, 118,37, 20, 131, 178,225, 208,
254, 207,156, 173,58, 11, 88, 105,4, 53, 102, 87, 192, 241,162, 147,189, 140,223, 238,
121, 72, 27, 42, 193, 240,163, 146,5, 52, 103, 86, 120, 73, 26, 43, 188, 141,222, 239,
130, 179,224, 209,70, 119,36, 21, 59, 10, 89, 104,255, 206,157, 172];
self.h = httplib2.Http()
self.h.add_credentials(self.s_user, self.s_password)
self.testuj = False;
self.lock = threading.RLock()
self.getVersion()
def setIleProb(self, potw):
self.testuj = potw
def getStatus(self):
"""Pobiera status ze sterownika i zapamiętuje go"""
try:
self._getRequest("02010006000000006103")
if (self.last_res.status == 200):
txt = str(self.last_content);
txt = txt[txt.index('[') + 1:txt.index(']')]
data = list(map(int, txt.split(',')));
self.s_statusdata = data;
return True;
else:
self.s_statusdata = None;
print ("Błąd: " + self.res.read());
return False
except:
self.s_statusdata = None;
print ("Błąd: ", sys.exc_info()[0])
return False
def _getRequest(self, req):
try:
with self.lock:
#time.sleep(0.001)
self.last_res, self.last_content = self.h.request("http://" + self.s_address + "/?com=" + req, "GET")
if (self.last_res.status == 200):
return True;
else:
print ("Błąd: " + self.last_content);
return False
except:
print ('Błąd: ', sys.exc_info()[0]);
return False
def getTrybAuto(self):
"""Zwraca True - jeżeli sterownik jest w trybie auto, False - jeżeli w trybie ręcznym"""
if (bool(self.s_statusdata)):
return (self.s_statusdata[34] == 1);
def setTrybAuto(self, state):
test = False
for x in range(self.ile_razy_testuj):
if (state):
test = self._getRequest("020100020033020001006503");
else:
test = self._getRequest("020100020033020000009103");
if (self.last_res.status == 200 ):
break
return test
#Temperatury
def getTempWew(self):
if (bool(self.s_statusdata)):
return self.__temp(self.s_statusdata[19],self.s_statusdata[18]);
def getTempZew(self):
if (bool(self.s_statusdata)):
return self.__temp(self.s_statusdata[21],self.s_statusdata[20]);
def getTempCWU(self):
if (bool(self.s_statusdata)):
return self.__temp(self.s_statusdata[23],self.s_statusdata[22]);
def getTempPowrot(self):
if (bool(self.s_statusdata)):
return self.__temp(self.s_statusdata[25],self.s_statusdata[24]);
def getTempPodajnik(self):
if (bool(self.s_statusdata)):
return self.__temp(self.s_statusdata[27],self.s_statusdata[26]);
def getTempCO(self):
if (bool(self.s_statusdata)):
return self.__temp(self.s_statusdata[29],self.s_statusdata[28]);
def getTempSpaliny(self):
if (bool(self.s_statusdata)):
return self.__temp(self.s_statusdata[31],self.s_statusdata[30]);
# Pompa CO
def getPompaCO(self):
if (bool(self.s_statusdata)):
return self.s_statusdata[32] & (1 << 2) != 0;
def setPompaCO(self, state):
test = False
for x in range(self.ile_razy_testuj):
if (state):
test = self._getRequest("02010005000D0100018D03");
else:
test = self._getRequest("02010005000D010000BC03");
if (self.last_res.status == 200 ):
break
return test
#Pompa CWU
def getPompaCWU(self):
if (bool(self.s_statusdata)):
return self.s_statusdata[32] & (1 << 3) != 0;
def setPompaCWU(self, state):
test = False
for x in range(self.ile_razy_testuj):
if (state):
test = self._getRequest("02010005000E0100011103");
else:
test = self._getRequest("02010005000E0100002003");
if (self.last_res.status == 200 ):
break
return test
#Pompa Obiegowa
def getPompaMieszObieg(self):
if (bool(self.s_statusdata)):
return self.s_statusdata[32] & (1 << 4) != 0;
# Podajnik
def getPodajnik(self):
if (bool(self.s_statusdata)):
return self.s_statusdata[32] & (1 << 1) != 0;
def getCzasPodajnika(self):
if (bool(self.s_statusdata)):
return (self.s_statusdata[65] << 8 | self.s_statusdata[64]) / 60.0;
def setPodajnik(self, state):
test = False
for x in range(self.ile_razy_testuj):
if (state):
test = self._getRequest("02010005000C0100011603");
else:
test = self._getRequest("02010005000C0100002703");
if (self.last_res.status == 200 ):
break
return test
def WlaczPodajnik_Async(self, sek):
threading.Thread(target=self.WlaczPodajnikNaXSekThread, args=[sek]).start()
def WlaczPodajnikNaXSekThread(self, sek):
self.setPodajnik(True);
time.sleep(sek);
self.setPodajnik(False);
# Dmuchawa
def getDmuchawa(self):
if (bool(self.s_statusdata)):
return self.s_statusdata[32] & (1 << 0) != 0;
def setDmuchawa(self, state):
test = False
for x in range(self.ile_razy_testuj):
if (state):
test = self._getRequest("02010005000B0100018403");
else:
test = self._getRequest("02010005000B010000B503");
if (self.last_res.status == 200 ):
break
return test
def getDmuchawaMoc(self):
if (bool(self.s_statusdata)):
return self.s_statusdata[39];
def setDmuchawaMoc(self, value):
v = int(value)
tab = [0x01, 0x00, 0x02, 0x00, 0x08, 0x02, 0x00, v & 0xff, 0x00];
crc = self.crc(tab);
tab.insert(0, 0x02);
tab.append(crc);
tab.append(0x03);
cmd = ''.join('{:02x}'.format(x) for x in tab);
test = False
for x in range(self.ile_razy_testuj):
test = self._getRequest(cmd);
if (self.last_res.status == 200 ):
break
return test
def getVersion(self):
test = False
for x in range(self.ile_razy_testuj):
test = self._getRequest("0201000500020000A903");
if (self.last_res.status == 200 ):
break
if (self.last_res.status == 200 ):
txt = str(self.last_content);
txt = txt[txt.index('[') + 1:txt.index(']')]
data = list(map(int, txt.split(',')));
if (data[8:11] == [48, 46, 49]):
self.version = "BRULI"
elif (data[8:11] == [48, 46, 51]):
self.version = "ECOAL"
else:
self.version = None
return self.version
def getTypKotla(self):
test = False
for x in range(self.ile_razy_testuj):
if (self.version == "BRULI"):
test = self._getRequest("02010001005000006C03");
elif (self.version == "ECOAL"):
test = self._getRequest("0201000100770000F603");
else:
return None
if (self.last_res.status == 200 ):
break
if (self.last_res.status == 200 ):
txt = str(self.last_content);
txt = txt[txt.index('[') + 1:txt.index(']')]
data = list(map(int, txt.split(',')));
if (self.version == "BRULI"):
if (data[8] == 0):
self.s_typ_kotla = "RETORTOWY-RECZNY"
elif (data[8] == 1):
self.s_typ_kotla = "RETORTOWY-GRUPOWY"
elif (data[8] == 2):
self.s_typ_kotla = "TLOKOWY-RECZNY"
elif (data[8] == 3):
self.s_typ_kotla = "TLOKOWY-AUTO"
elif (data[8] == 4):
self.s_typ_kotla = "ZASYPOWY"
else:
self.s_typ_kotla = None
elif (self.version == "ECOAL"):
if (data[8] == 0):
self.s_typ_kotla = "RETORTOWY-RECZNY"
elif (data[8] == 1):
self.s_typ_kotla = "RETORTOWY-GRUPOWY"
elif (data[8] == 2):
self.s_typ_kotla = "RETORTOWY-ECOAL"
elif (data[8] == 3):
self.s_typ_kotla = "ZASYPOWY"
else:
self.s_typ_kotla = None
else:
self.s_typ_kotla = None
return self.s_typ_kotla
def setRetRecznyPodawanie(self, value):
v = int(value)
tab = []
if (self.version == "BRULI"):
tab = [0x01, 0x00, 0x02, 0x00, 0x22, 0x02, 0x00, v & 0xff, (v>>8) & 0xff]
elif (self.version == "ECOAL"):
tab = [0x01, 0x00, 0x02, 0x00, 0x52, 0x02, 0x00, v & 0xff, (v>>8) & 0xff]
else:
return None
crc = self.crc(tab);
tab.insert(0, 0x02);
tab.append(crc);
tab.append(0x03);
cmd = ''.join('{:02x}'.format(x) for x in tab);
test = False
for x in range(self.ile_razy_testuj):
test = self._getRequest(cmd);
if (self.last_res.status == 200 ):
break
return test
def setRetRecznyPostoj(self, value):
v = int(value)
tab = []
if (self.version == "BRULI"):
tab = [0x01, 0x00, 0x02, 0x00, 0x23, 0x02, 0x00, v & 0xff, (v>>8) & 0xff]
elif (self.version == "ECOAL"):
tab = [0x01, 0x00, 0x02, 0x00, 0x76, 0x02, 0x00, v & 0xff, (v>>8) & 0xff]
else:
return None
crc = self.crc(tab);
tab.insert(0, 0x02);
tab.append(crc);
tab.append(0x03);
cmd = ''.join('{:02x}'.format(x) for x in tab);
test = False
for x in range(self.ile_razy_testuj):
test = self._getRequest(cmd);
if (self.last_res.status == 200 ):
break
return test
def setRetRecznyDmuchawa(self, value):
v = int(value)
tab = []
if (self.version == "BRULI"):
tab = [0x01, 0x00, 0x02, 0x00, 0x0F, 0x02, 0x00, v & 0xff, 0x00]
elif (self.version == "ECOAL"):
tab = [0x01, 0x00, 0x02, 0x00, 0x61, 0x02, 0x00, v & 0xff, 0x00]
else:
return None
crc = self.crc(tab);
tab.insert(0, 0x02);
tab.append(crc);
tab.append(0x03);
cmd = ''.join('{:02x}'.format(x) for x in tab);
test = False
for x in range(self.ile_razy_testuj):
test = self._getRequest(cmd);
if (self.last_res.status == 200 ):
break
return test
def setZadanaCO(self, value):
v = int(value)
tab = [0x01, 0x00, 0x02, 0x00, 0x28, 0x02, 0x00, v & 0xff, 0x00];
crc = self.crc(tab);
tab.insert(0, 0x02);
tab.append(crc);
tab.append(0x03);
cmd = ''.join('{:02x}'.format(x) for x in tab);
test = False
for x in range(self.ile_razy_testuj):
test = self._getRequest(cmd);
if (self.last_res.status == 200 ):
break
return test
def crc(self, msg):
runningCRC = 0
for c in msg:
runningCRC = self.crcByte(runningCRC, c)
return runningCRC
def crcByte(self, oldCrc, byte):
res = self.crcTable[oldCrc & 0xFF ^ byte & 0xFF];
return res