main.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. factory_info=['骑享','金茂换电']
  25. df_Diag_Ram=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  26. #..................................设置时间..........................................................
  27. start=time.time()
  28. end_time=datetime.datetime.now()
  29. start_time=end_time-datetime.timedelta(seconds=900)#10分钟跑一次,一次取10分钟数据
  30. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  31. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  32. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  33. port=3306
  34. db='safety_platform'
  35. user='qx_algo_readonly'
  36. password='qx@123456'
  37. mode=2
  38. tablename2='all_fault_info'
  39. DBRead = DBDw.DBDownload(host, port, db, user, password,mode)
  40. with DBRead as DBRead:
  41. for i in range (0,len(factory_info)):
  42. df_Diag_Ram_t = DBRead.getdata('start_time','end_time','product_id','code','level','info','advice','Batpos',tablename=tablename2,factory=factory_info[i],sn='',timename='',st='',sp='')
  43. df_Diag_Ram=df_Diag_Ram.append(df_Diag_Ram_t)
  44. df_Diag_Ram=df_Diag_Ram.dropna(axis=1,how='any')
  45. df_Diag_Ram=df_Diag_Ram.reset_index(drop=True)
  46. for sn in SNnums:#SN遍历
  47. print(sn)
  48. if 'PK500' in sn:
  49. celltype=1 #6040三元电芯
  50. FactoryType=1
  51. elif 'PK502' in sn:
  52. celltype=2 #4840三元电芯
  53. FactoryType=1
  54. elif 'K504B' in sn:
  55. celltype=99 #60ah林磷酸铁锂电芯
  56. FactoryType=1
  57. elif 'MGMLXN750' in sn:
  58. celltype=3 #力信50ah三元电芯
  59. FactoryType=3
  60. elif ('MGMCLN750' or 'UD') in sn:
  61. celltype=4 #CATL 50ah三元电芯
  62. FactoryType=2
  63. elif 'TJMCL'in sn:
  64. celltype=100 #CATL 50ah三元电芯
  65. FactoryType=0
  66. else:
  67. print('SN:{},未找到对应电池类型!!!'.format(sn))
  68. continue
  69. # sys.exit()
  70. param=QX_BatteryParam.BatParam(celltype)
  71. #读取原始数据库数据........................................................................................................................................................
  72. dbManager = DBManager.DBManager()
  73. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  74. df_bms = df_data['bms']
  75. if not df_bms.empty:
  76. df_bms=df_bms.dropna(subset=['总电流[A]'])
  77. df_bms=df_bms.reset_index(drop=True)
  78. #读取结果数据库数据........................................................................................................................................................
  79. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  80. port=3306
  81. db='qx_cas'
  82. user='qx_algo_readonly'
  83. password='qx@123456'
  84. mode=1
  85. tablename1='cellstateestimation_soh'
  86. tablename2='cellstateestimation_uniform_socvoltdiff'
  87. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  88. with DBRead as DBRead:
  89. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  90. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellmin_num,cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time)
  91. #读取电池当前运营状态.....................................................................................................................................................
  92. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  93. port=3306
  94. db='qixiang_manage'
  95. user='qx_query'
  96. password='@Qx_query'
  97. mode=4
  98. tablename1='py_battery'
  99. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  100. with DBRead as DBRead:
  101. df_OprtnSta=DBRead.getdata('qrcode','status', tablename=tablename1, sn=sn, timename='', st='', sp='',factory='')#取最后一条运营状态
  102. errorcode_map=DataFrame()
  103. #电池诊断.....................................................................................................................................................................
  104. CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  105. df_Diag_Ram_sn = df_Diag_Ram.loc[df_Diag_Ram['product_id']==sn]#历史故障
  106. df_Diag_Ram_sn_else = df_Diag_Ram.loc[df_Diag_Ram['product_id']!=sn]#sn之外的故障
  107. CellFltInfo = df_Diag_Ram_sn.drop('Batpos',axis=1)
  108. #获取当前故障电池的历史故障信息↑↑↑↑↑↑↑↑↑↑↑.......................................................................................................................................
  109. if not df_bms.empty:
  110. df_Diag_Batdiag_update_xq=SamplingSafty.main(sn,param,df_bms,CellFltInfo)#采样安全
  111. df_Diag_Batdiag_update_xq2=CtrlSafty.main(sn,param,df_bms,df_Diag_Batdiag_update_xq)#控制安全
  112. #batDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform, df_Diag_Batdiag_update_xq2)#鹏飞计算
  113. #df_Diag_Batdiag_update2=batDiag.diag()
  114. #df_Diag_Batdiag_update=BMSReportError.main(sn,df_bms,df_Diag_Batdiag_update2,FactoryType,errorcode_map)
  115. df_Diag_Batdiag_update=BMSReportError.main(sn,df_bms,df_Diag_Batdiag_update_xq2,FactoryType,errorcode_map)
  116. # df_Diag_Batdiag_update=BatDiag.diag()
  117. else:
  118. df_Diag_Batdiag_update_xq=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  119. df_Diag_Batdiag_update_xq2=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  120. #df_Diag_Batdiag_update2=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  121. df_Diag_Batdiag_update=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  122. 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)
  123. task_on=0
  124. #.................................统计程序...............................
  125. def DaTa_Sta_Week_Task():
  126. task_on=1
  127. factory_info=['骑享','金茂换电']
  128. all_period_fault_info=DataFrame(columns=['factory','week','level1_count','level2_count','level3_count','level4_count','level5_count','solve_rate'])
  129. df_fltinfopt=DataFrame(columns=['product_id','level','code','start_time','end_time','batpos','factory'])
  130. for i in range (0,len(factory_info)):
  131. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  132. port=3306
  133. db='safety_platform'
  134. user='qx_algo_readonly'
  135. password='qx@123456'
  136. mode=2
  137. tablename1='all_fault_info'
  138. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  139. with DBRead as DBRead:
  140. df_fltinfo=DBRead.getdata('product_id','level','code','start_time','end_time','batpos','factory',tablename=tablename1,factory=factory_info[i],sn='',timename='',st='',sp='')#dbdownload经过了改编
  141. df_fltinfopt=df_fltinfopt.append(df_fltinfo)
  142. df_fltinfopt=df_fltinfopt.reset_index(drop=True)
  143. print(df_fltinfopt)
  144. #............................获取数据................................
  145. for j in range(0,len(factory_info)):
  146. toweek='Week'+time.strftime('%W')
  147. df_fltinfo=df_fltinfopt[df_fltinfopt['factory']==factory_info[j]]
  148. #............................获取时间................................
  149. end_time=datetime.datetime.now()
  150. # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d')
  151. start_time_week=end_time-datetime.timedelta(days=7)
  152. start_time_week=start_time_week.strftime('%Y-%m-%d')
  153. end_time=end_time.strftime('%Y-%m-%d')
  154. FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(df_fltinfo,start_time_week,end_time,factory_info[j])
  155. FaultLvlCount=DataSta.WeekInfoSta(df_fltinfo,start_time_week,end_time)
  156. for i in range(1,6):
  157. if not FaultLvlCount[FaultLvlCount['level']==i]['product_id'].empty:
  158. all_period_fault_info.loc[j,'level'+str(i)+'_count']=int(FaultLvlCount[FaultLvlCount['level']==i]['product_id'].values)
  159. else:
  160. all_period_fault_info.loc[j,'level'+str(i)+'_count']=int(0)
  161. all_period_fault_info.loc[j,'factory']=factory_info[j]
  162. all_period_fault_info.loc[j,'week']=toweek
  163. all_period_fault_info.loc[j,'solve_rate']=FltAlarmInfo.loc[0,'OprationManageRate']
  164. all_period_fault_info.fillna(0,inplace=False)
  165. print(all_period_fault_info)
  166. task_on=0
  167. def DaTa_Sta_Minutes_Task():
  168. task_on=1
  169. factory_info=['骑享','金茂换电']
  170. #............................获取数据................................
  171. host='172.16.121.236'
  172. port=3306
  173. db='fastfun'
  174. user='readonly'
  175. password='Fast1234'
  176. mode=3
  177. tablename1='ff_battery_accum'
  178. tablename2='ff_location'
  179. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  180. with DBRead as DBRead:
  181. df_last_accum=DBRead.getdata('devcode','dsg_phaccum','dsg_ahaccum',tablename=tablename1,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  182. df_last_pos=DBRead.getdata('devcode','latitude','longitude',tablename=tablename2,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  183. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  184. port=3306
  185. db='qx_cas'
  186. user='qx_algo_readonly'
  187. password='qx@123456'
  188. mode=3
  189. tablename2='bat_first_data_time'
  190. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  191. with DBRead as DBRead:
  192. df_FirstDataTime=DBRead.getdata('sn','first_data_time',tablename=tablename2,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  193. #............................获取时间................................
  194. end_time=datetime.datetime.now()
  195. # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d')
  196. start_time=end_time-datetime.timedelta(days=1)
  197. start_time=start_time.strftime('%Y-%m-%d')
  198. end_time=end_time.strftime('%Y-%m-%d')
  199. #............................执行程序................................
  200. all_statistic_info=DataFrame(columns=['factory','total_alarm','alarm_total_today','alarm_not_close_today','alarm_close_today','alarm_uregent_total_today','alarm_uregent_close_today','alarm_uregent_not_close_today','alarm_close_total','run_time_total','dischrg_total','odo_total','max_dischrg_one','max_runtime_one','max_cycle_one','max_odo_one','alarm_total','cell_type','cell_type_count','cell_safety_risk_count','data_safety_risk_count','status_safety_risk_count','hv_safety_risk_count','system_safety_risk_count','sample_safety_risk_count','controller_safety_risk_count','design_safety_risk_count'])
  201. for j in range(0,len(factory_info)):
  202. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  203. port=3306
  204. db='safety_platform'
  205. user='qx_algo_readonly'
  206. password='qx@123456'
  207. mode=2
  208. tablename1='all_fault_info'
  209. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  210. with DBRead as DBRead:
  211. df_fltinfo=DBRead.getdata('product_id','level','code','start_time','batpos',tablename=tablename1,factory=factory_info[j],sn='',timename='',st='',sp='')#dbdownload经过了改编
  212. FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(df_fltinfo,start_time,end_time,factory_info[j])
  213. SatftyCount=DataSta.SftyWrngClsfy(df_fltinfo)
  214. MaxAccumAh,TotalAccumAh,MaxCycle,MaxRunningHour,TotalRunHour=DataSta.AccumInfo(df_last_accum,df_FirstDataTime,end_time,factory_info[j])
  215. all_location_info=DataSta.FltBatPosition(df_last_pos,df_fltinfo,factory_info[j])
  216. all_statistic_info.loc[j,'factory']=factory_info[j]
  217. all_statistic_info.loc[j,'total_alarm']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total']
  218. all_statistic_info.loc[j,'alarm_total_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day']
  219. all_statistic_info.loc[j,'alarm_close_today']=FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count']
  220. all_statistic_info.loc[j,'alarm_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day']-FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count']
  221. all_statistic_info.loc[j,'alarm_uregent_total_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day']
  222. all_statistic_info.loc[j,'alarm_uregent_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count']
  223. all_statistic_info.loc[j,'alarm_uregent_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day']-FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count']
  224. all_statistic_info.loc[j,'run_time_total']=TotalRunHour
  225. all_statistic_info.loc[j,'dischrg_total']=TotalAccumAh
  226. all_statistic_info.loc[j,'odo_total']=0
  227. all_statistic_info.loc[j,'max_dischrg_one']=MaxAccumAh
  228. all_statistic_info.loc[j,'max_runtime_one']=MaxRunningHour
  229. all_statistic_info.loc[j,'max_cycle_one']=MaxCycle
  230. all_statistic_info.loc[j,'max_odo_one']=0
  231. all_statistic_info.loc[j,'alarm_close_total']=FltAlarmInfo.loc[0,'CS_Warning_Total_Finish_Count']
  232. all_statistic_info.loc[j,'alarm_total']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total']
  233. CellType=Celltype.columns.tolist()
  234. CellType=','.join(CellType)
  235. all_statistic_info.loc[0,'cell_type']=str(CellType)
  236. CellTypeCount=Celltype.loc[0].tolist()
  237. CellTypeCount=[str(x) for x in CellTypeCount]
  238. CellTypeCount=','.join(CellTypeCount)
  239. all_statistic_info.loc[j,'cell_type_count']=str(CellTypeCount)
  240. all_statistic_info.loc[j,'cell_safety_risk_count']=SatftyCount.loc[0,'CellSaftyCount']
  241. all_statistic_info.loc[j,'data_safety_risk_count']=SatftyCount.loc[0,'DataSaftyCodeCount']
  242. all_statistic_info.loc[j,'status_safety_risk_count']=SatftyCount.loc[0,'StateSaftyCodeCount']
  243. all_statistic_info.loc[j,'hv_safety_risk_count']=SatftyCount.loc[0,'HvSaftyCodeCount']
  244. all_statistic_info.loc[j,'system_safety_risk_count']=SatftyCount.loc[0,'SysSaftyCodeCount']
  245. all_statistic_info.loc[j,'sample_safety_risk_count']=SatftyCount.loc[0,'SamplingSatyCount']
  246. all_statistic_info.loc[j,'controller_safety_risk_count']=SatftyCount.loc[0,'CtrlSaftyCodeCount']
  247. all_statistic_info.loc[j,'design_safety_risk_count']=SatftyCount.loc[0,'DsnSaftyCodeCount']
  248. end=time.time()
  249. #print(end-start)
  250. task_on=0
  251. print(all_statistic_info)
  252. all_statistic_info.to_excel('all_statistic_info.xlsx')
  253. #...............................................主函数.......................................................................................................................
  254. if __name__ == "__main__":
  255. global SNnums
  256. global task_on
  257. task_on=0
  258. excelpath=r'D:\ZLWORK\code\data_analyze_platform\USER\eric\SaftyCenter_CODE_V1\sn-20210903.xlsx'
  259. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  260. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  261. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  262. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  263. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  264. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  265. SNnums_6060=SNdata_6060['SN号'].tolist()
  266. SNnums_6040=SNdata_6040['SN号'].tolist()
  267. SNnums_4840=SNdata_4840['SN号'].tolist()
  268. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  269. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  270. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  271. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
  272. #SNnums=['TJMCL120502305038','TJMCL120502305032','TJMCL120502305022','TJMCL120502305026','TJMCL120502305032','TJMCL120502305044','TJMCL120502305048','TJMCL120502305012','TJMCL120502305010']
  273. SNnums = ['PK504B10100004341'] #SNnums_6040
  274. mylog=log.Mylog('log.txt','error')
  275. mylog.logcfg()
  276. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
  277. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  278. port=3306
  279. db='safety_platform'
  280. user='qx_algo_readonly'
  281. password='qx@123456'
  282. mode=2
  283. tablename2='all_fault_info'
  284. DBRead = DBDw.DBDownload(host, port, db, user, password,mode)
  285. with DBRead as DBRead:
  286. df_Diag_Ram = DBRead.getdata('start_time','end_time','product_id','code','level','info','advice','Batpos','factory',tablename=tablename2,factory='',sn='',timename='',st='',sp='')
  287. # result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result.csv',encoding='gbk')
  288. #定时任务.......................................................................................................................................................................
  289. scheduler = BlockingScheduler()
  290. diag_cal()
  291. DaTa_Sta_Minutes_Task()
  292. DaTa_Sta_Week_Task()
  293. if task_on==0:
  294. scheduler.add_job(DaTa_Sta_Week_Task, 'interval', days=7, id='Week_Task')
  295. scheduler.add_job(diag_cal, 'interval', seconds=900, id='diag_job')
  296. scheduler.add_job(DaTa_Sta_Minutes_Task, 'interval', seconds=180, id='Hour_Task')
  297. try:
  298. scheduler.start()
  299. except Exception as e:
  300. scheduler.shutdown()
  301. print(repr(e))
  302. mylog.logopt(e)