main.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import CBMSSafetyAlarm
  2. import pymysql
  3. import datetime
  4. import pandas as pd
  5. import multiprocessing
  6. from LIB.BACKEND import DBManager, Log
  7. import time, datetime
  8. from apscheduler.schedulers.blocking import BlockingScheduler
  9. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  10. #...................................电池包电芯安全诊断函数......................................................................................................................
  11. <<<<<<< HEAD
  12. def diag_cal(sn_list, df_bms_ram, df_alarm_ram,now_time):
  13. start=time.time()
  14. end_time=now_time
  15. start_time=now_time-datetime.timedelta(seconds=60)
  16. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  17. end_time=now_time.strftime('%Y-%m-%d %H:%M:%S')
  18. =======
  19. def diag_cal(sn_list, df_bms_ram, df_alarm_ram):
  20. start=time.time()
  21. now_time=datetime.datetime.now() #-datetime.timedelta(seconds=3600*24+3600*14.6)
  22. start_time=now_time-datetime.timedelta(seconds=180)
  23. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  24. end_time=now_time.strftime('%Y-%m-%d %H:%M:%S')
  25. start_time='2021-10-31 09:30:30'
  26. end_time='2021-10-31 09:31:30'
  27. >>>>>>> master
  28. #数据库配置
  29. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  30. port=3306
  31. user='qx_algo_readonly'
  32. password = 'qx@123456'
  33. #读取故障结果库中code=='C599'且end_time='0000-00-00 00:00:00'...............................
  34. db='safety_platform'
  35. mysql = pymysql.connect (host=host, port=port, user=user, password=password, database=db)
  36. cursor = mysql.cursor()
  37. param='start_time,end_time,product_id,code,level,info,advice'
  38. tablename='all_fault_info'
  39. sql = "select %s from %s where code='C599' and end_time='0000-00-00 00:00:00'" %(param,tablename)
  40. cursor.execute(sql)
  41. res = cursor.fetchall()
  42. df_diag_ram= pd.DataFrame(res,columns=param.split(','))
  43. cursor.close()
  44. mysql.close()
  45. for sn in sn_list:
  46. if 'PK500' in sn:
  47. celltype=1 #6040三元电芯
  48. elif 'PK502' in sn:
  49. celltype=2 #4840三元电芯
  50. elif 'K504B' in sn:
  51. celltype=99 #60ah林磷酸铁锂电芯
  52. elif 'MGMLXN750' in sn:
  53. celltype=3 #力信50ah三元电芯
  54. elif 'MGMCLN750' or 'UD' in sn:
  55. celltype=4 #CATL 50ah三元电芯
  56. else:
  57. print('SN:{},未找到对应电池类型!!!'.format(sn))
  58. continue
  59. # sys.exit()
  60. #读取原始数据库数据........................................................................................................................................................
  61. dbManager = DBManager.DBManager()
  62. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  63. df_bms = df_data['bms']
  64. # print(df_bms)
  65. #电池诊断................................................................................................................................................................
  66. df_diag_ram_sn=df_diag_ram[df_diag_ram['product_id']==sn]
  67. df_bms_ram_sn=df_bms_ram[df_bms_ram['sn']==sn]
  68. df_alarm_ram_sn=df_alarm_ram[df_alarm_ram['sn']==sn]
  69. if df_diag_ram_sn.empty:
  70. SafetyAlarm=CBMSSafetyAlarm.SafetyAlarm(sn,celltype,df_bms, df_bms_ram_sn, df_alarm_ram_sn)
  71. df_diag_res, df_bms_res, df_ram_res=SafetyAlarm.safety_alarm_diag()
  72. #更新bms的ram数据
  73. sn_index=df_bms_ram.loc[df_bms_ram['sn']==sn].index
  74. df_bms_ram=df_bms_ram.drop(index=sn_index)
  75. df_bms_ram=df_bms_ram.append(df_bms_res)
  76. sn_index=df_alarm_ram.loc[df_alarm_ram['sn']==sn].index
  77. df_alarm_ram=df_alarm_ram.drop(index=sn_index)
  78. df_alarm_ram=df_alarm_ram.append(df_ram_res)
  79. #当前热失控故障写入数据库
  80. if not df_diag_res.empty:
  81. with open(r'D:\Develop\User\Songpengfei\data_analyze_platform\WORK\01qixiang\06BatSafetyAlarm\热失控报警.txt','a') as file:
  82. file.write(str(tuple(df_diag_res.iloc[-1]))+'\n')
  83. #当前热失控已超过一天变为历史故障并更改数据库
  84. else:
  85. fault_time=datetime.datetime.strptime(df_diag_ram_sn.iloc[-1]['start_time'], '%Y-%m-%d %H:%M:%S')
  86. if (now_time-fault_time).total_seconds()>24*3600:
  87. df_diag_ram_sn['end_time']=end_time
  88. df_diag_ram_sn['Batpos']=1
  89. with open(r'D:\Develop\User\Songpengfei\data_analyze_platform\WORK\01qixiang\06BatSafetyAlarm\热失控报警.txt','a') as file:
  90. file.write(str(tuple(df_diag_ram_sn.iloc[-1]))+'\n')
  91. end=time.time()
  92. print(end-start)
  93. return df_bms_ram,df_alarm_ram
  94. #...................................................主进程...........................................................................................................
  95. def mainprocess():
  96. <<<<<<< HEAD
  97. global df_bms_ram1, df_bms_ram2, df_alarm_ram1, df_alarm_ram2, SNnums, now_time
  98. now_time=now_time+datetime.timedelta(seconds=60)
  99. =======
  100. global df_bms_ram1, df_bms_ram2, df_alarm_ram1, df_alarm_ram2, SNnums
  101. >>>>>>> master
  102. process = 2
  103. pool = multiprocessing.Pool(processes = process)
  104. res_list=[]
  105. ram_list1=[df_bms_ram1, df_bms_ram2]
  106. ram_list2=[df_alarm_ram1, df_alarm_ram1]
  107. for i in range(process):
  108. sn_list = SNnums[i]
  109. df_bms_ram=ram_list1[i]
  110. df_alarm_ram=ram_list2[i]
  111. <<<<<<< HEAD
  112. df_res=pool.apply_async(diag_cal, (sn_list,df_bms_ram,df_alarm_ram,now_time)).get()
  113. =======
  114. df_res=pool.apply_async(diag_cal, (sn_list,df_bms_ram,df_alarm_ram)).get()
  115. >>>>>>> master
  116. res_list.append(df_res)
  117. pool.close()
  118. pool.join()
  119. df_bms_ram1=res_list[0][0]
  120. df_bms_ram2=res_list[1][0]
  121. df_alarm_ram1=res_list[0][1]
  122. df_alarm_ram2=res_list[1][1]
  123. #...............................................主函数起定时作用.......................................................................................................................
  124. if __name__ == "__main__":
  125. <<<<<<< HEAD
  126. # excelpath=r'D:\Develop\User\Songpengfei\data_analyze_platform\WORK\01qixiang\sn-20210903.xlsx'
  127. # SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  128. # SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  129. # SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  130. # SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  131. # SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  132. # SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  133. # SNnums_6060=SNdata_6060['SN号'].tolist()
  134. # SNnums_6040=SNdata_6040['SN号'].tolist()
  135. # SNnums_4840=SNdata_4840['SN号'].tolist()
  136. # SNnums_L7255=SNdata_L7255['SN号'].tolist()
  137. # SNnums_C7255=SNdata_C7255['SN号'].tolist()
  138. # SNnums_U7255=SNdata_U7255['SN号'].tolist()
  139. # SNnums=[SNnums_L7255 + SNnums_C7255 + SNnums_U7255 + SNnums_4840, SNnums_6040 + SNnums_6060]
  140. SNnums=[['PK504B10100004487'], []]
  141. now_time=datetime.datetime.strptime('2022-5-24 19:49:00','%Y-%m-%d %H:%M:%S')
  142. =======
  143. excelpath=r'D:\Develop\User\Songpengfei\data_analyze_platform\WORK\01qixiang\sn-20210903.xlsx'
  144. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  145. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  146. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  147. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  148. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  149. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  150. SNnums_6060=SNdata_6060['SN号'].tolist()
  151. SNnums_6040=SNdata_6040['SN号'].tolist()
  152. SNnums_4840=SNdata_4840['SN号'].tolist()
  153. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  154. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  155. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  156. SNnums=[SNnums_L7255 + SNnums_C7255 + SNnums_U7255 + SNnums_4840, SNnums_6040 + SNnums_6060]
  157. SNnums=[['MGMCLN750N215N049','PK504B10100004328'], ['PK500A20100000752','PK504B10100004387']]
  158. >>>>>>> master
  159. mylog=log.Mylog('log_diag.txt','error')
  160. mylog.logcfg()
  161. #参数初始化
  162. df_bms_ram1=pd.DataFrame(columns=['time', 'sn', 'packvolt', 'cellvolt', 'celltemp'])
  163. df_bms_ram2=pd.DataFrame(columns=['time', 'sn', 'packvolt', 'cellvolt', 'celltemp'])
  164. df_alarm_ram1=pd.DataFrame(columns=['sn','time','safetywarning1','safetywarning2'])
  165. df_alarm_ram2=pd.DataFrame(columns=['sn','time','safetywarning1','safetywarning2'])
  166. mainprocess()
  167. #定时任务.......................................................................................................................................................................
  168. scheduler = BlockingScheduler()
  169. <<<<<<< HEAD
  170. scheduler.add_job(mainprocess, 'interval', seconds=0.1, id='diag_job')
  171. =======
  172. scheduler.add_job(mainprocess, 'interval', seconds=180, id='diag_job')
  173. >>>>>>> master
  174. try:
  175. scheduler.start()
  176. except Exception as e:
  177. scheduler.shutdown()
  178. print(repr(e))
  179. mylog.logopt(e)