#coding=utf-8 import os import datetime import pandas as pd from LIB.BACKEND import DBManager, Log from LIB.MIDDLE import SignalMonitor from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker import time, datetime import traceback from LIB.MIDDLE.LeakCurrent import LFPLeakCurrent20210812 as LFPLeakCurrent from urllib import parse import pymysql import dateutil.relativedelta dbManager = DBManager.DBManager() if __name__ == "__main__": # 时间设置 now_time = datetime.datetime.now() pre_time = now_time + dateutil.relativedelta.relativedelta(weeks=-2) end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 09:00:00") start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 09:00:00") # 更新sn列表 host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com' port=3306 db='qixiang_oss' user='qixiang_oss' password='Qixiang2021' conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db) cursor = conn.cursor() cursor.execute("select sn, imei from app_device") res = cursor.fetchall() df_sn = pd.DataFrame(res, columns=['sn', 'imei']) df_sn = df_sn.reset_index(drop=True) conn.close(); # 数据库配置 host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port = 3306 user = 'qx_cas' password = parse.quote_plus('Qx@123456') database = 'qx_cas' db_engine = create_engine( "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format( user, password, host, port, database )) DbSession = sessionmaker(bind=db_engine) # 日志配置 now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_") log_path = 'log/6060/' + now_str if not os.path.exists(log_path): os.makedirs(log_path) log = Log.Mylog(log_name='leak_current', log_level = 'info') log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100) log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100) logger = log.get_logger() logger.info("pid is {}".format(os.getpid())) # SNdata_6060 = pd.read_excel('骑享资产梳理-20210621.xlsx', sheet_name='6060') # SNnums_6060=SNdata_6060['SN号'] # # SNnums_6060 = ['PK504B00100004019','PK504B00100004029', 'PK504B00100004080', 'PK504B00100004264', 'PK504B10100004331'] # now_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # now_time = "2021-08-01 00:00:00" # 定时任务开启时,需要删除改行!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # now_time=datetime.datetime.strptime(now_time,'%Y-%m-%d %H:%M:%S') # start_time=now_time-datetime.timedelta(days=1000) # end_time=str(now_time) # start_time=str(start_time) for i in range(0, len(df_sn)): try: if df_sn.loc[i, 'imei'][6:9] != '660': continue sn = df_sn.loc[i, 'sn'] logger.info("pid-{} 6060 SN: {} START!".format(os.getpid(), sn)) res = LFPLeakCurrent.cal_LFPLeakCurrent(sn, end_time, start_time) if not res.empty: res.columns = ['time', 'sn', 'cell1', 'cell2', 'cell3', 'cell4', 'cell5', 'cell6', 'cell7', 'cell8', 'cell9', 'cell10', 'cell11', 'cell12', 'cell13', 'cell14', 'cell15', 'cell16', 'cell17', 'cell18', 'cell19', 'cell20'] res.to_sql("consistency_leak_current",con=db_engine, if_exists="append",index=False) logger.info("6060 SN: {} DONE!".format(sn)) except: logger.error(traceback.format_exc) logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)