from coreV0 import * import pymysql import datetime import pandas as pd from LIB.BACKEND import DBManager from sqlalchemy import create_engine from urllib import parse import datetime, time from apscheduler.schedulers.blocking import BlockingScheduler from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload #from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log from LIB.MIDDLE.CellStateEstimation.Common import log #...................................充电技术指标统计函数...................................................................................................................... def diag_cal(): global SNnums 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='qx_cas' user='qx_read' 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 )) mylog=log.Mylog('log_info_charge.txt','error') mylog.logcfg() 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('gps.csv') #调用主函数................................................................................................................................................................ if not df_bms.empty: #BMS数据非空 df_merge=pd.merge(df_bms,df_gps,how='outer',sort=True) df_merge['sn']=sn df_diag_ram_sn=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: #该sn相关结果非空 df_diag_ram_sn.reset_index(inplace=True,drop=True) df_diag_ram_sn['time_end']=list(map(lambda x: datetime.datetime.strptime(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=df_diag_ram_sn.iloc[-1] time_end =df_diag_ram_sn['time_end'] df_diag_ram_sn.reset_index(inplace=True,drop=True) df_diag_new,df_diag_change=pro_output(df_merge,sn,gpscity,df_diag_ram_sn) 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)) 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: print(repr(e)) mylog.logopt(e) cursor.close() mysql.close() #...............................................主函数起定时作用....................................................................................................................... if __name__ == "__main__": #excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\spf\01qixiang\sn-20210903.xlsx' excelpath='sn-20210903.xlsx' dataSOH = pd.read_excel('sn-20210903.xlsx',sheet_name='sn-20210903') SNnums = list(dataSOH['sn']) mylog=log.Mylog('log_info_charge.txt','error') mylog.logcfg() diag_cal() #定时任务....................................................................................................................................................................... scheduler = BlockingScheduler() scheduler.add_job(diag_cal, 'interval', hours=24) try: scheduler.start() except Exception as e: scheduler.shutdown() print(repr(e)) mylog.logopt(e)