33 from p2dbstore
import *
68 USER_PLUMBER =
"52610300007B"
71 USER_NORMAL =
"526103000001"
74 USER_NORMAL_ALT =
"5261030000F3"
77 USER_SERVICE =
"52610300FFF9"
102 res = P2Furn.USER_NORMAL
104 if user ==
'service':
105 res = P2Furn.USER_SERVICE
106 elif user ==
'normal_alt':
107 res = P2Furn.USER_NORMAL_ALT
108 elif user ==
'plumber':
109 res = P2Furn.USER_PLUMBER
127 return self.com.read()
136 self.com.sendMsg(msg)
147 def runAuth(self, user = USER_SERVICE, maxRetry = 3, retryWait = 10):
149 logger.info(
"Entering authentication stage")
150 logger.debug(
"Trying to auth with '"+user+
"'")
161 logger.debug(
"Sending user Id, waiting for reply.")
164 msg = self.com.read()
166 logger.debug(
"Answer received : "+msg.getStr())
174 logger.info(
"Entering initialisation stage")
177 Waiting for the initialisation to end
182 firstHeader = [0x4D,0x41]
184 outMsg.prepare(firstHeader, outData)
190 Sending message with same header as reply while reply's header doesn't
193 while self.
curStage == P2Furn.STAGE_INIT:
194 logger.debug(
"Init message exchange #"+str(counter))
195 self.com.sendMsg(outMsg)
198 inMsg = self.com.read()
199 except P2ComError
as e:
200 if e.getErrno() == 9:
203 if e.getData() ==
None:
204 logger.error(
"Message failure : no message")
206 logger.error(
"Message failure : "+e.getData().getStr())
216 recvHeader = e.getData().getHeader(P2Msg.FMT_LIST)
218 logger.debug(
"Received headers : "+str(recvHeader))
221 recvHeader = inMsg.getHeader(P2Msg.FMT_LIST)
225 if recvHeader[0] == 0x4D
and (recvHeader[1] & 0xF0) == 0x30:
226 self.
curStage = P2Furn.STAGE_POST_INIT
228 outMsg.prepare(recvHeader)
230 logger.info(
"Initialisation successfully terminated with "+str(counter)+
" exchange between computer and furnace")
233 First initialisaion stage end
236 outMsg.prepare([0x4D,0x32],[0x01])
238 ser = self.com.getCom()
239 for i
in range(nbMaxTs):
240 self.com.sendMsg(outMsg)
242 if ser.inWaiting() > 0:
243 inMsg = self.com.read()
244 logger.info(
"2nd init message received")
245 logger.debug(
"Message : "+inMsg.getStr())
247 logger.debug(
"No message...")
249 logger.debug(str(nbMaxTs-i)+
" message left before 2nd init end.")
252 logger.info(
"Sending rb request")
253 outMsg.prepare([0x52,0x62],[0x00,0x00,0x01])
255 self.com.sendMsg(outMsg)
257 inMsg = self.com.read()
258 logger.info(
"Rb acknowledge received")
259 except P2ComError
as e:
260 logger.error(
"Timeout waiting rb aknowledge")
263 logger.info(
"Second Init stage success.")
266 logger.info(
"Waiting 3s before sending the first M2 request")
268 logger.debug(
"Sending the first M2 request")
269 outMsg.prepare([0x4D,0x32],[0x01])
275 self.com.sendMsg(outMsg)
277 inMsg = self.com.read()
278 logger.debug(
"M2 Acknowledge received")
280 except P2ComError
as e:
282 logger.warning(str(i)+
" timeout waiting acknowledge for 3th init stage M2 message, retrying...")
285 logger.error(
"Abording... Too much timeout received for 3th init stage M2 message.")
290 logger.info(
"Initialisation successfully terminated...")
299 def readData(self, waitdata = 0.5, storage=[(
"sqlite",
"p2.db")], dateFromP2 =
False):
301 logger.info(
"Entering data exchange mode")
307 headers = {
"m1" : [0x4D,0x31],
"m2" : [0x4D,0x32],
"m3" : [0x4D,0x33]}
314 for (method,name)
in storage:
315 if method ==
"sqlite":
316 storObj.append((
"sqlite",
P2DbStore(name)))
317 elif method ==
"lastdata":
318 storObj.append((
"lastdata", name))
319 elif method ==
"file":
320 storObj.append((
"file", open(name,
'a')))
321 elif method ==
"csv":
322 storObj.append((
'csv',csv.writer(open(name,
'wb'), delimiter=
',')))
324 raise TypeError(
'Waiting for a tuple of the form (["file" | "sqlite" | "csv", filename), but got ('+str(method)+
','+str(name)+
')')
326 outMsg.prepare(headers[
"m2"], [0x01])
328 inMsg.prepare([0x4D,0x32],[0x01])
335 inHead = inMsg.getHeader(P2Msg.FMT_HEX_STR)
338 outMsg.prepare(headers[
"m3"])
339 logger.info(
"M3 message received")
342 logger.debug(
"Sending a M1 message")
343 outMsg.prepare(headers[
"m1"])
346 logger.debug(
"Sending a M2 message")
347 outMsg.prepare(headers[
"m2"])
350 self.com.sendMsg(outMsg)
353 inMsg = self.com.read()
356 Incoming frame process
358 if inMsg.getHeader(P2Msg.FMT_HEX_STR) ==
"4D31" and (curDate !=
None or not dateFromP2):
360 logger.debug(
"M1 received")
363 curDate = datetime.datetime.now()
366 for (family, obj)
in storObj:
367 if family ==
"sqlite":
370 obj.insert(curDate.strftime(
"%s"), inMsg.getData(P2Msg.FMT_HEX_STR))
371 elif family ==
"lastdata":
373 lfile = open(obj,
"w+")
374 lfile.write(curDate.strftime(
"%s")+
"\n"+inMsg.getData(P2Msg.FMT_HEX_STR))
376 elif family ==
"csv":
378 obj.writerow([curDate.strftime(
"%s")]+inMsg.getData(P2Msg.FMT_LIST))
379 elif family ==
"file":
381 obj.write(str(curDate.strftime(
"%s"))+
" ::"+inMsg.getData(P2Msg.FMT_HEX_STR))
383 obj.write(str(curDate.strftime(
"%s"))+
" :invalid:"+inMsg.getData(P2Msg.FMT_HEX_STR))
387 logger.debug(
"M2 received")
389 if inMsg.getHeader(P2Msg.FMT_HEX_STR) ==
"4D32":
391 msgData = inMsg.getData(P2Msg.FMT_LIST)
393 curDate = datetime.datetime(msgData[0]+2000,msgData[2],msgData[3],msgData[4],msgData[5],msgData[6])
395 logger.debug(
"Received message : "+inMsg.getStr())
402 self.
curStage = P2Furn.STAGE_PREINIT