main_test.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import CBMSBatDiag
  2. from SC_SamplingSafty import SamplingSafty
  3. import datetime
  4. import pandas as pd
  5. from LIB.BACKEND import DBManager, Log
  6. from sqlalchemy import create_engine
  7. import time, datetime
  8. from apscheduler.schedulers.blocking import BlockingScheduler
  9. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload as DBDownload
  10. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  11. from pandas.core.frame import DataFrame
  12. from LIB.MIDDLE.SaftyCenter.Common import QX_BatteryParam
  13. from LIB.MIDDLE.SaftyCenter.Common import DBDownload as DBDw
  14. from DiagDataMerge import DiagDataMerge
  15. import pymysql
  16. #...................................电池包电芯安全诊断函数......................................................................................................................
  17. def diag_cal():
  18. task_on=1
  19. global SNnums
  20. #..................................设置时间..........................................................
  21. start=time.time()
  22. end_time=datetime.datetime.now()
  23. start_time=end_time-datetime.timedelta(seconds=600)#10分钟跑一次,一次取10分钟数据
  24. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  25. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  26. #数据库配置
  27. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  28. port=3306
  29. user='qx_read'
  30. password='Qx@123456'
  31. #读取故障结果库中code==110且end_time='0000-00-00 00:00:00'...............................
  32. db='safety_platform'
  33. mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
  34. cursor = mysql.cursor()
  35. param='start_time,end_time,product_id,code,level,info,advice,Batpos'
  36. tablename='all_fault_info'
  37. sql = "select %s from %s where end_time='0000-00-00 00:00:00'" %(param,tablename)
  38. cursor.execute(sql)
  39. res = cursor.fetchall()
  40. df_Diag_Ram= pd.DataFrame(res,columns=param.split(','))
  41. cursor.close()
  42. mysql.close()
  43. for sn in SNnums:#SN遍历
  44. print(sn)
  45. if 'PK500' in sn:
  46. celltype=1 #6040三元电芯
  47. elif 'PK502' in sn:
  48. celltype=2 #4840三元电芯
  49. elif 'K504B' in sn:
  50. celltype=99 #60ah林磷酸铁锂电芯
  51. elif 'MGMLXN750' in sn:
  52. celltype=3 #力信50ah三元电芯
  53. elif 'MGMCLN750' or 'UD' in sn:
  54. celltype=4 #CATL 50ah三元电芯
  55. else:
  56. print('SN:{},未找到对应电池类型!!!'.format(sn))
  57. continue
  58. # sys.exit()
  59. param=QX_BatteryParam.BatteryInfo(celltype)
  60. #读取原始数据库数据........................................................................................................................................................
  61. dbManager = DBManager.DBManager()
  62. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  63. df_bms = df_data['bms']
  64. #读取结果数据库数据........................................................................................................................................................
  65. db='qx_cas'
  66. mode=1
  67. tablename1='cellstateestimation_soh'
  68. tablename2='cellstateestimation_uniform_socvoltdiff'
  69. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  70. with DBRead as DBRead:
  71. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  72. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellmin_num,cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time)
  73. #电池诊断.....................................................................................................................................................................
  74. CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  75. df_Diag_Ram_sn = df_Diag_Ram.loc[df_Diag_Ram['product_id']==sn]#历史故障
  76. # df_Diag_Ram_sn_else = pd.concat([df_Diag_Ram,df_Diag_Ram_sn,df_Diag_Ram_sn]).drop_duplicates(subset=['product_id'],keep=False)#sn之外的故障
  77. df_Diag_Ram_sn_else=df_Diag_Ram.drop(df_Diag_Ram[df_Diag_Ram.product_id==sn].index)#sn之外的故障
  78. CellFltInfo = df_Diag_Ram_sn.drop('Batpos',axis=1)
  79. #获取当前故障电池的历史故障信息↑↑↑↑↑↑↑↑↑↑↑.......................................................................................................................................
  80. if not df_bms.empty:
  81. # df_Diag_Batdiag_update_xq=SamplingSafty.main(sn,param,df_bms,CellFltInfo)#学琦计算故障
  82. batDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform, CellFltInfo)#鹏飞计算
  83. df_Diag_Batdiag_update=batDiag.diag()
  84. # df_Diag_Batdiag_update=BatDiag.diag()
  85. if not df_Diag_Batdiag_update.empty:
  86. with open(r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\07BatSafetyWarning\电压排名.txt','a') as file:
  87. file.write(str(df_Diag_Batdiag_update)+'\n')
  88. else:
  89. pass
  90. #...............................................主函数.......................................................................................................................
  91. if __name__ == "__main__":
  92. excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx'
  93. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  94. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  95. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  96. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  97. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  98. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  99. SNnums_6060=SNdata_6060['SN号'].tolist()
  100. SNnums_6040=SNdata_6040['SN号'].tolist()
  101. SNnums_4840=SNdata_4840['SN号'].tolist()
  102. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  103. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  104. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  105. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_U7255 + SNnums_6040 + SNnums_4840 + SNnums_6060
  106. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
  107. # SNnums=['MGMCLN750N215I005','PK504B10100004341','PK504B00100004172','MGMLXN750N2189014']
  108. SNnums = ['MGMLXN750N218N004'] #SNnums_6040
  109. mylog=log.Mylog('log_diag.txt','error')
  110. mylog.logcfg()
  111. diag_cal()
  112. #定时任务.......................................................................................................................................................................
  113. scheduler = BlockingScheduler()
  114. scheduler.add_job(diag_cal, 'interval', seconds=5, id='diag_job')
  115. try:
  116. scheduler.start()
  117. except Exception as e:
  118. scheduler.shutdown()
  119. print(repr(e))
  120. mylog.logopt(e)