import CBMSBatDiag import pymysql import datetime import pandas as pd from LIB.BACKEND import DBManager import datetime from apscheduler.schedulers.blocking import BlockingScheduler from USER.SPF.alibaba.Common import DBDownload from USER.SPF.alibaba.Common import log #...................................电池包电芯安全诊断函数...................................................................................................................... def diag_cal(): global SNnums now_time=datetime.datetime.now() start_time=now_time-datetime.timedelta(seconds=65) start_time=start_time.strftime('%Y-%m-%d %H:%M:%S') end_time=now_time.strftime('%Y-%m-%d %H:%M:%S') #数据库配置 host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port=3306 db='qx_cas' user='qx_read' password='Qx@123456' #读取故障结果库中当前故障...................................................... param='time_st,time_sp,sn,faultcode,faultlv,faultinfo,faultadvice' tablename='' mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db) cursor = mysql.cursor() sql = "select %s from %s where time_sp='0000-00-00 00:00:00'" %(param,tablename) cursor.execute(sql) res = cursor.fetchall() df_diag_ram= pd.DataFram(res,columns=param.split(',')) cursor.close() mysql.close() for sn in SNnums: if 'PK500' in sn: celltype=1 #6040三元电芯 elif 'PK502' in sn: celltype=2 #4840三元电芯 elif 'K504B' in sn: celltype=99 #60ah林磷酸铁锂电芯 elif 'MGMLXN750' in sn: celltype=3 #力信50ah三元电芯 elif 'MGMCLN750' or 'UD' in sn: celltype=4 #CATL 50ah三元电芯 else: print('SN:{},未找到对应电池类型!!!'.format(sn)) continue # sys.exit() #读取原始数据库数据........................................................................................................................................................ dbManager = DBManager.DBManager() df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms']) df_bms = df_data['bms'] #读取结果数据库数据........................................................................................................................................................ mode=1 tablename1='cellstateestimation_soh' tablename2='cellstateestimation_uniform_socvoltdiff' tablename3='cellstateestimation_soc' DBRead=DBDownload.DBDownload(host, port, db, user, password,mode) with DBRead as DBRead: df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time) df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellmin_num,cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time) df_soc=DBRead.getdata('time','sn','packsoc', tablename=tablename3, sn=sn) #电池诊断................................................................................................................................................................ if not df_bms.empty: df_diag_ram_sn=df_diag_ram[df_diag_ram['sn']==sn] df_diag_ram_sn.reset_index(inplace=True,drop=True) batdiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms,df_soh,df_soc,df_uniform,df_diag_ram_sn) df_diag_res, df_health_res=batdiag.diag() #获取电池故障结果和电池评分结果 #电池评分写入数据库 if not df_health_res.empty: #变为历史故障更改数据库 with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\05BatDiag\电池评分.txt','a') as file: file.write(str(tuple(df_health_res))+'\n') #历史故障筛选并更改数据库故障结束时间......................................................... if not df_diag_res.empty: df_diag_now=df_diag_res[df_diag_res['time_sp'] == '0000-00-00 00:00:00'] #去除历史故障 df_diag_new = pd.concat([df_diag_res,df_diag_ram_sn,df_diag_ram_sn]).drop_duplicates(subset=['time_st','faultcode'],keep=False)#此次判断中新增故障 df_diag_end=pd.concat([df_diag_res,df_diag_new,df_diag_new]).drop_duplicates(subset=['time_st','faultcode'],keep=False)#此次判断中新增故障 df_diag_end=df_diag_end[df_diag_end['time_sp'] != '0000-00-00 00:00:00'] df_diag_end.reset_index(inplace=True,drop=True) #重置索引 if not df_diag_end.empty: #变为历史故障更改数据库 with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\05BatDiag\故障报警.txt','a') as file: file.write(str(tuple(df_diag_end))+'\n') #新增故障筛选并存入数据库..................................................................... if not df_diag_new.empty: #新增写入数据库 with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\05BatDiag\故障报警.txt','a') as file: file.write(str(tuple(df_diag_new))+'\n') #更新diag的Ram数据 df_diag_ram=df_diag_ram.drop(df_diag_ram[df_diag_ram.sn==sn].index) df_diag_ram=df_diag_ram.append(df_diag_now, ignore_index=True) df_diag_ram.reset_index(inplace=True,drop=True) #重置索引 #健康度评分存入数据库 #...............................................主函数起定时作用....................................................................................................................... if __name__ == "__main__": excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx' SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060') SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040') SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840') SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255') SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255') SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255') SNnums_6060=SNdata_6060['SN号'].tolist() SNnums_6040=SNdata_6040['SN号'].tolist() SNnums_4840=SNdata_4840['SN号'].tolist() SNnums_L7255=SNdata_L7255['SN号'].tolist() SNnums_C7255=SNdata_C7255['SN号'].tolist() SNnums_U7255=SNdata_U7255['SN号'].tolist() SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_U7255, SNnums_6040 + SNnums_4840 + SNnums_6060 SNnums=['HUATING'] mylog=log.Mylog('log_diag.txt','error') mylog.logcfg() #定时任务....................................................................................................................................................................... scheduler = BlockingScheduler() scheduler.add_job(diag_cal, 'interval', seconds=60) try: scheduler.start() except Exception as e: scheduler.shutdown() print(repr(e)) mylog.logopt(e)