main_2.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import CBMSBatDiag
  2. from SC_SamplingSafty import SamplingSafty
  3. from DataStatistics import DataSta
  4. from DiagDataMerge import DiagDataMerge
  5. from SC_CtrlSafty import CtrlSafty
  6. from SC_BMSUploadError import BMSReportError
  7. import datetime
  8. import pandas as pd
  9. from LIB.BACKEND import DBManager, Log
  10. from sqlalchemy import create_engine
  11. import time, datetime
  12. from apscheduler.schedulers.blocking import BlockingScheduler
  13. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload as DBDownload
  14. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  15. from pandas.core.frame import DataFrame
  16. import datacompy
  17. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import BatParam as QX_BatteryParam
  18. from LIB.MIDDLE.SaftyCenter.Common import DBDownload as DBDw
  19. #...................................电池包电芯安全诊断函数......................................................................................................................
  20. def diag_cal():
  21. task_on=1
  22. global SNnums
  23. global start
  24. #..................................设置时间..........................................................
  25. start=time.time()
  26. end_time=datetime.datetime.now()
  27. start_time=end_time-datetime.timedelta(seconds=900)#10分钟跑一次,一次取10分钟数据
  28. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  29. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  30. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  31. port=3306
  32. db='safety_platform'
  33. user='qx_algo_readonly'
  34. password='qx@123456'
  35. mode=2
  36. tablename2='all_fault_info_copy'
  37. DBRead = DBDw.DBDownload(host, port, db, user, password,mode)
  38. with DBRead as DBRead:
  39. df_Diag_Ram = DBRead.getdata('start_time','end_time','product_id','code','level','info','advice','Batpos',tablename=tablename2,factory='',sn='',timename='',st='',sp='')
  40. df_Diag_Ram=df_Diag_Ram.dropna(axis=1,how='any')
  41. df_Diag_Ram=df_Diag_Ram.reset_index(drop=True)
  42. for sn in SNnums:#SN遍历
  43. print(sn)
  44. start_time = '2021-12-30 18:00:04'
  45. end_time = '2021-12-30 18:15:04'
  46. if 'PK500' in sn:
  47. celltype=1 #6040三元电芯
  48. FactoryType=1
  49. elif 'PK502' in sn:
  50. celltype=2 #4840三元电芯
  51. FactoryType=1
  52. elif 'K504B' in sn:
  53. celltype=99 #60ah林磷酸铁锂电芯
  54. FactoryType=1
  55. elif 'MGMLXN750' in sn:
  56. celltype=3 #力信50ah三元电芯
  57. FactoryType=3
  58. elif ('MGMCLN750' or 'UD') in sn:
  59. celltype=4 #CATL 50ah三元电芯
  60. FactoryType=2
  61. elif 'TJMCL'in sn:
  62. celltype=100 #CATL 50ah三元电芯
  63. FactoryType=0
  64. else:
  65. print('SN:{},未找到对应电池类型!!!'.format(sn))
  66. continue
  67. # sys.exit()
  68. param=QX_BatteryParam.BatParam(celltype)
  69. #读取原始数据库数据........................................................................................................................................................
  70. dbManager = DBManager.DBManager()
  71. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  72. df_bms = df_data['bms']
  73. df_bms=df_bms.dropna(subset=['总电流[A]'])
  74. df_bms=df_bms.reset_index(drop=True)
  75. print(df_bms)
  76. #读取结果数据库数据........................................................................................................................................................
  77. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  78. port=3306
  79. db='qx_cas'
  80. user='qx_algo_readonly'
  81. password='qx@123456'
  82. mode=1
  83. tablename1='cellstateestimation_soh'
  84. tablename2='cellstateestimation_uniform_socvoltdiff'
  85. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  86. with DBRead as DBRead:
  87. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  88. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellmin_num,cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time)
  89. #读取电池当前运营状态.....................................................................................................................................................
  90. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  91. port=3306
  92. db='qixiang_manage'
  93. user='qx_query'
  94. password='@Qx_query'
  95. mode=4
  96. tablename1='py_battery'
  97. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  98. with DBRead as DBRead:
  99. df_OprtnSta=DBRead.getdata('qrcode','status', tablename=tablename1, sn=sn, timename='', st='', sp='',factory='')#取最后一条运营状态
  100. errorcode_map=DataFrame()
  101. #电池诊断.....................................................................................................................................................................
  102. CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  103. df_Diag_Ram_sn = df_Diag_Ram.loc[df_Diag_Ram['product_id']==sn]#历史故障
  104. df_Diag_Ram_sn_else = df_Diag_Ram.loc[df_Diag_Ram['product_id']!=sn]#sn之外的故障
  105. CellFltInfo = df_Diag_Ram_sn.drop('Batpos',axis=1)
  106. #获取当前故障电池的历史故障信息↑↑↑↑↑↑↑↑↑↑↑.......................................................................................................................................
  107. if not df_bms.empty:
  108. df_Diag_Batdiag_update_xq=SamplingSafty.main(sn,param,df_bms,CellFltInfo)#采样安全
  109. df_Diag_Batdiag_update_xq2=CtrlSafty.main(sn,param,df_bms,df_Diag_Batdiag_update_xq)#控制安全
  110. batDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform, df_Diag_Batdiag_update_xq2)#鹏飞计算
  111. df_Diag_Batdiag_update2=batDiag.diag()
  112. df_Diag_Batdiag_update=BMSReportError.main(sn,df_bms,df_Diag_Batdiag_update2,FactoryType,errorcode_map)
  113. # df_Diag_Batdiag_update=BatDiag.diag()
  114. else:
  115. df_Diag_Batdiag_update_xq=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  116. df_Diag_Batdiag_update_xq2=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  117. df_Diag_Batdiag_update2=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  118. df_Diag_Batdiag_update=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  119. df_Diag_Ram_add,df_Diag_Ram_Update=DiagDataMerge.DetaMerge(df_Diag_Ram_sn,df_Diag_Batdiag_update,df_OprtnSta,df_Diag_Ram_sn_else)
  120. task_on=0
  121. #...............................................主函数.......................................................................................................................
  122. if __name__ == "__main__":
  123. global SNnums
  124. global task_on
  125. task_on=0
  126. SNnums = ['PK504B10100004349']
  127. mylog=log.Mylog('log.txt','error')
  128. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
  129. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  130. port=3306
  131. db='safety_platform'
  132. user='qx_algo_readonly'
  133. password='qx@123456'
  134. mode=2
  135. tablename2='all_fault_info'
  136. DBRead = DBDw.DBDownload(host, port, db, user, password,mode)
  137. with DBRead as DBRead:
  138. df_Diag_Ram = DBRead.getdata('start_time','end_time','product_id','code','level','info','advice','Batpos','factory',tablename=tablename2,factory='',sn='',timename='',st='',sp='')
  139. # result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result.csv',encoding='gbk')
  140. #定时任务.......................................................................................................................................................................
  141. scheduler = BlockingScheduler()
  142. diag_cal()
  143. # DaTa_Sta_Minutes_Task()
  144. # DaTa_Sta_Week_Task()
  145. # if task_on==0:
  146. # scheduler.add_job(DaTa_Sta_Week_Task, 'interval', days=7, id='Week_Task')
  147. # scheduler.add_job(diag_cal, 'interval', seconds=900, id='diag_job')
  148. # scheduler.add_job(DaTa_Sta_Minutes_Task, 'interval', seconds=180, id='Hour_Task')
  149. # try:
  150. # scheduler.start()
  151. # except Exception as e:
  152. # scheduler.shutdown()
  153. # print(repr(e))
  154. # mylog.logopt(e)