import web import time import sys import time import modbus_tk import modbus_tk.defines as cst import modbus_tk.modbus_tcp as modbus_tcp import datetime import os import math ################################ init Discret Input ################################################################################### def initDiscretInput(): global diNameUnit global diArray global diArrayColorOn global diArrayColorOff global diArrayAlarmOn global diArrayAlarmOff global diX global diY diArray = [] diNameUnit=[] diArrayColorOn=[] diArrayColorOff=[] diArrayAlarmOn = [] diArrayAlarmOff = [] diX=[] diY=[] for counter in range(0,1000): diArray.append(counter) diNameUnit.append(counter) diArrayColorOn.append(counter) diArrayColorOff.append(counter) diArrayAlarmOn.append(counter) diArrayAlarmOff.append(counter) diX.append(counter) diY.append(counter) diArray[counter]=0 counter += 1 try: counter=1 with open(diPath) as f: for line in f: param=line.split(';') if(param[0] == 'di'): diX[counter] = param[3] diY[counter] = param[4] diArrayColorOn[counter] = param[7] diArrayColorOff[counter] = param[8] diArrayAlarmOn[counter] = param[9] diArrayAlarmOff[counter] = param[10] diNameUnit[counter] = param[11] counter += 1 diArray[0] = counter - 1 f.close() except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) return diArray ################################ init Analog Input ################################################################################### def initAnalogInput(): global aiArray global aiArrayKoef global aiNameUnit global aiX global aiY global aiArrayColor aiArray = [] aiArrayKoef=[] aiNameUnit=[] aiX=[] aiY=[] aiArrayColor=[] for counter in range(0,1000): aiArray.append(counter) aiArrayKoef.append(counter) aiNameUnit.append(counter) aiX.append(counter) aiY.append(counter) aiArrayColor.append(counter) aiArray[counter]=(1,' ') counter += 1 try: counter=1 with open(aiPath) as f: for line in f: param=line.split(';') if(param[0] == 'ai'): aiX[counter] = param[2] aiY[counter] = param[3] aiArrayColor[counter] = param[4] aiArrayKoef[counter] = param[5] aiNameUnit[counter] = param[7] counter += 1 aiArray[0] = counter - 1 f.close() except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) return aiArray class modbus(): def GET(self, name): global master outStringDI='' outStringAI='' jssStr='' jsStr='' try: getDI = master.execute(1, cst.READ_DISCRETE_INPUTS, 0,diArray[0]) getAI = master.execute(1, cst.READ_INPUT_REGISTERS, 0,aiArray[0]) headString_start = ' \ \ scadapy v.1.1 '+time.strftime('%H:%M:%S')+'\ ' headS= headString_start + jssStr + headString_end endStr='' except Exception, e: print 'Error coennct modbas ',e master = modbus_tcp.TcpMaster(host=slaveIP, port=int(slavePort)) master.set_timeout(1.0) headS=' Error connection' endStr='' return headS + endStr #################################################################################### if __name__ == "__main__": if(os.name=='nt'): appPath=os.path.dirname(sys.argv[0]) + '\\' if(appPath=='\\'): appPath='.\\' else: appPath=os.path.dirname(sys.argv[0]) + '/' if(appPath=='/'): appPath='./' global master global delayTime with open(appPath+'settings.cfg') as f: for line in f: param=line.split('=') if(param[0] == 'slaveIP'): slaveIP=param[1].strip() if(param[0] == 'slavePort'): slavePort=param[1].strip() if(param[0] == 'discretCfg'): discretCfg=param[1].strip() if(param[0] == 'analogCfg'): analogCfg=param[1].strip() if(param[0] == 'bgimage'): bgimage=param[1].strip() if(param[0] == 'delayTime'): delayTime=param[1].strip() f.close() backGroundPath=appPath+bgimage diPath=appPath+discretCfg aiPath=appPath+analogCfg diArray=initDiscretInput() aiArray=initAnalogInput() master = modbus_tcp.TcpMaster(host=slaveIP, port=int(slavePort)) master.set_timeout(1.0) urls = ('/(.*)', 'modbus') app = web.application(urls, globals()) app.run()