import pandas as pd import pymysql from LIB.BACKEND import DBManager, Log from apscheduler.schedulers.blocking import BlockingScheduler import time, datetime from sqlalchemy import create_engine from urllib import parse from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log import CBMSBatInterShort import CBMSBatUniform import VoltStray import CBMSSafetyWarning #电池热安全预警核心算法函数...................................................................................................................... def saftywarning_cal(): global SNnums global df_warning_ram global df_warning_ram1 global df_warning_ram2 global df_warning_ram3 global df_warning_ram4 global df_lfp_ram global df_lfp_ram1 global now_time # pd.set_option('display.width', 300) # 设置字符显示宽度 # pd.set_option('display.max_rows', None) # 设置显示最大行 # pd.set_option('display.max_columns', None) # 设置显示最大列,None为显示所有列 start=time.time() now_time=datetime.datetime.now() start_time=now_time-datetime.timedelta(hours=6) start_time1=now_time-datetime.timedelta(days=7) start_time2=now_time-datetime.timedelta(days=3) start_time3=now_time-datetime.timedelta(days=1) start_time=start_time.strftime('%Y-%m-%d %H:%M:%S') start_time1=start_time1.strftime('%Y-%m-%d %H:%M:%S') start_time2=start_time2.strftime('%Y-%m-%d %H:%M:%S') start_time3=start_time3.strftime('%Y-%m-%d %H:%M:%S') end_time=now_time.strftime('%Y-%m-%d %H:%M:%S') # now_time=now_time+datetime.timedelta(hours=6) # start_time=now_time-datetime.timedelta(hours=6) # start_time1=now_time-datetime.timedelta(days=7) # start_time2=now_time-datetime.timedelta(days=3) # start_time3=now_time-datetime.timedelta(days=1) # start_time=start_time.strftime('%Y-%m-%d %H:%M:%S') # start_time1=start_time1.strftime('%Y-%m-%d %H:%M:%S') # start_time2=start_time2.strftime('%Y-%m-%d %H:%M:%S') # start_time3=start_time3.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 user='qx_algo_rw' password='qx@123456' #读取故障结果库中code==110且end_time='0000-00-00 00:00:00'............................... db='safety_platform' mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db) cursor = mysql.cursor() param='start_time,end_time,product_id,code,level,info,advice' tablename='all_fault_info_copy' sql = "select %s from %s where end_time='0000-00-00 00:00:00' and level=4" %(param,tablename) cursor.execute(sql) res = cursor.fetchall() df_fault_ram= pd.DataFrame(res,columns=param.split(',')) db_res_engine1 = create_engine( "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format( user, parse.quote_plus(password), host, port, db )) db='qx_cas' db_res_engine2 = create_engine( "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format( user, parse.quote_plus(password), host, port, db )) for sn in SNnums: try: 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' in sn) or ('UD' in sn): celltype=4 #CATL 50ah三元电芯 elif 'TJMCL'in sn: celltype=100 #重卡换电 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'] df_bms=df_bms.dropna(axis=0,subset=['总电流[A]','SOH[%]','SOC[%]']) #去除有空值的行 df_bms.reset_index(inplace=True,drop=True) #重置索引 # print(df_bms) # df_bms.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\98Download\\'+sn+'_BMS_'+'.csv',encoding='GB18030') #读取结果数据库数据........................................................................................................................................................ db='qx_cas' mode=1 DBRead=DBDownload.DBDownload(host, port, db, user, password,mode) with DBRead as DBRead: df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename='cellstateestimation_soh', sn=sn, timename='time_sp', st=start_time, sp=end_time) df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellvolt_diff,cellmin_num,cellmax_num,cellvolt_rank', tablename='cellstateestimation_uniform_socvoltdiff', sn=sn, timename='time', st=start_time1, sp=end_time) if not df_bms.empty: #ram处理............................................................................................................... df_warning_ram_sn=df_warning_ram[df_warning_ram['sn']==sn] df_warning_ram_sn1=df_warning_ram1[df_warning_ram1['sn']==sn] df_warning_ram_sn2=df_warning_ram2[df_warning_ram2['sn']==sn] df_warning_ram_sn3=df_warning_ram3[df_warning_ram3['sn']==sn] df_warning_ram_sn4=df_warning_ram4[df_warning_ram4['sn']==sn] df_warning_ram_sn.reset_index(inplace=True,drop=True) #重置索引 df_warning_ram_sn1.reset_index(inplace=True,drop=True) #重置索引 df_warning_ram_sn2.reset_index(inplace=True,drop=True) #重置索引 df_warning_ram_sn3.reset_index(inplace=True,drop=True) #重置索引 df_warning_ram_sn4.reset_index(inplace=True,drop=True) #重置索引 if celltype>50 and (not df_lfp_ram.empty): df_lfp_ram_sn=df_lfp_ram[df_lfp_ram['sn']==sn] df_lfp_ram_sn.reset_index(inplace=True,drop=True) #重置索引 else: df_lfp_ram_sn=pd.DataFrame() df_lfp_ram=pd.DataFrame(columns=df_bms.columns.tolist()+['sn']) if celltype>50 and (not df_lfp_ram1.empty): df_lfp_ram_sn1=df_lfp_ram1[df_lfp_ram1['sn']==sn] df_lfp_ram_sn1.reset_index(inplace=True,drop=True) #重置索引 else: df_lfp_ram_sn1=pd.DataFrame() df_lfp_ram1=pd.DataFrame(columns=df_bms.columns.tolist()+['sn']) #内短路计算.................................................................................................................................................. BatShort=CBMSBatInterShort.BatInterShort(sn,celltype,df_bms,df_soh,df_warning_ram_sn,df_warning_ram_sn1,df_warning_ram_sn2,df_warning_ram_sn3,df_lfp_ram_sn) df_short_res, df_ram_res, df_ram_res1, df_ram_res2, df_ram_res3, df_ram_res5=BatShort.intershort() if not df_short_res.empty: df_short_res.to_sql("cellstateestimation_intershort",con=db_res_engine2, if_exists="append",index=False) #静置电压排名.................................................................................................................................................. BatUniform=CBMSBatUniform.BatUniform(sn,celltype,df_bms,df_uniform,df_ram_res3,df_lfp_ram_sn1) df_rank_res, df_ram_res3, df_ram_res6=BatUniform.batuniform() if not df_rank_res.empty: df_uniform=df_rank_res df_uniform.to_sql("cellstateestimation_uniform_socvoltdiff",con=db_res_engine2, if_exists="append",index=False) #电压离群..................................................................................................................................................... df_voltsigma,df_ram_res4=VoltStray.main(sn,df_bms,df_soh,celltype,df_warning_ram_sn4) if not df_voltsigma.empty: df_voltsigma.to_sql("outlier_voltchangeratio",con=db_res_engine2, if_exists="append",index=False) #ram处理................................................................................................................ df_warning_ram=df_warning_ram.drop(df_warning_ram[df_warning_ram.sn==sn].index) df_warning_ram1=df_warning_ram1.drop(df_warning_ram1[df_warning_ram1.sn==sn].index) df_warning_ram2=df_warning_ram2.drop(df_warning_ram2[df_warning_ram2.sn==sn].index) df_warning_ram3=df_warning_ram3.drop(df_warning_ram3[df_warning_ram3.sn==sn].index) df_warning_ram4=df_warning_ram4.drop(df_warning_ram4[df_warning_ram4.sn==sn].index) df_warning_ram=pd.concat([df_warning_ram,df_ram_res],ignore_index=True) df_warning_ram1=pd.concat([df_warning_ram1,df_ram_res1],ignore_index=True) df_warning_ram2=pd.concat([df_warning_ram2,df_ram_res2],ignore_index=True) df_warning_ram3=pd.concat([df_warning_ram3,df_ram_res3],ignore_index=True) df_warning_ram4=pd.concat([df_warning_ram4,df_ram_res4],ignore_index=True) if celltype>50: df_lfp_ram=df_lfp_ram.drop(df_lfp_ram[df_lfp_ram.sn==sn].index) df_lfp_ram=pd.concat([df_lfp_ram,df_ram_res5],ignore_index=True) df_lfp_ram1=df_lfp_ram1.drop(df_lfp_ram1[df_lfp_ram1.sn==sn].index) df_lfp_ram1=pd.concat([df_lfp_ram1,df_ram_res6],ignore_index=True) else: df_voltsigma=pd.DataFrame() #电池热安全预警.............................................................................................................................................................. #读取内短路、析锂和一致性结果数据库数据 db='qx_cas' mode=2 tablename1='cellstateestimation_intershort' tablename3='cellstateestimation_uniform_socvoltdiff' tablename4='outlier_voltchangeratio' #电压离群表单 DBRead=DBDownload.DBDownload(host, port, db, user, password,mode) with DBRead as DBRead: df_short=DBRead.getdata('time_sp,sn,short_current', tablename=tablename1, sn=sn, timename='time_sp', st=start_time1, sp=end_time) # df_liplated=DBRead.getdata('time,sn,liplated,liplated_amount', tablename=tablename2, sn=sn, timename='time', st=start_time2, sp=end_time) df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellvolt_diff,cellmin_num,cellmax_num,cellvolt_rank', tablename=tablename3, sn=sn, timename='time', st=start_time2, sp=end_time) df_voltsigma=DBRead.getdata('time,sn,VolOl_Uni,VolChng_Uni', tablename=tablename4, sn=sn, timename='time', st=start_time3, sp=end_time) df_uniform=df_uniform.dropna(axis=0,how='any') #获取sn的故障RAM df_fault_ram_sn=df_fault_ram[df_fault_ram['product_id']==sn] df_fault_ram_sn.reset_index(inplace=True,drop=True) #重置索引 #热安全预警 BatWarning=CBMSSafetyWarning.SafetyWarning(sn,celltype,df_short,df_uniform,df_voltsigma,df_soh,df_fault_ram_sn) df_warning_res=BatWarning.diag() if not df_warning_res.empty: df_warning_new = pd.concat([df_warning_res,df_fault_ram_sn,df_fault_ram_sn]).drop_duplicates(subset=['start_time','code'],keep=False)#此次判断中新增故障 df_warning_new.reset_index(inplace=True,drop=True) #重置索引 df_warning_end=pd.concat([df_warning_res,df_warning_new,df_warning_new]).drop_duplicates(subset=['start_time','code'],keep=False)#此次判断中新增故障 df_warning_end=df_warning_end[df_warning_end['end_time'] != '0000-00-00 00:00:00'] df_warning_end.reset_index(inplace=True,drop=True) #重置索引 #新增热失控预警写入数据库 if not df_warning_new.empty: df_warning_new.to_sql("all_fault_info_copy",con=db_res_engine1, if_exists="append",index=False) #结束热失控预警更新数据库 if not df_warning_end.empty: for j in range(len(df_warning_end)): cursor.execute(''' update all_fault_info_copy set update_time='{}',end_time='{}', Batpos={} where product_id='{}' and end_time='0000-00-00 00:00:00' and code='{}' '''.format(datetime.datetime.now(), df_warning_end.loc[j,'end_time'], 1 ,sn, df_warning_end.loc[j,'code'])) mysql.commit() end=time.time() print(end-start) except Exception as e: print(repr(e)) mylog.logopt(sn+str(e)) mysql.close() cursor.close() #...............................................主函数起定时作用....................................................................................................................... if __name__ == "__main__": excelpath=r'D:\Develop\User\Songpengfei\data_analyze_platform\WORK\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_6060 + SNnums_6040 + SNnums_4840 + SNnums_L7255 + SNnums_C7255 + SNnums_U7255 # SNnums=['MGMCLN750N215N044'] mylog=log.Mylog('log_warning.txt','error') mylog.logcfg() #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................................... df_warning_ram=pd.DataFrame(columns=['sn','time','deltsoc','cellsoc']) df_warning_ram1=pd.DataFrame(columns=['sn','time1','deltsoc1']) df_warning_ram2=pd.DataFrame(columns=['sn','time2','deltAs2']) df_warning_ram3=pd.DataFrame(columns=['sn','time3','standingtime','standingtime1','standingtime2']) df_warning_ram4=pd.DataFrame(columns=['sn','time4','cellsoc']) df_lfp_ram=pd.DataFrame() df_lfp_ram1=pd.DataFrame() now_time='2022-2-15 10:00:56' now_time=datetime.datetime.strptime(now_time,'%Y-%m-%d %H:%M:%S') #定时任务....................................................................................................................................................................... saftywarning_cal() scheduler = BlockingScheduler() scheduler.add_job(saftywarning_cal, 'interval', hours=6) try: scheduler.start() except Exception as e: scheduler.shutdown() print(repr(e)) mylog.logopt(e)