deploy.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. elif df_sn.loc[i, 'imei'][5:9] == 'L420':
  52. celltype=101 #20ah磷酸铁锂电芯
  53. else:
  54. logger.info("pid-{} celltype-{} SN: {} SKIP!".format(os.getpid(), "未知", sn))
  55. continue
  56. logger.info("pid-{} celltype-{} SN: {} START!".format(os.getpid(), celltype, sn))
  57. param = BatParam.BatParam(celltype)
  58. #读取原始数据库数据........................................................................................................................................................
  59. dbManager = DBManager.DBManager()
  60. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  61. df_bms = df_data['bms']
  62. if not df_bms.empty:
  63. Diag_sorvol_temp = sor_est.sor_est(sn,celltype,df_bms)#电压内阻估计
  64. df_sor_add = Diag_sorvol_temp.sor_cal()
  65. if not df_sor_add.empty:
  66. df_sor_add = df_sor_add.drop_duplicates(subset = ['sn','time'], keep = 'first', inplace = False)
  67. df_sor_add['add_time'] = datetime.datetime.now()
  68. df_sor_add.to_sql("mechanism_sorout",con=db_engine, if_exists="append",index=False)
  69. logger.info("pid-{} celltype-{} SN: {} DONE!".format(os.getpid(), celltype, sn))
  70. except:
  71. logger.error(traceback.format_exc)
  72. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  73. logger.info("cycle DONE !!!!!!!!!!!!!!!!!!!!")
  74. db_engine.dispose()
  75. if __name__ == "__main__":
  76. # 时间设置
  77. # now_time = datetime.datetime.now()
  78. # pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)# 前一日
  79. # end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 00:00:00")
  80. # start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 00:00:00")
  81. history_run_flag = False # 历史数据运行标志
  82. # 更新sn列表
  83. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  84. port=3306
  85. db='qixiang_oss'
  86. user='qixiang_oss'
  87. password='Qixiang2021'
  88. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  89. cursor = conn.cursor()
  90. cursor.execute("select sn, imei, add_time from app_device")
  91. res = cursor.fetchall()
  92. df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
  93. df_sn = df_sn.reset_index(drop=True)
  94. conn.close();
  95. # 数据库配置
  96. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  97. port = 3306
  98. user = 'qx_cas'
  99. password = parse.quote_plus('Qx@123456')
  100. database = 'qx_cas'
  101. db_engine = create_engine(
  102. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  103. user, password, host, port, database
  104. ))
  105. DbSession = sessionmaker(bind=db_engine)
  106. # 运行历史数据配置
  107. df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
  108. # 日志配置
  109. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  110. log_path = 'log/' + now_str
  111. if not os.path.exists(log_path):
  112. os.makedirs(log_path)
  113. log = Log.Mylog(log_name='saftyCenter_sorout', log_level = 'info')
  114. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  115. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  116. logger = log.get_logger()
  117. logger.info("pid is {}".format(os.getpid()))
  118. # 算法参数
  119. # host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  120. # port=3306
  121. # db='safety_platform'
  122. # user='qx_read'
  123. # password=parse.quote_plus('Qx@123456')
  124. # tablename='all_fault_info'
  125. # db_res_engine = create_engine(
  126. # "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  127. # user, password, host, port, db
  128. # ))
  129. #定时任务.......................................................................................................................................................................
  130. cell_sor_test(df_sn)
  131. # scheduler = BlockingScheduler()
  132. # scheduler.add_job(fun, 'interval', seconds=600, id='diag_job')
  133. # try:
  134. # scheduler.start()
  135. # except Exception as e:
  136. # scheduler.shutdown()
  137. # logger.error(str(e))