# -*- coding: utf-8 -*- # @Author : LoganFan # @Time : 2019-02-16 # @File : run.py from SqlServerDB import SqlServerDB from PBParser import PBParser import os import pyodbc from twisted.internet import reactor import logging from logging.handlers import RotatingFileHandler logger = logging.getLogger('') logger.setLevel(logging.DEBUG) Rthandlder = RotatingFileHandler('WISCANConclusionUpdate_log.txt',maxBytes=5*1024*1024) formatter = logging.Formatter('%(asctime)s - - %(message)s') Rthandlder.setFormatter(formatter) logger.addHandler(Rthandlder) class Manager(object): def __init__(self): DRIVERs = pyodbc.drivers() DRIVER = DRIVERs[0] if DRIVERs else '' self.config = {} dirPath =os.path.dirname(os.path.abspath(__file__)) txtPath = os.path.join(dirPath,'config.txt') with open(txtPath, 'r', encoding='utf-8') as f: for line in f: if line.strip() and ('=' in line): v = [x.strip() for x in line.split('=')] self.config[v[0]] = v[1] self.dbManager = SqlServerDB(DRIVER=DRIVER, UID=self.config['DBUSER'], PWD=self.config['DBPASSWORD'], SERVER=self.config['DBHOST'], DATABASE=self.config['DBNAME']) self.pbParser = PBParser() def updateConclusionWithPB(self, imaPICNo): selectSql = "SELECT a.CHECKUNITID FROM IDR_WISCAN_CONCLUSION AS a INNER JOIN IDR_CHECK_UNIT AS b \ ON a.CHECKUNITID = b.ID INNER JOIN IDR_IMAGE AS c ON c.ID = b.IMAGEID AND c.PICNO='{}'".format(imaPICNo) self.dbManager.dbpool.runQuery(selectSql).addCallback(self.execUpdateDB,imaPICNo) reactor.run() def execUpdateDB(self,result,imaPICNo): serialNo = imaPICNo[-4:] day = imaPICNo[-8: -4] year = imaPICNo[-12: -8] equipNo = imaPICNo[-len(imaPICNo):-12] BaseDir = r'{}'.format(self.config['BaseDir']) dirPath = os.path.join(BaseDir, equipNo, year, day, serialNo) pb_path = '' # pb_path='/Users/logan/Desktop/深圳陆运/Source_data_20181224/0069/86755MB04201502060069_0.pb' logger.debug('pbPath :{}'.format(pb_path)) for dirpath, dirnames, filenames in os.walk(dirPath): for file_ in filenames: if '.pb' in file_: pb_path = os.path.join(dirpath, file_) break if pb_path: result_dict = self.pbParser.get_PbDict(pb_path) contraband = self.pbParser.get_contraband_result(result_dict) hiding = self.pbParser.get_hiding_result(result_dict) goodname = self.pbParser.get_goodname_result(result_dict) empty = self.pbParser.get_emptycontainer_result(result_dict) waste = self.pbParser.get_waste_result(result_dict) chaging = self.pbParser.get_changing_result(result_dict) coordinate = self.pbParser.get_cheti_cordinate(result_dict) time = self.pbParser.get_algorithm_time(result_dict) checkunitid = result[0][0] logger.debug('checkunitid : {}'.format(checkunitid)) updateSql = "UPDATE IDR_WISCAN_CONCLUSION SET WASTE='{waste}',ALGORITHM_TIME='{time}',CHANGING='{chaging}', \ EMPTY_CONTAINER='{empty}',TRUCK_COORDINATE='{coordinate}',RESERVE2='{goodname}',RESERVE3='{hiding}', \ RESERVE5='{contraband}' WHERE CHECKUNITID='{checkunitid}'".format(waste=waste, time=time, chaging=chaging, empty=empty, \ coordinate=coordinate, goodname=goodname, hiding=hiding, contraband=contraband, checkunitid=checkunitid) self.dbManager.dbpool.runOperation(updateSql).addCallback(self.receiveResult) def receiveResult(self,result): logging.debug("Update success!") reactor.stop() def errorquery(self, result): logging.debug("Error received!", result) reactor.stop() def main(imaPICNo): logging.debug('imaPICNO : {}'.format(imaPICNo)) manager = Manager() manager.updateConclusionWithPB(imaPICNo=imaPICNo) if __name__ == '__main__': imaPICNo = '86532FS03201808170037' checkunitid = 'A73E5FB3-5290-4876-9BCF-A9C610B723BE' main(imaPICNo)