main.py 7.3 KB

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