main.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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):
  12. # start=time.time()
  13. now_time=datetime.datetime.now()
  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==119且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=119 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. if df_diag_ram_sn.empty:
  58. if not df_bms.empty:
  59. SafetyAlarm=CBMSSafetyAlarm.SafetyAlarm(sn,celltype,df_bms, df_bms_ram_sn)
  60. df_diag_res, df_bms_res=SafetyAlarm.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. #当前热失控故障写入数据库
  66. if not df_diag_res.empty:
  67. with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\06BatSafetyAlarm\热失控报警.txt','a') as file:
  68. file.write(str(tuple(df_diag_res.iloc[-1]))+'\n')
  69. #当前热失控已超过一天变为历史故障并更改数据库
  70. else:
  71. fault_time=datetime.datetime.strptime(df_diag_ram_sn.iloc[-1]['start_time'], '%Y-%m-%d %H:%M:%S')
  72. if (now_time-fault_time).total_seconds()>24*3600:
  73. df_diag_ram_sn['end_time']=end_time
  74. df_diag_ram_sn['Batpos']=1
  75. with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\06BatSafetyAlarm\热失控报警.txt','a') as file:
  76. file.write(str(tuple(df_diag_ram_sn.iloc[-1]))+'\n')
  77. # end=time.time()
  78. # print(end-start)
  79. #...................................................主进程...........................................................................................................
  80. def mainprocess():
  81. global SNnums
  82. global df_bms_ram
  83. process = 2
  84. pool = multiprocessing.Pool(processes = process)
  85. for i in range(process):
  86. sn_list = SNnums[i]
  87. pool.apply_async(diag_cal, (sn_list,df_bms_ram))
  88. pool.close()
  89. pool.join()
  90. #...............................................主函数起定时作用.......................................................................................................................
  91. if __name__ == "__main__":
  92. excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  93. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  94. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  95. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  96. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  97. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  98. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  99. SNnums_6060=SNdata_6060['SN号'].tolist()
  100. SNnums_6040=SNdata_6040['SN号'].tolist()
  101. SNnums_4840=SNdata_4840['SN号'].tolist()
  102. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  103. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  104. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  105. SNnums=[SNnums_L7255 + SNnums_C7255 + SNnums_U7255, SNnums_6040 + SNnums_4840 + SNnums_6060]
  106. SNnums=[[], ['PK504B10100004447']]
  107. mylog=log.Mylog('log_diag.txt','error')
  108. mylog.logcfg()
  109. #参数初始化
  110. df_bms_ram=pd.DataFrame(columns=['time', 'sn', 'packvolt', 'cellvolt', 'celltemp'])
  111. #定时任务.......................................................................................................................................................................
  112. scheduler = BlockingScheduler()
  113. scheduler.add_job(mainprocess, 'interval', seconds=10, id='diag_job')
  114. try:
  115. scheduler.start()
  116. except Exception as e:
  117. scheduler.shutdown()
  118. print(repr(e))
  119. mylog.logopt(e)