deploy.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. __author__ = 'lmstack'
  2. #coding=utf-8
  3. import os
  4. import datetime
  5. import pandas as pd
  6. from LIB.BACKEND import DBManager
  7. from sqlalchemy import create_engine
  8. from sqlalchemy.orm import sessionmaker
  9. import datetime
  10. import dateutil.relativedelta
  11. import traceback
  12. from pandas.core.frame import DataFrame
  13. from urllib import parse
  14. import pymysql
  15. from LIB.MIDDLE.SaftyCenter.Common import DBDownload as DBDw
  16. from LIB.MIDDLE.SaftyCenter.Common import QX_BatteryParam
  17. from LIB.MIDDLE.SaftyCenter.Low_Soc_Alarm.low_soc_alarm import Low_soc_alarm
  18. import logging
  19. import logging.handlers
  20. import re
  21. if __name__ == "__main__":
  22. # 时间设置
  23. now_time = datetime.datetime.now()
  24. pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)# 前一日
  25. end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 07:00:00")
  26. start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 07:00:00")
  27. history_run_flag = False # 历史数据运行标志
  28. # 更新sn列表
  29. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  30. port=3306
  31. db='qixiang_oss'
  32. user='qixiang_oss'
  33. password='Qixiang2021'
  34. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  35. cursor = conn.cursor()
  36. cursor.execute("select sn, imei, add_time from app_device")
  37. res = cursor.fetchall()
  38. df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
  39. df_sn = df_sn.reset_index(drop=True)
  40. conn.close();
  41. # 数据库配置
  42. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  43. port = 3306
  44. user = 'qx_cas'
  45. password = parse.quote_plus('Qx@123456')
  46. database = 'qx_cas'
  47. db_engine = create_engine(
  48. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  49. user, password, host, port, database
  50. ))
  51. DbSession = sessionmaker(bind=db_engine)
  52. # 运行历史数据配置
  53. df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
  54. db_engine.dispose()
  55. # 日志
  56. log_path = 'log/'
  57. if not os.path.exists(log_path):
  58. os.makedirs(log_path)
  59. logger = logging.getLogger("main")
  60. logger.setLevel(logging.DEBUG)
  61. # 根据日期滚动(每天产生1个文件)
  62. fh = logging.handlers.TimedRotatingFileHandler(filename='{}/main_info.log'.format(log_path), when="D", interval=1, backupCount=30,
  63. encoding="utf-8")
  64. formatter = logging.Formatter("%(asctime)s - %(name)s-%(levelname)s %(message)s")
  65. fh.suffix = "%Y-%m-%d_%H-%M-%S"
  66. fh.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}")
  67. fh.setFormatter(formatter)
  68. fh.setLevel(logging.INFO)
  69. logger.addHandler(fh)
  70. fh = logging.handlers.TimedRotatingFileHandler(filename='{}/main_error.log'.format(log_path), when="D", interval=1, backupCount=30,
  71. encoding="utf-8")
  72. formatter = logging.Formatter("%(asctime)s - %(name)s-%(levelname)s %(message)s")
  73. fh.suffix = "%Y-%m-%d_%H-%M-%S"
  74. fh.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}")
  75. fh.setFormatter(formatter)
  76. fh.setLevel(logging.ERROR)
  77. logger.addHandler(fh)
  78. logger.info("pid is {}".format(os.getpid()))
  79. # 算法参数
  80. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  81. port=3306
  82. db='qixiang_manage'
  83. user='qx_query'
  84. password='@Qx_query'
  85. mode=4
  86. tablename1='py_battery'
  87. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  88. dbManager = DBManager.DBManager()
  89. low_soc_bat_list=DataFrame(columns=['sn','time','level'])
  90. with DBRead as DBRead:
  91. for i in range(0, len(df_sn)):
  92. try:
  93. if df_sn.loc[i, 'imei'][5:9] == 'N640':
  94. celltype=1 #6040三元电芯
  95. elif df_sn.loc[i, 'imei'][5:9] == 'N440':
  96. celltype=2 #4840三元电芯
  97. elif df_sn.loc[i, 'imei'][5:9] == 'L660':
  98. celltype=99 # 6060锂电芯
  99. elif df_sn.loc[i, 'imei'][3:5] == 'LX' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  100. celltype=3 #力信 50ah三元电芯
  101. elif df_sn.loc[i, 'imei'][3:9] == 'CLL128':
  102. celltype=100 # 重卡
  103. elif df_sn.loc[i, 'imei'][3:5] == 'CL' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  104. celltype=4 #CATL 50ah三元电芯
  105. elif df_sn.loc[i, 'imei'][5:9] == 'L420':
  106. celltype=101 #20ah磷酸铁锂电芯
  107. elif df_sn.loc[i, 'imei'][5:9] == 'L264':
  108. celltype=102 #120ah磷酸铁锂电芯
  109. # else:
  110. # logger.info("pid-{} celltype-{} SN: {} SKIP!".format(os.getpid(), "未知", sn))
  111. # continue
  112. sn = df_sn.loc[i, 'sn']
  113. param=QX_BatteryParam.BatteryInfo(celltype)
  114. logger.info("pid-{} celltype-{} SN: {} START!".format(os.getpid(), celltype, sn))
  115. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  116. df_bms = df_data['bms']
  117. # 处理运行历史数据
  118. if (history_run_flag):
  119. this_sn = df_first_data_time[df_first_data_time['sn']==sn]
  120. if (len(this_sn) == 0):
  121. 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")
  122. else:
  123. first_data_time = df_first_data_time.loc[df_first_data_time[df_first_data_time['sn']==sn].index, 'first_data_time'].values[0]
  124. if pd.isnull(first_data_time):
  125. start_time = "2018-01-01 00:00:00"
  126. else:
  127. 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")
  128. df_OprtnSta=DBRead.getdata('qrcode','status', tablename=tablename1, sn=sn, timename='', st='', sp='',factory='')#取最后一条运营状态
  129. low_soc_bat_list=Low_soc_alarm.low_soc_alarm(param,df_bms,low_soc_bat_list,sn,df_OprtnSta)
  130. logger.info("pid-{} celltype-{} SN: {} DONE!".format(os.getpid(), celltype, sn))
  131. except:
  132. logger.error(traceback.format_exc)
  133. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  134. try:
  135. if not low_soc_bat_list.empty:
  136. low_soc_bat_list['add_time'] = datetime.datetime.now()
  137. low_soc_bat_list.to_sql("lowsoc_info",con=db_engine, if_exists="append",index=False)
  138. logger.info("入库成功!")
  139. except:
  140. logger.error(traceback.format_exc)
  141. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  142. finally:
  143. db_engine.dispose()