from LIB.MIDDLE.InfoChrgDrive.Charge.V1_0_0.coreV0 import * import pymysql import datetime import pandas as pd from LIB.BACKEND import DBManager dbManager = DBManager.DBManager() from sqlalchemy import create_engine from urllib import parse import datetime, time from apscheduler.schedulers.blocking import BlockingScheduler import os import traceback import logging import logging.handlers #...................................充电技术指标统计函数...................................................................................................................... def diag_cal(): global SNnums global kmeans1,kmeans2,kmeans3,kmeans4,kmeans5 start=time.time() 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') #数据库配置 host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port=3306 db='safety_platform' user='qx_algo_rw' password='qx@123456' #读取结果库数据...................................................... param='sn,time_st,time_end,status,delta_time,soc_st,soc_end,volt_st,volt_end,diffvolt_st,diffvolt_end, \ temp_max,temp_min,temp_incr,temp_mean,temp_st_mean,temp_end_mean,difftem_max,meancrnt,max_meancrnt, \ sts_flg,full_chrg_flg,ovchrg_flg,ovchrg_prop,gps_lon,gps_lat,standtime_f,standtime_b,city,airtemp_st,airtemp_end,charge_env' tablename='algo_charge_info' mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db) cursor = mysql.cursor() sql = "select %s from %s where time_end='0000-00-00 00:00:00'" %(param,tablename) cursor.execute(sql) res = cursor.fetchall() df_diag_ram= pd.DataFrame(res,columns=param.split(',')) db_res_engine = create_engine( "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format( user, parse.quote_plus(password), host, port, db )) for sn in SNnums: try: #读取原始数据库数据........................................................................................................................................................ 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('LIB/MIDDLE/InfoChrgDrive/Charge/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) df_diag_new=prediction(df_diag_new,kmeans1,kmeans2,kmeans3,kmeans4,kmeans5) df_diag_change=prediction(df_diag_change,kmeans1,kmeans2,kmeans3,kmeans4,kmeans5) if not df_diag_change.empty: #需变更的结果非空 cursor.execute("DELETE FROM algo_charge_info WHERE time_end = '{}' and sn='{}'".format(time_end,sn)) mysql.commit() df_diag_change.to_sql("algo_charge_info",con=db_res_engine, if_exists="append",index=False) #新增结果存入结果库..................................................................... if not df_diag_new.empty: #需新增的结果非空 df_diag_new.to_sql("algo_charge_info",con=db_res_engine, if_exists="append",index=False) end=time.time() print(end-start) except Exception as e: logger.error(str(e)) logger.error(traceback.format_exc()) cursor.close() mysql.close() #...............................................主函数起定时作用....................................................................................................................... if __name__ == "__main__": # 日志 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("pid is {}".format(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) conn.close(); SNnums = list(df_sn['sn']) kmeans1 = joblib.load('LIB/MIDDLE/InfoChrgDrive/Charge/kmeans1.pkl') kmeans2 = joblib.load('LIB/MIDDLE/InfoChrgDrive/Charge/kmeans2.pkl') kmeans3 = joblib.load('LIB/MIDDLE/InfoChrgDrive/Charge/kmeans3.pkl') kmeans4 = joblib.load('LIB/MIDDLE/InfoChrgDrive/Charge/kmeans4.pkl') kmeans5 = joblib.load('LIB/MIDDLE/InfoChrgDrive/Charge/kmeans5.pkl') diag_cal() #定时任务....................................................................................................................................................................... scheduler = BlockingScheduler() scheduler.add_job(diag_cal, 'interval', hours=24) try: scheduler.start() except Exception as e: scheduler.shutdown() logger.error(str(e)) logger.error(traceback.format_exc())