deploy.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 pandas.core.frame import DataFrame
  14. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload
  15. from urllib import parse
  16. import pymysql
  17. import pdb
  18. from apscheduler.schedulers.blocking import BlockingScheduler
  19. import datacompy
  20. from LIB.MIDDLE.SaftyCenter.Common import DBDownload as DBDw
  21. from LIB.MIDDLE.SaftyCenter.Common import QX_BatteryParam
  22. from LIB.MIDDLE.SaftyCenter.Low_Soc_Alarm.low_soc_alarm import Low_soc_alarm
  23. if __name__ == "__main__":
  24. # 时间设置
  25. now_time = datetime.datetime.now()
  26. pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)# 前一日
  27. end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 07:00:00")
  28. start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 07:00:00")
  29. history_run_flag = False # 历史数据运行标志
  30. # 更新sn列表
  31. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  32. port=3306
  33. db='qixiang_oss'
  34. user='qixiang_oss'
  35. password='Qixiang2021'
  36. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  37. cursor = conn.cursor()
  38. cursor.execute("select sn, imei, add_time from app_device")
  39. res = cursor.fetchall()
  40. df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
  41. df_sn = df_sn.reset_index(drop=True)
  42. conn.close();
  43. # 数据库配置
  44. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  45. port = 3306
  46. user = 'qx_cas'
  47. password = parse.quote_plus('Qx@123456')
  48. database = 'qx_cas'
  49. db_engine = create_engine(
  50. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  51. user, password, host, port, database
  52. ))
  53. DbSession = sessionmaker(bind=db_engine)
  54. # 运行历史数据配置
  55. df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
  56. db_engine.dispose()
  57. # 日志配置
  58. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  59. log_path = 'log/' + now_str
  60. if not os.path.exists(log_path):
  61. os.makedirs(log_path)
  62. log = Log.Mylog(log_name='saftyCenter_lowSocAlarm', log_level = 'info')
  63. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  64. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  65. logger = log.get_logger()
  66. logger.info("pid is {}".format(os.getpid()))
  67. # 算法参数
  68. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  69. port=3306
  70. db='qixiang_manage'
  71. user='qx_query'
  72. password='@Qx_query'
  73. mode=4
  74. tablename1='py_battery'
  75. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  76. dbManager = DBManager.DBManager()
  77. low_soc_bat_list=DataFrame(columns=['sn','time','level'])
  78. with DBRead as DBRead:
  79. for i in range(0, len(df_sn)):
  80. try:
  81. if df_sn.loc[i, 'imei'][5:9] == 'N640':
  82. celltype=1 #6040三元电芯
  83. elif df_sn.loc[i, 'imei'][5:9] == 'N440':
  84. celltype=2 #4840三元电芯
  85. elif df_sn.loc[i, 'imei'][5:9] == 'L660':
  86. celltype=99 # 6060锂电芯
  87. elif df_sn.loc[i, 'imei'][3:5] == 'LX' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  88. celltype=3 #力信 50ah三元电芯
  89. elif df_sn.loc[i, 'imei'][3:9] == 'CLL128':
  90. celltype=100 # 重卡
  91. elif df_sn.loc[i, 'imei'][3:5] == 'CL' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  92. celltype=4 #CATL 50ah三元电芯
  93. # else:
  94. # logger.info("pid-{} celltype-{} SN: {} SKIP!".format(os.getpid(), "未知", sn))
  95. # continue
  96. sn = df_sn.loc[i, 'sn']
  97. param=QX_BatteryParam.BatteryInfo(celltype)
  98. logger.info("pid-{} celltype-{} SN: {} START!".format(os.getpid(), celltype, sn))
  99. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  100. df_bms = df_data['bms']
  101. # 处理运行历史数据
  102. if (history_run_flag):
  103. this_sn = df_first_data_time[df_first_data_time['sn']==sn]
  104. if (len(this_sn) == 0):
  105. start_time = pd.to_datetime(str(df_sn.loc[df_sn[df_sn['sn']==sn].index, 'add_time'].values[0])).strftime("%Y-%m-%d 00:00:00")
  106. else:
  107. first_data_time = df_first_data_time.loc[df_first_data_time[df_first_data_time['sn']==sn].index, 'first_data_time'].values[0]
  108. if pd.isnull(first_data_time):
  109. start_time = "2018-01-01 00:00:00"
  110. else:
  111. start_time = pd.to_datetime(str(df_first_data_time.loc[df_first_data_time[df_first_data_time['sn']==sn].index, 'first_data_time'].values[0])).strftime("%Y-%m-%d 00:00:00")
  112. df_OprtnSta=DBRead.getdata('qrcode','status', tablename=tablename1, sn=sn, timename='', st='', sp='',factory='')#取最后一条运营状态
  113. low_soc_bat_list=Low_soc_alarm.low_soc_alarm(param,df_bms,low_soc_bat_list,sn,df_OprtnSta)
  114. logger.info("pid-{} celltype-{} SN: {} DONE!".format(os.getpid(), celltype, sn))
  115. except:
  116. logger.error(traceback.format_exc)
  117. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  118. try:
  119. if not low_soc_bat_list.empty:
  120. low_soc_bat_list['add_time'] = datetime.datetime.now()
  121. low_soc_bat_list.to_sql("lowsoc_info",con=db_engine, if_exists="append",index=False)
  122. logger.info("入库成功!")
  123. finally:
  124. db_engine.dispose()