main.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import CBMSSafetyAlarm
  2. import datetime
  3. import pandas as pd
  4. import multiprocessing
  5. from LIB.BACKEND import DBManager, Log
  6. import time, datetime
  7. from apscheduler.schedulers.blocking import BlockingScheduler
  8. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  9. #...................................电池包电芯安全诊断函数......................................................................................................................
  10. def diag_cal(sn_list, df_diag_ram, df_bms_ram):
  11. start=time.time()
  12. now_time=datetime.datetime.now()
  13. start_time=now_time-datetime.timedelta(seconds=70)
  14. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  15. end_time=now_time.strftime('%Y-%m-%d %H:%M:%S')
  16. for sn in sn_list:
  17. if 'PK500' in sn:
  18. celltype=1 #6040三元电芯
  19. elif 'PK502' in sn:
  20. celltype=2 #4840三元电芯
  21. elif 'K504B' in sn:
  22. celltype=99 #60ah林磷酸铁锂电芯
  23. elif 'MGMLXN750' in sn:
  24. celltype=3 #力信50ah三元电芯
  25. elif 'MGMCLN750' or 'UD' in sn:
  26. celltype=4 #CATL 50ah三元电芯
  27. else:
  28. print('SN:{},未找到对应电池类型!!!'.format(sn))
  29. continue
  30. # sys.exit()
  31. #读取原始数据库数据........................................................................................................................................................
  32. dbManager = DBManager.DBManager()
  33. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  34. df_bms = df_data['bms']
  35. # print(df_bms)
  36. #电池诊断................................................................................................................................................................
  37. if not df_bms.empty:
  38. df_diag_ram_sn=df_diag_ram[df_diag_ram['product_id']==sn]
  39. df_bms_ram_sn=df_bms_ram[df_bms_ram['sn']==sn]
  40. if df_diag_ram_sn.empty:
  41. SafetyAlarm=CBMSSafetyAlarm.SafetyAlarm(sn,celltype,df_bms, df_bms_ram_sn)
  42. df_diag_res, df_bms_res=SafetyAlarm.diag()
  43. #更新bms的ram数据 和 diag的Ram数据
  44. sn_index=df_bms_ram.loc[df_bms_ram['sn']==sn].index
  45. df_bms_ram=df_bms_ram.drop(index=sn_index)
  46. df_bms_ram=df_bms_ram.append(df_bms_res)
  47. sn_index=df_diag_ram.loc[df_diag_ram['product_id']==sn].index
  48. df_diag_ram=df_diag_ram.drop(index=sn_index)
  49. df_diag_ram=df_diag_ram.append(df_diag_res)
  50. df_diag_ram.reset_index(inplace=True,drop=True) #重置索引
  51. #当前热失控故障写入数据库
  52. if not df_diag_res.empty:
  53. with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\06BatSafetyAlarm\热失控.txt','a') as file:
  54. file.write(str(tuple(df_diag_res.iloc[-1]))+'\n')
  55. #当前热失控已超过三天变为历史故障并写入数据库,并删除原有数据库中的当前故障和ram中的当前故障
  56. elif (now_time-df_bms_ram_sn.iloc[-1]['time']).total_seconds()>3*24*3600:
  57. df_diag_ram=df_diag_ram.drop(df_diag_ram['sn']==sn) #删除ram中的当前故障
  58. df_bms_ram_sn.iloc[-1]['end_time']=now_time
  59. with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\06BatSafetyAlarm\热失控.txt','a') as file:
  60. file.write(str(tuple(df_diag_res.iloc[-1]))+'\n')
  61. #故障处理........................................................................................................................................................
  62. end=time.time()
  63. print(end-start)
  64. # print(df_soh)
  65. #...................................................主进程...........................................................................................................
  66. def mainprocess():
  67. global SNnums
  68. global df_diag_ram
  69. global df_bms_ram
  70. process = 2
  71. pool = multiprocessing.Pool(processes = process)
  72. for i in range(process):
  73. sn_list = SNnums[i]
  74. pool.apply_async(diag_cal, (sn_list,df_diag_ram,df_bms_ram))
  75. pool.close()
  76. pool.join()
  77. #...............................................主函数起定时作用.......................................................................................................................
  78. if __name__ == "__main__":
  79. excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  80. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  81. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  82. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  83. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  84. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  85. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  86. SNnums_6060=SNdata_6060['SN号'].tolist()
  87. SNnums_6040=SNdata_6040['SN号'].tolist()
  88. SNnums_4840=SNdata_4840['SN号'].tolist()
  89. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  90. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  91. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  92. SNnums=[SNnums_L7255 + SNnums_C7255 + SNnums_U7255, SNnums_6040 + SNnums_4840 + SNnums_6060]
  93. # SNnums=['PK50201A000002201']
  94. mylog=log.Mylog('log_diag.txt','error')
  95. mylog.logcfg()
  96. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取...................................
  97. result=pd.read_excel(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\06BatSafetyAlarm\result.xlsx')
  98. df_diag_ram=result[(result['end_time']=='0000-00-00 00:00:00') & (result['code']==119)]
  99. df_bms_ram=pd.DataFrame(columns=['time', 'sn', 'packvolt', 'cellvolt', 'celltemp'])
  100. #定时任务.......................................................................................................................................................................
  101. scheduler = BlockingScheduler()
  102. scheduler.add_job(mainprocess, 'interval', seconds=60, id='diag_job')
  103. try:
  104. scheduler.start()
  105. except Exception as e:
  106. scheduler.shutdown()
  107. print(repr(e))
  108. mylog.logopt(e)