main.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #coding=utf-8
  2. import os
  3. import datetime
  4. import pandas as pd
  5. from LIB.BACKEND import DBManager, Log
  6. from LIB.MIDDLE import SignalMonitor
  7. from sqlalchemy import create_engine
  8. from sqlalchemy.orm import sessionmaker
  9. import time, datetime
  10. import traceback
  11. from urllib import parse
  12. dbManager = DBManager.DBManager()
  13. if __name__ == "__main__":
  14. # 读取sn列表
  15. df_sn = pd.read_csv('sn_list.csv')
  16. df_sn = df_sn.reset_index(drop=True)
  17. df_sn['StartTime'] = pd.to_datetime(df_sn['StartTime'])
  18. df_sn['EndTime'] = pd.to_datetime(df_sn['EndTime'])
  19. df_sn['ExitTime'] = pd.to_datetime(df_sn['ExitTime'])
  20. signalMonitor = SignalMonitor.SignalMonitor()
  21. cal_period = 24 # 计算间隔,单位h
  22. for i in range(0, len(df_sn['sn'])): # 遍历SN号
  23. sn = [df_sn.loc[i,'sn']]
  24. if not (sn[0][0:2] == 'PK' or sn[0][0:2] == 'MG' or sn[0][0:2] == 'UD'):
  25. continue
  26. st = df_sn.loc[i, 'StartTime']
  27. if df_sn.loc[i, 'Service'] == 0:
  28. if pd.isnull(df_sn.loc[i, 'EndTime']) and pd.isnull(df_sn.loc[i, 'ExitTime']):
  29. continue
  30. elif pd.isnull(df_sn.loc[i, 'ExitTime']):
  31. et = df_sn.loc[i, 'EndTime']
  32. else:
  33. et = df_sn.loc[i, 'ExitTime']
  34. elif pd.isnull(df_sn.loc[i, 'EndTime']):
  35. otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
  36. et = pd.to_datetime(otherStyleTime)
  37. else:
  38. et = df_sn.loc[i, 'EndTime']
  39. df_last_state = pd.DataFrame(
  40. columns=['sn', 'current', 'Timestamp', 'PackState', 'LineState']) # 每日最后BMS数据
  41. df_last_state_gps = pd.DataFrame(
  42. columns=['sn', 'Timestamp', 'PackState', 'LineState', 'latitude', 'longitude']) # 每日最后GPS数据
  43. df_res = pd.DataFrame(columns=[
  44. 'sn', 'PackState', 'LineState', 'StartTime', 'EndTime', 'OfflineTime']) # 初始化BMS信号统计数据
  45. df_res_gps = pd.DataFrame(columns=[
  46. 'sn', 'PackState', 'LineState', 'StartTime', 'EndTime', 'OfflineTime', 'latitude', 'longitude']) # 初始化GPS信号统计数据
  47. while st < et:
  48. df_res, df_state, df_last_state = signalMonitor.get_bms_offline_stat(
  49. sn, st, et, df_res, df_last_state, cal_period) # 计算每日BMS信号统计数据
  50. df_res_gps, df_last_state_gps = signalMonitor.get_gps_offline_stat(
  51. sn, st, et, df_state, df_res_gps, df_last_state_gps, cal_period) # 计算每日GPS信号统计数据
  52. # 数据入库
  53. st = st + datetime.timedelta(hours=cal_period)
  54. SignalMonitor._file_write(r'D:\result_04.xls', df_res) # BMS信号统计数据入库
  55. SignalMonitor._file_write(r'D:\result_05.xls', df_res_gps) # GPS信号统计数据入库
  56. SignalMonitor._file_write(r'D:\result_06.xls', df_last_state)
  57. SignalMonitor._file_write(r'D:\result_07.xls', df_last_state_gps)
  58. # 数据入库