main.py 7.3 KB

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