main.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import CBMSSafetyAlarm
  2. import pymysql
  3. import datetime
  4. import pandas as pd
  5. import datetime
  6. from apscheduler.schedulers.blocking import BlockingScheduler
  7. from USER.SPF.alibaba.Common import log
  8. #...................................电池包电芯安全诊断函数......................................................................................................................
  9. def diag_cal():
  10. global SNnums
  11. global df_bms_ram, df_alarm_ram
  12. # start=time.time()
  13. now_time=datetime.datetime.now()
  14. start_time=now_time-datetime.timedelta(seconds=60)
  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='time_st,time_sp,sn,faultcode,faultlv,faultinfo,faultadvice'
  27. tablename='all_fault_info'
  28. sql = "select %s from %s where faultcode=119 and time_sp='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 SNnums:
  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. df_bms= pd.read_excel(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\98Download\力神40Ah-SOH原始数据.xlsx')
  51. # print(df_bms)
  52. #电池诊断................................................................................................................................................................
  53. df_diag_ram_sn=df_diag_ram[df_diag_ram['sn']==sn]
  54. df_bms_ram_sn=df_bms_ram[df_bms_ram['sn']==sn]
  55. df_alarm_ram_sn=df_alarm_ram[df_alarm_ram['sn']==sn]
  56. SafetyAlarm=CBMSSafetyAlarm.SafetyAlarm(sn,celltype,df_bms,df_diag_ram_sn, df_bms_ram_sn, df_alarm_ram_sn)
  57. df_diag_res, df_bms_res, df_ram_res=SafetyAlarm.safety_alarm_diag()
  58. #更新bms和结果ram的ram数据
  59. sn_index=df_bms_ram.loc[df_bms_ram['sn']==sn].index
  60. df_bms_ram=df_bms_ram.drop(index=sn_index)
  61. df_bms_ram=df_bms_ram.append(df_bms_res)
  62. sn_index=df_alarm_ram.loc[df_alarm_ram['sn']==sn].index
  63. df_alarm_ram=df_alarm_ram.drop(index=sn_index)
  64. df_alarm_ram=df_alarm_ram.append(df_ram_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. # end=time.time()
  70. # print(end-start)
  71. #...............................................主函数起定时作用.......................................................................................................................
  72. if __name__ == "__main__":
  73. excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  74. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  75. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  76. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  77. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  78. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  79. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  80. SNnums_6060=SNdata_6060['SN号'].tolist()
  81. SNnums_6040=SNdata_6040['SN号'].tolist()
  82. SNnums_4840=SNdata_4840['SN号'].tolist()
  83. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  84. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  85. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  86. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_U7255 + SNnums_6040 + SNnums_4840 + SNnums_6060
  87. mylog=log.Mylog('log_diag.txt','error')
  88. mylog.logcfg()
  89. #参数初始化
  90. df_bms_ram=pd.DataFrame(columns=['time', 'sn', 'packvolt', 'cellvolt', 'celltemp', 'packsoc', 'packcrnt'])
  91. df_alarm_ram=pd.DataFrame(columns=['sn','time','safetywarning1','safetywarning2'])
  92. #定时任务.......................................................................................................................................................................
  93. scheduler = BlockingScheduler()
  94. scheduler.add_job(diag_cal, 'interval', seconds=60, id='diag_job')
  95. try:
  96. scheduler.start()
  97. except Exception as e:
  98. scheduler.shutdown()
  99. print(repr(e))
  100. mylog.logopt(e)