__author__ = 'lmstack' #coding=utf-8 import os import datetime import pandas as pd from LIB.BACKEND import DBManager from sqlalchemy import create_engine import time, datetime import traceback from LIB.MIDDLE.InfoChrgDrive.Charge.V1_0_0.coreV0 import * from urllib import parse import pymysql from apscheduler.schedulers.blocking import BlockingScheduler import logging import logging.handlers #...................................电池包电芯安全诊断函数...................................................................................................................... def info_charge(): # 日志 log_path = 'log/' if not os.path.exists(log_path): os.makedirs(log_path) logger = logging.getLogger("main") logger.setLevel(logging.DEBUG) # 根据日期滚动(每天产生1个文件) fh = logging.handlers.TimedRotatingFileHandler(filename='{}/main_info.log'.format(log_path), when="D", interval=1, backupCount=30, encoding="utf-8") formatter = logging.Formatter("%(asctime)s - %(name)s-%(levelname)s %(message)s") fh.suffix = "%Y-%m-%d_%H-%M-%S" fh.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}") fh.setFormatter(formatter) fh.setLevel(logging.INFO) logger.addHandler(fh) fh = logging.handlers.TimedRotatingFileHandler(filename='{}/main_error.log'.format(log_path), when="D", interval=1, backupCount=30, encoding="utf-8") formatter = logging.Formatter("%(asctime)s - %(name)s-%(levelname)s %(message)s") fh.suffix = "%Y-%m-%d_%H-%M-%S" fh.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}") fh.setFormatter(formatter) fh.setLevel(logging.ERROR) logger.addHandler(fh) logger.info(os.getpid()) # 更新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, add_time from app_device where status in (1,2,3)") res = cursor.fetchall() df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time']) df_sn = df_sn.reset_index(drop=True) cursor.close() conn.close() SNnums = list(df_sn['sn']) host2='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port2=3306 db2='safety_platform' user2='qx_algo_rw' password2='qx@123456' db_res_engine = create_engine( "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format( user2, parse.quote_plus(password2), host2, port2, db2 )) conn = pymysql.connect(host=host2, port=port2, user=user2, password=password2, database=db2) cursor = conn.cursor() df_diag_ram=pd.read_sql("select * from algo_charge_info", db_res_engine) now_time=datetime.datetime.now() start_time=now_time-datetime.timedelta(hours=24) start_time=start_time.strftime('%Y-%m-%d %H:%M:%S') end_time=now_time.strftime('%Y-%m-%d %H:%M:%S') for sn in SNnums: try: logger.info("pid-{} SN: {} START!".format(os.getpid(), sn)) #读取原始数据库数据........................................................................................................................................................ dbManager = DBManager.DBManager() df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms','gps']) df_bms = df_data['bms'] df_gps = df_data['gps'] #读取城市天气数据........................................................................................................................................................ gpscity=pd.read_csv('gps.csv') #调用主函数................................................................................................................................................................ if not df_bms.empty: #BMS数据非空 df_merge=pd.merge(df_bms,df_gps,how='outer',sort=True) df_merge['sn']=sn chrg_last=pd.Series() if not df_diag_ram.empty: #结果库非空 df_diag_ram_sn=df_diag_ram[df_diag_ram['sn']==sn] if not df_diag_ram_sn.empty: df_diag_ram_sn['time_end']=list(map(lambda x: datetime.datetime.strptime(str(x),'%Y-%m-%d %H:%M:%S'),list(df_diag_ram_sn['time_end']))) df_diag_ram_sn = df_diag_ram_sn.sort_values(by = 'time_end') df_diag_ram_sn.reset_index(inplace=True,drop=True) chrg_last=df_diag_ram_sn.iloc[-1] time_end=chrg_last['time_end'] df_diag_new,df_diag_change=pro_output(df_merge,sn,gpscity,chrg_last) kmeans1 = joblib.load('kmeans1.pkl') kmeans2 = joblib.load('kmeans2.pkl') kmeans3 = joblib.load('kmeans3.pkl') df_diag_new=prediction(df_diag_new,kmeans1,kmeans2,kmeans3) df_diag_change=prediction(df_diag_change,kmeans1,kmeans2,kmeans3) if not df_diag_change.empty: #需变更的结果非空 cursor.execute("DELETE FROM algo_charge_info WHERE time_end = '{}' and sn='{}'".format(time_end,sn)) df_diag_change.to_sql("algo_charge_info",con=db_res_engine, if_exists="append",index=False) logger.info(u"{} 更新成功!!!\n".format(sn), exc_info=True) #新增结果存入结果库..................................................................... if not df_diag_new.empty: #需新增的结果非空 df_diag_new.to_sql("algo_charge_info",con=db_res_engine, if_exists="append",index=False) logger.info(u"{} 写入成功!!!\n".format(sn), exc_info=True) except Exception as e: logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True) logger.error(traceback.format_exc) cursor.close() conn.close() if __name__ == "__main__": info_charge() #定时任务....................................................................................................................................................................... scheduler = BlockingScheduler() scheduler.add_job(info_charge, 'interval', hours=24) try: scheduler.start() except Exception as e: scheduler.shutdown() print(e)