197 lines
7.1 KiB
Python
197 lines
7.1 KiB
Python
import SocketServer
|
|
import sys
|
|
|
|
class IncomingPacketHandler(SocketServer.BaseRequestHandler):
|
|
|
|
def handle(self):
|
|
self.data = self.request.recv(6).strip()
|
|
handler_keyboard_cmd(self.data)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
#TODO
|
|
#1. get serial port parameter from command line arguements
|
|
#2. print usage with examples before Enter your input
|
|
#3. add debug mode
|
|
|
|
#initialize global vars
|
|
sys_up = False
|
|
run = True
|
|
valid_cmd = False
|
|
int_ser_data = [0,0,0,0,0,0,0,0,0,0,0,0,0]
|
|
hex_ser_data = [0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0]
|
|
|
|
#functions defined here
|
|
def write_serial(ser, out_ser_pkt):
|
|
#function writes 13 bytes to serial port
|
|
# ser.write(out_ser_pkt)
|
|
import time
|
|
time.sleep(5 / 1000000.0)
|
|
for i in range(0, 13):
|
|
ser.write(chr(out_ser_pkt[i]))
|
|
# ser.write(out_ser_pkt[i])
|
|
# print "wrote i=%i out_ser_pkt=%i" % (i,out_ser_pkt[i])
|
|
# time.sleep(5)
|
|
|
|
#serial port packet handler
|
|
def handler_serial_pkt():
|
|
global sys_up
|
|
global int_ser_data
|
|
global out_ser_pkt
|
|
global valid_cmd
|
|
|
|
#print "in hsp int_ser_data[4]= %i", (int_ser_data[4])
|
|
#print "in hsp valid_cmd= %i", (valid_cmd)
|
|
#do the needful for sys up
|
|
if sys_up == False and int_ser_data[4] == 01:
|
|
out_ser_pkt=[0x7e,0x7e,0xff,0xfa,0x0,0x01,0x01,0x0,0x61,0x0,0x0,0xff,0x7e]
|
|
write_serial(ser,out_ser_pkt)
|
|
sys_up = True
|
|
print "sent packet sys up"
|
|
else:
|
|
#the basic switch which was used in c is here
|
|
#print "looping back"
|
|
if int_ser_data[4] == 01:
|
|
#send device cmd & data to panel
|
|
if valid_cmd == True:
|
|
out_ser_pkt=[0x7e,0x7e,0xff,0xfa,0x0,0x01,dev_cmd,0x01,0xAA,panel_dev,dev_no,0xff,0x7e]
|
|
print "prepared out_ser_pkt "
|
|
write_serial(ser,out_ser_pkt)
|
|
print "sent packet out_ser_pkt "
|
|
#if device cmd & data ack from panel
|
|
elif int_ser_data[4] == 0x5a:
|
|
valid_cmd = False
|
|
print "Done device cmd"
|
|
elif int_ser_data[4] == 0x55:
|
|
valid_cmd = False
|
|
print "FATAL ERROR!!! Received NAK from panel"
|
|
#no data for panel send keep alive
|
|
else:
|
|
out_ser_pkt=[0x7e,0x7e,0xff,0xfa,0x0,0x01,0x0,0x01,0xAA,0x52,0x01,0xff,0x7e]
|
|
write_serial(ser,out_ser_pkt)
|
|
|
|
#keyboard command handler
|
|
def handler_keyboard_cmd(kbd_cmd):
|
|
# global kdb_cmd
|
|
|
|
global dev
|
|
global str_dev_no
|
|
global dev_no
|
|
global dev_cmd
|
|
global str_dev_cmd
|
|
global valid_cmd
|
|
global panel_dev
|
|
print kbd_cmd
|
|
#if lenght of keyboard cmd equals 6, parse keyboard input
|
|
if kbd_cmd[0] == 'r' or kbd_cmd[0] == 'R'or kbd_cmd[0] == 'd' or kbd_cmd[0] == 'D':
|
|
import string
|
|
dev=string.upper(kbd_cmd[0])
|
|
print "dev=%s" % (dev)
|
|
str_dev_no=kbd_cmd[1] + kbd_cmd[2]
|
|
print "str_dev_no=%s" %str_dev_no
|
|
if str_dev_no =='01':
|
|
dev_no=1
|
|
elif str_dev_no =='02':
|
|
dev_no=2
|
|
elif str_dev_no =='03':
|
|
dev_no=3
|
|
elif str_dev_no =='04':
|
|
dev_no=4
|
|
elif str_dev_no =='05':
|
|
dev_no=5
|
|
elif str_dev_no =='06':
|
|
dev_no=6
|
|
elif str_dev_no =='07':
|
|
dev_no=7
|
|
elif str_dev_no =='08':
|
|
dev_no=8
|
|
elif str_dev_no =='09':
|
|
dev_no=9
|
|
elif str_dev_no =='10':
|
|
#print "debug in 10"
|
|
dev_no=0xa
|
|
elif str_dev_no =='11':
|
|
#print "debug in 11"
|
|
dev_no=0xb
|
|
elif str_dev_no =='12':
|
|
#print "debug in 12"
|
|
dev_no=0xc
|
|
elif str_dev_no =='13':
|
|
#print "debug in 13"
|
|
dev_no=0xd
|
|
elif str_dev_no =='14':
|
|
#print "debug in 14"
|
|
dev_no=0xe
|
|
elif str_dev_no =='15':
|
|
#print "debug in 15"
|
|
dev_no=0xf
|
|
elif str_dev_no =='16':
|
|
#print "debug in 16"
|
|
dev_no=0x10
|
|
else:
|
|
dev_no=0
|
|
#print "invalid device no"
|
|
if dev_no >= 1 and dev_no <= 0x10:
|
|
print "--- valid device no.---"
|
|
str_dev_cmd = kbd_cmd[4] + kbd_cmd[5]
|
|
dev_cmd=int(str_dev_cmd, 16)
|
|
print "str_dev_cmd=%s" % (str_dev_cmd)
|
|
print "dev_cmd= %x " %(dev_cmd)
|
|
if dev == 'R':
|
|
if dev_cmd == 00 or dev_cmd == 01:
|
|
valid_cmd = True
|
|
panel_dev=0x52
|
|
#print "valid_cmd = %s" % (valid_cmd)
|
|
else:
|
|
valid_cmd = False
|
|
print "for relay valid_cmd = %s" % (valid_cmd)
|
|
else:
|
|
if dev_cmd >= 0x0 and dev_cmd <= 0xff:
|
|
valid_cmd = True
|
|
panel_dev=0x44
|
|
dev_no=dev_no-1
|
|
#print "for dac valid_cmd = %s" % (valid_cmd)
|
|
else:
|
|
valid_cmd = False
|
|
print "valid_cmd = %s" % (valid_cmd)
|
|
else:
|
|
print "Invlaid device"
|
|
else:
|
|
print "Invalid Command"
|
|
|
|
|
|
#---- main program here
|
|
|
|
#init serial port
|
|
import serial
|
|
ser = serial.Serial('/dev/ttyS0', 115200,timeout=20)#init serial port TODO serial port params should be command line arguements
|
|
sys_up=False;
|
|
print "Connecting to Panel. Please wait..."
|
|
while run:
|
|
ser_data = ser.read(13)#read 1 packet of 13bytes from panel
|
|
for i in range(0, 13):
|
|
int_ser_data[i]=ord(ser_data[i])#convert packet received from char to integer
|
|
# print "i = %i int_ser_data = %i" % (i,int_ser_data[i])
|
|
|
|
#validate packet byte 1, byte 2, byte 13 =126 viz 0x7e
|
|
if int_ser_data[0] == 126 and int_ser_data[1] == 126 and int_ser_data[12] == 126:
|
|
#print "b4 hsp int_ser_data[4]= %i", (int_ser_data[4])
|
|
handler_serial_pkt()
|
|
# else:
|
|
# print "imporper packet"
|
|
if valid_cmd == False:
|
|
HOST, PORT = "127.0.0.1", 4321
|
|
server = SocketServer.TCPServer((HOST, PORT), IncomingPacketHandler)
|
|
server.serve_forever()
|
|
|
|
|
|
# l=len(kbd_cmd)
|
|
# print "kbd_cmd=%s l=%i " % (kbd_cmd,l)
|
|
# if l==6:
|
|
# handler_keyboard_cmd(kbd_cmd)
|
|
# else:
|
|
# print "Invalid Input"
|
|
|
|
|
|
|