main.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import CBMSBatDiag
  2. import pymysql
  3. import datetime
  4. import pandas as pd
  5. from LIB.BACKEND import DBManager
  6. from sqlalchemy import create_engine
  7. from urllib import parse
  8. import datetime, time
  9. from apscheduler.schedulers.blocking import BlockingScheduler
  10. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload
  11. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  12. #...................................电池包电芯安全诊断函数......................................................................................................................
  13. def diag_cal():
  14. global SNnums, df_bms_ram
  15. start=time.time()
  16. now_time=datetime.datetime.now()
  17. start_time=now_time-datetime.timedelta(seconds=310)
  18. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  19. end_time=now_time.strftime('%Y-%m-%d %H:%M:%S')
  20. #数据库配置
  21. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  22. port=3306
  23. db='safety_platform'
  24. user='qx_read'
  25. password='Qx@123456'
  26. #读取故障结果库中当前故障......................................................
  27. param='start_time,end_time,product_id,code,level,info,advice'
  28. tablename='all_fault_info_copy'
  29. mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  30. cursor = mysql.cursor()
  31. sql = "select %s from %s where end_time='0000-00-00 00:00:00'" %(param,tablename)
  32. cursor.execute(sql)
  33. res = cursor.fetchall()
  34. df_diag_ram= pd.DataFrame(res,columns=param.split(','))
  35. db_res_engine = create_engine(
  36. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  37. user, parse.quote_plus(password), host, port, db
  38. ))
  39. mylog=log.Mylog('log_diag.txt','error')
  40. mylog.logcfg()
  41. for sn in SNnums:
  42. try:
  43. if 'PK500' in sn:
  44. celltype=1 #6040三元电芯
  45. elif 'PK502' in sn:
  46. celltype=2 #4840三元电芯
  47. elif 'K504B' in sn:
  48. celltype=99 #60ah林磷酸铁锂电芯
  49. elif 'MGMLXN750' in sn:
  50. celltype=3 #力信50ah三元电芯
  51. elif ('MGMCLN750' in sn) or ('UD' in sn):
  52. celltype=4 #CATL 50ah三元电芯
  53. else:
  54. celltype=100
  55. print('SN:{},未找到对应电池类型!!!'.format(sn))
  56. continue
  57. # sys.exit()
  58. #读取原始数据库数据........................................................................................................................................................
  59. dbManager = DBManager.DBManager()
  60. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  61. df_bms = df_data['bms']
  62. #读取结果数据库数据........................................................................................................................................................
  63. db='qx_cas'
  64. mode=1
  65. tablename1='cellstateestimation_soh'
  66. tablename2='cellstateestimation_uniform_socvoltdiff'
  67. # tablename3='cellstateestimation_soc'
  68. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  69. with DBRead as DBRead:
  70. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  71. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellmin_num,cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time)
  72. # df_soc=DBRead.getdata('time','sn','packsoc', tablename=tablename3, sn=sn)
  73. #电池诊断................................................................................................................................................................
  74. if not df_bms.empty:
  75. df_diag_ram_sn=df_diag_ram[df_diag_ram['product_id']==sn]
  76. df_diag_ram_sn.reset_index(inplace=True,drop=True)
  77. df_bms_ram_sn=df_bms_ram[df_bms_ram['sn']==sn]
  78. df_bms_ram_sn.reset_index(inplace=True,drop=True)
  79. df_bms=df_bms.dropna(axis=0,subset=['总电流[A]','SOH[%]','SOC[%]']) #去除有空值的行
  80. df_bms.reset_index(inplace=True,drop=True) #重置索引
  81. batquality=CBMSBatDiag.BatDiag(sn,celltype,df_bms,df_diag_ram_sn,df_bms_ram_sn,df_soh,df_uniform)
  82. df_diag_res, df_bms_res=batquality.diag() #获取电池故障结果和电池评分结果
  83. #更新bms的ram数据
  84. sn_index=df_bms_ram.loc[df_bms_ram['sn']==sn].index
  85. df_bms_ram=df_bms_ram.drop(index=sn_index)
  86. df_bms_ram=df_bms_ram.append(df_bms_res)
  87. #历史故障筛选并更改数据库故障结束时间.........................................................
  88. if not df_diag_res.empty:
  89. df_diag_new = pd.concat([df_diag_res,df_diag_ram_sn,df_diag_ram_sn]).drop_duplicates(subset=['start_time','code'],keep=False)#此次判断中新增故障
  90. df_diag_new.reset_index(inplace=True,drop=True) #重置索引
  91. df_diag_end=pd.concat([df_diag_res,df_diag_new,df_diag_new]).drop_duplicates(subset=['start_time','code'],keep=False)#此次判断中新增故障
  92. df_diag_end=df_diag_end[df_diag_end['end_time'] != '0000-00-00 00:00:00']
  93. df_diag_end.reset_index(inplace=True,drop=True) #重置索引
  94. if not df_diag_end.empty: #变为历史故障更改数据库
  95. for j in range(len(df_diag_end)):
  96. cursor.execute('''
  97. update all_fault_info_copy set update_time='{}',end_time='{}', Batpos={} where product_id='{}' and end_time='0000-00-00 00:00:00' and code='{}'
  98. '''.format(datetime.datetime.now(), df_diag_end.loc[j,'end_time'], 1 ,sn, df_diag_end.loc[j,'code']))
  99. mysql.commit()
  100. #新增故障筛选并存入数据库.....................................................................
  101. if not df_diag_new.empty: #新增写入数据库
  102. df_diag_new.to_sql("all_fault_info_copy",con=db_res_engine, if_exists="append",index=False)
  103. end=time.time()
  104. print(end-start)
  105. except Exception as e:
  106. print(repr(e))
  107. mylog.logopt(e)
  108. cursor.close()
  109. mysql.close()
  110. #...............................................主函数起定时作用.......................................................................................................................
  111. if __name__ == "__main__":
  112. excelpath=r'D:\Develop\User\Songpengfei\data_analyze_platform\WORK\01qixiang\sn-20210903.xlsx'
  113. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  114. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  115. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  116. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  117. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  118. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  119. SNnums_6060=SNdata_6060['SN号'].tolist()
  120. SNnums_6040=SNdata_6040['SN号'].tolist()
  121. SNnums_4840=SNdata_4840['SN号'].tolist()
  122. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  123. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  124. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  125. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_U7255 + SNnums_6040 + SNnums_4840 + SNnums_6060
  126. SNnums=['TJMCL120502305010']
  127. mylog=log.Mylog('log_diag.txt','error')
  128. mylog.logcfg()
  129. #参数初始化
  130. df_bms_ram=pd.DataFrame(columns=['time', 'sn', 'packvolt', 'cellvolt', 'celltemp'])
  131. diag_cal()
  132. #定时任务.......................................................................................................................................................................
  133. scheduler = BlockingScheduler()
  134. scheduler.add_job(diag_cal, 'interval', seconds=300)
  135. try:
  136. scheduler.start()
  137. except Exception as e:
  138. scheduler.shutdown()
  139. print(repr(e))
  140. mylog.logopt(e)