main test.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import CBMSSafetyAlarm
  2. import pymysql
  3. import datetime
  4. import pandas as pd
  5. from LIB.BACKEND import DBManager, Log
  6. import datetime, time
  7. from apscheduler.schedulers.blocking import BlockingScheduler
  8. from USER.SPF.alibaba.Common import log
  9. #...................................电池包电芯安全诊断函数......................................................................................................................
  10. def diag_cal():
  11. global SNnums
  12. global df_bms_ram
  13. global start_time
  14. global df_bms
  15. start=time.time()
  16. start_time=start_time+datetime.timedelta(seconds=60)
  17. end_time=start_time+datetime.timedelta(seconds=60)
  18. now_time=datetime.datetime.now()
  19. #数据库配置
  20. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  21. port=3306
  22. user='qx_algo_readonly'
  23. password = 'qx@123456'
  24. #读取故障结果库中code==119且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=119 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 SNnums:
  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. celltype=2
  52. #读取原始数据库数据........................................................................................................................................................
  53. # df_bms= pd.read_excel(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\98Download\18650-故障诊断原始数据.xlsx')
  54. df_bms1=df_bms[(df_bms['time'] > start_time) & (df_bms['time'] < end_time)]
  55. df_bms1.reset_index(inplace=True,drop=True)
  56. print(df_bms1)
  57. #电池诊断................................................................................................................................................................
  58. df_diag_ram_sn=df_diag_ram[df_diag_ram['product_id']==sn]
  59. df_bms_ram_sn=df_bms_ram[df_bms_ram['sn']==sn]
  60. if df_diag_ram_sn.empty:
  61. if not df_bms1.empty:
  62. SafetyAlarm=CBMSSafetyAlarm.SafetyAlarm(sn,celltype,df_bms1, df_bms_ram_sn)
  63. df_diag_res, df_bms_res=SafetyAlarm.diag()
  64. #更新bms的ram数据
  65. sn_index=df_bms_ram.loc[df_bms_ram['sn']==sn].index
  66. df_bms_ram=df_bms_ram.drop(index=sn_index)
  67. df_bms_ram=df_bms_ram.append(df_bms_res)
  68. #当前热失控故障写入数据库
  69. if not df_diag_res.empty:
  70. with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\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:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\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. #...............................................主函数起定时作用.......................................................................................................................
  83. if __name__ == "__main__":
  84. # excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  85. # SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  86. # SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  87. # SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  88. # SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  89. # SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  90. # SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  91. # SNnums_6060=SNdata_6060['SN号'].tolist()
  92. # SNnums_6040=SNdata_6040['SN号'].tolist()
  93. # SNnums_4840=SNdata_4840['SN号'].tolist()
  94. # SNnums_L7255=SNdata_L7255['SN号'].tolist()
  95. # SNnums_C7255=SNdata_C7255['SN号'].tolist()
  96. # SNnums_U7255=SNdata_U7255['SN号'].tolist()
  97. # SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_U7255 + SNnums_6040 + SNnums_4840 + SNnums_6060
  98. SNnums=['HUATING']
  99. mylog=log.Mylog('log_alarm.txt','error')
  100. mylog.logcfg()
  101. #参数初始化
  102. df_bms_ram=pd.DataFrame(columns=['time', 'sn', 'packvolt', 'cellvolt', 'celltemp'])
  103. start_time=datetime.datetime.strptime('2021-10-29 18:10:24','%Y-%m-%d %H:%M:%S')
  104. df_bms= pd.read_excel(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\98Download\18650-故障诊断原始数据.xlsx')
  105. df_bms['time']=pd.to_datetime(df_bms['time'], format='%Y-%m-%d %H:%M:%S')
  106. #定时任务.......................................................................................................................................................................
  107. scheduler = BlockingScheduler()
  108. scheduler.add_job(diag_cal, 'interval', seconds=0.5, id='diag_job')
  109. try:
  110. scheduler.start()
  111. except Exception as e:
  112. scheduler.shutdown()
  113. print(repr(e))
  114. mylog.logopt(e)