maincopy.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #from test.spf.BatDiag import CBMSBatDiag, Log
  2. import CBMSBatDiag
  3. import QX_BatteryParam
  4. from SC_SamplingSafty import SamplingSafty
  5. import datetime
  6. import pandas as pd
  7. from LIB.BACKEND import DBManager, Log
  8. from sqlalchemy import create_engine
  9. import time, datetime
  10. from apscheduler.schedulers.blocking import BlockingScheduler
  11. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload
  12. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  13. from pandas.core.frame import DataFrame
  14. import datacompy
  15. import GetFeiShuData
  16. #...................................电池包电芯安全诊断函数......................................................................................................................
  17. def diag_cal():
  18. global SNnums
  19. global df_Diag_Ram
  20. start=time.time()
  21. end_time=datetime.datetime.now()
  22. start_time=end_time-datetime.timedelta(seconds=60)
  23. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  24. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  25. for sn in SNnums:
  26. if 'PK500' in sn:
  27. celltype=1 #6040三元电芯
  28. elif 'PK502' in sn:
  29. celltype=2 #4840三元电芯
  30. elif 'K504B' in sn:
  31. celltype=99 #60ah林磷酸铁锂电芯
  32. elif 'MGMLXN750' in sn:
  33. celltype=3 #力信50ah三元电芯
  34. elif 'MGMCLN750' or 'UD' in sn:
  35. celltype=4 #CATL 50ah三元电芯
  36. else:
  37. print('SN:{},未找到对应电池类型!!!'.format(sn))
  38. continue
  39. # sys.exit()
  40. param=QX_BatteryParam.BatteryInfo(celltype)
  41. # sn='PK50201A000002039'
  42. # celltype=2
  43. # start_time='2021-05-02 09:12:26'
  44. # end_time='2021-06-03 19:12:26'
  45. # # df_bms= pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\01qixiang\98Download\\'+'BMS_'+sn+'.csv',encoding='GB18030')
  46. #读取原始数据库数据........................................................................................................................................................
  47. dbManager = DBManager.DBManager()
  48. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  49. df_bms = df_data['bms']
  50. #df_bms.to_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\\''BMS_'+sn+'.csv',encoding='GB18030')
  51. #读取结果数据库数据........................................................................................................................................................
  52. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  53. port=3306
  54. db='qx_cas'
  55. user='qx_read'
  56. password='Qx@123456'
  57. mode=1
  58. tablename1='cellstateestimation_soh'
  59. tablename2='cellstateestimation_uniform_socvoltdiff'
  60. tablename3='cellstateestimation_soc'
  61. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  62. with DBRead as DBRead:
  63. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  64. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellmin_num,cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time)
  65. # df_soc=DBRead.getdata('time','sn','packsoc', tablename=tablename3, sn=sn)
  66. #电池诊断................................................................................................................................................................
  67. #BatDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform)
  68. #df_res=BatDiag.diag()
  69. df_Diag_Ram_old=df_Diag_Ram.drop('Batpos',axis=1)
  70. df_Diag_Ram_Update=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  71. CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  72. if not df_bms.empty:
  73. CellFltInfo=df_Diag_Ram_old[df_Diag_Ram_old['product_id']==sn]#历史故障
  74. df_Diag_Batdiag_update_xq=SamplingSafty.main(sn,param,df_bms,CellFltInfo)#学琦计算故障
  75. BatDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform, CellFltInfo)#鹏飞计算
  76. df_Diag_Batdiag_update=BatDiag.diag()
  77. df_Diag_Cal_Update_temp=df_Diag_Batdiag_update_xq.append(df_Diag_Batdiag_update)
  78. if not df_Diag_Cal_Update_temp.empty:
  79. #------------------------------合并两者故障,并将同一sn号下的车辆故障放一起----------------------------------------------
  80. df_Diag_Cal_Update = df_Diag_Cal_Update_temp.append(df_Diag_Ram_old)
  81. df_read_Yunw = GetFeiShuData.getFeiShuDATA()#运维表格数据
  82. df_read_Yunw.rename(columns={'电池编码':'product_id'},inplace=True)
  83. set_diff_df = pd.concat([df_Diag_Cal_Update,df_read_Yunw,df_read_Yunw]).drop_duplicates(subset=['product_id','code','start_time'],keep=False)#新增故障的sn,报出故障减去原文档中的sn
  84. new_sn = set_diff_df['product_id']
  85. same_sn = df_read_Yunw.loc[(df_read_Yunw['维修信息']== '需正常返仓') | (df_read_Yunw['维修信息']== '需紧急返仓')]['product_id']#筛选待修改和需返回车辆
  86. #set_same_df = df_Diag_Cal_Update.loc[df_Diag_Cal_Update['product_id'].isin(same_sn['product_id'])]#筛选待修改和需返回车辆
  87. need_fix_sn = df_Diag_Cal_Update.loc[df_Diag_Cal_Update['end_time'] == '0000-00-00 00:00:00']['product_id']
  88. df_temp_sn = pd.concat([new_sn,same_sn,need_fix_sn])#新增及待改进车辆
  89. df_diag_frame = df_Diag_Cal_Update.loc[df_Diag_Cal_Update['product_id'].isin(df_temp_sn)]#筛选待修改和需返回车辆
  90. df_tempnum = df_diag_frame.groupby(["product_id"]).size()#获取每个sn的故障总数
  91. col1 = df_tempnum[df_tempnum>1].reset_index()[["product_id"]]#多故障sn号
  92. col2 = df_tempnum[df_tempnum==1].reset_index()[["product_id"]]#单故障sn号
  93. df_temp1 = pd.DataFrame()
  94. if not col1.empty:
  95. for item in col1['product_id']:
  96. temp_data = df_diag_frame.loc[df_diag_frame['product_id'] == item]
  97. temp_data.sort_values(by = "start_time", axis = 0, ascending=True,inplace=True)#对故障信息按照时间进行排序
  98. df_temp1.append(temp_data)
  99. df_temp2 = pd.merge(col2,df_diag_frame,on=["product_id"])#单故障码数据筛选
  100. df_temp3 = pd.concat([df_temp1,df_temp2])#多故障及单故障合并
  101. df_temp4 = df_temp3.reset_index(drop=True)
  102. #-------------------------------差集加入状态1--------------------------------
  103. set_diff_df_add = pd.concat([df_Diag_Cal_Update,df_temp4,df_temp4]).drop_duplicates(subset=['product_id','code','start_time'],keep=False)
  104. set_diff_df_add['Batpos'] = 1
  105. #--------------------------------交集加入状态0------------------------------
  106. df_temp4['Batpos'] = 0
  107. df_Diag_Ram_Update = df_temp4.append(set_diff_df)#计算故障信息
  108. diag_temp = df_Diag_Ram_Update.reset_index(drop=True)
  109. df_Diag_Ram_Update = diag_temp[['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice','Batpos']]
  110. #-------------------------------故障--------------------------------------------
  111. df_Diag_Ram = df_Diag_Ram_Update
  112. # sn_index=df_Diag_Ram[df_Diag_Ram['product_id']==sn].index
  113. # df_Diag_Ram=df_Diag_Ram.drop(index=sn_index)
  114. # df_Diag_Ram=df_Diag_Ram.append(df_Diag_Ram_Update)
  115. # df_Diag_Ram.reset_index(inplace=True,drop=True)
  116. # Diag_Ram_Dif=datacompy.Compare(df_Diag_Ram_Update,CellFltInfo,join_columns=['product_id','end_time','code'])
  117. # Diag_Ram_Dif=Diag_Ram_Dif.df1_unq_rows
  118. if len(df_Diag_Ram)>0:#Diag_Ram_Dif
  119. Diag_Ram_Dif_New=df_Diag_Ram[df_Diag_Ram['end_time']=='0000-00-00 00:00:00']
  120. Diag_Ram_Dif_Finish=df_Diag_Ram[df_Diag_Ram['end_time']!='0000-00-00 00:00:00']
  121. if len(Diag_Ram_Dif_New)>0:
  122. result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\01Screen_Problem\result.csv',encoding='gbk')
  123. result=result.append(Diag_Ram_Dif_New)
  124. result.to_csv(r'D:\Work\Code_write\data_analyze_platform\01Screen_Problem\result.csv',index=False,encoding='GB18030')
  125. if len(Diag_Ram_Dif_Finish)>0:
  126. result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\01Screen_Problem\result.csv',encoding='gbk')
  127. Diag_Ram_Dif_Finish=Diag_Ram_Dif_Finish.reset_index(drop=True)
  128. for i in range(0,len(Diag_Ram_Dif_Finish)):
  129. aa_id = result['product_id']==Diag_Ram_Dif_Finish.loc[i,'product_id']
  130. bb_code = result['code']==Diag_Ram_Dif_Finish.loc[i,'code']
  131. result.loc[result.loc[aa_id & bb_code].index,'end_time'] = Diag_Ram_Dif_Finish.loc[i,'end_time']
  132. # result.loc[result[result.loc[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']
  133. result.to_csv(r'D:\Work\Code_write\data_analyze_platform\01Screen_Problem\result.csv',index=False,encoding='GB18030')
  134. end=time.time()
  135. print(end-start)
  136. # print(df_soh)
  137. #...................................电池包电芯安全诊断函数......................................................................................................................
  138. def shortdiag_cal():
  139. global SNnums
  140. global df_Diag_Ram
  141. start=time.time()
  142. end_time=datetime.datetime.now()
  143. start_time=end_time-datetime.timedelta(days=30)
  144. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  145. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  146. for sn in SNnums:
  147. if 'PK500' in sn:
  148. celltype=1 #6040三元电芯
  149. elif 'PK502' in sn:
  150. celltype=2 #4840三元电芯
  151. elif 'K504B' in sn:
  152. celltype=99 #60ah林磷酸铁锂电芯
  153. elif 'MGMLXN750' in sn:
  154. celltype=3 #力信50ah三元电芯
  155. elif 'MGMCLN750' or 'UD' in sn:
  156. celltype=4 #CATL 50ah三元电芯
  157. else:
  158. print('SN:{},未找到对应电池类型!!!'.format(sn))
  159. continue
  160. # sys.exit()
  161. #读取结果数据库数据........................................................................................................................................................
  162. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  163. port=3306
  164. db='qx_cas'
  165. user='qx_read'
  166. password='Qx@123456'
  167. mode=2
  168. tablename4='cellstateestimation_intershort'
  169. DBRead=DBDownload.DBDownload(host, port, db, user, password, mode) #mode==1取数据库最后一行数据
  170. with DBRead as DBRead:
  171. df_short=DBRead.getdata('time_sp,sn,short_current', tablename=tablename4, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  172. #电池诊断................................................................................................................................................................
  173. ShortDiag=CBMSBatDiag.ShortDiag(sn,celltype, df_short)
  174. df_res=ShortDiag.shortdiag()
  175. df_res.to_csv(r'D:\Work\Code_write\data_analyze_platform\01Screen_Problem\\'+'CBMS_diag_'+sn+'.csv',encoding='GB18030')
  176. print(df_res)
  177. end=time.time()
  178. print(end-start)
  179. # print(df_soh)
  180. #...............................................主函数.......................................................................................................................
  181. if __name__ == "__main__":
  182. excelpath=r'D:\Work\Code_write\data_analyze_platform\01Screen_Problem\sn-20210903.xlsx'
  183. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  184. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  185. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  186. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  187. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  188. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  189. SNnums_6060=SNdata_6060['SN号'].tolist()
  190. SNnums_6040=SNdata_6040['SN号'].tolist()
  191. SNnums_4840=SNdata_4840['SN号'].tolist()
  192. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  193. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  194. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  195. #SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
  196. # SNnums=['MGMCLN750N215I005','PK504B10100004341','PK504B00100004172','MGMLXN750N2189014']
  197. SNnums = SNnums_6040
  198. mylog=log.Mylog('log_diag.txt','error')
  199. mylog.logcfg()
  200. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
  201. result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\01Screen_Problem\result.csv',encoding='gbk')
  202. df_Diag_Ram=result[result['end_time']=='0000-00-00 00:00:00']
  203. print('----------------输入--------')
  204. print(df_Diag_Ram)
  205. print('-------done-----------')
  206. #定时任务.......................................................................................................................................................................
  207. scheduler = BlockingScheduler()
  208. scheduler.add_job(diag_cal, 'interval', seconds=60, id='diag_job')
  209. scheduler.add_job(shortdiag_cal, 'interval', days=7, id='shortdiag_job')
  210. try:
  211. scheduler.start()
  212. except Exception as e:
  213. scheduler.shutdown()
  214. print(repr(e))
  215. mylog.logopt(e)