deploy.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. __author__ = 'lmstack'
  2. #coding=utf-8
  3. import os
  4. import datetime
  5. import pandas as pd
  6. from LIB.BACKEND import DBManager, Log
  7. from sqlalchemy import create_engine
  8. from sqlalchemy.orm import sessionmaker
  9. import time, datetime
  10. import dateutil.relativedelta
  11. import traceback
  12. from LIB.MIDDLE.CellStateEstimation.Common import log
  13. from LIB.MIDDLE.SaftyCenter.StatusSta.V1_0_0 import status_sta
  14. from urllib import parse
  15. import pymysql
  16. from LIB.BACKEND import DBManager
  17. if __name__ == "__main__":
  18. # 日志配置
  19. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  20. log_path = 'log/' + now_str
  21. if not os.path.exists(log_path):
  22. os.makedirs(log_path)
  23. log = Log.Mylog(log_name='saftyCenter_status_sta', log_level = 'info')
  24. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  25. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  26. logger = log.get_logger()
  27. logger.info("pid is {}".format(os.getpid()))
  28. try:
  29. # 时间设置
  30. now_time = datetime.datetime.now()
  31. pre_time = now_time + dateutil.relativedelta.relativedelta(hours=-1)#上小时时间
  32. end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d %H:00:00")
  33. start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d %H:00:00")
  34. history_run_flag = False # 历史数据运行标志
  35. # 更新sn列表
  36. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  37. port=3306
  38. db='qixiang_oss'
  39. user='qx_algo_rw'
  40. password='qx@123456'
  41. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  42. cursor = conn.cursor()
  43. cursor.execute("select sn, imei, add_time from app_device where status in (1,2,3)")
  44. res = cursor.fetchall()
  45. df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
  46. df_sn = df_sn.reset_index(drop=True)
  47. cursor.close()
  48. conn.close();
  49. df_sn['factory'] = '骑享'
  50. # db = 'qx_cas'
  51. # conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  52. # cursor = conn.cursor()
  53. # cursor.execute("select sn, factory from sn_list")
  54. # res = cursor.fetchall()
  55. # df_sn2 = pd.DataFrame(res, columns=['sn', 'factory'])
  56. # df_sn2['imei'] = ""
  57. # df_sn = pd.concat([df_sn, df_sn2], axis=0)
  58. # df_sn = df_sn.reset_index(drop=True)
  59. # 数据库配置
  60. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  61. port = 3306
  62. user = 'qx_cas'
  63. password = parse.quote_plus('Qx@123456')
  64. database = 'qx_cas'
  65. db_engine = create_engine(
  66. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  67. user, password, host, port, database
  68. ))
  69. DbSession = sessionmaker(bind=db_engine)
  70. # 运行历史数据配置
  71. df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
  72. # 算法参数
  73. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  74. port=3306
  75. db='safety_platform'
  76. user='qx_read'
  77. password=parse.quote_plus('Qx@123456')
  78. db_res_engine = create_engine(
  79. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  80. user, password, host, port, db
  81. ))
  82. except Exception as e:
  83. logger.error("任务初始出错" + str(e), exc_info=True)
  84. dbManager = DBManager.DBManager()
  85. charge_count = 0
  86. drive_count = 0
  87. stand_count = 0
  88. charge_count_jinmao = 0
  89. drive_count_jinmao = 0
  90. stand_count_jinmao = 0
  91. for i in range(0, len(df_sn)):
  92. sn = df_sn.loc[i, 'sn']
  93. try:
  94. logger.info("pid-{} SN: {} START!".format(os.getpid(), sn))
  95. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms', 'gps'])
  96. df_bms = df_data['bms']
  97. df_gps = df_data['gps']
  98. if df_sn.loc[i, 'imei'][3:9] == 'CLL128':
  99. factory = '金茂'
  100. charge_count_jinmao, drive_count_jinmao, stand_count_jinmao = status_sta.status_sta(df_bms, df_gps, start_time, charge_count_jinmao, drive_count_jinmao, stand_count_jinmao, factory)
  101. else:
  102. factory = '骑享'
  103. charge_count, drive_count, stand_count = status_sta.status_sta(df_bms, df_gps, start_time, charge_count, drive_count, stand_count)
  104. logger.info("pid-{} SN: {} DONE!".format(os.getpid(), sn))
  105. except:
  106. logger.error(traceback.format_exc)
  107. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  108. df_result = pd.DataFrame({'factory':['骑享'], 'time':[start_time], 'charge_count':[charge_count], 'drive_count':[drive_count], 'stand_count':stand_count})
  109. df_result_jinmao = pd.DataFrame({'factory':['金茂换电'], 'time':[start_time], 'charge_count':[charge_count_jinmao], 'drive_count':[drive_count_jinmao], 'stand_count':stand_count_jinmao})
  110. try:
  111. if not df_result.empty:
  112. df_result.columns = ['factory', 'time', 'charge_count', 'drive_count', 'stand_count']
  113. df_result.to_sql("period_status_statistic",con=db_res_engine, if_exists="append",index=False)
  114. logger.info("数据库写入成功!")
  115. if not df_result_jinmao.empty:
  116. df_result_jinmao.columns = ['factory', 'time', 'charge_count', 'drive_count', 'stand_count']
  117. df_result_jinmao.to_sql("period_status_statistic",con=db_res_engine, if_exists="append",index=False)
  118. logger.info("数据库写入成功!")
  119. except:
  120. logger.error(traceback.format_exc)
  121. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  122. db_res_engine.dispose()
  123. db_engine.dispose()