main_test.py 7.5 KB

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