main.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. print('SN:{},未找到对应电池类型!!!'.format(sn))
  55. continue
  56. # sys.exit()
  57. #读取原始数据库数据........................................................................................................................................................
  58. dbManager = DBManager.DBManager()
  59. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  60. df_bms = df_data['bms']
  61. #读取结果数据库数据........................................................................................................................................................
  62. db='qx_cas'
  63. mode=1
  64. tablename1='cellstateestimation_soh'
  65. tablename2='cellstateestimation_uniform_socvoltdiff'
  66. # tablename3='cellstateestimation_soc'
  67. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  68. with DBRead as DBRead:
  69. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  70. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellmin_num,cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time)
  71. # df_soc=DBRead.getdata('time','sn','packsoc', tablename=tablename3, sn=sn)
  72. #电池诊断................................................................................................................................................................
  73. if not df_bms.empty:
  74. df_diag_ram_sn=df_diag_ram[df_diag_ram['product_id']==sn]
  75. df_diag_ram_sn.reset_index(inplace=True,drop=True)
  76. df_bms_ram_sn=df_bms_ram[df_bms_ram['sn']==sn]
  77. df_bms_ram_sn.reset_index(inplace=True,drop=True)
  78. df_bms=df_bms.dropna(axis=0,subset=['总电流[A]','SOH[%]','SOC[%]','总电压[V]']) #去除有空值的行
  79. df_bms.reset_index(inplace=True,drop=True) #重置索引
  80. batquality=CBMSBatDiag.BatDiag(sn,celltype,df_bms,df_diag_ram_sn,df_bms_ram_sn,df_soh,df_uniform)
  81. df_diag_res, df_bms_res=batquality.diag() #获取电池故障结果和电池评分结果
  82. #更新bms的ram数据
  83. sn_index=df_bms_ram.loc[df_bms_ram['sn']==sn].index
  84. df_bms_ram=df_bms_ram.drop(index=sn_index)
  85. df_bms_ram=df_bms_ram.append(df_bms_res)
  86. #历史故障筛选并更改数据库故障结束时间.........................................................
  87. if not df_diag_res.empty:
  88. df_diag_new = pd.concat([df_diag_res,df_diag_ram_sn,df_diag_ram_sn]).drop_duplicates(subset=['start_time','code'],keep=False)#此次判断中新增故障
  89. df_diag_new.reset_index(inplace=True,drop=True) #重置索引
  90. df_diag_end=pd.concat([df_diag_res,df_diag_new,df_diag_new]).drop_duplicates(subset=['start_time','code'],keep=False)#此次判断中新增故障
  91. df_diag_end=df_diag_end[df_diag_end['end_time'] != '0000-00-00 00:00:00']
  92. df_diag_end.reset_index(inplace=True,drop=True) #重置索引
  93. if not df_diag_end.empty: #变为历史故障更改数据库
  94. for j in range(len(df_diag_end)):
  95. cursor.execute('''
  96. 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='{}'
  97. '''.format(datetime.datetime.now(), df_diag_end.loc[j,'end_time'], 1 ,sn, df_diag_end.loc[j,'code']))
  98. mysql.commit()
  99. #新增故障筛选并存入数据库.....................................................................
  100. if not df_diag_new.empty: #新增写入数据库
  101. df_diag_new.to_sql("all_fault_info_copy",con=db_res_engine, if_exists="append",index=False)
  102. end=time.time()
  103. print(end-start)
  104. except Exception as e:
  105. print(repr(e))
  106. mylog.logopt(e)
  107. cursor.close()
  108. mysql.close()
  109. #...............................................主函数起定时作用.......................................................................................................................
  110. if __name__ == "__main__":
  111. excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  112. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  113. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  114. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  115. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  116. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  117. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  118. SNnums_6060=SNdata_6060['SN号'].tolist()
  119. SNnums_6040=SNdata_6040['SN号'].tolist()
  120. SNnums_4840=SNdata_4840['SN号'].tolist()
  121. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  122. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  123. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  124. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_U7255 + SNnums_6040 + SNnums_4840 + SNnums_6060
  125. SNnums=['PK50201A000002006']
  126. mylog=log.Mylog('log_diag.txt','error')
  127. mylog.logcfg()
  128. #参数初始化
  129. df_bms_ram=pd.DataFrame(columns=['time', 'sn', 'packvolt', 'cellvolt', 'celltemp'])
  130. diag_cal()
  131. #定时任务.......................................................................................................................................................................
  132. scheduler = BlockingScheduler()
  133. scheduler.add_job(diag_cal, 'interval', seconds=300)
  134. try:
  135. scheduler.start()
  136. except Exception as e:
  137. scheduler.shutdown()
  138. print(repr(e))
  139. mylog.logopt(e)