#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=60) 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.drop('Batpos',axis=1) 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_old[df_Diag_Ram_old['product_id']==sn]#历史故障 df_Diag_Batdiag_update_xq=SamplingSafty.main(sn,param,df_bms,CellFltInfo)#学琦计算故障 BatDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform, CellFltInfo)#鹏飞计算 df_Diag_Batdiag_update=BatDiag.diag() df_Diag_Cal_Update_temp=df_Diag_Batdiag_update_xq.append(df_Diag_Batdiag_update) if not df_Diag_Cal_Update_temp.empty: #------------------------------合并两者故障,并将同一sn号下的车辆故障放一起---------------------------------------------- df_Diag_Cal_Update = df_Diag_Cal_Update_temp.append(df_Diag_Ram_old) df_read_Yunw = GetFeiShuData.getFeiShuDATA()#运维表格数据 df_read_Yunw.rename(columns={'电池编码':'product_id'},inplace=True) set_diff_df = pd.concat([df_Diag_Cal_Update,df_read_Yunw,df_read_Yunw]).drop_duplicates(subset=['product_id','code','start_time'],keep=False)#新增故障的sn,报出故障减去原文档中的sn new_sn = set_diff_df['product_id'] same_sn = df_read_Yunw.loc[(df_read_Yunw['维修信息']== '需正常返仓') | (df_read_Yunw['维修信息']== '需紧急返仓')]['product_id']#筛选待修改和需返回车辆 #set_same_df = df_Diag_Cal_Update.loc[df_Diag_Cal_Update['product_id'].isin(same_sn['product_id'])]#筛选待修改和需返回车辆 need_fix_sn = df_Diag_Cal_Update.loc[df_Diag_Cal_Update['end_time'] == '0000-00-00 00:00:00']['product_id'] df_temp_sn = pd.concat([new_sn,same_sn,need_fix_sn])#新增及待改进车辆 df_diag_frame = df_Diag_Cal_Update.loc[df_Diag_Cal_Update['product_id'].isin(df_temp_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.DataFrame() if not col1.empty: for item in col1['product_id']: temp_data = df_diag_frame.loc[df_diag_frame['product_id'] == item] temp_data.sort_values(by = "start_time", axis = 0, ascending=True,inplace=True)#对故障信息按照时间进行排序 df_temp1.append(temp_data) df_temp2 = pd.merge(col2,df_diag_frame,on=["product_id"])#单故障码数据筛选 df_temp3 = pd.concat([df_temp1,df_temp2])#多故障及单故障合并 df_temp4 = df_temp3.reset_index(drop=True) #-------------------------------差集加入状态1-------------------------------- set_diff_df_add = pd.concat([df_Diag_Cal_Update,df_temp4,df_temp4]).drop_duplicates(subset=['product_id','code','start_time'],keep=False) set_diff_df_add['Batpos'] = 1 #--------------------------------交集加入状态0------------------------------ df_temp4['Batpos'] = 0 df_Diag_Ram_Update = df_temp4.append(set_diff_df)#计算故障信息 diag_temp = df_Diag_Ram_Update.reset_index(drop=True) df_Diag_Ram_Update = diag_temp[['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice','Batpos']] #-------------------------------故障-------------------------------------------- df_Diag_Ram = df_Diag_Ram_Update # 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(df_Diag_Ram)>0:#Diag_Ram_Dif Diag_Ram_Dif_New=df_Diag_Ram[df_Diag_Ram['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\01Screen_Problem\result.csv',encoding='gbk') result=result.append(Diag_Ram_Dif_New) result.to_csv(r'D:\Work\Code_write\data_analyze_platform\01Screen_Problem\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\01Screen_Problem\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)): aa_id = result['product_id']==Diag_Ram_Dif_Finish.loc[i,'product_id'] bb_code = result['code']==Diag_Ram_Dif_Finish.loc[i,'code'] result.loc[result.loc[aa_id & bb_code].index,'end_time'] = Diag_Ram_Dif_Finish.loc[i,'end_time'] # result.loc[result[result.loc[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\01Screen_Problem\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\01Screen_Problem\\'+'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\01Screen_Problem\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_6040 mylog=log.Mylog('log_diag.txt','error') mylog.logcfg() #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................ result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\01Screen_Problem\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=60, 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)