day_sta.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. __author__ = 'lmstack'
  2. # 每日指标统计函数
  3. # import CONFIGURE.PathSetting as PathSetting
  4. # import sys
  5. # sys.path.append(PathSetting.backend_path)
  6. # sys.path.append(PathSetting.middle_path)
  7. from LIB.BACKEND import DBManager, Log, DataPreProcess
  8. from LIB.MIDDLE import IndexStaByPeriod, IndexStaBOneCycle
  9. from LIB.BACKEND import DBManager
  10. from LIB.BACKEND import DBManager
  11. from LIB.BACKEND import DBManager
  12. from LIB.BACKEND import DBManager
  13. import importlib
  14. import datetime
  15. import os
  16. import pandas as pd
  17. import time
  18. importlib.reload(IndexStaByPeriod)
  19. dbManager = DBManager.DBManager()
  20. dataPrePro = DataPreProcess.DataPreProcess()
  21. indexPerSta = IndexStaByPeriod.IndexStaByPeriod()
  22. # log 文件配置
  23. myLog = Log.Mylog('day_sta')
  24. myLog.set_file_hl(file_name=r'D:\Platform\platform\FRONTEND\day_sta\day_sta.log', log_level='info')
  25. myLog.set_stream_hl(log_level='info')
  26. logger = myLog.get_logger()
  27. logger.info(str(os.getpid()))
  28. # sn文件读取
  29. sn_list = list(pd.read_excel('D:\Platform\platform\苏州电池列表.xlsx')['sn'])
  30. sn_list.extend(list(pd.read_excel('D:\Platform\platform\骑享北京6040电池包统计更新20210407.xlsx')['SN号']))
  31. sn = sn_list[0]
  32. # 字段设置及结果文件生成
  33. columns = ['sn', 'time', 'sumDriveTime', 'sumDriveSoc', 'sumDriveAh', 'sumDriveEnergy']
  34. st = datetime.datetime.strptime('00:00:00', '%H:%M:%S')
  35. for i in range(96):
  36. et = st + datetime.timedelta(minutes=15)
  37. columns.append(st.strftime('%H:%M') + '-' + et.strftime('%H:%M'))
  38. st = et
  39. result_path = r'D:\Platform\platform\FRONTEND\day_sta\result.csv'
  40. df_res = pd.DataFrame(columns=columns)
  41. if not os.path.exists(result_path):
  42. df_res.to_csv(result_path, index=False)
  43. # 时间范围设置
  44. start_time = '{} 00:00:00'.format('2020-01-01')
  45. end_time = '{} 00:00:00'.format('2021-06-01')
  46. sta_days = (datetime.datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S') - datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')).days
  47. count= 0
  48. sn_result = {}
  49. for sn in sn_list[:]:
  50. count += 1
  51. logger.info('{} start, {}/{} '.format(sn, str(count), str(len(sn_list))))
  52. if sn[2:5] == '500':
  53. cap = 40
  54. elif sn[2:5] == '504':
  55. cap = 55
  56. else:
  57. logger.info('{} cap error'.format(sn))
  58. cap = None
  59. continue
  60. sn_result.update({'sn':sn})
  61. logger.info('{} :{} to {} start'.format(sn, str(start_time), str(end_time)))
  62. # 获取数据
  63. df_bms, df_gps = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, gps_switch=True, mode=0)
  64. if df_bms.empty:
  65. continue
  66. # 数据预处理
  67. # 时间完全相同的数据仅保留一行
  68. df_bms_pro, df_gps_pro = dataPrePro.time_filter(df_bms, df_gps)
  69. # bms数据按照电流和状态分段, 然后在状态分段内部,根据时间跳变继续分段(解决段内数据丢失)
  70. df_bms_pro = dataPrePro.data_split_by_status(df_bms_pro)
  71. df_bms_pro = dataPrePro.data_split_by_time(df_bms_pro)
  72. # bms数据将两次充电间的状态合并
  73. df_bms_pro = dataPrePro.combine_drive_stand(df_bms_pro)
  74. # bms 数据计算行车和充电开始前后的静置时间
  75. df_bms_pro = dataPrePro.cal_stand_time(df_bms_pro)
  76. # gps 数据可靠性判断, 并增加里程和速度至gps数据(根据未合并的数据段判断)
  77. df_bms_pro, df_gps_pro, res_record= dataPrePro.gps_data_judge(df_bms_pro, df_gps_pro)
  78. # gps 数据可靠性判断, 并增加里程和速度至gps数据(根据已合并的数据段判断)
  79. df_bms_pro, df_gps_pro, res_record= dataPrePro.data_gps_judge_after_combine(df_bms_pro, df_gps_pro)
  80. for sta_day in range(sta_days):
  81. try:
  82. st_ = datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(days=sta_day)
  83. et_ =datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(days=sta_day+1)
  84. # 按天统计指标
  85. sn_result.update({'time':st_.strftime('%Y-%m-%d')})
  86. df_bms_period = df_bms_pro[(df_bms_pro['时间戳'] > st_.strftime('%Y-%m-%d %H:%M:%S')) & (df_bms_pro['时间戳'] <= et_.strftime('%Y-%m-%d %H:%M:%S'))]
  87. #df_gps_period = df_gps_pro[(df_gps_pro['时间戳'] > st_.strftime('%Y-%m-%d %H:%M:%S')) & (df_gps_pro['时间戳'] <= et_.strftime('%Y-%m-%d %H:%M:%S'))]
  88. sn_result.update({'sumDriveTime':[indexPerSta.drive_time_sta(df_bms_period)]})
  89. sn_result.update({'sumDriveSoc':[indexPerSta.drive_soc_sta(df_bms_period)]})
  90. sn_result.update({'sumDriveAh':[indexPerSta.drive_capacity_sta(cap, df_bms_period)]})
  91. sn_result.update({'sumDriveEnergy':[indexPerSta.drive_energy_sta(cap, df_bms_period)]})
  92. # 每天间隔15分钟 统计一次
  93. for i in range(96):
  94. cur_result = []
  95. st__ = st_ + datetime.timedelta(minutes=15 * i)
  96. et__ = st_ + datetime.timedelta(minutes=15 * (i+1))
  97. df_bms_period = df_bms_pro[(df_bms_pro['时间戳'] > st__.strftime('%Y-%m-%d %H:%M:%S')) & (df_bms_pro['时间戳'] <= et__.strftime('%Y-%m-%d %H:%M:%S'))]
  98. #df_gps_period = df_gps_pro[(df_gps_pro['时间戳'] > st__.strftime('%Y-%m-%d %H:%M:%S')) & (df_gps_pro['时间戳'] <= et__.strftime('%Y-%m-%d %H:%M:%S'))]
  99. cur_result.append(indexPerSta.drive_time_sta(df_bms_period))
  100. cur_result.append(indexPerSta.drive_soc_sta(df_bms_period))
  101. cur_result.append(indexPerSta.drive_capacity_sta(cap, df_bms_period))
  102. cur_result.append(indexPerSta.drive_energy_sta(cap, df_bms_period))
  103. key = st__.strftime('%H:%M') + '-' + et__.strftime('%H:%M')
  104. sn_result.update({key:[cur_result]})
  105. df_cur_res = pd.DataFrame(sn_result)
  106. df_cur_res = df_cur_res[columns]
  107. # 防止写入结果时,结果文件被打开
  108. write_flag = False
  109. while not write_flag:
  110. try:
  111. df_cur_res.to_csv(result_path, mode='a+', index=False, header=False)
  112. except PermissionError as e:
  113. logger.info('{} error:{}'.format(sn, str(e)))
  114. time.sleep(10)
  115. continue
  116. else:
  117. write_flag = True
  118. except Exception as e:
  119. logger.info('{} {}_{} error: {}'.format(sn, st_.strftime('%Y-%m-%d %H:%M:%S'), et_.strftime('%Y-%m-%d %H:%M:%S'), str(e)))
  120. continue
  121. else:
  122. continue
  123. logger.info('{} done, {}/{} '.format(sn, str(count), str(len(sn_list))))