forked from avirad/npbc-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpbc_communication.py
165 lines (123 loc) · 5.4 KB
/
npbc_communication.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
#!/usr/bin/python
from datetime import datetime
class failResponse(object):
pass
class successResponse(object):
def __init__(self, data):
self.RawData = data
class generalInformationResponse(successResponse):
def __init__(self, data):
super(generalInformationResponse, self).__init__(data)
self.SwVer = "{0:X}".format(data[1])[:1] + "." + "{0:X}".format(data[1])[1:]
self.Date = datetime(2000 + int("{0:X}".format(data[7])), int("{0:X}".format(data[6])), int("{0:X}".format(data[5])),
int("{0:X}".format(data[2])), int("{0:X}".format(data[3])), int("{0:X}".format(data[4])))
self.Mode = data[8]
self.State = data[9]
self.Status = data[10]
self.IgnitionFail = (data[13] & (1 << 0)) != 0
self.PelletJam = (data[13] & (1 << 5)) != 0
self.Tset = data[16]
self.Tboiler = data[17]
self.Flame = data[20]
self.Heater = (data[21] & (1 << 1)) != 0
self.CHPump = (data[21] & (1 << 3)) != 0
self.BF = (data[21] & (1 << 4)) != 0
self.FF = (data[21] & (1 << 5)) != 0
self.Fan = data[23]
self.Power = data[24]
self.ThermostatStop = (data[25] & (1 << 7)) != 0
self.FFWorkTime = data[27]
class commandBase(object):
__header = [0x5A, 0x5A]
def __init__(self, commandId):
self.__commandId = commandId
self.IsSuccessful = False
def getRequestData(self, data):
request = bytearray()
# data length = 1 byte command Id + requestData.Length + 1 byte checksum
request.append(len(data) + 2)
# command Id
request.append(self.__commandId)
# request data
request.extend(data)
# checksum
request.append(self.__calculateCheckSum(request))
# increment request data values
for i in range(2, len(data) + 3, 1):
request[i] = request[i] + i - 1
# header
for i in range(len(self.__header) - 1, -1, -1):
request.insert(0, self.__header[i])
return request
def processResponseData(self, data):
self.IsSuccessful = False
response = bytearray()
if (len(data) < len(self.__header) + 2):
print "Invalid response"
return bytearray()
# check header
for i in range(len(self.__header)):
if (data[i] != self.__header[i]):
print "Invalid response header"
return bytearray()
# check length
if (len(data) != len(self.__header) + 1 + data[len(self.__header)]):
print "Invalid response length"
return bytearray()
for i in range(2, len(data) - 1, 1):
response.append(data[i])
# decrement response data values
for i in range(1, len(response), 1):
response[i] = response[i] - i + 1
# checksum validation
if (data[len(data) - 1] != ((self.__calculateCheckSum(response) + len(response) - 1) & 0xFF)):
print "Response checksum validation failed"
return bytearray()
del response[0:1]
self.IsSuccessful = True
return response
def __calculateCheckSum(self, data):
return (sum(data) & 0xFF) ^ 0xFF
class generalInformationCommand(commandBase):
def __init__(self):
super(generalInformationCommand, self).__init__(0x01)
def getRequestData(self):
return super(generalInformationCommand, self).getRequestData(bytearray())
def processResponseData(self, response):
try:
responseData = super(generalInformationCommand, self).processResponseData(response)
except:
self.IsSuccessful = False
if (self.IsSuccessful == True):
return generalInformationResponse(responseData)
else:
return failResponse()
class setBoilerTemperatureCommand(commandBase):
def __init__(self, boilerTemperature):
super(setBoilerTemperatureCommand, self).__init__(0x07)
self.__boilerTemperature = boilerTemperature
def getRequestData(self):
return super(setBoilerTemperatureCommand, self).getRequestData(bytearray(self.__boilerTemperature))
def processResponseData(self, response):
try:
responseData = super(setBoilerTemperatureCommand, self).processResponseData(response)
except:
self.IsSuccessful = False
if (self.IsSuccessful == True and len(responseData) == 1 and responseData[0] == 0x34):
return successResponse(responseData)
else:
return failResponse()
class resetFFWorkTimeCounterCommand(commandBase):
def __init__(self):
super(resetFFWorkTimeCounterCommand, self).__init__(0x09)
def getRequestData(self):
return super(resetFFWorkTimeCounterCommand, self).getRequestData(bytearray())
def processResponseData(self, response):
try:
responseData = super(resetFFWorkTimeCounterCommand, self).processResponseData(response)
except:
self.IsSuccessful = False
if (self.IsSuccessful == True and len(responseData) == 1 and responseData[0] == 0x34):
return successResponse(responseData)
else:
return failResponse()