zhuxi 2 лет назад
Родитель
Сommit
25ca40f505

+ 27 - 2
LIB/MIDDLE/InfoChrgDrive/Charge/V1_0_0/coreV0.py

@@ -435,7 +435,7 @@ def pro_output(df_merge,sn,gpscity,chrg_last):
     return new,change
 
 ############################ Test ##########################
-def prediction(datatest,kmeans1,kmeans2,kmeans3):
+def prediction(datatest,kmeans1,kmeans2,kmeans3,kmeans4,kmeans5):
     if len(datatest)>0:
         datatest.reset_index(drop=True,inplace=True)
         #筛选充电数据
@@ -447,7 +447,8 @@ def prediction(datatest,kmeans1,kmeans2,kmeans3):
         Xt=pd.concat([X00t,X01t])
         #按温度切分数据
         Xt1=Xt[Xt['airtemp_st']>=18]
-        Xt2=Xt[Xt['airtemp_st']<18]
+        Xt2=Xt[(Xt['airtemp_st']<18)&(Xt['airtemp_st']>=10)]
+        Xt3=Xt[Xt['airtemp_st']<10]
         #温暖天气模型预测
         if len(Xt1)>0:
             #KMeans1
@@ -484,4 +485,28 @@ def prediction(datatest,kmeans1,kmeans2,kmeans3):
                     Xt182=Xt17[Xt17['cluster_db']==1]
                     for k in list(Xt182.index):
                         datatest.loc[k,'charge_env']='疑似室内'
+        if len(Xt2)>0:
+            #KMeans4
+            Xt21=Xt2[['standtime_f','temp_incr']]
+            y_hat = kmeans4.predict(np.array(Xt21))
+            Xt21['cluster_db']=y_hat
+            Xt221=Xt21[(Xt21['cluster_db']==0)|(Xt21['cluster_db']==1)]
+            Xt221['charge_env']='室外'
+            datatest=pd.merge(datatest,Xt221['charge_env'],how='outer',right_index=True,left_index=True)
+            #KMeans5
+            Xt22=Xt21[(Xt21['cluster_db']==2)|(Xt21['cluster_db']==3)]
+            if len(Xt22)>0:
+                Xt23=pd.merge(Xt22['cluster_db'],Xt2,how='left',right_index=True,left_index=True)
+                Xt24=Xt23[['standtime_f','delta_time']]
+                y_hat2 = kmeans5.predict(np.array(Xt24))
+                Xt24['cluster_db']=y_hat2
+                Xt251=Xt24[(Xt24['cluster_db']==0)|(Xt24['cluster_db']==2)]
+                for k in list(Xt251.index):
+                    datatest.loc[k,'charge_env']='室外'
+                Xt250=Xt24[Xt24['cluster_db']==1]
+                for k in list(Xt250.index):
+                    datatest.loc[k,'charge_env']='室内'
+                Xt252=Xt24[Xt24['cluster_db']==3]
+                for k in list(Xt252.index):
+                    datatest.loc[k,'charge_env']='疑似室内'
     return datatest

BIN
LIB/MIDDLE/InfoChrgDrive/Charge/kmeans4.pkl


BIN
LIB/MIDDLE/InfoChrgDrive/Charge/kmeans5.pkl


+ 65 - 25
LIB/MIDDLE/InfoChrgDrive/Charge/main_V0.py

@@ -1,19 +1,22 @@
-from coreV0 import *
+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
-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
+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()
@@ -24,9 +27,9 @@ def diag_cal():
     #数据库配置
     host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
     port=3306
-    db='qx_cas'
-    user='qx_read'
-    password='Qx@123456'
+    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, \
@@ -46,8 +49,6 @@ def diag_cal():
             user, parse.quote_plus(password), host, port, db
         ))
 
-    mylog=log.Mylog('log_info_charge.txt','error')
-    mylog.logcfg()
 
     for sn in SNnums:
         try:
@@ -59,7 +60,7 @@ def diag_cal():
             df_gps = df_data['gps']
 
             #读取城市天气数据........................................................................................................................................................
-            gpscity=pd.read_csv('gps.csv')
+            gpscity=pd.read_csv('LIB/MIDDLE/InfoChrgDrive/Charge/gps.csv')
             
 
             #调用主函数................................................................................................................................................................
@@ -78,11 +79,9 @@ def diag_cal():
                         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)
+
+                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()
@@ -96,22 +95,63 @@ def diag_cal():
             print(end-start)  
             
         except Exception as e:
-            print(repr(e))
-            mylog.logopt(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)
     
-    #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'])
+     # 根据日期滚动(每天产生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();
     
-    mylog=log.Mylog('log_info_charge.txt','error')
-    mylog.logcfg()
+    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()
     #定时任务.......................................................................................................................................................................
@@ -122,5 +162,5 @@ if __name__ == "__main__":
         scheduler.start()
     except Exception as e:
         scheduler.shutdown()
-        print(repr(e))
-        mylog.logopt(e)
+        logger.error(str(e))
+        logger.error(traceback.format_exc())