main.py 17 KB

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