day_sta.py 6.3 KB

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