main.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import pandas as pd
  2. import pymysql
  3. from LIB.BACKEND import DBManager, Log
  4. from apscheduler.schedulers.blocking import BlockingScheduler
  5. import time, datetime
  6. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload
  7. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  8. import CBMSBatInterShort
  9. import CBMSSafetyWarning
  10. #电池热安全预警核心算法函数......................................................................................................................
  11. def saftywarning_cal():
  12. global SNnums
  13. global df_warning_ram
  14. global df_warning_ram1
  15. global df_warning_ram2
  16. global df_warning_ram3
  17. global df_lfp_ram
  18. now_time=datetime.datetime.now()
  19. start_time=now_time-datetime.timedelta(hours=12)
  20. start_time1=now_time-datetime.timedelta(days=7)
  21. start_time2=now_time-datetime.timedelta(days=90)
  22. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  23. start_time1=start_time1.strftime('%Y-%m-%d %H:%M:%S')
  24. end_time=now_time.strftime('%Y-%m-%d %H:%M:%S')
  25. #数据库配置
  26. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  27. port=3306
  28. user='qx_read'
  29. password='Qx@123456'
  30. #读取故障结果库中code==110且end_time='0000-00-00 00:00:00'...............................
  31. db='safety_platform'
  32. mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  33. cursor = mysql.cursor()
  34. param='start_time,end_time,product_id,code,level,info,advice'
  35. tablename='all_fault_info'
  36. sql = "select %s from %s where code=110 and end_time='0000-00-00 00:00:00'" %(param,tablename)
  37. cursor.execute(sql)
  38. res = cursor.fetchall()
  39. df_fault_ram= pd.DataFrame(res,columns=param.split(','))
  40. cursor.close()
  41. mysql.close()
  42. for sn in SNnums:
  43. if 'PK500' in sn:
  44. celltype=1 #6040三元电芯
  45. elif 'PK502' in sn:
  46. celltype=2 #4840三元电芯
  47. elif 'K504B' in sn:
  48. celltype=99 #60ah林磷酸铁锂电芯
  49. elif 'MGMLXN750' in sn:
  50. celltype=3 #力信50ah三元电芯
  51. elif 'MGMCLN750' or 'UD' in sn:
  52. celltype=4 #CATL 50ah三元电芯
  53. else:
  54. print('SN:{},未找到对应电池类型!!!'.format(sn))
  55. continue
  56. # sys.exit()
  57. #读取原始数据库数据........................................................................................................................................................
  58. dbManager = DBManager.DBManager()
  59. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  60. df_bms = df_data['bms']
  61. # tim=start_time.replace(':','_')
  62. # df_bms.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\98Download\\'+sn+'_BMS_'+tim+'.csv',encoding='GB18030')
  63. #读取结果数据库数据........................................................................................................................................................
  64. db='qx_cas'
  65. mode=1
  66. tablename='cellstateestimation_soh'
  67. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  68. with DBRead as DBRead:
  69. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename=tablename, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  70. if not df_bms.empty:
  71. #电池内短路计算................................................................................................................................................................
  72. df_warning_ram_sn=df_warning_ram[df_warning_ram['sn']==sn]
  73. df_warning_ram_sn1=df_warning_ram1[df_warning_ram1['sn']==sn]
  74. df_warning_ram_sn2=df_warning_ram2[df_warning_ram2['sn']==sn]
  75. df_warning_ram_sn3=df_warning_ram3[df_warning_ram3['sn']==sn]
  76. df_warning_ram_sn.reset_index(inplace=True,drop=True) #重置索引
  77. df_warning_ram_sn1.reset_index(inplace=True,drop=True) #重置索引
  78. df_warning_ram_sn2.reset_index(inplace=True,drop=True) #重置索引
  79. df_warning_ram_sn3.reset_index(inplace=True,drop=True) #重置索引
  80. if celltype>50:
  81. df_lfp_ram=df_lfp_ram.reindex(columns=df_bms.columns.tolist()+['sn'])
  82. df_lfp_ram_sn=df_lfp_ram[df_lfp_ram['sn']==sn]
  83. df_lfp_ram_sn.reset_index(inplace=True,drop=True) #重置索引
  84. else:
  85. df_lfp_ram_sn=pd.DataFrame()
  86. 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)
  87. df_short_res, df_ram_res, df_ram_res1, df_ram_res2, df_ram_res3, df_ram_res4=BatShort.intershort()
  88. if not df_short_res.empty:
  89. with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\07BatSafetyWarning\内短路.txt','a') as file:
  90. file.write(str(df_short_res)+'\n')
  91. #内短路ram处理.........................................................
  92. df_warning_ram=df_warning_ram.drop(df_warning_ram[df_warning_ram.sn==sn].index)
  93. df_warning_ram1=df_warning_ram1.drop(df_warning_ram1[df_warning_ram1.sn==sn].index)
  94. df_warning_ram2=df_warning_ram2.drop(df_warning_ram2[df_warning_ram2.sn==sn].index)
  95. df_warning_ram3=df_warning_ram3.drop(df_warning_ram3[df_warning_ram3.sn==sn].index)
  96. df_warning_ram=pd.concat([df_warning_ram,df_ram_res],ignore_index=True)
  97. df_warning_ram1=pd.concat([df_warning_ram1,df_ram_res1],ignore_index=True)
  98. df_warning_ram2=pd.concat([df_warning_ram2,df_ram_res2],ignore_index=True)
  99. df_warning_ram3=pd.concat([df_warning_ram3,df_ram_res3],ignore_index=True)
  100. if celltype>50:
  101. df_lfp_ram=df_lfp_ram.drop(df_lfp_ram[df_lfp_ram.sn==sn].index)
  102. df_lfp_ram=pd.concat([df_lfp_ram,df_ram_res4],ignore_index=True)
  103. #电池热安全预警..............................................................................................................................................................
  104. #读取内短路、析锂和一致性结果数据库数据
  105. db='qx_cas'
  106. mode=2
  107. tablename1='cellstateestimation_intershort'
  108. tablename2='' #析锂表单
  109. tablename3='cellstateestimation_uniform_socvoltdiff'
  110. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  111. with DBRead as DBRead:
  112. df_short=DBRead.getdata('time_sp,sn,short_current', tablename=tablename1, sn=sn, timename='time_sp', st=start_time1, sp=end_time)
  113. # df_liplated=DBRead.getdata('time,sn,liplated,lipltd_amount', tablename=tablename2, sn=sn, timename='time_sp', st=start_time2, sp=end_time)
  114. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellmin_num', tablename=tablename3, sn=sn, timename='time', st=start_time1, sp=end_time)
  115. df_liplated=pd.DataFrame()
  116. #获取sn的故障RAM
  117. df_fault_ram_sn=df_fault_ram[df_fault_ram['product_id']==sn]
  118. #热安全预警
  119. BatWarning=CBMSSafetyWarning.SafetyWarning(sn,celltype,df_short,df_liplated,df_uniform,df_fault_ram_sn)
  120. df_warning_res=BatWarning.diag()
  121. df_warning_end=df_warning_res[df_warning_res['end_time'] != '0000-00-00 00:00:00']
  122. if not df_warning_end.empty: #df_warning_end历史故障筛选并更改数据库故障结束时间
  123. pass
  124. elif df_fault_ram_sn.empty and (not df_warning_res.empty):
  125. with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\07BatSafetyWarning\热失控预警.txt','a') as file:
  126. file.write(str(tuple(df_warning_res.iloc[-1]))+'\n')
  127. else:
  128. pass
  129. #...............................................主函数起定时作用.......................................................................................................................
  130. if __name__ == "__main__":
  131. excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  132. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  133. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  134. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  135. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  136. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  137. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  138. SNnums_6060=SNdata_6060['SN号'].tolist()
  139. SNnums_6040=SNdata_6040['SN号'].tolist()
  140. SNnums_4840=SNdata_4840['SN号'].tolist()
  141. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  142. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  143. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  144. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_U7255, SNnums_6040 + SNnums_4840 + SNnums_6060
  145. SNnums=['MGMCLN750N215N099','PK504B10100004425']
  146. mylog=log.Mylog('log_warning.txt','error')
  147. mylog.logcfg()
  148. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取...................................
  149. df_warning_ram=pd.DataFrame(columns=['sn','time','deltsoc','cellsoc'])
  150. df_warning_ram1=pd.DataFrame(columns=['sn','time1','deltsoc1'])
  151. df_warning_ram2=pd.DataFrame(columns=['sn','time2','deltAs2'])
  152. df_warning_ram3=pd.DataFrame(columns=['sn','standingtime','standingtime1'])
  153. df_lfp_ram=pd.DataFrame()
  154. #定时任务.......................................................................................................................................................................
  155. scheduler = BlockingScheduler()
  156. scheduler.add_job(saftywarning_cal, 'interval', seconds=5)
  157. try:
  158. scheduler.start()
  159. except Exception as e:
  160. scheduler.shutdown()
  161. print(repr(e))
  162. mylog.logopt(e)