main.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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=BatDiag.diag()
  116. else:
  117. df_Diag_Batdiag_update_xq=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  118. df_Diag_Batdiag_update_xq2=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  119. df_Diag_Batdiag_update2=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  120. df_Diag_Batdiag_update=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  121. 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)
  122. task_on=0
  123. #.................................统计程序...............................
  124. def DaTa_Sta_Week_Task():
  125. task_on=1
  126. factory_info=['骑享','金茂换电']
  127. all_period_fault_info=DataFrame(columns=['factory','week','level1_count','level2_count','level3_count','level4_count','level5_count','solve_rate'])
  128. df_fltinfopt=DataFrame(columns=['product_id','level','code','start_time','end_time','batpos','factory'])
  129. for i in range (0,len(factory_info)):
  130. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  131. port=3306
  132. db='safety_platform'
  133. user='qx_algo_readonly'
  134. password='qx@123456'
  135. mode=2
  136. tablename1='all_fault_info'
  137. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  138. with DBRead as DBRead:
  139. 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经过了改编
  140. df_fltinfopt=df_fltinfopt.append(df_fltinfo)
  141. df_fltinfopt=df_fltinfopt.reset_index(drop=True)
  142. print(df_fltinfopt)
  143. #............................获取数据................................
  144. for j in range(0,len(factory_info)):
  145. toweek='Week'+time.strftime('%W')
  146. df_fltinfo=df_fltinfopt[df_fltinfopt['factory']==factory_info[j]]
  147. #............................获取时间................................
  148. end_time=datetime.datetime.now()
  149. # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d')
  150. start_time_week=end_time-datetime.timedelta(days=7)
  151. start_time_week=start_time_week.strftime('%Y-%m-%d')
  152. end_time=end_time.strftime('%Y-%m-%d')
  153. FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(df_fltinfo,start_time_week,end_time,factory_info[j])
  154. FaultLvlCount=DataSta.WeekInfoSta(df_fltinfo,start_time_week,end_time)
  155. for i in range(1,6):
  156. if not FaultLvlCount[FaultLvlCount['level']==i]['product_id'].empty:
  157. all_period_fault_info.loc[j,'level'+str(i)+'_count']=int(FaultLvlCount[FaultLvlCount['level']==i]['product_id'].values)
  158. else:
  159. all_period_fault_info.loc[j,'level'+str(i)+'_count']=int(0)
  160. all_period_fault_info.loc[j,'factory']=factory_info[j]
  161. all_period_fault_info.loc[j,'week']=toweek
  162. all_period_fault_info.loc[j,'solve_rate']=FltAlarmInfo.loc[0,'OprationManageRate']
  163. all_period_fault_info.fillna(0,inplace=False)
  164. print(all_period_fault_info)
  165. task_on=0
  166. def DaTa_Sta_Minutes_Task():
  167. task_on=1
  168. factory_info=['骑享','金茂换电']
  169. #............................获取数据................................
  170. host='172.16.121.236'
  171. port=3306
  172. db='fastfun'
  173. user='readonly'
  174. password='Fast1234'
  175. mode=3
  176. tablename1='ff_battery_accum'
  177. tablename2='ff_location'
  178. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  179. with DBRead as DBRead:
  180. df_last_accum=DBRead.getdata('devcode','dsg_phaccum','dsg_ahaccum',tablename=tablename1,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  181. df_last_pos=DBRead.getdata('devcode','latitude','longitude',tablename=tablename2,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  182. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  183. port=3306
  184. db='qx_cas'
  185. user='qx_algo_readonly'
  186. password='qx@123456'
  187. mode=3
  188. tablename2='bat_first_data_time'
  189. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  190. with DBRead as DBRead:
  191. df_FirstDataTime=DBRead.getdata('sn','first_data_time',tablename=tablename2,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  192. #............................获取时间................................
  193. end_time=datetime.datetime.now()
  194. # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d')
  195. start_time=end_time-datetime.timedelta(days=1)
  196. start_time=start_time.strftime('%Y-%m-%d')
  197. end_time=end_time.strftime('%Y-%m-%d')
  198. #............................执行程序................................
  199. 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'])
  200. for j in range(0,len(factory_info)):
  201. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  202. port=3306
  203. db='safety_platform'
  204. user='qx_algo_readonly'
  205. password='qx@123456'
  206. mode=2
  207. tablename1='all_fault_info'
  208. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  209. with DBRead as DBRead:
  210. df_fltinfo=DBRead.getdata('product_id','level','code','start_time','batpos',tablename=tablename1,factory=factory_info[j],sn='',timename='',st='',sp='')#dbdownload经过了改编
  211. FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(df_fltinfo,start_time,end_time,factory_info[j])
  212. SatftyCount=DataSta.SftyWrngClsfy(df_fltinfo)
  213. MaxAccumAh,TotalAccumAh,MaxCycle,MaxRunningHour,TotalRunHour=DataSta.AccumInfo(df_last_accum,df_FirstDataTime,end_time,factory_info[j])
  214. all_location_info=DataSta.FltBatPosition(df_last_pos,df_fltinfo,factory_info[j])
  215. all_statistic_info.loc[j,'factory']=factory_info[j]
  216. all_statistic_info.loc[j,'total_alarm']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total']
  217. all_statistic_info.loc[j,'alarm_total_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day']
  218. all_statistic_info.loc[j,'alarm_close_today']=FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count']
  219. all_statistic_info.loc[j,'alarm_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day']-FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count']
  220. all_statistic_info.loc[j,'alarm_uregent_total_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day']
  221. all_statistic_info.loc[j,'alarm_uregent_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count']
  222. all_statistic_info.loc[j,'alarm_uregent_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day']-FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count']
  223. all_statistic_info.loc[j,'run_time_total']=TotalRunHour
  224. all_statistic_info.loc[j,'dischrg_total']=TotalAccumAh
  225. all_statistic_info.loc[j,'odo_total']=0
  226. all_statistic_info.loc[j,'max_dischrg_one']=MaxAccumAh
  227. all_statistic_info.loc[j,'max_runtime_one']=MaxRunningHour
  228. all_statistic_info.loc[j,'max_cycle_one']=MaxCycle
  229. all_statistic_info.loc[j,'max_odo_one']=0
  230. all_statistic_info.loc[j,'alarm_close_total']=FltAlarmInfo.loc[0,'CS_Warning_Total_Finish_Count']
  231. all_statistic_info.loc[j,'alarm_total']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total']
  232. CellType=Celltype.columns.tolist()
  233. CellType=','.join(CellType)
  234. all_statistic_info.loc[0,'cell_type']=str(CellType)
  235. CellTypeCount=Celltype.loc[0].tolist()
  236. CellTypeCount=[str(x) for x in CellTypeCount]
  237. CellTypeCount=','.join(CellTypeCount)
  238. all_statistic_info.loc[j,'cell_type_count']=str(CellTypeCount)
  239. all_statistic_info.loc[j,'cell_safety_risk_count']=SatftyCount.loc[0,'CellSaftyCount']
  240. all_statistic_info.loc[j,'data_safety_risk_count']=SatftyCount.loc[0,'DataSaftyCodeCount']
  241. all_statistic_info.loc[j,'status_safety_risk_count']=SatftyCount.loc[0,'StateSaftyCodeCount']
  242. all_statistic_info.loc[j,'hv_safety_risk_count']=SatftyCount.loc[0,'HvSaftyCodeCount']
  243. all_statistic_info.loc[j,'system_safety_risk_count']=SatftyCount.loc[0,'SysSaftyCodeCount']
  244. all_statistic_info.loc[j,'sample_safety_risk_count']=SatftyCount.loc[0,'SamplingSatyCount']
  245. all_statistic_info.loc[j,'controller_safety_risk_count']=SatftyCount.loc[0,'CtrlSaftyCodeCount']
  246. all_statistic_info.loc[j,'design_safety_risk_count']=SatftyCount.loc[0,'DsnSaftyCodeCount']
  247. end=time.time()
  248. #print(end-start)
  249. task_on=0
  250. print(all_statistic_info)
  251. all_statistic_info.to_excel('all_statistic_info.xlsx')
  252. #...............................................主函数.......................................................................................................................
  253. if __name__ == "__main__":
  254. global SNnums
  255. global task_on
  256. task_on=0
  257. excelpath=r'D:\ZLWORK\code\data_analyze_platform\USER\eric\SaftyCenter_CODE_V1\sn-20210903.xlsx'
  258. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  259. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  260. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  261. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  262. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  263. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  264. SNnums_6060=SNdata_6060['SN号'].tolist()
  265. SNnums_6040=SNdata_6040['SN号'].tolist()
  266. SNnums_4840=SNdata_4840['SN号'].tolist()
  267. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  268. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  269. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  270. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
  271. #SNnums=['TJMCL120502305038','TJMCL120502305032','TJMCL120502305022','TJMCL120502305026','TJMCL120502305032','TJMCL120502305044','TJMCL120502305048','TJMCL120502305012','TJMCL120502305010']
  272. SNnums = ['PK504B10100004341'] #SNnums_6040
  273. mylog=log.Mylog('log.txt','error')
  274. mylog.logcfg()
  275. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
  276. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  277. port=3306
  278. db='safety_platform'
  279. user='qx_algo_readonly'
  280. password='qx@123456'
  281. mode=2
  282. tablename2='all_fault_info'
  283. DBRead = DBDw.DBDownload(host, port, db, user, password,mode)
  284. with DBRead as DBRead:
  285. df_Diag_Ram = DBRead.getdata('start_time','end_time','product_id','code','level','info','advice','Batpos','factory',tablename=tablename2,factory='',sn='',timename='',st='',sp='')
  286. # result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result.csv',encoding='gbk')
  287. #定时任务.......................................................................................................................................................................
  288. scheduler = BlockingScheduler()
  289. diag_cal()
  290. DaTa_Sta_Minutes_Task()
  291. DaTa_Sta_Week_Task()
  292. if task_on==0:
  293. scheduler.add_job(DaTa_Sta_Week_Task, 'interval', days=7, id='Week_Task')
  294. scheduler.add_job(diag_cal, 'interval', seconds=900, id='diag_job')
  295. scheduler.add_job(DaTa_Sta_Minutes_Task, 'interval', seconds=180, id='Hour_Task')
  296. try:
  297. scheduler.start()
  298. except Exception as e:
  299. scheduler.shutdown()
  300. print(repr(e))
  301. mylog.logopt(e)