main.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. pd.set_option('display.width', 300) # 设置字符显示宽度
  23. pd.set_option('display.max_rows', None) # 设置显示最大行
  24. pd.set_option('display.max_columns', None) # 设置显示最大列,None为显示所有列
  25. # start=time.time()
  26. now_time=datetime.datetime.now()
  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. #数据库配置
  37. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  38. port=3306
  39. user='qx_read'
  40. password='Qx@123456'
  41. #读取故障结果库中code==110且end_time='0000-00-00 00:00:00'...............................
  42. db='safety_platform'
  43. mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  44. cursor = mysql.cursor()
  45. param='start_time,end_time,product_id,code,level,info,advice'
  46. tablename='all_fault_info'
  47. sql = "select %s from %s where code=110 and end_time='0000-00-00 00:00:00'" %(param,tablename)
  48. cursor.execute(sql)
  49. res = cursor.fetchall()
  50. df_fault_ram= pd.DataFrame(res,columns=param.split(','))
  51. cursor.close()
  52. mysql.close()
  53. for sn in SNnums:
  54. if 'PK500' in sn:
  55. celltype=1 #6040三元电芯
  56. elif 'PK502' in sn:
  57. celltype=2 #4840三元电芯
  58. elif 'K504B' in sn:
  59. celltype=99 #60ah林磷酸铁锂电芯
  60. elif 'MGMLXN750' in sn:
  61. celltype=3 #力信50ah三元电芯
  62. elif 'MGMCLN750' or 'UD' in sn:
  63. celltype=4 #CATL 50ah三元电芯
  64. else:
  65. print('SN:{},未找到对应电池类型!!!'.format(sn))
  66. continue
  67. # sys.exit()
  68. #读取原始数据库数据........................................................................................................................................................
  69. dbManager = DBManager.DBManager()
  70. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  71. df_bms = df_data['bms']
  72. # print(df_bms)
  73. # df_bms.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\98Download\\'+sn+'_BMS_'+tim+'.csv',encoding='GB18030')
  74. #读取结果数据库数据........................................................................................................................................................
  75. db='qx_cas'
  76. mode=1
  77. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  78. with DBRead as DBRead:
  79. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename='cellstateestimation_soh', sn=sn, timename='time_sp', st=start_time, sp=end_time)
  80. 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)
  81. df_uniform.loc[0,'cellvolt_rank']=str([1]*20)
  82. df_voltsigma=pd.DataFrame()
  83. if not df_bms.empty:
  84. #ram处理...............................................................................................................
  85. df_warning_ram_sn=df_warning_ram[df_warning_ram['sn']==sn]
  86. df_warning_ram_sn1=df_warning_ram1[df_warning_ram1['sn']==sn]
  87. df_warning_ram_sn2=df_warning_ram2[df_warning_ram2['sn']==sn]
  88. df_warning_ram_sn3=df_warning_ram3[df_warning_ram3['sn']==sn]
  89. df_chrgvolt_ram_sn=df_chrgvolt_ram[df_chrgvolt_ram['sn']==sn]
  90. df_warning_ram_sn.reset_index(inplace=True,drop=True) #重置索引
  91. df_warning_ram_sn1.reset_index(inplace=True,drop=True) #重置索引
  92. df_warning_ram_sn2.reset_index(inplace=True,drop=True) #重置索引
  93. df_warning_ram_sn3.reset_index(inplace=True,drop=True) #重置索引
  94. df_chrgvolt_ram_sn.reset_index(inplace=True,drop=True)
  95. if celltype>50 and (not df_lfp_ram.empty):
  96. df_lfp_ram_sn=df_lfp_ram[df_lfp_ram['sn']==sn]
  97. df_lfp_ram_sn.reset_index(inplace=True,drop=True) #重置索引
  98. else:
  99. df_lfp_ram_sn=pd.DataFrame()
  100. df_lfp_ram=pd.DataFrame(columns=df_bms.columns.tolist()+['sn'])
  101. if celltype>50 and (not df_lfp_ram1.empty):
  102. df_lfp_ram_sn1=df_lfp_ram1[df_lfp_ram1['sn']==sn]
  103. df_lfp_ram_sn1.reset_index(inplace=True,drop=True) #重置索引
  104. else:
  105. df_lfp_ram_sn1=pd.DataFrame()
  106. df_lfp_ram1=pd.DataFrame(columns=df_bms.columns.tolist()+['sn'])
  107. #内短路计算..................................................................................................................................................
  108. 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)
  109. df_short_res, df_ram_res, df_ram_res1, df_ram_res2, df_ram_res3, df_ram_res4, df_chrgvolt_ram_sn=BatShort.intershort()
  110. if not df_short_res.empty:
  111. pass
  112. # with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\07BatSafetyWarning\内短路.txt','a') as file:
  113. # file.write(str(df_short_res)+'\n')
  114. #静置电压排名..................................................................................................................................................
  115. BatUniform=CBMSBatUniform.BatUniform(sn,celltype,df_bms,df_uniform,df_ram_res3,df_lfp_ram_sn1)
  116. df_rank_res, df_ram_res3, df_ram_res5=BatUniform.batuniform()
  117. if not df_rank_res.empty:
  118. df_uniform=df_rank_res
  119. # with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\07BatSafetyWarning\电压排名.txt','a') as file:
  120. # file.write(str(df_rank_res)+'\n')
  121. #电压离群.....................................................................................................................................................
  122. df_voltsigma=VoltStray.main(sn,df_bms,celltype)
  123. if not df_voltsigma.empty:
  124. pass
  125. # with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\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_chrgvolt_ram=df_chrgvolt_ram.drop(df_chrgvolt_ram[df_chrgvolt_ram.sn==sn].index)
  133. df_warning_ram=pd.concat([df_warning_ram,df_ram_res],ignore_index=True)
  134. df_warning_ram1=pd.concat([df_warning_ram1,df_ram_res1],ignore_index=True)
  135. df_warning_ram2=pd.concat([df_warning_ram2,df_ram_res2],ignore_index=True)
  136. df_warning_ram3=pd.concat([df_warning_ram3,df_ram_res3],ignore_index=True)
  137. df_chrgvolt_ram=pd.concat([df_chrgvolt_ram,df_chrgvolt_ram_sn],ignore_index=True)
  138. if celltype>50:
  139. df_lfp_ram=df_lfp_ram.drop(df_lfp_ram[df_lfp_ram.sn==sn].index)
  140. df_lfp_ram=pd.concat([df_lfp_ram,df_ram_res4],ignore_index=True)
  141. df_lfp_ram1=df_lfp_ram1.drop(df_lfp_ram1[df_lfp_ram1.sn==sn].index)
  142. df_lfp_ram1=pd.concat([df_lfp_ram1,df_ram_res5],ignore_index=True)
  143. #电池热安全预警..............................................................................................................................................................
  144. #读取内短路、析锂和一致性结果数据库数据
  145. db='qx_cas'
  146. mode=2
  147. tablename1='cellstateestimation_intershort'
  148. tablename3='cellstateestimation_uniform_socvoltdiff'
  149. tablename4='outlier_voltchangeratio' #电压离群表单
  150. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  151. with DBRead as DBRead:
  152. df_short=DBRead.getdata('time_sp,sn,method,short_current', tablename=tablename1, sn=sn, timename='time_sp', st=start_time1, sp=end_time)
  153. 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)
  154. df_voltsigma=DBRead.getdata('time,sn,VolOl_Uni,VolChng_Uni', tablename=tablename4, sn=sn, timename='time', st=start_time3, sp=end_time)
  155. #获取sn的故障RAM
  156. df_fault_ram_sn=df_fault_ram[df_fault_ram['product_id']==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:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\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:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\07BatSafetyWarning\热失控预警.txt','a',encoding="utf-8") as file:
  171. file.write(str(tuple(df_warning_res.iloc[-1]))+'\n')
  172. #...............................................主函数起定时作用.......................................................................................................................
  173. if __name__ == "__main__":
  174. # excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  175. # SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  176. # SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  177. # SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  178. # SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  179. # SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  180. # SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  181. # SNnums_6060=SNdata_6060['SN号'].tolist()
  182. # SNnums_6040=SNdata_6040['SN号'].tolist()
  183. # SNnums_4840=SNdata_4840['SN号'].tolist()
  184. # SNnums_L7255=SNdata_L7255['SN号'].tolist()
  185. # SNnums_C7255=SNdata_C7255['SN号'].tolist()
  186. # SNnums_U7255=SNdata_U7255['SN号'].tolist()
  187. # SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_U7255 + SNnums_6040 + SNnums_4840 + SNnums_6060
  188. SNnums=['PK50001A100000577']
  189. # mylog=log.Mylog('log_warning.txt','error')
  190. # mylog.logcfg()
  191. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取...................................
  192. df_warning_ram=pd.DataFrame(columns=['sn','time','deltsoc','cellsoc'])
  193. df_warning_ram1=pd.DataFrame(columns=['sn','time1','deltsoc1'])
  194. df_warning_ram2=pd.DataFrame(columns=['sn','time2','deltAs2'])
  195. df_warning_ram3=pd.DataFrame(columns=['sn','time3','standingtime','standingtime1','standingtime2'])
  196. df_lfp_ram=pd.DataFrame()
  197. df_lfp_ram1=pd.DataFrame()
  198. df_chrgvolt_ram=pd.DataFrame(columns=['sn','time','deltvolt','packcrnt_avg','temp_avg'])
  199. #定时任务.......................................................................................................................................................................
  200. scheduler = BlockingScheduler()
  201. scheduler.add_job(saftywarning_cal, 'interval', seconds=6)
  202. try:
  203. scheduler.start()
  204. except Exception as e:
  205. scheduler.shutdown()
  206. print(repr(e))
  207. # mylog.logopt(e)