deploy_old.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. import pymysql
  12. from urllib import parse
  13. dbManager = DBManager.DBManager()
  14. if __name__ == "__main__":
  15. # 更新sn列表
  16. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  17. port=3306
  18. db='qixiang_oss'
  19. user='qixiang_oss'
  20. password='Qixiang2021'
  21. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  22. cursor = conn.cursor()
  23. cursor.execute("select sn, imei from app_device")
  24. res = cursor.fetchall()
  25. df_sn = pd.DataFrame(res, columns=['sn', 'imei'])
  26. conn.close();
  27. # 数据库配置
  28. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  29. port = 3306
  30. user = 'qx_cas'
  31. password = parse.quote_plus('Qx@123456')
  32. database = 'qx_cas'
  33. db_engine = create_engine(
  34. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  35. user, password, host, port, database
  36. ))
  37. DbSession = sessionmaker(bind=db_engine)
  38. # 日志配置
  39. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  40. if not os.path.exists('log/' + now_str):
  41. os.makedirs('log/' + now_str)
  42. log = Log.Mylog(log_name='signal_monitor', log_level = 'info')
  43. log.set_file_hl(file_name='log/{}/info.log'.format(now_str), log_level='info', size=1024* 1024 * 100)
  44. log.set_file_hl(file_name='log/{}/error.log'.format(now_str), log_level='error', size=1024* 1024 * 100)
  45. logger = log.get_logger()
  46. logger.info("pid is + {}".format(os.getpid()))
  47. # 读取sn列表
  48. df_sn = pd.read_csv('sn_list.csv')
  49. df_sn = df_sn.reset_index(drop=True)
  50. df_sn['StartTime'] = pd.to_datetime(df_sn['StartTime'])
  51. df_sn['EndTime'] = pd.to_datetime(df_sn['EndTime'])
  52. df_sn['ExitTime'] = pd.to_datetime(df_sn['ExitTime'])
  53. signalMonitor = SignalMonitor.SignalMonitor()
  54. cal_period = 24 # 计算间隔,单位h
  55. for i in range(1450, len(df_sn['sn'])): # 遍历SN号
  56. sn = [df_sn.loc[i,'sn']]
  57. if not (sn[0][0:2] == 'PK' or sn[0][0:2] == 'MG' or sn[0][0:2] == 'UD'):
  58. continue
  59. st = df_sn.loc[i, 'StartTime']
  60. if df_sn.loc[i, 'Service'] == 0:
  61. if pd.isnull(df_sn.loc[i, 'EndTime']) and pd.isnull(df_sn.loc[i, 'ExitTime']):
  62. continue
  63. elif pd.isnull(df_sn.loc[i, 'ExitTime']):
  64. et = df_sn.loc[i, 'EndTime']
  65. else:
  66. et = df_sn.loc[i, 'ExitTime']
  67. elif pd.isnull(df_sn.loc[i, 'EndTime']):
  68. otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
  69. et = pd.to_datetime(otherStyleTime)
  70. else:
  71. et = df_sn.loc[i, 'EndTime']
  72. df_last_state = pd.DataFrame(
  73. columns=['sn', 'current', 'Timestamp', 'PackState', 'LineState']) # 每日最后BMS数据
  74. df_last_state_gps = pd.DataFrame(
  75. columns=['sn', 'Timestamp', 'PackState', 'LineState', 'latitude', 'longitude']) # 每日最后GPS数据
  76. df_res = pd.DataFrame(columns=[
  77. 'sn', 'PackState', 'LineState', 'StartTime', 'EndTime', 'OfflineTime']) # 初始化BMS信号统计数据
  78. df_res_gps = pd.DataFrame(columns=[
  79. 'sn', 'PackState', 'LineState', 'StartTime', 'EndTime', 'OfflineTime', 'latitude', 'longitude']) # 初
  80. logger.info("{} {} START!".format(sn, str(st)))
  81. while st < et:
  82. try:
  83. df_res, df_state, df_last_state = signalMonitor.get_bms_offline_stat(
  84. sn, st, et, df_res, df_last_state, cal_period) # 计算每日BMS信号统计数据
  85. df_res_gps, df_last_state_gps = signalMonitor.get_gps_offline_stat(
  86. sn, st, et, df_state, df_res_gps, df_last_state_gps, cal_period) # 计算每日GPS信号统计数据
  87. st = st + datetime.timedelta(hours=cal_period)
  88. except Exception as e:
  89. st = st + datetime.timedelta(hours=cal_period)
  90. logger.error(traceback.format_exc)
  91. logger.error(u"{} :{},{} 任务运行错误".format(sn,st,et), exc_info=True)
  92. # 数据入库
  93. df_tosql = df_res_gps.copy()
  94. df_tosql.columns = ['sn', 'pack_state', 'line_state', 'start_time', 'end_time', 'offline_time','latitude', 'longitude']
  95. df_tosql.to_sql("signal_monitor_gps",con=db_engine, if_exists="append",index=False)
  96. df_tosql = df_res.copy()
  97. df_tosql.columns = ['sn', 'pack_state', 'line_state', 'start_time', 'end_time', 'offline_time']
  98. df_tosql.to_sql("signal_monitor_bms",con=db_engine, if_exists="append",index=False)
  99. # 数据入库
  100. df_tosql = df_last_state.copy()
  101. df_tosql.columns = ['sn', 'current', 'time_stamp', 'pack_state', 'line_state']
  102. df_tosql.to_sql("signal_monitor_bms_last_day",con=db_engine, if_exists="append",index=False)
  103. df_tosql = df_last_state_gps.copy()
  104. df_tosql.columns = ['sn', 'time_stamp', 'pack_state', 'line_state','latitude', 'longitude']
  105. df_tosql.to_sql("signal_monitor_gps_last_day",con=db_engine, if_exists="append",index=False)
  106. logger.info("{} {} DONE!".format(sn, str(st)))