import CBMSBatDiag from SC_SamplingSafty import SamplingSafty from DataStatistics import DataSta from DiagDataMerge import DiagDataMerge from SC_CtrlSafty import CtrlSafty from SC_BMSUploadError import BMSReportError 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.CellStateEstimation.Common.V1_0_1 import BatParam as QX_BatteryParam from LIB.MIDDLE.SaftyCenter.Common import DBDownload as DBDw #...................................电池包电芯安全诊断函数...................................................................................................................... def diag_cal(): task_on=1 global SNnums global start #..................................设置时间.......................................................... start=time.time() end_time=datetime.datetime.now() start_time=end_time-datetime.timedelta(seconds=900)#10分钟跑一次,一次取10分钟数据 start_time=start_time.strftime('%Y-%m-%d %H:%M:%S') end_time=end_time.strftime('%Y-%m-%d %H:%M:%S') host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port=3306 db='safety_platform' user='qx_algo_readonly' password='qx@123456' mode=2 tablename2='all_fault_info_copy' 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='') df_Diag_Ram=df_Diag_Ram.dropna(axis=1,how='any') df_Diag_Ram=df_Diag_Ram.reset_index(drop=True) for sn in SNnums:#SN遍历 print(sn) start_time = '2021-12-30 18:00:04' end_time = '2021-12-30 18:15:04' if 'PK500' in sn: celltype=1 #6040三元电芯 FactoryType=1 elif 'PK502' in sn: celltype=2 #4840三元电芯 FactoryType=1 elif 'K504B' in sn: celltype=99 #60ah林磷酸铁锂电芯 FactoryType=1 elif 'MGMLXN750' in sn: celltype=3 #力信50ah三元电芯 FactoryType=3 elif ('MGMCLN750' or 'UD') in sn: celltype=4 #CATL 50ah三元电芯 FactoryType=2 elif 'TJMCL'in sn: celltype=100 #CATL 50ah三元电芯 FactoryType=0 else: print('SN:{},未找到对应电池类型!!!'.format(sn)) continue # sys.exit() param=QX_BatteryParam.BatParam(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'] df_bms=df_bms.dropna(subset=['总电流[A]']) df_bms=df_bms.reset_index(drop=True) print(df_bms) #读取结果数据库数据........................................................................................................................................................ host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port=3306 db='qx_cas' user='qx_algo_readonly' 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='')#取最后一条运营状态 errorcode_map=DataFrame() #电池诊断..................................................................................................................................................................... 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 = df_Diag_Ram.loc[df_Diag_Ram['product_id']!=sn]#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)#采样安全 df_Diag_Batdiag_update_xq2=CtrlSafty.main(sn,param,df_bms,df_Diag_Batdiag_update_xq)#控制安全 batDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform, df_Diag_Batdiag_update_xq2)#鹏飞计算 df_Diag_Batdiag_update2=batDiag.diag() df_Diag_Batdiag_update=BMSReportError.main(sn,df_bms,df_Diag_Batdiag_update2,FactoryType,errorcode_map) # 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_xq2=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos']) df_Diag_Batdiag_update2=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_add,df_Diag_Ram_Update=DiagDataMerge.DetaMerge(df_Diag_Ram_sn,df_Diag_Batdiag_update,df_OprtnSta,df_Diag_Ram_sn_else) task_on=0 #...............................................主函数....................................................................................................................... if __name__ == "__main__": global SNnums global task_on task_on=0 SNnums = ['PK504B10100004349'] mylog=log.Mylog('log.txt','error') #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................ host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port=3306 db='safety_platform' user='qx_algo_readonly' 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','factory',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') #定时任务....................................................................................................................................................................... scheduler = BlockingScheduler() diag_cal() # DaTa_Sta_Minutes_Task() # DaTa_Sta_Week_Task() # if task_on==0: # scheduler.add_job(DaTa_Sta_Week_Task, 'interval', days=7, id='Week_Task') # scheduler.add_job(diag_cal, 'interval', seconds=900, id='diag_job') # scheduler.add_job(DaTa_Sta_Minutes_Task, 'interval', seconds=180, id='Hour_Task') # try: # scheduler.start() # except Exception as e: # scheduler.shutdown() # print(repr(e)) # mylog.logopt(e)