import CBMSBatDiag 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 as DBDownload from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log from pandas.core.frame import DataFrame import datacompy from LIB.MIDDLE.SaftyCenter.Common import QX_BatteryParam from LIB.MIDDLE.SaftyCenter.Common import DBDownload as DBDw from DataStatistics import DataSta from DiagDataMerge import DiagDataMerge #...................................电池包电芯安全诊断函数...................................................................................................................... def diag_cal(): global SNnums global df_Diag_Ram global start #..................................设置时间.......................................................... start=time.time() end_time=datetime.datetime.now() start_time=end_time-datetime.timedelta(seconds=900)#15分钟跑一次,一次取15分钟数据 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:#SN遍历 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) #读取原始数据库数据........................................................................................................................................................ 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) #读取电池当前运营状态..................................................................................................................................................... host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com' port=3306 db='qixiang_manage' user='qx_query' password='@Qx_query' mode=4 tablename1='py_battery' DBRead=DBDw.DBDownload(host, port, db, user, password,mode) with DBRead as DBRead: df_OprtnSta=DBRead.getdata('qrcode','status', tablename=tablename1, sn=sn, timename='', st='', sp='',factory='')#取最后一条运营状态 #电池诊断..................................................................................................................................................................... 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) #获取当前故障电池的历史故障信息↑↑↑↑↑↑↑↑↑↑↑....................................................................................................................................... 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_Batdiag_update=BatDiag.diag() else: df_Diag_Batdiag_update_xq=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos']) df_Diag_Batdiag_update=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos']) df_Diag_Ram,df_Diag_Ram_add,df_Diag_Ram_Update=DiagDataMerge.DetaMerge(df_Diag_Ram_sn,df_Diag_Batdiag_update_xq,df_Diag_Batdiag_update,df_OprtnSta,df_Diag_Ram_sn_else) df_Diag_Ram.to_csv('df_Diag_Ram.csv') df_Diag_Ram_add.to_csv('df_Diag_Ram_add.csv') df_Diag_Ram_Update.to_csv('df_Diag_Ram_add.csv') #.................................统计程序............................... def DaTa_Sta_Week_Task(): all_period_fault_info=DataFrame(columns=['factory','week','level1_count','level2_count','level3_count','level4_count','level5_count','solve_rate']) #............................获取数据................................ toweek='Week'+time.strftime('%W') host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port=3306 db='safety_platform' user='qx_read' password='Qx@123456' mode=2 tablename1='all_fault_info' DBRead=DBDw.DBDownload(host, port, db, user, password,mode) with DBRead as DBRead: df_fltinfo=DBRead.getdata('product_id','level','code','start_time','end_time','batpos',tablename=tablename1,factory='骑享',sn='',timename='',st='',sp='')#dbdownload经过了改编 #............................获取时间................................ end_time=datetime.datetime.now() # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d') start_time=end_time-datetime.timedelta(days=7) start_time=start_time.strftime('%Y-%m-%d') end_time=end_time.strftime('%Y-%m-%d') FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(df_fltinfo,start_time,end_time) FaultLvlCount=DataSta.WeekInfoSta(df_fltinfo,start_time,end_time) for i in range(1,6): if not FaultLvlCount[FaultLvlCount['level']==i]['product_id'].empty: all_period_fault_info.loc[0,'level'+str(i)+'_count']=int(FaultLvlCount[FaultLvlCount['level']==i]['product_id'].values) else: all_period_fault_info.loc[0,'level'+str(i)+'_count']=int(0) all_period_fault_info.loc[0,'factory']='骑享' all_period_fault_info.loc[0,'week']=toweek all_period_fault_info.loc[0,'solve_rate']=FltAlarmInfo.loc[0,'OprationManageRate'] all_period_fault_info.fillna(0,inplace=False) all_period_fault_info.to_csv('all_period_fault_info.csv') def DaTa_Sta_Minutes_Task(): #............................获取数据................................ host='172.16.121.236' port=3306 db='fastfun' user='readonly' password='Fast1234' mode=3 tablename1='ff_battery_accum' tablename2='ff_location' DBRead=DBDw.DBDownload(host, port, db, user, password,mode) with DBRead as DBRead: df_last_accum=DBRead.getdata('devcode','dsg_phaccum','dsg_ahaccum',tablename=tablename1,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编 df_last_pos=DBRead.getdata('devcode','latitude','longitude',tablename=tablename2,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编 host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port=3306 db='qx_cas' user='qx_read' password='Qx@123456' mode=3 tablename2='bat_first_data_time' DBRead=DBDw.DBDownload(host, port, db, user, password,mode) with DBRead as DBRead: df_FirstDataTime=DBRead.getdata('sn','first_data_time',tablename=tablename2,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编 host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port=3306 db='safety_platform' user='qx_read' password='Qx@123456' mode=2 tablename1='all_fault_info' DBRead=DBDw.DBDownload(host, port, db, user, password,mode) with DBRead as DBRead: df_fltinfo=DBRead.getdata('product_id','level','code','start_time','batpos',tablename=tablename1,factory='骑享',sn='',timename='',st='',sp='')#dbdownload经过了改编 #............................获取时间................................ end_time=datetime.datetime.now() # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d') start_time=end_time-datetime.timedelta(days=1) start_time=start_time.strftime('%Y-%m-%d') end_time=end_time.strftime('%Y-%m-%d') #............................执行程序................................ FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(df_fltinfo,start_time,end_time) SatftyCount=DataSta.SftyWrngClsfy(df_fltinfo) MaxAccumAh,TotalAccumAh,MaxCycle,MaxRunningHour,TotalRunHour=DataSta.AccumInfo(df_last_accum,df_FirstDataTime,end_time) df_Diag_Ram_Pos=DataSta.FltBatPosition(df_last_pos,df_Diag_Ram) all_statistic_info=DataFrame(columns=['factory','total_alarm','alarm_total_today','alarm_not_close_today','alarm_close_today','alarm_uregent_total_today','alarm_uregent_close_today','alarm_uregent_not_close_today','alarm_close_total','run_time_total','dischrg_total','odo_total','max_dischrg_one','max_runtime_one','max_cycle_one','max_odo_one','alarm_close_total','alarm_total','cell_type','cell_type_count','cell_safety_risk_count','data_safety_risk_count','status_safety_risk_count','hv_safety_risk_count','system_safety_risk_count','sample_safety_risk_count','controller_safety_risk_count','design_safety_risk_count']) all_statistic_info.loc[0,'factory']='骑享' all_statistic_info.loc[0,'total_alarm']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total'] all_statistic_info.loc[0,'alarm_total_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day'] all_statistic_info.loc[0,'alarm_close_today']=FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count'] all_statistic_info.loc[0,'alarm_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day']-FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count'] all_statistic_info.loc[0,'alarm_uregent_total_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day'] all_statistic_info.loc[0,'alarm_uregent_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count'] all_statistic_info.loc[0,'alarm_uregent_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day']-FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count'] all_statistic_info.loc[0,'run_time_total']=TotalRunHour all_statistic_info.loc[0,'dischrg_total']=TotalAccumAh all_statistic_info.loc[0,'odo_total']=0 all_statistic_info.loc[0,'max_dischrg_one']=MaxAccumAh all_statistic_info.loc[0,'max_runtime_one']=MaxRunningHour all_statistic_info.loc[0,'max_cycle_one']=MaxCycle all_statistic_info.loc[0,'max_odo_one']=0 all_statistic_info.loc[0,'alarm_close_total']=FltAlarmInfo.loc[0,'CS_Warning_Total_Finish_Count'] all_statistic_info.loc[0,'alarm_total']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total'] CellType=Celltype.columns.tolist() CellType=','.join(CellType) all_statistic_info.loc[0,'cell_type']=str(CellType) CellTypeCount=Celltype.loc[0].tolist() CellTypeCount=[str(x) for x in CellTypeCount] CellTypeCount=','.join(CellTypeCount) all_statistic_info.loc[0,'cell_type_count']=str(CellTypeCount) all_statistic_info.loc[0,'cell_safety_risk_count']=SatftyCount.loc[0,'CellSaftyCount'] all_statistic_info.loc[0,'data_safety_risk_count']=SatftyCount.loc[0,'DataSaftyCodeCount'] all_statistic_info.loc[0,'status_safety_risk_count']=SatftyCount.loc[0,'StateSaftyCodeCount'] all_statistic_info.loc[0,'hv_safety_risk_count']=SatftyCount.loc[0,'HvSaftyCodeCount'] all_statistic_info.loc[0,'system_safety_risk_count']=SatftyCount.loc[0,'SysSaftyCodeCount'] all_statistic_info.loc[0,'sample_safety_risk_count']=SatftyCount.loc[0,'SamplingSatyCount'] all_statistic_info.loc[0,'controller_safety_risk_count']=SatftyCount.loc[0,'CtrlSaftyCodeCount'] all_statistic_info.loc[0,'design_safety_risk_count']=SatftyCount.loc[0,'DsnSaftyCodeCount'] all_statistic_info.to_csv('all_statistic_info.csv') end=time.time() print(end-start) #...............................................主函数....................................................................................................................... if __name__ == "__main__": global SNnums excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\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的数据,需要从数据库中读取................ host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port=3306 db='safety_platform' user='qx_read' password='Qx@123456' mode=2 tablename2='all_fault_info' DBRead = DBDw.DBDownload(host, port, db, user, password,mode) with DBRead as DBRead: df_Diag_Ram = DBRead.getdata('start_time','end_time','product_id','code','level','info','advice','Batpos',tablename=tablename2,factory='骑享',sn='',timename='',st='',sp='') # result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result.csv',encoding='gbk') diag_cal() DaTa_Sta_Minutes_Task() DaTa_Sta_Week_Task() #定时任务....................................................................................................................................................................... scheduler = BlockingScheduler() scheduler.add_job(diag_cal, 'interval', seconds=900, id='diag_job') scheduler.add_job(DaTa_Sta_Week_Task, 'interval',start_date='2021-11-15 08:00:00', days=7, id='Week_Task') scheduler.add_job(DaTa_Sta_Minutes_Task, 'interval', seconds=180, id='Minutes_Task') try: scheduler.start() except Exception as e: scheduler.shutdown() print(repr(e)) mylog.logopt(e)