123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import CBMSSafetyAlarm
- import pymysql
- import datetime
- import pandas as pd
- import datetime
- from apscheduler.schedulers.blocking import BlockingScheduler
- from USER.SPF.alibaba.Common import log
- #...................................电池包电芯安全诊断函数......................................................................................................................
- def diag_cal():
- global SNnums
- global df_bms_ram, df_alarm_ram
- # start=time.time()
- now_time=datetime.datetime.now()
- start_time=now_time-datetime.timedelta(seconds=60)
- start_time=start_time.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_readonly'
- password = 'qx@123456'
- #读取故障结果库中code==119且end_time='0000-00-00 00:00:00'...............................
- db='safety_platform'
- mysql = pymysql.connect (host=host, port=port, user=user, password=password, database=db)
- cursor = mysql.cursor()
- param='time_st,time_sp,sn,faultcode,faultlv,faultinfo,faultadvice'
- tablename='all_fault_info'
- sql = "select %s from %s where faultcode=119 and time_sp='0000-00-00 00:00:00'" %(param,tablename)
- cursor.execute(sql)
- res = cursor.fetchall()
- df_diag_ram= pd.DataFrame(res,columns=param.split(','))
- cursor.close()
- mysql.close()
- 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()
- #读取原始数据库数据........................................................................................................................................................
- df_bms= pd.read_excel(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\98Download\力神40Ah-SOH原始数据.xlsx')
- # print(df_bms)
- #电池诊断................................................................................................................................................................
- df_diag_ram_sn=df_diag_ram[df_diag_ram['sn']==sn]
- df_bms_ram_sn=df_bms_ram[df_bms_ram['sn']==sn]
- df_alarm_ram_sn=df_alarm_ram[df_alarm_ram['sn']==sn]
-
- SafetyAlarm=CBMSSafetyAlarm.SafetyAlarm(sn,celltype,df_bms,df_diag_ram_sn, df_bms_ram_sn, df_alarm_ram_sn)
- df_diag_res, df_bms_res, df_ram_res=SafetyAlarm.safety_alarm_diag()
- #更新bms和结果ram的ram数据
- sn_index=df_bms_ram.loc[df_bms_ram['sn']==sn].index
- df_bms_ram=df_bms_ram.drop(index=sn_index)
- df_bms_ram=df_bms_ram.append(df_bms_res)
- sn_index=df_alarm_ram.loc[df_alarm_ram['sn']==sn].index
- df_alarm_ram=df_alarm_ram.drop(index=sn_index)
- df_alarm_ram=df_alarm_ram.append(df_ram_res)
- #当前热失控故障写入数据库
- if not df_diag_res.empty:
- with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\06BatSafetyAlarm\热失控报警.txt','a') as file:
- file.write(str(tuple(df_diag_res.iloc[-1]))+'\n')
-
-
- # 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
-
- mylog=log.Mylog('log_diag.txt','error')
- mylog.logcfg()
-
- #参数初始化
- df_bms_ram=pd.DataFrame(columns=['time', 'sn', 'packvolt', 'cellvolt', 'celltemp', 'packsoc', 'packcrnt'])
- df_alarm_ram=pd.DataFrame(columns=['sn','time','safetywarning1','safetywarning2'])
- #定时任务.......................................................................................................................................................................
- scheduler = BlockingScheduler()
- scheduler.add_job(diag_cal, 'interval', seconds=60, id='diag_job')
- try:
- scheduler.start()
- except Exception as e:
- scheduler.shutdown()
- print(repr(e))
- mylog.logopt(e)
|