123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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 FeiShuData
- #...................................电池包电芯安全诊断函数......................................................................................................................
- def diag_cal():
- global SNnums
- global df_Diag_Ram
-
- start=time.time()
- end_time=datetime.datetime.now()
- start_time=end_time-datetime.timedelta(seconds=130)
- start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
- end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
- df_read_Yunw = FeiShuData.getFeiShuDATA()#运维表格数据
- df_read_Yunw.rename(columns={'电池编码':'product_id'},inplace=True)
- df_read_Yunw.rename(columns={'内容描述':'info'},inplace=True)
- df_read_Yunw.rename(columns={'发生时间':'start_time'},inplace=True)
- df_read_Yunw.rename(columns={'维修信息':'advice'},inplace=True)
- for sn in SNnums:
- print(sn)
- 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)
- print(sn)
- #读取原始数据库数据........................................................................................................................................................
- 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']
- #读取结果数据库数据........................................................................................................................................................
- 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'
- #电池诊断................................................................................................................................................................
- 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)
- #电池诊断................................................................................................................................................................
- CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
- df_Diag_Ram_sn = df_Diag_Ram.loc[df_Diag_Ram['product_id']==sn]#历史故障
- df_Diag_Ram_sn_else = pd.concat([df_Diag_Ram,df_Diag_Ram_sn,df_Diag_Ram_sn]).drop_duplicates(subset=['product_id','code','start_time','Batpos','info'],keep=False)#sn之外的故障
- CellFltInfo = df_Diag_Ram_sn.drop('Batpos',axis=1)
- df_Diag_Ram_fix = df_Diag_Ram.loc[df_Diag_Ram['Batpos'] == 1]
- df_Diag_Ram_unfix = df_Diag_Ram.loc[df_Diag_Ram['Batpos'] == 0]
- if not df_bms.empty:
- 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_add = pd.concat([CellFltInfo,df_Diag_Batdiag_update_xq,df_Diag_Batdiag_update])#重新计算的该SN下的故障
- df_Diag_Cal_Update_temp = df_Diag_Cal_Update_add.drop_duplicates(subset=['product_id','start_time','end_time','code','info'], keep='first', inplace=False, ignore_index=False)#去除相同故障
- df_Diag_cal_early_unfix = pd.DataFrame()
- df_sn_car_fix = pd.DataFrame()
- df_Diag_Cal_finish = pd.DataFrame()
- df_Diag_cal_early_fix = pd.DataFrame()
- if not df_Diag_Cal_Update_temp.empty:
- #------------------------------合并两者故障,并将同一sn号下的车辆故障放一起----------------------------------------------
- df_Diag_Cal_Update = df_Diag_Cal_Update_temp#替换上一行
- df_Diag_Cal_finish = df_Diag_Cal_Update.loc[df_Diag_Cal_Update['end_time'] != '0000-00-00 00:00:00']
- df_Diag_Cal_new = df_Diag_Cal_Update.loc[df_Diag_Cal_Update['end_time'] == '0000-00-00 00:00:00']
- df_Diag_Cal_finish['Batpos'] = 1
- df_Diag_Cal_new['Batpos'] = 0
- df_feishu_sta = df_read_Yunw.loc[(df_read_Yunw['product_id'] == sn)]#飞书中该sn车辆状态
- if df_feishu_sta.empty:
- df_Diag_cal_early_unfix = df_Diag_Cal_new#如果为新出故障,则直接记录在df_diag_frame中
- else:
- df_Diag_cal_later = df_Diag_Cal_new.loc[df_Diag_Cal_new['start_time'] > max(df_feishu_sta['start_time'])]#故障表中故障时间晚于飞书记录时间
- df_Diag_cal_early = pd.concat([df_Diag_Cal_new,df_Diag_cal_later,df_Diag_cal_later]).drop_duplicates(subset=['product_id','code','start_time'],keep=False)#故障表中故障时间早于飞书记录时间
- df_feishu_sta_latest = df_feishu_sta.loc[df_feishu_sta['start_time'] == max(df_feishu_sta['start_time'])]#飞书中该SN下的最新故障
- df_feishu_diag_unfix = (df_feishu_sta_latest['advice'] == '需正常返仓') | (df_feishu_sta_latest['advice'] == '需紧急返仓')
- df_sn_car_unfix = pd.DataFrame()
- if any(df_feishu_diag_unfix):
- df_Diag_cal_early_unfix = df_Diag_Cal_new
- else:
- df_Diag_cal_early_fix = df_Diag_cal_early
- df_Diag_cal_early_unfix = df_Diag_cal_later
- if not df_Diag_cal_early_fix.empty:
- df_Diag_cal_early_fix['Batpos'] = 1
- df_Diag_Ram_Update = pd.concat([df_Diag_cal_early_unfix,df_Diag_cal_early_fix,df_Diag_Cal_finish])
- df_Diag_Ram_Update.sort_values(by = ['start_time'], axis = 0, ascending=True,inplace=True)#对故障信息按照时间进行排序
- df_temp5 = pd.concat([df_Diag_Ram_Update,df_Diag_Ram_sn_else])
- df_Diag_Ram_sum = df_temp5.drop_duplicates(subset=['product_id','start_time','end_time','code','info'], keep='first', inplace=False, ignore_index=False)#去除相同故障
- df_tempnum = df_Diag_Ram_sum.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_Ram_sum.loc[df_Diag_Ram_sum['product_id'] == item]
- temp_data.sort_values(by = ['start_time'], axis = 0, ascending=True,inplace=True)#对故障信息按照时间进行排序
- df_temp1 = df_temp1.append(temp_data)
- df_temp2 = pd.merge(col2,df_Diag_Ram_sum,on=["product_id"])#单故障码数据筛选
- df_temp3 = pd.concat([df_temp1,df_temp2])#多故障及单故障合并
- df_temp4 = df_temp3.reset_index(drop=True)
- df_Diag_Ram = df_temp4
- df_Diag_Ram_fix = df_Diag_Ram.loc[df_Diag_Ram['Batpos'] == 1]
- df_Diag_Ram_unfix = df_Diag_Ram.loc[df_Diag_Ram['Batpos'] == 0]
- if len(df_Diag_Ram) > 0:
- df_Diag_Ram.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result.csv',index=False,encoding='GB18030')
- if len(df_Diag_Ram_fix) > 0:
- df_Diag_Ram_fix.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result_fix.csv',index=False,encoding='GB18030')
- if len(df_Diag_Ram_unfix) > 0:
- df_Diag_Ram_unfix.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problemm\result_unfix.csv',index=False,encoding='GB18030')
- end=time.time()
- print(end-start)
- #...............................................主函数.......................................................................................................................
- if __name__ == "__main__":
- global SNnums
-
- excelpath=r'D:\Work\Code_write\data_analyze_platform\USER\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 = ['MGMLXN750N21B5004'] #SNnums_6040
-
- mylog=log.Mylog('log_diag.txt','error')
- mylog.logcfg()
- #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
- result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result.csv',encoding='gbk')
-
- # df_Diag_Ram=result[result['end_time']=='0000-00-00 00:00:00']
- df_Diag_Ram=result#[result['Batpos'] == 0]#将故障依然存在的赋值
- print('----------------输入--------')
- print(df_Diag_Ram)
- print('-------计算中-----------')
- #定时任务.......................................................................................................................................................................
- scheduler = BlockingScheduler()
- scheduler.add_job(diag_cal, 'interval', seconds=120, id='diag_job')
- try:
- scheduler.start()
- except Exception as e:
- scheduler.shutdown()
- print(repr(e))
- mylog.logopt(e)
|