main.py 10 KB

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