main.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import CBMSBatDiag
  2. import QX_BatteryParam
  3. from SC_SamplingSafty import SamplingSafty
  4. import datetime
  5. import pandas as pd
  6. from LIB.BACKEND import DBManager, Log
  7. from sqlalchemy import create_engine
  8. import time, datetime
  9. from apscheduler.schedulers.blocking import BlockingScheduler
  10. from LIB.MIDDLE.CellStateEstimation.Common import DBDownload
  11. from LIB.MIDDLE.CellStateEstimation.Common import log
  12. from pandas.core.frame import DataFrame
  13. import datacompy
  14. #...................................电池包电芯安全诊断函数......................................................................................................................
  15. def diag_cal():
  16. global SNnums
  17. global df_Diag_Ram
  18. start=time.time()
  19. end_time=datetime.datetime.now()
  20. start_time=end_time-datetime.timedelta(seconds=120)
  21. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  22. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  23. for sn in SNnums:
  24. if 'PK500' in sn:
  25. celltype=1 #6040三元电芯
  26. elif 'PK502' in sn:
  27. celltype=2 #4840三元电芯
  28. elif 'K504B' in sn:
  29. celltype=99 #60ah林磷酸铁锂电芯
  30. elif 'MGMLXN750' in sn:
  31. celltype=3 #力信50ah三元电芯
  32. elif 'MGMCLN750' or 'UD' in sn:
  33. celltype=4 #CATL 50ah三元电芯
  34. else:
  35. print('SN:{},未找到对应电池类型!!!'.format(sn))
  36. continue
  37. # sys.exit()
  38. param=QX_BatteryParam.BatteryInfo(celltype)
  39. # sn='PK50201A000002039'
  40. # celltype=2
  41. # start_time='2021-05-02 09:12:26'
  42. # end_time='2021-06-03 19:12:26'
  43. # # df_bms= pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\01qixiang\98Download\\'+'BMS_'+sn+'.csv',encoding='GB18030')
  44. #读取原始数据库数据........................................................................................................................................................
  45. dbManager = DBManager.DBManager()
  46. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  47. df_bms = df_data['bms']
  48. # df_bms.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\01qixiang\99Result\\''BMS_'+sn+'.csv',encoding='GB18030')
  49. #读取结果数据库数据........................................................................................................................................................
  50. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  51. port=3306
  52. db='qx_cas'
  53. user='qx_read'
  54. password='Qx@123456'
  55. mode=1
  56. tablename1='cellstateestimation_soh'
  57. tablename2='cellstateestimation_uniform_socvoltdiff'
  58. tablename3='cellstateestimation_soc'
  59. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  60. with DBRead as DBRead:
  61. df_soh=DBRead.getdata('time_st','sn','soh', 'cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  62. df_uniform=DBRead.getdata('time','sn','cellsoc_diff','cellmin_num','cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time)
  63. # df_soc=DBRead.getdata('time','sn','packsoc', tablename=tablename3, sn=sn)
  64. #电池诊断................................................................................................................................................................
  65. #BatDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform)
  66. #df_res=BatDiag.diag()
  67. #df_Diag_Ram_old=df_Diag_Ram
  68. df_Diag_Ram_Update=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  69. CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  70. if not df_bms.empty:
  71. CellFltInfo=df_Diag_Ram[df_Diag_Ram['product_id']==sn]
  72. df_Diag_Ram_Update=SamplingSafty.main(sn,param,df_bms,CellFltInfo)
  73. if not df_Diag_Ram_Update.empty:
  74. sn_index=df_Diag_Ram[df_Diag_Ram['product_id']==sn].index
  75. df_Diag_Ram=df_Diag_Ram.drop(index=sn_index)
  76. df_Diag_Ram=df_Diag_Ram.append(df_Diag_Ram_Update)
  77. df_Diag_Ram.reset_index(inplace=True,drop=True)
  78. Diag_Ram_Dif=datacompy.Compare(df_Diag_Ram_Update,CellFltInfo,join_columns=['product_id','end_time','code'])
  79. Diag_Ram_Dif=Diag_Ram_Dif.df1_unq_rows
  80. if len(Diag_Ram_Dif)>0:
  81. Diag_Ram_Dif_New=Diag_Ram_Dif[Diag_Ram_Dif['end_time']=='0000-00-00 00:00:00']
  82. Diag_Ram_Dif_Finish=df_Diag_Ram[df_Diag_Ram['end_time']!='0000-00-00 00:00:00']
  83. if len(Diag_Ram_Dif_New)>0:
  84. result=pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\result.csv',encoding='gbk')
  85. result=result.append(Diag_Ram_Dif_New)
  86. result.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\result.csv',index=False,encoding='GB18030')
  87. if len(Diag_Ram_Dif_Finish)>0:
  88. result=pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\result.csv',encoding='gbk')
  89. Diag_Ram_Dif_Finish=Diag_Ram_Dif_Finish.reset_index(drop=True)
  90. for i in range(0,len(Diag_Ram_Dif_Finish)):
  91. result.loc[result[result[result['product_id']==Diag_Ram_Dif_Finish.loc[i,'product_id']]['code']==Diag_Ram_Dif_Finish.loc[i,'code']].index,'end_time']=Diag_Ram_Dif_Finish.loc[i,'end_time']
  92. result.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\result.csv',index=False,encoding='GB18030')
  93. end=time.time()
  94. print(end-start)
  95. # print(df_soh)
  96. #...................................电池包电芯安全诊断函数......................................................................................................................
  97. def shortdiag_cal():
  98. global SNnums
  99. global df_Diag_Ram
  100. start=time.time()
  101. end_time=datetime.datetime.now()
  102. start_time=end_time-datetime.timedelta(days=30)
  103. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  104. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  105. for sn in SNnums:
  106. if 'PK500' in sn:
  107. celltype=1 #6040三元电芯
  108. elif 'PK502' in sn:
  109. celltype=2 #4840三元电芯
  110. elif 'K504B' in sn:
  111. celltype=99 #60ah林磷酸铁锂电芯
  112. elif 'MGMLXN750' in sn:
  113. celltype=3 #力信50ah三元电芯
  114. elif 'MGMCLN750' or 'UD' in sn:
  115. celltype=4 #CATL 50ah三元电芯
  116. else:
  117. print('SN:{},未找到对应电池类型!!!'.format(sn))
  118. continue
  119. # sys.exit()
  120. #读取结果数据库数据........................................................................................................................................................
  121. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  122. port=3306
  123. db='qx_cas'
  124. user='qx_read'
  125. password='Qx@123456'
  126. mode=2
  127. tablename1='cellstateestimation_intershort'
  128. DBRead=DBDownload.DBDownload(host, port, db, user, password, mode) #mode==1取数据库最后一行数据
  129. with DBRead as DBRead:
  130. df_short=DBRead.getdata('time_sp','sn','short_current', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  131. #电池诊断................................................................................................................................................................
  132. ShortDiag=CBMSBatDiag.ShortDiag(sn,celltype, df_short)
  133. df_res=ShortDiag.shortdiag()
  134. # df_res.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\01qixiang\99Result\\'+'CBMS_diag_'+sn+'.csv',encoding='GB18030')
  135. print(df_res)
  136. end=time.time()
  137. print(end-start)
  138. # print(df_soh)
  139. #...............................................主函数.......................................................................................................................
  140. if __name__ == "__main__":
  141. excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\sn-20210903.xlsx'
  142. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  143. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  144. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  145. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  146. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  147. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  148. SNnums_6060=SNdata_6060['SN号'].tolist()
  149. SNnums_6040=SNdata_6040['SN号'].tolist()
  150. SNnums_4840=SNdata_4840['SN号'].tolist()
  151. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  152. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  153. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  154. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
  155. #SNnums=['MGMCLN750N215I005','PK504B10100004341','PK504B00100004172','MGMLXN750N2189014']
  156. #SNnums=['MGMLXN750N2189014']
  157. mylog=log.Mylog('log_diag.txt','error')
  158. mylog.logcfg()
  159. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
  160. result=pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\result.csv',encoding='gbk')
  161. df_Diag_Ram=result[result['end_time']=='0000-00-00 00:00:00']
  162. print('----------------输入--------')
  163. print(df_Diag_Ram)
  164. #定时任务.......................................................................................................................................................................
  165. scheduler = BlockingScheduler()
  166. scheduler.add_job(diag_cal, 'interval', seconds=120, id='diag_job')
  167. scheduler.add_job(shortdiag_cal, 'interval', days=7, id='shortdiag_job')
  168. try:
  169. scheduler.start()
  170. except Exception as e:
  171. scheduler.shutdown()
  172. print(repr(e))
  173. mylog.logopt(e)