main.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import CBMSBatDiag
  2. import pymysql
  3. import datetime
  4. import pandas as pd
  5. from LIB.BACKEND import DBManager
  6. import datetime
  7. from apscheduler.schedulers.blocking import BlockingScheduler
  8. from USER.SPF.alibaba.Common import DBDownload
  9. from USER.SPF.alibaba.Common import log
  10. #...................................电池包电芯安全诊断函数......................................................................................................................
  11. def diag_cal():
  12. global SNnums
  13. now_time=datetime.datetime.now()
  14. start_time=now_time-datetime.timedelta(seconds=65)
  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. db='qx_cas'
  21. user='qx_read'
  22. password='Qx@123456'
  23. #读取故障结果库中当前故障......................................................
  24. param='time_st,time_sp,sn,faultcode,faultlv,faultinfo,faultadvice'
  25. tablename=''
  26. mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  27. cursor = mysql.cursor()
  28. sql = "select %s from %s where time_sp='0000-00-00 00:00:00'" %(param,tablename)
  29. cursor.execute(sql)
  30. res = cursor.fetchall()
  31. df_diag_ram= pd.DataFram(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. 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. #读取结果数据库数据........................................................................................................................................................
  54. mode=1
  55. tablename1='cellstateestimation_soh'
  56. tablename2='cellstateestimation_uniform_socvoltdiff'
  57. tablename3='cellstateestimation_soc'
  58. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  59. with DBRead as DBRead:
  60. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  61. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellmin_num,cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time)
  62. df_soc=DBRead.getdata('time','sn','packsoc', tablename=tablename3, sn=sn)
  63. #电池诊断................................................................................................................................................................
  64. if not df_bms.empty:
  65. df_diag_ram_sn=df_diag_ram[df_diag_ram['sn']==sn]
  66. df_diag_ram_sn.reset_index(inplace=True,drop=True)
  67. batdiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms,df_soh,df_soc,df_uniform,df_diag_ram_sn)
  68. df_diag_res, df_health_res=batdiag.diag() #获取电池故障结果和电池评分结果
  69. #电池评分写入数据库
  70. if not df_health_res.empty: #变为历史故障更改数据库
  71. with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\05BatDiag\电池评分.txt','a') as file:
  72. file.write(str(tuple(df_health_res))+'\n')
  73. #历史故障筛选并更改数据库故障结束时间.........................................................
  74. if not df_diag_res.empty:
  75. df_diag_now=df_diag_res[df_diag_res['time_sp'] == '0000-00-00 00:00:00'] #去除历史故障
  76. df_diag_new = pd.concat([df_diag_res,df_diag_ram_sn,df_diag_ram_sn]).drop_duplicates(subset=['time_st','faultcode'],keep=False)#此次判断中新增故障
  77. df_diag_end=pd.concat([df_diag_res,df_diag_new,df_diag_new]).drop_duplicates(subset=['time_st','faultcode'],keep=False)#此次判断中新增故障
  78. df_diag_end=df_diag_end[df_diag_end['time_sp'] != '0000-00-00 00:00:00']
  79. df_diag_end.reset_index(inplace=True,drop=True) #重置索引
  80. if not df_diag_end.empty: #变为历史故障更改数据库
  81. with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\05BatDiag\故障报警.txt','a') as file:
  82. file.write(str(tuple(df_diag_end))+'\n')
  83. #新增故障筛选并存入数据库.....................................................................
  84. if not df_diag_new.empty: #新增写入数据库
  85. with open(r'D:\00WorkSpace\01Python\data_analyze_platform\USER\alibaba\05BatDiag\故障报警.txt','a') as file:
  86. file.write(str(tuple(df_diag_new))+'\n')
  87. #更新diag的Ram数据
  88. df_diag_ram=df_diag_ram.drop(df_diag_ram[df_diag_ram.sn==sn].index)
  89. df_diag_ram=df_diag_ram.append(df_diag_now, ignore_index=True)
  90. df_diag_ram.reset_index(inplace=True,drop=True) #重置索引
  91. #健康度评分存入数据库
  92. #...............................................主函数起定时作用.......................................................................................................................
  93. if __name__ == "__main__":
  94. excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  95. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  96. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  97. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  98. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  99. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  100. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  101. SNnums_6060=SNdata_6060['SN号'].tolist()
  102. SNnums_6040=SNdata_6040['SN号'].tolist()
  103. SNnums_4840=SNdata_4840['SN号'].tolist()
  104. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  105. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  106. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  107. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_U7255, SNnums_6040 + SNnums_4840 + SNnums_6060
  108. SNnums=['HUATING']
  109. mylog=log.Mylog('log_diag.txt','error')
  110. mylog.logcfg()
  111. #定时任务.......................................................................................................................................................................
  112. scheduler = BlockingScheduler()
  113. scheduler.add_job(diag_cal, 'interval', seconds=60)
  114. try:
  115. scheduler.start()
  116. except Exception as e:
  117. scheduler.shutdown()
  118. print(repr(e))
  119. mylog.logopt(e)