Start reading from serial for paradox alarm

parent a3197384
......@@ -3,23 +3,74 @@ from zope.interface import implements
from twisted.plugin import IPlugin
from twisted.internet.serialport import SerialPort
from twisted.protocols.basic import LineReceiver
from twisted.internet.protocol import BaseProtocol
from twisted.internet import reactor
import serial
class ParadoxProtocol(LineReceiver):
SERIAL_PORT="/dev/ttyUSB0"
BAUDRATE=9600
def lineReceived(self, line):
print line
def format37ByteMessage(message):
checksum = 0
if len(message) % 37 != 0:
for val in message:
checksum += ord(val)
while checksum > 255:
checksum = checksum - (checksum / 256) * 256
message += bytes(bytearray([checksum])) # Add check to end of message
return message
class ParadoxProtocol(BaseProtocol):
packet=[]
def __init__(self, logger, core):
self.log = logger
self.core = core
def dataReceived(self, data):
if len(self.packet) < 37:
self.packet+=[data]
if len(self.packet) >= 37:
self.log.debug("RECEIVED: "+str(self.packet))
self.packet=[]
def connectionMade(self):
self.log.info("Serial port correctly connected")
self.login()
def login(self):
message = '\x72\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
self.transport.write(format37ByteMessage(message))
class Paradox(object):
implements(IPlugin, imodules.IModules)
def _openSerial(self, port=SERIAL_PORT, retry=3):
if retry > 0:
try:
self.port = SerialPort(ParadoxProtocol(self.log, self.core), port, reactor, baudrate=BAUDRATE)
except serial.SerialException as err:
self.log.info("Serial Port ERROR: "+str(err))
reactor.callLater(1, self._openSerial, port, retry-1)
else:
self.log.info("Unable to open Serial Port: retry in 1 minute")
reactor.callLater(60, self._openSerial, port)
def initialize(self, callback, logger):
self.log = logger
self.core = callback
self.port = SerialPort(ParadoxProtocol(), '/dev/ttyUSB0', reactor)
logger.info("Initialize Serial Connection...")
self._openSerial()
logger.info("Plugin initialized")
......
......@@ -93,7 +93,8 @@ class penguidomDaemon(Daemonizer):
if __name__ == "__main__":
# Starting all loggers
# file di log da 100 mega, per 5 rotazioni
formatter = logging.Formatter('%(asctime)s => %(name)-12s: %(levelname)-8s %(message)s')
LOGFORMAT='%(asctime)s => %(name)-12s: %(levelname)-8s %(message)s'
formatter = logging.Formatter(LOGFORMAT)
logdict={"corelog":
{"file":"penguidom.log","name":[("Core","general")]},
......@@ -110,7 +111,7 @@ if __name__ == "__main__":
logdict[l]["handler"].setLevel(logging.DEBUG)
logdict[l]["handler"].setFormatter(formatter)
logging.basicConfig()
logging.basicConfig(format=LOGFORMAT)
log = logging.getLogger( 'DaemonStarter' )
log.addHandler(logdict["corelog"]["handler"])
......@@ -127,7 +128,7 @@ if __name__ == "__main__":
lh.setLevel( loglevels[daemoncfg.get(n[1], 'loglevel')] )
lh.addHandler( logdict[l]["handler"] )
logging.basicConfig()
logging.basicConfig(format=LOGFORMAT)
# staring the application
if len(sys.argv) > 1:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment