def readArguments(): """ This function parse the arguments """ try: opts, args = getopt(sys.argv[1:], 'd:sh', ['device-conf=', 'sync', 'help']) DEVICE_FILE = None SYNC_FLAG = False for opt, arg in opts: if (opt == '-d') or (opt == '--device-conf'): DEVICE_FILE = arg if (opt == '-h') or (opt == '--help'): Help() if (opt == '-s') or (opt == '--sync'): print(SYNC_FLAG) SYNC_FLAG = True if DEVICE_FILE is None: log = osLogger.cOSLogger() log.printLog('Please, input the device config file option', 'ERROR') Help() return None return SYNC_FLAG except GetoptError as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex)) Help() return None
def isValid(_pData=None): try: if _pData is None: raise Exception('[run:isValid] Invalid Input Data : None Type') if len(_pData) <= 2: raise Exception('[run:isValid] Invalid Input Data length : ' + str(len(_pData))) if str(type(_pData)) == str(type(str())): pass elif str(type(_pData)) == str(type(bytes())): calcVal = osCRC.cCRC(_pData[:-2], _pMode=16).convertCRC16() if calcVal == _pData[-2:]: return True else: logMsg = 'Rcv Packet : ' + Global.byte2Hex(_pData) + '\nRcv CRC : ' + str(_pData[-2:]) + '\nCalc CRC : ' + str(calcVal) log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(logMsg) return False else: raise Exception('[run:isValid] Invalid input data Type : ' + str(type(_pData))) except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex)) return None
def exitServ(): StatusLog = None ExceptLog = None try: # 서버 시작 로그 생성 LogMsg = 'Data Collector Server Stop [' + str(os.getpid()) + ']' if StatusLog is None: StatusLog = osLogger.cOSLogger(_pPrefix='STATUS', _pLevel='INFO') StatusLog.writeLog(LogMsg) except Exception as Ex: if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[run:exitServ] ' + str(Ex))
def checkCRC_Eco(self, _pData: bytearray = None): try: crcCnt = len(_pData) - 2 print(crcCnt) except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex))
def executeS(sql: str = None, args: dict = None): ExceptLog = None retVal = None connector = None cursor = None try: if sql is None: raise Exception('Invalid Sql : None type') connector, cursor = connect() if args is None: cursor.execute(sql) else: cursor.execute(sql, args) connector.commit() retVal = True except Exception as Ex: if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[osDB:executeS] ' + str(Ex)) ExceptLog.writeLog('SQL : \n' + sql) ExceptLog.writeLog('Variable : ' | str(args)) if connector is not None: connector.rollback() retVal = False finally: close(connector, cursor) return retVal
def connect(DB_TYPE: str = None): ExceptLog = None conn = None curs = None try: import mysql.connector from mysql.connector import errorcode conn = mysql.connector.connect(user=Global.MARIA_DB_USER, password=Global.MARIA_DB_PASS, host=Global.MARIA_DB_IP, port=Global.MARIA_DB_PORT, database=Global.MARIA_DB_NAME, charset='utf8') curs = conn.cursor() except Exception as Ex: if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[osDB:connect] ' + str(Ex)) conn = None curs = None finally: return conn, curs
def sendPkt(_pData: list = None, _pEndian: str = 'B', _pFunc: str = '03', _pMaker: str = None): try: #print(_pEndian, _pMaker) maker = _pMaker if _pData is None: raise Exception('[sendPkt] Invalid input : None type') if len(_pData) != 5: raise Exception('[sendPkt] Invalid data length : ' + str(len(_pData))) if str(type(_pData)) != str(type(list())): raise Exception('[sendPkt] Invalid data type : ' + str(type(_pData))) #print(_pData) Pkt = bytearray() if _pEndian == 'B': Pkt = bytes.fromhex(_pData[0]) + bytes.fromhex(_pFunc) + bytes.fromhex(_pData[1]) + bytes.fromhex(_pData[2]) + bytes.fromhex(_pData[3]) + bytes.fromhex(_pData[4]) elif _pEndian == 'L': Pkt = bytes.fromhex(_pData[0]) + bytes.fromhex(_pFunc) + bytes.fromhex(_pData[2]) + bytes.fromhex(_pData[1]) + bytes.fromhex(_pData[4]) + bytes.fromhex(_pData[3]) else: raise Exception('[sendPkt] Invalid endian type : \'' + _pEndian + '\'') CheckSum = osCRC.cCRC(Pkt, _pMode=16, _pMaker=maker).convertCRC16() return Pkt + CheckSum except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex)) return None
def __init__(self, _pData: str = None, _pType: str = None, _pMode: int = 8, _pMaker: str = None): try: if len(_pData) == 0: raise Exception('[CRC] None Data is not supported') #print(_pData, _pType, _pMode, _pMaker) if _pMode == 8: if _pType is None: if str(type(_pData)) != str(type(str())): raise Exception( '[CRC] The type of input data must be a string') chk1, chk2 = self.calcCRC(_pData) self._mCRC = chr(chk1) + chr(chk2) elif _pType == 'b': chk1, chk2 = self.calcCRC2(_pData) self._mCRC = chr(chk1) + chr(chk2) else: self.initTable() #print(_pMaker) if _pMaker is None: # Modbus CRC 16 self._mCRC = self.calcCRC16(_pData) else: self._mCRC = self.calcCRC_Eco(_pData) except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex))
def makeDeamon(): ExceptLog = None try: if os.fork(): os._exit(0) os.setpgrp() os.umask(0) sys.stdin.close() sys.stdout = None sys.stderr = None signal.signal(signal.SIGINT, stopServ) signal.signal(signal.SIGTERM, stopServ) except Exception as Ex: # 예외 로그 생성 if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[run:makeDeamon] ' + str(Ex)) finally: # 로그 객체 삭제 if ExceptLog is not None: del ExceptLog
def readDeviceConf(maker : str = None): """ This function parse the arguments """ try: if maker is None: raise Exception('Invalid Input Maker : None type') DeviceConf = dict() Device_type = '' ConInfoFile = open('/home/seah/app/bestFems/connect.conf', 'r', encoding='UTF8') Coninfo = ConInfoFile.read() ConInfoFile.close Conin = Coninfo.split(',') # Oracle info mapping ora_ip = Conin[0] ora_port = Conin[1] ora_user = Conin[2] ora_pw = Conin[3].rstrip('\n') ora_string = ora_user+'/'+ora_pw+'@'+ora_ip+':'+ora_port+'/BESTSF' ora_con = cx_Oracle.connect(ora_string) ora_db = ora_con.cursor() if maker == 'JB' or maker == 'EL': Device_type = 'C' kind = '2' elif maker == 'MI' or maker == 'LS' or maker == 'VI' or maker == 'ECO': Device_type = 'E' kind = '1' ora_sql = """SELECT DISTINCT(COM_CONNECT_DEVICE_ID) AS DEVICE_ID FROM DEVICE_MASTER WHERE DEVICE_TYPE = '{0}' AND USE_YN = 'Y' AND MAKER LIKE '%{1}%' """.format(Device_type, maker) tfv = ora_db.execute(ora_sql) DeviceList = [] for token in ora_db: tmp_row = [] DeviceID = token[0] tmp_row.append(DeviceID) DeviceList.append(tmp_row) for var in DeviceList: ora_sql1 = """SELECT DEVICE_ADDRESS FROM DEVICE_MASTER WHERE DEVICE_TYPE = 'D' AND USE_YN = 'Y' AND DEVICE_ID = '{0}' """.format(var[0]) fv = ora_db.execute(ora_sql1) for token in ora_db: Device_addr = token[0] DeviceConf[var[0]] = [Device_addr, '1470', 'B', maker, kind] ora_db.close() ora_con.close() #DeviceConf[DeviceList] #DeviceConf[DEVICE_INFO[0]] = [DEVICE_INFO[1], DEVICE_INFO[2], DEVICE_INFO[3], DEVICE_INFO[4], DEVICE_INFO[5]] return DeviceConf except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex)) return None
def tag_split(tag): try: tagNm = tag.split('/') tagNm = tagNm[2].replace('.conf','') return tagNm except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex)) return None
def convertCRC16(self): try: hexVal = '{:04x}'.format(self._mCRC) hiVal = hexVal[:2] lowVal = hexVal[-2:] retVal = bytes.fromhex(hiVal) + bytes.fromhex(lowVal) #retVal = bytes.fromhex(lowVal) + bytes.fromhex(hiVal) return retVal except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex))
def run(self): ExceptLog = None ConnectLog = None lock = threading.Lock() try: LogMsg = '[' + datetime.now().strftime( '%Y.%m.%d. %H:%M:%S') + '] <' + str( self._mDetail[0]) + ':' + str( self._mDetail[1]) + '> Connection Open' if ConnectLog is None: ConnectLog = osLogger.cOSLogger(_pPrefix='CONNECT', _pLevel='INFO') lock.acquire() ConnectLog.writeLog(LogMsg) lock.release() while True: # 데이터 수신 대기 RecvPacket = str() RecvByteStream = (self._mChannel.recv(Global.MAX_BUF)) RecvPacket = RecvByteStream.decode() print(RecvPacket) except Exception as Ex: if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[run:run] ' + str(Ex)) finally: self._mChannel.close() LogMsg = '[' + datetime.now().strftime( '%Y.%m.%d. %H:%M:%S') + '] <' + str( self._mDetail[0]) + ':' + str( self._mDetail[1]) + '> Connection Close' if ConnectLog is None: ConnectLog = osLogger.cOSLogger(_pPrefix='CONNECT', _pLevel='INFO') lock.acquire() ConnectLog.writeLog(LogMsg) lock.release()
def calcCRC16(self, _pData: bytearray = None): try: print('ModbusCRC') crc = 0xffff for a in _pData: idx = self._mCRC_Table[(crc ^ byte2int(a)) & 0xff] crc = ((crc >> 8) & 0xff) ^ idx swapped = ((crc << 8) & 0xff00) | ((crc >> 8) & 0x00ff) return swapped except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex))
def __init__(self, Channel, Detail): ExceptLog = None try: self._mChannel = Channel self._mDetail = Detail threading.Thread.__init__(self) except Exception as Ex: if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[run:__init__] ' + str(Ex)) finally: pass
def stopServ(_pSignum, _pFrame): ExceptLog = None StatusLog = None try: LogMsg = Global.getCurrentTime( ) + ' Mosquito Data Collector Server Stop [' + str(os.getpid()) + ']' if StatusLog is None: StatusLog = osLogger.cOSLogger(_pPrefix='STATUS', _pLevel='INFO') StatusLog.writeLog(LogMsg) sys.exit(0) except Exception as Ex: # 예외 로그 생성 if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[run:stopServ] ' + str(Ex)) finally: # 로그 객체 삭제 if ExceptLog is not None: del ExceptLog if StatusLog is not None: del StatusLog
def Help(): """ This function is 'Help Screen' """ try: print('Read the register of modbus device') print(sys.argv[0], ' -d <Device Config>') print('Option Detail') print('\t-d --device-conf : Device Configuration Maker, Ex)JB, EL, LS, VI, MI') print('\t-s --sync : Thread sync (with join)') print('\t-h --help : this screen') except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex))
def savePid(pid: str = None): ExceptLog = None try: if pid is None: raise Exception('Invalid input param : None type') except Exception as Ex: # 예외 로그 생성 if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[run:savePid] ' + str(Ex)) finally: # 로그 객체 삭제 if ExceptLog is not None: del ExceptLog
def calcCRC_Eco(self, _pData: bytearray = None): try: #print('ECOCRC') uchCRCHi = 0xFF uchCRCLo = 0xFF for a in _pData: idx = np.uint8(uchCRCHi) ^ np.uint8((byte2int(a))) uchCRCHi = np.uint8(uchCRCLo) ^ (self._mCRC_Table[idx] & 0xFF) uchCRCLo = np.uint8((self._mCRC_Table[idx] >> 8) & 0xFF) return (uchCRCHi << 8) | uchCRCLo except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex))
def execute(sqlObject: object = None, argsObject: object = None): retVal = None ExceptLog = None try: executeS(sqlObject, argsObject) retVal = True except Exception as Ex: if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[osDB:execute] ' + str(Ex)) retVal = False finally: return retVal
def initTable(self): try: result = [] for byte in range(256): crc = 0x0000 for _ in range(8): if (byte ^ crc) & 0x0001: crc = (crc >> 1) ^ 0xa001 else: crc >>= 1 byte >>= 1 result.append(crc) self._mCRC_Table = result except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex))
def calcCRC2(self, _pData: bytearray = None): try: if _pData is None: raise Exception('[CRC] None data is not supported') iLen = len(_pData) CharSum = 0 for i in range(0, iLen): CharSum = np.uint8(np.uint8(CharSum) + np.uint8(_pData[i])) CheckSum1 = np.uint8(((CharSum & 0xF0) >> 4) + 0x30) CheckSum2 = np.uint8(((CharSum & 0x0F) >> 0) + 0x30) return CheckSum1, CheckSum2 except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex)) return None, None
def close(connector: object = None, cursor: object = None): ExceptLog = None retVal = None try: if cursor is not None: cursor.close() if connector is not None: connector.close() retVal = True except Exception as Ex: if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[osDB:close] ' + str(Ex)) retVal = False finally: return retVal
def run(kafka_client, kafka_topic, tag_lists): try: # arguments Sync = readArguments() # devices conf DeviceList = readDeviceConf(Maker) procs = [] # 카프카 접속정보 #kafka_ip, kafka_port, kafka_topic = kafka_connetion_info() # 카프카 클라이언트 선언 #kafka_client = realtime ( kafka_ip, kafka_port ) if DeviceList is None: raise Exception('Fail to read Device Configuration file') # device info for deviceInfo in DeviceList.keys(): try: print(deviceInfo) #thread = threading.Thread(target=getResponse, name=deviceInfo, args=(DeviceList, deviceInfo, kafka_client, kafka_topic, tag_lists)) #thread.start() #if Sync: # thread.join() #else: # pass proc = Process(target=getResponse, name=deviceInfo, args=(DeviceList, deviceInfo, kafka_client, kafka_topic, tag_lists)) procs.append(proc) proc.start() except: pass # print('----------------------re:'+deviceInfo) # proc = Process(target=getResponse, name=deviceInfo, args=(DeviceList, deviceInfo, kafka_client, kafka_topic, tag_lists)) # procs.append(proc) # proc.start() for proc in procs: proc.join() return True except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex)) return False
def getChecksum(Data: str = None): ExceptLog = None RetVal = None try: if Data is None: raise Exception('Invalid Input Data : None') iLen = len(str(Data)) CharSum = 0 for i in range(0, iLen): CharSum = CharSum + ord(Data[i]) CheckSum1 = ((CharSum & 0xF0) >> 4) + 0x30 CheckSum2 = ((CharSum & 0x0F) >> 0) + 0x30 RetVal = chr(CheckSum1) + chr(CheckSum2) except Exception as Ex: if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[sendData:getChecksum] ' + str(Ex)) finally: return RetVal
def getDataFrame(sql: str = None, args: dict = None, OP_TYPE: str = None): ExceptLog = None retVal = None connector = None cursor = None try: if sql is None: raise Exception('Invalid Sql : None Type') connector, cursor = connect(OP_TYPE=OP_TYPE) retVal = pandas.read_sql(sql=sql, con=connector, params=args) except Exception as Ex: if ExceptLog is None: ExceptLog = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') ExceptLog.writeLog('[osDB:getDataFrame] ' + str(Ex)) ExceptLog.writeLog('SQL : \n' + sql) ExceptLog.writeLog('Variable : ' + str(args)) retVal = None finally: close(connector, cursor) return retVal
def readDeviceInfo(Device: str = None, maker: str = None): try: if Device is None: raise Exception('Invalid Input Device : None type') REGISTERS_INFOS = list() DeviceList = list() tagList = dict() DeviceDic = dict() ConInfoFile = open('/home/seah/app/bestFems/connect.conf', 'r', encoding='UTF8') Coninfo = ConInfoFile.read() ConInfoFile.close Conin = Coninfo.split(',') # Oracle info mapping ora_ip = Conin[0] ora_port = Conin[1] ora_user = Conin[2] ora_pw = Conin[3].rstrip('\n') ora_string = ora_user+'/'+ora_pw+'@'+ora_ip+':'+ora_port+'/BESTSF' ora_con = cx_Oracle.connect(ora_string) ora_db = ora_con.cursor() if maker == 'JB' or maker == 'EL': ora_sql = """ SELECT DEVICE_ID, DEVICE_ADDRESS, DEVICE_USE FROM DEVICE_MASTER WHERE DEVICE_TYPE = 'C' AND USE_YN = 'Y' AND COM_CONNECT_DEVICE_ID = '{0}' AND MAKER LIKE '%{1}%'""".format(Device, maker) tfv = ora_db.execute(ora_sql) for token in ora_db: tmp_row=[] subId = token[0] Deviceaddr = token[1] kind = token[2] DeviceList.append(subId) DeviceDic[subId] = Deviceaddr for var in DeviceList: ora_sql1 = """SELECT a.TAG_ID, a.DATA_ADDRESS, b.FACTORY_ID, a.CORRECT_RATIO FROM TAG_MASTER a inner join DEVICE_MASTER b on 1=1 and a.DEVICE_ID = b.DEVICE_ID WHERE a.DATA_ADDRESS IS NOT NULL AND b.DEVICE_ID = '{0}'""".format(var) #rint(ora_sql1) addr=DeviceDic[var] if len(addr) == 1: addr = '0'+addr fv = ora_db.execute(ora_sql1) for token in ora_db: tag = token[0] Device_addr = token[1] FactoryID = token[2] unit = token[3] REGISTERS_INFO = Device_addr.split() REGISTERS_INFO.insert(0, addr) REGISTERS_INFOS.append(REGISTERS_INFO) tagList[tag] = [REGISTERS_INFO, kind, FactoryID, unit] elif maker == 'VI' or maker == 'MI' or maker == 'ECO' or maker == 'LS': ora_sql = """SELECT DEVICE_ID, DEVICE_ADDRESS, DEVICE_USE FROM DEVICE_MASTER WHERE DEVICE_TYPE = 'E' AND USE_YN = 'Y' AND COM_CONNECT_DEVICE_ID = '{0}' AND MAKER LIKE '%{1}%'""".format(Device, maker) #ora_sql = """SELECT DEVICE_ID, DEVICE_ADDRESS FROM DEVICE_MASTER WHERE DEVICE_TYPE = 'E' AND USE_YN = 'Y' AND COM_CONNECT_DEVICE_ID = 'D00007' AND MAKER LIKE '%{1}%'""".format(Device, maker) #print(ora_sql) tfv = ora_db.execute(ora_sql) for token in ora_db: tmp_row=[] subId = token[0] Deviceaddr = token[1] kind = token[2] DeviceList.append(subId) DeviceDic[subId] = Deviceaddr for var in DeviceList: ora_sql1 = """SELECT a.TAG_ID, a.DATA_ADDRESS, b.FACTORY_ID, a.CORRECT_RATIO FROM TAG_MASTER a INNER JOIN DEVICE_MASTER B ON 1=1 AND a.DEVICE_ID = b.DEVICE_ID WHERE a.VIRTUAL_TAG_YN = 'N' AND a.COLL_YN = 'Y' AND a.DEVICE_ID = '{0}'""".format(var) addr=DeviceDic[var] if len(addr) == 1: addr = '0'+addr fv = ora_db.execute(ora_sql1) for token in ora_db: tag = token[0] Device_addr = token[1] FactoryID = token[2] unit = token[3] REGISTERS_INFO = Device_addr.split() REGISTERS_INFO.insert(0, addr) REGISTERS_INFOS.append(REGISTERS_INFO) tagList[tag] = [REGISTERS_INFO, kind, FactoryID, unit] ora_db.close() ora_con.close() return REGISTERS_INFOS, tagList except Exception as Ex: log = osLogger.cOSLogger(_pPrefix='Exception', _pLevel='ERROR') log.writeLog(str(Ex)) return None
atexit.register(exitServ) ExceptLog = None StatusLog = None try: # 변수 지정 MAX_THREAD = 4 SERVICE_PORT = 6363 # 데몬 모드 시작 # if (Global.OPERATION_MODE == 'D') and (Global.getOS() != 'Windows'): # makeDeamon() # 서버 시작 로그 생성 LogMsg = 'Data Collector Server Start [' + str(os.getpid()) + ']' if StatusLog is None: StatusLog = osLogger.cOSLogger(_pPrefix='STATUS', _pLevel='INFO') StatusLog.writeLog(LogMsg) # 서버 시작 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(('', SERVICE_PORT)) server.listen(MAX_THREAD) while True: channel, details = server.accept() DCSInstance = cDataCapture(channel, details) DCSInstance.start() gc.collect() except KeyboardInterrupt as Ex: pass except Exception as Ex: