main.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import pandas as pd
  2. import pymysql
  3. from LIB.BACKEND import DBManager, Log
  4. from apscheduler.schedulers.blocking import BlockingScheduler
  5. import datetime
  6. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload
  7. # from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  8. from LIB.MIDDLE.CellStateEstimation.BatSafetyWarning.V1_0_1 import CBMSBatInterShort
  9. from LIB.MIDDLE.CellStateEstimation.BatSafetyWarning.V1_0_1 import CBMSBatUniform
  10. from LIB.MIDDLE.CellStateEstimation.BatSafetyWarning.V1_0_1 import VoltStray
  11. from LIB.MIDDLE.CellStateEstimation.BatSafetyWarning.V1_0_1 import CBMSSafetyWarning
  12. #电池热安全预警核心算法函数......................................................................................................................
  13. def saftywarning_cal():
  14. global SNnums
  15. global df_warning_ram
  16. global df_warning_ram1
  17. global df_warning_ram2
  18. global df_warning_ram3
  19. global df_lfp_ram
  20. global df_lfp_ram1
  21. global df_chrgvolt_ram
  22. global now_time
  23. pd.set_option('display.width', 300) # 设置字符显示宽度
  24. pd.set_option('display.max_rows', None) # 设置显示最大行
  25. pd.set_option('display.max_columns', None) # 设置显示最大列,None为显示所有列
  26. # start=time.time()
  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='C490' 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. for sn in SNnums:
  55. if 'PK500' in sn:
  56. celltype=1 #6040三元电芯
  57. elif 'PK502' in sn:
  58. celltype=2 #4840三元电芯
  59. elif 'K504B' in sn:
  60. celltype=99 #60ah林磷酸铁锂电芯
  61. elif 'MGMLXN750' in sn:
  62. celltype=3 #力信50ah三元电芯
  63. elif 'MGMCLN750' or 'UD' in sn:
  64. celltype=4 #CATL 50ah三元电芯
  65. else:
  66. print('SN:{},未找到对应电池类型!!!'.format(sn))
  67. continue
  68. # sys.exit()
  69. #读取原始数据库数据........................................................................................................................................................
  70. dbManager = DBManager.DBManager()
  71. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  72. df_bms = df_data['bms']
  73. # print(df_bms)
  74. # df_bms.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\98Download\\'+sn+'_BMS_'+tim+'.csv',encoding='GB18030')
  75. #读取结果数据库数据........................................................................................................................................................
  76. db='qx_cas'
  77. mode=1
  78. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  79. with DBRead as DBRead:
  80. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename='cellstateestimation_soh', sn=sn, timename='time_sp', st=start_time, sp=end_time)
  81. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellvolt_diff,cellmin_num,cellmax_num,cellvolt_rank', tablename='cellstateestimation_uniform_socvoltdiff', sn=sn, timename='time', st=start_time1, sp=end_time)
  82. df_uniform.loc[0,'cellvolt_rank']=str([1]*20)
  83. df_voltsigma=pd.DataFrame()
  84. if not df_bms.empty:
  85. #ram处理...............................................................................................................
  86. df_warning_ram_sn=df_warning_ram[df_warning_ram['sn']==sn]
  87. df_warning_ram_sn1=df_warning_ram1[df_warning_ram1['sn']==sn]
  88. df_warning_ram_sn2=df_warning_ram2[df_warning_ram2['sn']==sn]
  89. df_warning_ram_sn3=df_warning_ram3[df_warning_ram3['sn']==sn]
  90. df_chrgvolt_ram_sn=df_chrgvolt_ram[df_chrgvolt_ram['sn']==sn]
  91. df_warning_ram_sn.reset_index(inplace=True,drop=True) #重置索引
  92. df_warning_ram_sn1.reset_index(inplace=True,drop=True) #重置索引
  93. df_warning_ram_sn2.reset_index(inplace=True,drop=True) #重置索引
  94. df_warning_ram_sn3.reset_index(inplace=True,drop=True) #重置索引
  95. df_chrgvolt_ram_sn.reset_index(inplace=True,drop=True)
  96. if celltype>50 and (not df_lfp_ram.empty):
  97. df_lfp_ram_sn=df_lfp_ram[df_lfp_ram['sn']==sn]
  98. df_lfp_ram_sn.reset_index(inplace=True,drop=True) #重置索引
  99. else:
  100. df_lfp_ram_sn=pd.DataFrame()
  101. df_lfp_ram=pd.DataFrame(columns=df_bms.columns.tolist()+['sn'])
  102. if celltype>50 and (not df_lfp_ram1.empty):
  103. df_lfp_ram_sn1=df_lfp_ram1[df_lfp_ram1['sn']==sn]
  104. df_lfp_ram_sn1.reset_index(inplace=True,drop=True) #重置索引
  105. else:
  106. df_lfp_ram_sn1=pd.DataFrame()
  107. df_lfp_ram1=pd.DataFrame(columns=df_bms.columns.tolist()+['sn'])
  108. #内短路计算..................................................................................................................................................
  109. 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,df_chrgvolt_ram_sn)
  110. df_short_res, df_ram_res, df_ram_res1, df_ram_res2, df_ram_res3, df_ram_res4, df_chrgvolt_ram_sn=BatShort.intershort()
  111. if not df_short_res.empty:
  112. pass
  113. # with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\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:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\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. pass
  126. # with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\07BatSafetyWarning\电压离群.txt','a') as file:
  127. # file.write(str(df_voltsigma)+'\n')
  128. #ram处理................................................................................................................
  129. df_warning_ram=df_warning_ram.drop(df_warning_ram[df_warning_ram.sn==sn].index)
  130. df_warning_ram1=df_warning_ram1.drop(df_warning_ram1[df_warning_ram1.sn==sn].index)
  131. df_warning_ram2=df_warning_ram2.drop(df_warning_ram2[df_warning_ram2.sn==sn].index)
  132. df_warning_ram3=df_warning_ram3.drop(df_warning_ram3[df_warning_ram3.sn==sn].index)
  133. df_chrgvolt_ram=df_chrgvolt_ram.drop(df_chrgvolt_ram[df_chrgvolt_ram.sn==sn].index)
  134. df_warning_ram=pd.concat([df_warning_ram,df_ram_res],ignore_index=True)
  135. df_warning_ram1=pd.concat([df_warning_ram1,df_ram_res1],ignore_index=True)
  136. df_warning_ram2=pd.concat([df_warning_ram2,df_ram_res2],ignore_index=True)
  137. df_warning_ram3=pd.concat([df_warning_ram3,df_ram_res3],ignore_index=True)
  138. df_chrgvolt_ram=pd.concat([df_chrgvolt_ram,df_chrgvolt_ram_sn],ignore_index=True)
  139. if celltype>50:
  140. df_lfp_ram=df_lfp_ram.drop(df_lfp_ram[df_lfp_ram.sn==sn].index)
  141. df_lfp_ram=pd.concat([df_lfp_ram,df_ram_res4],ignore_index=True)
  142. df_lfp_ram1=df_lfp_ram1.drop(df_lfp_ram1[df_lfp_ram1.sn==sn].index)
  143. df_lfp_ram1=pd.concat([df_lfp_ram1,df_ram_res5],ignore_index=True)
  144. #电池热安全预警..............................................................................................................................................................
  145. #读取内短路、析锂和一致性结果数据库数据
  146. db='qx_cas'
  147. mode=2
  148. tablename1='cellstateestimation_intershort'
  149. tablename3='cellstateestimation_uniform_socvoltdiff'
  150. tablename4='outlier_voltchangeratio' #电压离群表单
  151. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  152. with DBRead as DBRead:
  153. df_short=DBRead.getdata('time_st,time_sp,sn,method,short_current', tablename=tablename1, sn=sn, timename='time_sp', st=start_time1, sp=end_time)
  154. 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)
  155. df_voltsigma=DBRead.getdata('time,sn,VolOl_Uni,VolChng_Uni', tablename=tablename4, sn=sn, timename='time', st=start_time3, sp=end_time)
  156. #获取sn的故障RAM
  157. df_fault_ram_sn=df_fault_ram[df_fault_ram['product_id']==sn]
  158. #热安全预警
  159. if df_fault_ram_sn.empty:
  160. BatWarning=CBMSSafetyWarning.SafetyWarning(sn,celltype,df_short,df_uniform,df_soh,df_fault_ram_sn)
  161. df_res_new, df_res_chg=BatWarning.diag()
  162. #当前热失控故障写入数据库
  163. if not df_res_new.empty:
  164. with open(r'热失控预警.txt','a',encoding="utf-8") as file:
  165. file.write(str(tuple(df_res_new.iloc[-1]))+'\n')
  166. if not df_res_chg.empty:
  167. with open(r'热失控预警.txt','a',encoding="utf-8") as file:
  168. file.write(str(tuple(df_res_chg.iloc[-1]))+'\n')
  169. # else:
  170. # fault_time=datetime.datetime.strptime(df_fault_ram_sn.iloc[-1]['start_time'], '%Y-%m-%d %H:%M:%S')
  171. # if (now_time-fault_time).total_seconds()>3*24*3600: #df_warning_end历史故障筛选并更改数据库故障结束时间
  172. # df_fault_ram_sn['end_time']=end_time
  173. # df_fault_ram_sn['Batpos']=1
  174. # with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\07BatSafetyWarning\热失控预警.txt','a',encoding="utf-8") as file:
  175. # file.write(str(tuple(df_res_new.iloc[-1]))+'\n')
  176. #...............................................主函数起定时作用.......................................................................................................................
  177. if __name__ == "__main__":
  178. SNnums=['PK50201A000002052']
  179. now_time='2022-11-22 14:00:00'
  180. now_time=datetime.datetime.strptime(now_time,'%Y-%m-%d %H:%M:%S')
  181. # mylog=log.Mylog('log_warning.txt','error')
  182. # mylog.logcfg()
  183. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取...................................
  184. df_warning_ram=pd.DataFrame(columns=['sn','time','deltsoc','cellsoc'])
  185. df_warning_ram1=pd.DataFrame(columns=['sn','time1','deltsoc1'])
  186. df_warning_ram2=pd.DataFrame(columns=['sn','time2','deltAs2'])
  187. df_warning_ram3=pd.DataFrame(columns=['sn','time3','standingtime','standingtime1','standingtime2'])
  188. df_lfp_ram=pd.DataFrame()
  189. df_lfp_ram1=pd.DataFrame()
  190. df_chrgvolt_ram=pd.DataFrame(columns=['sn','time','deltvolt','packcrnt_avg','temp_avg'])
  191. #定时任务.......................................................................................................................................................................
  192. scheduler = BlockingScheduler()
  193. scheduler.add_job(saftywarning_cal, 'interval', seconds=10)
  194. try:
  195. scheduler.start()
  196. except Exception as e:
  197. scheduler.shutdown()
  198. print(repr(e))
  199. # mylog.logopt(e)