main.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import CBMSSafetyAlarm
  2. import pymysql
  3. from urllib import parse
  4. import datetime
  5. import pandas as pd
  6. import multiprocessing
  7. from LIB.BACKEND import DBManager, Log
  8. import time, datetime
  9. from apscheduler.schedulers.blocking import BlockingScheduler
  10. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  11. #...................................电池包电芯安全诊断函数......................................................................................................................
  12. def diag_cal(sn_list, df_bms_ram):
  13. start=time.time()
  14. now_time=datetime.datetime.now()
  15. start_time=now_time-datetime.timedelta(seconds=70)
  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. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  20. port=3306
  21. user='qx_algo_readonly'
  22. password = parse.quote_plus('qx@123456')
  23. #读取故障结果库中code==119且end_time='0000-00-00 00:00:00'...............................
  24. db='safety_platform'
  25. mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  26. cursor = mysql.cursor()
  27. param='start_time, end_time, product_id, code, level, info,advice'
  28. tablename='all_fault_info'
  29. sql = "select %s from %s where code=119 and end_time='0000-00-00 00:00:00'" %(param,tablename)
  30. cursor.execute(sql)
  31. res = cursor.fetchall()
  32. df_diag_ram= pd.DataFrame(res,columns=param.split(','))
  33. cursor.close()
  34. mysql.close()
  35. for sn in sn_list:
  36. if 'PK500' in sn:
  37. celltype=1 #6040三元电芯
  38. elif 'PK502' in sn:
  39. celltype=2 #4840三元电芯
  40. elif 'K504B' in sn:
  41. celltype=99 #60ah林磷酸铁锂电芯
  42. elif 'MGMLXN750' in sn:
  43. celltype=3 #力信50ah三元电芯
  44. elif 'MGMCLN750' or 'UD' in sn:
  45. celltype=4 #CATL 50ah三元电芯
  46. else:
  47. print('SN:{},未找到对应电池类型!!!'.format(sn))
  48. continue
  49. # sys.exit()
  50. #读取原始数据库数据........................................................................................................................................................
  51. dbManager = DBManager.DBManager()
  52. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  53. df_bms = df_data['bms']
  54. print(df_bms)
  55. #电池诊断................................................................................................................................................................
  56. if not df_bms.empty:
  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. if df_diag_ram_sn.empty:
  60. SafetyAlarm=CBMSSafetyAlarm.SafetyAlarm(sn,celltype,df_bms, df_bms_ram_sn)
  61. df_diag_res, df_bms_res=SafetyAlarm.diag()
  62. #更新bms的ram数据
  63. sn_index=df_bms_ram.loc[df_bms_ram['sn']==sn].index
  64. df_bms_ram=df_bms_ram.drop(index=sn_index)
  65. df_bms_ram=df_bms_ram.append(df_bms_res)
  66. #当前热失控故障写入数据库
  67. if not df_diag_res.empty:
  68. with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\06BatSafetyAlarm\热失控.txt','a') as file:
  69. file.write(str(tuple(df_diag_res.iloc[-1]))+'\n')
  70. #当前热失控已超过一天变为历史故障并更改数据库
  71. elif (now_time-df_diag_ram_sn.iloc[-1]['start_time']).total_seconds()>24*3600:
  72. df_diag_ram_sn.iloc[-1]['end_time']=now_time
  73. df_diag_ram_sn['Batpos']=1
  74. with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\06BatSafetyAlarm\热失控.txt','a') as file:
  75. file.write(str(tuple(df_diag_res.iloc[-1]))+'\n')
  76. #故障处理........................................................................................................................................................
  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=[[],['PK504B00100004161']]
  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=60, id='diag_job')
  114. try:
  115. scheduler.start()
  116. except Exception as e:
  117. scheduler.shutdown()
  118. print(repr(e))
  119. mylog.logopt(e)