#from test.spf.BatDiag import CBMSBatDiag, Log import CBMSBatDiag import QX_BatteryParam from SC_SamplingSafty import SamplingSafty import datetime import pandas as pd from LIB.BACKEND import DBManager, Log from sqlalchemy import create_engine import time, datetime from apscheduler.schedulers.blocking import BlockingScheduler from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log from pandas.core.frame import DataFrame import datacompy import GetFeiShuData #...................................电池包电芯安全诊断函数...................................................................................................................... def diag_cal(): global SNnums global df_Diag_Ram start=time.time() end_time=datetime.datetime.now() start_time=end_time-datetime.timedelta(seconds=120) start_time=start_time.strftime('%Y-%m-%d %H:%M:%S') end_time=end_time.strftime('%Y-%m-%d %H:%M:%S') 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() param=QX_BatteryParam.BatteryInfo(celltype) # sn='PK50201A000002039' # celltype=2 # start_time='2021-05-02 09:12:26' # end_time='2021-06-03 19:12:26' # # df_bms= pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\01qixiang\98Download\\'+'BMS_'+sn+'.csv',encoding='GB18030') #读取原始数据库数据........................................................................................................................................................ 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'] df_bms.to_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\\''BMS_'+sn+'.csv',encoding='GB18030') #读取结果数据库数据........................................................................................................................................................ host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port=3306 db='qx_cas' user='qx_read' password='Qx@123456' 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) #电池诊断................................................................................................................................................................ #BatDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform) #df_res=BatDiag.diag() #df_Diag_Ram_old=df_Diag_Ram df_Diag_Ram_Update=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice']) CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice']) if not df_bms.empty: CellFltInfo=df_Diag_Ram[df_Diag_Ram['product_id']==sn] df_Diag_Ram_Update=SamplingSafty.main(sn,param,df_bms,CellFltInfo)#学琦计算故障 BatDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform, CellFltInfo)#鹏飞计算 df_res=BatDiag.diag() df_Diag_Ram_Update=df_Diag_Ram_Update.append(df_res) #------------------------------合并两者故障,并将同一sn号下的车辆故障放一起---------------------------------------------- df_read_Yunw = GetFeiShuData.getFeiShuDATA()#运维表格数据 set_diff_df = pd.concat([df_Diag_Ram_Update,df_read_Yunw,df_read_Yunw]).drop_duplicates(subset=['product_id'],keep=False)#新增故障的sn,报出故障减去原文档中的sn same_sn = df_read_Yunw[(df_read_Yunw['维修信息']== '需正常返仓') | (df_read_Yunw['维修信息']== '需紧急返仓')]#筛选待修改和需返回车辆 set_same_df = df_Diag_Ram_Update.loc[df_Diag_Ram_Update['product_id'].isin(same_sn['product_id'])]#筛选待修改和需返回车辆 df_temp = set_diff_df.append(set_same_df)#新增及待改进车辆 set_same_df_else = pd.concat([df_Diag_Ram_Update,df_temp,df_temp]).drop_duplicates(subset=['product_id'],keep=False)#寻找旧sn中新增故障的sn #先取sn相同的车辆,然后对比时间,保留故障码时间晚于运营记录的信息 set_same_df_oth = pd.DataFrame() for i in set_same_df_else['product_id']: if max(df_Diag_Ram_Update.loc[df_Diag_Ram_Update['product_id'] == i]['start_time']) > max(df_read_Yunw.loc[df_read_Yunw['product_id'] == i]['发生时间']): temp_df = df_Diag_Ram_Update.loc[[df_Diag_Ram_Update['product_id'] == i]['start_time'] > max(df_read_Yunw.loc[df_read_Yunw['product_id'] == i]['发生时间'])]#故障时间晚于运维记录时间的sn故障 set_same_df_oth = set_same_df_oth.append(temp_df) else: pass df_diag_frame = pd.concat([set_diff_df,same_sn,set_same_df_oth])#筛选新增sn故障、维修信息依旧故障sn、旧sn中新增故障 df_tempnum = df_diag_frame.groupby(["product_id"]).size()#获取每个sn的故障总数 col1 = df_tempnum[df_tempnum>1].reset_index()[["product_id"]]#多故障sn号 col2 = df_tempnum[df_tempnum==1].reset_index()[["product_id"]]#单故障sn号 df_temp1 = pd.merge(col1,df_diag_frame,on=["product_id"])#多故障码数据筛选 df_temp1.sort_values(by = "start_time", axis = 0, ascending=True,inplace=True)#对故障信息按照sn号进行排序 df_temp2 = pd.merge(col2,df_diag_frame,on=["product_id"])#单故障码数据筛选 df_temp3 = pd.concat([df_temp1,df_temp2], ignore_index=True)#多故障及单故障合并 df_Diag_Ram_Update = df_temp3#计算故障信息 #-------------------------------故障-------------------------------------------- if not df_Diag_Ram_Update.empty: sn_index=df_Diag_Ram[df_Diag_Ram['product_id']==sn].index df_Diag_Ram=df_Diag_Ram.drop(index=sn_index) df_Diag_Ram=df_Diag_Ram.append(df_Diag_Ram_Update) df_Diag_Ram.reset_index(inplace=True,drop=True) Diag_Ram_Dif=datacompy.Compare(df_Diag_Ram_Update,CellFltInfo,join_columns=['product_id','end_time','code']) Diag_Ram_Dif=Diag_Ram_Dif.df1_unq_rows if len(Diag_Ram_Dif)>0: Diag_Ram_Dif_New=Diag_Ram_Dif[Diag_Ram_Dif['end_time']=='0000-00-00 00:00:00'] Diag_Ram_Dif_Finish=df_Diag_Ram[df_Diag_Ram['end_time']!='0000-00-00 00:00:00'] if len(Diag_Ram_Dif_New)>0: result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\result.csv',encoding='gbk') result=result.append(Diag_Ram_Dif_New) result.to_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\result.csv',index=False,encoding='GB18030') if len(Diag_Ram_Dif_Finish)>0: result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\result.csv',encoding='gbk') Diag_Ram_Dif_Finish=Diag_Ram_Dif_Finish.reset_index(drop=True) for i in range(0,len(Diag_Ram_Dif_Finish)): result.loc[result[result[result['product_id']==Diag_Ram_Dif_Finish.loc[i,'product_id']]['code']==Diag_Ram_Dif_Finish.loc[i,'code']].index,'end_time']=Diag_Ram_Dif_Finish.loc[i,'end_time'] result.to_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\result.csv',index=False,encoding='GB18030') end=time.time() print(end-start) # print(df_soh) #...................................电池包电芯安全诊断函数...................................................................................................................... def shortdiag_cal(): global SNnums global df_Diag_Ram start=time.time() end_time=datetime.datetime.now() start_time=end_time-datetime.timedelta(days=30) start_time=start_time.strftime('%Y-%m-%d %H:%M:%S') end_time=end_time.strftime('%Y-%m-%d %H:%M:%S') 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() #读取结果数据库数据........................................................................................................................................................ host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port=3306 db='qx_cas' user='qx_read' password='Qx@123456' mode=2 tablename4='cellstateestimation_intershort' DBRead=DBDownload.DBDownload(host, port, db, user, password, mode) #mode==1取数据库最后一行数据 with DBRead as DBRead: df_short=DBRead.getdata('time_sp,sn,short_current', tablename=tablename4, sn=sn, timename='time_sp', st=start_time, sp=end_time) #电池诊断................................................................................................................................................................ ShortDiag=CBMSBatDiag.ShortDiag(sn,celltype, df_short) df_res=ShortDiag.shortdiag() df_res.to_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\\'+'CBMS_diag_'+sn+'.csv',encoding='GB18030') print(df_res) end=time.time() print(end-start) # print(df_soh) #...............................................主函数....................................................................................................................... if __name__ == "__main__": excelpath=r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\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_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060 SNnums=['MGMCLN750N215I005','PK504B10100004341','PK504B00100004172','MGMLXN750N2189014'] #SNnums = SNnums_6060 mylog=log.Mylog('log_diag.txt','error') mylog.logcfg() #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................ result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\result.csv',encoding='gbk') df_Diag_Ram=result[result['end_time']=='0000-00-00 00:00:00'] print('----------------输入--------') print(df_Diag_Ram) print('-------done-----------') #定时任务....................................................................................................................................................................... scheduler = BlockingScheduler() scheduler.add_job(diag_cal, 'interval', seconds=120, id='diag_job') scheduler.add_job(shortdiag_cal, 'interval', days=7, id='shortdiag_job') try: scheduler.start() except Exception as e: scheduler.shutdown() print(repr(e)) mylog.logopt(e)