main_test.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import pandas as pd
  2. import numpy as np
  3. import pymysql
  4. from LIB.BACKEND import DBManager, Log
  5. from apscheduler.schedulers.blocking import BlockingScheduler
  6. import time, datetime
  7. from USER.SPF.alibaba.Common import DBDownload
  8. from USER.SPF.alibaba.Common import log
  9. import CBMSBatInterShort
  10. import CBMSBatUniform
  11. import VoltStray
  12. import CBMSSafetyWarning
  13. #电池热安全预警核心算法函数......................................................................................................................
  14. def saftywarning_cal():
  15. global SNnums
  16. global df_warning_ram
  17. global df_warning_ram1
  18. global df_warning_ram2
  19. global df_warning_ram3
  20. global df_lfp_ram
  21. global df_lfp_ram1
  22. global now_time
  23. global df_data
  24. start=time.time()
  25. mylog=log.Mylog('log_warning.txt','error')
  26. mylog.logcfg()
  27. start_time=now_time-datetime.timedelta(hours=6)
  28. start_time1=now_time-datetime.timedelta(days=7)
  29. start_time2=now_time-datetime.timedelta(days=3)
  30. start_time3=now_time-datetime.timedelta(days=1)
  31. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  32. start_time1=start_time1.strftime('%Y-%m-%d %H:%M:%S')
  33. start_time2=start_time2.strftime('%Y-%m-%d %H:%M:%S')
  34. start_time3=start_time3.strftime('%Y-%m-%d %H:%M:%S')
  35. end_time=now_time.strftime('%Y-%m-%d %H:%M:%S')
  36. now_time=now_time+datetime.timedelta(hours=6)
  37. #数据库配置
  38. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  39. port=3306
  40. user='qx_read'
  41. password='Qx@123456'
  42. #读取故障结果库中code==110且end_time='0000-00-00 00:00:00'...............................
  43. # db='safety_platform'
  44. # mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  45. # cursor = mysql.cursor()
  46. # param='start_time,end_time,product_id,code,level,info,advice'
  47. # tablename='all_fault_info'
  48. # sql = "select %s from %s where code=110 and end_time='0000-00-00 00:00:00'" %(param,tablename)
  49. # cursor.execute(sql)
  50. # res = cursor.fetchall()
  51. # df_fault_ram= pd.DataFrame(res,columns=param.split(','))
  52. # cursor.close()
  53. # mysql.close()
  54. df_fault_ram=pd.DataFrame(columns=['time_st','time_sp','sn','faultcode','faultlv','faultinfo','faultadvice'])
  55. for sn in SNnums:
  56. try:
  57. if 'KPD' in sn:
  58. celltype=1
  59. elif 'LS' in sn:
  60. celltype=99
  61. else:
  62. celltype=2
  63. #读取原始数据库数据........................................................................................................................................................
  64. df_bms= df_data[(df_data['time']>=start_time) & (df_data['time']<end_time)]
  65. df_bms=df_bms.reset_index(drop=True)
  66. if not df_bms.empty:
  67. cellvolt_name=['CellVolt'+str(x) for x in range(1,73)]
  68. celltemp_name=['CellTemp'+str(x) for x in range(1,33)]
  69. df_volt=pd.DataFrame([x[0].split(",") for x in np.array(df_bms[['cell_volt']])])
  70. df_volt.columns = cellvolt_name
  71. df_volt=df_volt.astype('float')
  72. df_temp=pd.DataFrame([x[0].split(",") for x in np.array(df_bms[['cell_temp']])])
  73. df_temp.columns = celltemp_name
  74. df_temp=df_temp.astype('float')
  75. df_bms=pd.concat([df_bms,df_volt,df_temp],axis=1)
  76. df_bms.dropna(axis=0, inplace=True)
  77. df_bms=df_bms.reset_index(drop=True)
  78. #读取结果数据库数据........................................................................................................................................................
  79. # db='qx_cas'
  80. # mode=1
  81. # DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  82. # with DBRead as DBRead:
  83. # df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename='cellstateestimation_soh', sn=sn, timename='time_sp', st=start_time, sp=end_time)
  84. # df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellvolt_diff,cellmin_num,cellmax_num', tablename='cellstateestimation_uniform_socvoltdiff', sn=sn, timename='time', st=start_time1, sp=end_time)
  85. df_soh=pd.DataFrame()
  86. df_uniform=pd.DataFrame()
  87. if not df_bms.empty:
  88. #ram处理...............................................................................................................
  89. df_warning_ram_sn=df_warning_ram[df_warning_ram['sn']==sn]
  90. df_warning_ram_sn1=df_warning_ram1[df_warning_ram1['sn']==sn]
  91. df_warning_ram_sn2=df_warning_ram2[df_warning_ram2['sn']==sn]
  92. df_warning_ram_sn3=df_warning_ram3[df_warning_ram3['sn']==sn]
  93. df_warning_ram_sn.reset_index(inplace=True,drop=True) #重置索引
  94. df_warning_ram_sn1.reset_index(inplace=True,drop=True) #重置索引
  95. df_warning_ram_sn2.reset_index(inplace=True,drop=True) #重置索引
  96. df_warning_ram_sn3.reset_index(inplace=True,drop=True) #重置索引
  97. if celltype>50 and (not df_lfp_ram.empty):
  98. df_lfp_ram_sn=df_lfp_ram[df_lfp_ram['sn']==sn]
  99. df_lfp_ram_sn.reset_index(inplace=True,drop=True) #重置索引
  100. else:
  101. df_lfp_ram_sn=pd.DataFrame()
  102. df_lfp_ram=pd.DataFrame(columns=df_bms.columns.tolist())
  103. if celltype>50 and (not df_lfp_ram1.empty):
  104. df_lfp_ram_sn1=df_lfp_ram1[df_lfp_ram1['sn']==sn]
  105. df_lfp_ram_sn1.reset_index(inplace=True,drop=True) #重置索引
  106. else:
  107. df_lfp_ram_sn1=pd.DataFrame()
  108. df_lfp_ram1=pd.DataFrame(columns=df_bms.columns.tolist())
  109. #内短路计算..................................................................................................................................................
  110. BatShort=CBMSBatInterShort.BatInterShort(sn,celltype,df_bms,df_soh,df_warning_ram_sn,df_warning_ram_sn1,df_warning_ram_sn2,df_warning_ram_sn3,df_lfp_ram_sn)
  111. df_short_res, df_ram_res, df_ram_res1, df_ram_res2, df_ram_res3, df_ram_res4=BatShort.intershort()
  112. if not df_short_res.empty:
  113. with open(r'D:\01WorkSpace\python\data_analyze_platform\USER\SPF\alibaba\07BatSafetyWarning\内短路.txt','a') as file:
  114. file.write(str(df_short_res)+'\n')
  115. #静置电压排名..................................................................................................................................................
  116. BatUniform=CBMSBatUniform.BatUniform(sn,celltype,df_bms,df_uniform,df_ram_res3,df_lfp_ram_sn1)
  117. df_rank_res, df_ram_res3, df_ram_res5=BatUniform.batuniform()
  118. if not df_rank_res.empty:
  119. df_uniform=df_rank_res
  120. with open(r'D:\01WorkSpace\python\data_analyze_platform\USER\SPF\alibaba\07BatSafetyWarning\电压排名.txt','a') as file:
  121. file.write(str(df_rank_res)+'\n')
  122. #电压离群.....................................................................................................................................................
  123. df_voltsigma=VoltStray.main(sn,df_bms,celltype)
  124. if not df_voltsigma.empty:
  125. with open(r'D:\01WorkSpace\python\data_analyze_platform\USER\SPF\alibaba\07BatSafetyWarning\电压离群.txt','a') as file:
  126. file.write(str(df_voltsigma)+'\n')
  127. #ram处理................................................................................................................
  128. df_warning_ram=df_warning_ram.drop(df_warning_ram[df_warning_ram.sn==sn].index)
  129. df_warning_ram1=df_warning_ram1.drop(df_warning_ram1[df_warning_ram1.sn==sn].index)
  130. df_warning_ram2=df_warning_ram2.drop(df_warning_ram2[df_warning_ram2.sn==sn].index)
  131. df_warning_ram3=df_warning_ram3.drop(df_warning_ram3[df_warning_ram3.sn==sn].index)
  132. df_warning_ram=pd.concat([df_warning_ram,df_ram_res],ignore_index=True)
  133. df_warning_ram1=pd.concat([df_warning_ram1,df_ram_res1],ignore_index=True)
  134. df_warning_ram2=pd.concat([df_warning_ram2,df_ram_res2],ignore_index=True)
  135. df_warning_ram3=pd.concat([df_warning_ram3,df_ram_res3],ignore_index=True)
  136. if celltype>50:
  137. df_lfp_ram=df_lfp_ram.drop(df_lfp_ram[df_lfp_ram.sn==sn].index)
  138. df_lfp_ram=pd.concat([df_lfp_ram,df_ram_res4],ignore_index=True)
  139. df_lfp_ram1=df_lfp_ram1.drop(df_lfp_ram1[df_lfp_ram1.sn==sn].index)
  140. df_lfp_ram1=pd.concat([df_lfp_ram1,df_ram_res5],ignore_index=True)
  141. #电池热安全预警..............................................................................................................................................................
  142. #读取内短路、析锂和一致性结果数据库数据
  143. db='qx_cas'
  144. mode=2
  145. tablename1='cellstateestimation_intershort'
  146. tablename3='cellstateestimation_uniform_socvoltdiff'
  147. tablename4='outlier_voltchangeratio' #电压离群表单
  148. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  149. with DBRead as DBRead:
  150. df_short=DBRead.getdata('time_sp,sn,short_current', tablename=tablename1, sn=sn, timename='time_sp', st=start_time1, sp=end_time)
  151. # df_liplated=DBRead.getdata('time,sn,liplated,liplated_amount', tablename=tablename2, sn=sn, timename='time', st=start_time2, sp=end_time)
  152. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellvolt_diff,cellmin_num,cellmax_num,cellvolt_rank', tablename=tablename3, sn=sn, timename='time', st=start_time2, sp=end_time)
  153. df_voltsigma=DBRead.getdata('time,sn,VolOl_Uni,VolChng_Uni', tablename=tablename4, sn=sn, timename='time', st=start_time3, sp=end_time)
  154. df_uniform=df_uniform.dropna(axis=0,how='any')
  155. #获取sn的故障RAM
  156. df_fault_ram_sn=df_fault_ram[df_fault_ram['sn']==sn]
  157. #热安全预警
  158. if df_fault_ram_sn.empty:
  159. BatWarning=CBMSSafetyWarning.SafetyWarning(sn,celltype,df_short,df_uniform,df_voltsigma,df_soh)
  160. df_warning_res=BatWarning.diag()
  161. #当前热失控故障写入数据库
  162. if not df_warning_res.empty:
  163. with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\07BatSafetyWarning\热失控预警.txt','a',encoding="utf-8") as file:
  164. file.write(str(tuple(df_warning_res.iloc[-1]))+'\n')
  165. else:
  166. fault_time=datetime.datetime.strptime(df_fault_ram_sn.iloc[-1]['start_time'], '%Y-%m-%d %H:%M:%S')
  167. if (now_time-fault_time).total_seconds()>3*24*3600: #df_warning_end历史故障筛选并更改数据库故障结束时间
  168. df_fault_ram_sn['end_time']=end_time
  169. df_fault_ram_sn['Batpos']=1
  170. with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\07BatSafetyWarning\热失控预警.txt','a',encoding="utf-8") as file:
  171. file.write(str(tuple(df_warning_res.iloc[-1]))+'\n')
  172. end=time.time()
  173. print(end-start)
  174. except Exception as e:
  175. print(repr(e))
  176. mylog.logopt(e)
  177. #...............................................主函数起定时作用.......................................................................................................................
  178. if __name__ == "__main__":
  179. # excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  180. # SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  181. # SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  182. # SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  183. # SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  184. # SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  185. # SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  186. # SNnums_6060=SNdata_6060['SN号'].tolist()
  187. # SNnums_6040=SNdata_6040['SN号'].tolist()
  188. # SNnums_4840=SNdata_4840['SN号'].tolist()
  189. # SNnums_L7255=SNdata_L7255['SN号'].tolist()
  190. # SNnums_C7255=SNdata_C7255['SN号'].tolist()
  191. # SNnums_U7255=SNdata_U7255['SN号'].tolist()
  192. # SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_U7255 + SNnums_6040 + SNnums_4840 + SNnums_6060
  193. SNnums=['LS002']
  194. mylog=log.Mylog('log_warning.txt','error')
  195. mylog.logcfg()
  196. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取...................................
  197. df_warning_ram=pd.DataFrame(columns=['sn','time','deltsoc','cellsoc'])
  198. df_warning_ram1=pd.DataFrame(columns=['sn','time1','deltsoc1'])
  199. df_warning_ram2=pd.DataFrame(columns=['sn','time2','deltAs2'])
  200. df_warning_ram3=pd.DataFrame(columns=['sn','time3','standingtime','standingtime1','standingtime2'])
  201. df_lfp_ram=pd.DataFrame()
  202. df_lfp_ram1=pd.DataFrame()
  203. now_time=datetime.datetime.strptime('2022-4-26 18:00:00','%Y-%m-%d %H:%M:%S')
  204. df_data= pd.read_excel(r'D:\01WorkSpace\项目\Alibaba\data\原始数据_2022-01-10_2022-02-10\力神_LS002.xlsx')
  205. df_data['time']=pd.to_datetime(df_data['time'], format='%Y-%m-%d %H:%M:%S')
  206. #定时任务.......................................................................................................................................................................
  207. scheduler = BlockingScheduler()
  208. scheduler.add_job(saftywarning_cal, 'interval', seconds=5)
  209. try:
  210. scheduler.start()
  211. except Exception as e:
  212. scheduler.shutdown()
  213. print(repr(e))
  214. mylog.logopt(e)