deploy.py 6.2 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, 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 urllib import parse
  15. import pymysql
  16. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import BatParam
  17. import datacompy
  18. from LIB.MIDDLE.SaftyCenter.Sorest import sor_est
  19. def cell_sor_test(df_sn):
  20. # 读取结果数据库
  21. host2='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  22. port2=3306
  23. db2='qx_cas'
  24. user2='qx_read'
  25. password2='Qx@123456'
  26. db_engine = create_engine(
  27. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  28. user, password, host, port, database
  29. ))
  30. start=time.time()
  31. end_time=datetime.datetime.now()
  32. start_time=end_time-datetime.timedelta(days=7)
  33. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  34. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  35. logger.info("cycle start !!!!!!!!!!!!!!!!!!!!")
  36. for i in range(0, len(df_sn)):
  37. try:
  38. sn = df_sn.loc[i, 'sn']
  39. if df_sn.loc[i, 'imei'][5:9] == 'N640':
  40. celltype=1 #6040三元电芯
  41. elif df_sn.loc[i, 'imei'][5:9] == 'N440':
  42. celltype=2 #4840三元电芯
  43. elif df_sn.loc[i, 'imei'][5:9] == 'L660':
  44. celltype=99 # 6060锂电芯
  45. elif df_sn.loc[i, 'imei'][3:5] == 'LX' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  46. celltype=3 #力信 50ah三元电芯
  47. elif df_sn.loc[i, 'imei'][3:9] == 'CLL128':
  48. celltype=100 # 重卡
  49. elif df_sn.loc[i, 'imei'][3:5] == 'CL' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  50. celltype=4 #CATL 50ah三元电芯
  51. else:
  52. logger.info("pid-{} celltype-{} SN: {} SKIP!".format(os.getpid(), "未知", sn))
  53. continue
  54. logger.info("pid-{} celltype-{} SN: {} START!".format(os.getpid(), celltype, sn))
  55. param = BatParam.BatParam(celltype)
  56. #读取原始数据库数据........................................................................................................................................................
  57. dbManager = DBManager.DBManager()
  58. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  59. df_bms = df_data['bms']
  60. if not df_bms.empty:
  61. Diag_sorvol_temp = sor_est.sor_est(sn,celltype,df_bms)#电压内阻估计
  62. df_sor_add = Diag_sorvol_temp.sor_cal()
  63. if not df_sor_add.empty:
  64. df_sor_add = df_sor_add.drop_duplicates(subset = ['sn','time'], keep = 'first', inplace = False)
  65. df_sor_add['add_time'] = datetime.datetime.now()
  66. df_sor_add.to_sql("mechanism_sorout",con=db_engine, if_exists="append",index=False)
  67. logger.info("pid-{} celltype-{} SN: {} DONE!".format(os.getpid(), celltype, sn))
  68. except:
  69. logger.error(traceback.format_exc)
  70. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  71. logger.info("cycle DONE !!!!!!!!!!!!!!!!!!!!")
  72. db_engine.dispose()
  73. if __name__ == "__main__":
  74. # 时间设置
  75. # now_time = datetime.datetime.now()
  76. # pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)# 前一日
  77. # end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 00:00:00")
  78. # start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 00:00:00")
  79. history_run_flag = False # 历史数据运行标志
  80. # 更新sn列表
  81. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  82. port=3306
  83. db='qixiang_oss'
  84. user='qixiang_oss'
  85. password='Qixiang2021'
  86. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  87. cursor = conn.cursor()
  88. cursor.execute("select sn, imei, add_time from app_device")
  89. res = cursor.fetchall()
  90. df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
  91. df_sn = df_sn.reset_index(drop=True)
  92. conn.close();
  93. # 数据库配置
  94. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  95. port = 3306
  96. user = 'qx_cas'
  97. password = parse.quote_plus('Qx@123456')
  98. database = 'qx_cas'
  99. db_engine = create_engine(
  100. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  101. user, password, host, port, database
  102. ))
  103. DbSession = sessionmaker(bind=db_engine)
  104. # 运行历史数据配置
  105. df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
  106. # 日志配置
  107. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  108. log_path = 'log/' + now_str
  109. if not os.path.exists(log_path):
  110. os.makedirs(log_path)
  111. log = Log.Mylog(log_name='saftyCenter_sorout', log_level = 'info')
  112. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  113. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  114. logger = log.get_logger()
  115. logger.info("pid is {}".format(os.getpid()))
  116. # 算法参数
  117. # host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  118. # port=3306
  119. # db='safety_platform'
  120. # user='qx_read'
  121. # password=parse.quote_plus('Qx@123456')
  122. # tablename='all_fault_info'
  123. # db_res_engine = create_engine(
  124. # "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  125. # user, password, host, port, db
  126. # ))
  127. #定时任务.......................................................................................................................................................................
  128. cell_sor_test(df_sn)
  129. # scheduler = BlockingScheduler()
  130. # scheduler.add_job(fun, 'interval', seconds=600, id='diag_job')
  131. # try:
  132. # scheduler.start()
  133. # except Exception as e:
  134. # scheduler.shutdown()
  135. # logger.error(str(e))