deploy.py 6.4 KB

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