123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- import CBMSBatDiag
- import pymysql
- import datetime
- import pandas as pd
- from LIB.BACKEND import DBManager
- import time, datetime
- from apscheduler.schedulers.blocking import BlockingScheduler
- from USER.SPF.alibaba.Common import DBDownload
- from USER.SPF.alibaba.Common import log
- #...................................电池包电芯安全诊断函数......................................................................................................................
- def diag_cal():
- global SNnums
- global start_time
- global df_bms
- start=time.time()
- start_time=start_time+datetime.timedelta(seconds=55)
- end_time=start_time+datetime.timedelta(seconds=60)
- #数据库配置
- host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
- port=3306
- db='qx_cas'
- user='qx_read'
- password='Qx@123456'
- #读取故障结果库中的当前故障...............................
- # param='time_st,time_sp,sn,faultcode,faultlv,faultinfo,faultadvice'
- # tablename=''
- # mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
- # cursor = mysql.cursor()
- # sql = "select %s from %s where time_sp='0000-00-00 00:00:00'" %(param,tablename)
- # cursor.execute(sql)
- # res = cursor.fetchall()
- # df_diag_ram= pd.DataFram(res,columns=param.split(','))
- # cursor.close()
- # mysql.close()
- df_diag_ram=pd.DataFrame(columns=['time_st','time_sp','sn','faultcode','faultlv','faultinfo','faultadvice'])
- 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()
- celltype=2
- #读取原始数据库数据........................................................................................................................................................
- # 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_bms1=df_bms[(df_bms['time'] > start_time) & (df_bms['time'] < end_time)]
- df_bms1.reset_index(inplace=True,drop=True)
- print(df_bms1)
- #读取结果数据库数据........................................................................................................................................................
- 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)
- df_soc=pd.DataFrame()
- #电池诊断................................................................................................................................................................
- if not df_bms1.empty:
- df_diag_ram_sn=df_diag_ram[df_diag_ram['sn']==sn]
- batdiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms1,df_soh,df_soc,df_uniform,df_diag_ram_sn)
- df_diag_res, df_health_res=batdiag.diag() #获取电池故障结果和电池评分结果
- #电池评分写入数据库
- if not df_health_res.empty: #变为历史故障更改数据库
- with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\05BatDiag\电池评分.txt','a',encoding='utf8') as file:
- file.write(str(df_health_res)+'\n')
- #历史故障筛选并更改数据库故障结束时间.........................................................
- df_diag_end=df_diag_res[df_diag_res['time_sp'] != '0000-00-00 00:00:00']
- if not df_diag_end.empty: #变为历史故障更改数据库
- with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\05BatDiag\故障报警.txt','a',encoding='utf8') as file:
- file.write(str(df_diag_end)+'\n')
- #新增故障筛选并存入数据库.....................................................................
- df_diag_now=df_diag_res[df_diag_res['time_sp'] == '0000-00-00 00:00:00'] #去除历史故障
- df_diag_all=df_diag_now.append(df_diag_ram_sn)
- df_diag_new=df_diag_all.drop_duplicates(subset=['faultcode'],keep=False)
- if not df_diag_new.empty: #新增写入数据库
- with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\05BatDiag\故障报警.txt','a',encoding='utf8') as file:
- file.write(str(df_diag_new)+'\n')
- #更新diag的Ram数据
- df_diag_ram=df_diag_ram.drop(df_diag_ram[df_diag_ram.sn==sn].index)
- df_diag_ram=df_diag_ram.append(df_diag_now, ignore_index=True)
- df_diag_ram.reset_index(inplace=True,drop=True) #重置索引
- #健康度评分存入数据库
- end=time.time()
- print(end-start)
- #...............................................主函数起定时作用.......................................................................................................................
- if __name__ == "__main__":
-
- # excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\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_L7255 + SNnums_C7255 + SNnums_U7255, SNnums_6040 + SNnums_4840 + SNnums_6060
- SNnums=['HUATING']
-
- mylog=log.Mylog('log_diag.txt','error')
- mylog.logcfg()
- start_time=datetime.datetime.strptime('2021-10-29 14:27:40','%Y-%m-%d %H:%M:%S')
- df_bms= pd.read_excel(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\98Download\18650-故障诊断原始数据.xlsx')
- df_bms['time']=pd.to_datetime(df_bms['time'], format='%Y-%m-%d %H:%M:%S')
- #定时任务.......................................................................................................................................................................
- scheduler = BlockingScheduler()
- scheduler.add_job(diag_cal, 'interval', seconds=1)
- try:
- scheduler.start()
- except Exception as e:
- scheduler.shutdown()
- print(repr(e))
- mylog.logopt(e)
|