Pārlūkot izejas kodu

更新数据获取

lmstack 3 gadi atpakaļ
vecāks
revīzija
a876acda66
1 mainītis faili ar 71 papildinājumiem un 34 dzēšanām
  1. 71 34
      LIB/BACKEND/DBManager.py

+ 71 - 34
LIB/BACKEND/DBManager.py

@@ -103,26 +103,45 @@ class DBManager():
         return data
 
     @staticmethod
-    def _convert_to_dataframe_bms(data, mode=0):
+    def _convert_to_dataframe_bms(data, mode=0, CellUNum=0, CellTNum=0, OtherTNum=0):
         CellU = []
         CellT = []
         OtherT = []
-        CellU_Num = 0
-        CellT_Num = 0
-        OtherT_Num = 0
-        CellU_Num = len(data['ffBatteryStatus']['cellVoltageList'])
-        CellT_Num = len(data['ffBatteryStatus']['cellTempList'])
+        CellU_Num = CellUNum
+        CellT_Num = CellTNum
+        OtherT_Num = OtherTNum
+        # try:
+        #     CellU_Num = len(data['ffBatteryStatus']['cellVoltageList'])
+        #     CellU = np.array(data['ffBatteryStatus']['cellVoltageList']) * 1000
+        #     CellU = CellU.tolist()
+        # except:
+        #     CellU_Num = 0
+        # try:
+        #     CellT_Num = len(data['ffBatteryStatus']['cellTempList'])
+        #     CellU.extend(data['ffBatteryStatus']['cellTempList'])
+        # except:
+        #     CellT_Num = 0
 
-        try:
-            OtherT_Num = len(data['ffBatteryStatus']['otherTempList'])
-        except:
-            OtherT_Num = 0
-        for i in range(CellU_Num):
-            CellU.append(data['ffBatteryStatus']['cellVoltageList'][i]*1000)
-        for i in range(CellT_Num):
-            CellU.append(data['ffBatteryStatus']['cellTempList'][i])
-        for i in range(OtherT_Num):
-            CellU.append(data['ffBatteryStatus']['otherTempList'][i])
+        # try:
+        #     OtherT_Num = len(data['ffBatteryStatus']['otherTempList'])
+        #     CellU.extend(data['ffBatteryStatus']['otherTempList'])
+        # except:
+        #     OtherT_Num = 0
+   
+        CellU = np.array(data['ffBatteryStatus'].get('cellVoltageList',[]))
+        CellT = data['ffBatteryStatus'].get('cellTempList',[])
+        OtherT = data['ffBatteryStatus'].get('otherTempList',[])
+        if (len(CellU) != CellU_Num or len(CellT) != CellT_Num or len(OtherT) != OtherT_Num):
+            return pd.DataFrame()
+        CellU = CellU * 1000
+
+
+        # for i in range(CellU_Num):
+        #     CellU.append(data['ffBatteryStatus']['cellVoltageList'][i]*1000)
+        # for i in range(CellT_Num):
+        #     CellU.append(data['ffBatteryStatus']['cellTempList'][i])
+        # for i in range(OtherT_Num):
+        #     CellU.append(data['ffBatteryStatus']['otherTempList'][i])
         if mode == 0:
             data_len = 16
             
@@ -147,7 +166,7 @@ class DBManager():
         data_block = np.append(data_block,CellT)
         data_block = np.append(data_block,OtherT)
         data_block = data_block.reshape(1,len(data_block))
-        return data_block,CellU_Num,CellT_Num,OtherT_Num
+        return data_block
 
     @staticmethod
     def _convert_to_dataframe_gps(data, mode=0):
@@ -204,6 +223,8 @@ class DBManager():
                     columns=['时间戳','累计充电电量','累计充电能量','累计放电电量','累计放电能量', '累积里程'],data=data_block)
         return df
     
+    
+    
     @staticmethod
     def _get_data(urls,type_name,mode=0):
         data = DBManager._download_json_data(urls)
@@ -214,46 +235,61 @@ class DBManager():
             elif mode == 1:
                 name_const = ['时间戳','GSM信号','故障等级', '故障代码','开关状态', '总电流[A]','总电压[V]','充电状态', '单体压差', 'SOC[%]','SOH[%]', '绝缘电阻']
             i=0
+            st = time.time()
+            
+            # 计算本次最大电芯数量
             CellUNum = 0
             CellTNum = 0
-            OtherTNumm = 0
-            st = time.time()
+            OtherTNum = 0
+            for line in data:
+                temp = len(line['ffBatteryStatus'].get('cellVoltageList', []))
+                if (temp > CellUNum):
+                    CellUNum = temp
+                temp = len(line['ffBatteryStatus'].get('cellTempList', []))
+                if (temp > CellTNum):
+                    CellTNum = temp
+                temp = len(line['ffBatteryStatus'].get('otherTempList', []))
+                if (temp > OtherTNum):
+                    OtherTNum = temp
             
+            data_blocks = pd.DataFrame()
             for line in data:
                 et = time.time()
                 try:
                     if i==0:
-                        data_blocks,CellUNum,CellTNum,OtherTNumm = DBManager._convert_to_dataframe_bms(line, mode)
-                        i+=1
+                        data_blocks = DBManager._convert_to_dataframe_bms(line, mode, CellUNum, CellTNum, OtherTNum)
+                        if (len(data_blocks)>0):
+                            i+=1
                         continue
                 except Exception as e:
-                    print(str(e))
-                    i = 0
+                    continue
+                    
                 try:
-                    data_block,CellUNum,CellTNum,OtherTNumm = DBManager._convert_to_dataframe_bms(line, mode)
+                    data_block = DBManager._convert_to_dataframe_bms(line, mode, CellUNum, CellTNum, OtherTNum)
                 except Exception as e:
-                    print(str(e))
                     continue
+           
                 try:
-                    data_blocks = np.concatenate((data_blocks,data_block),axis=0)
-                except Exception as e: 
+                    if (len(data_block)>0):
+                        data_blocks = np.concatenate((data_blocks,data_block),axis=0)
+                except Exception as e:
+                    print(e)
                     if 'all the input array dimensions for the concatenation axis must match exactly' in str(e) or \
                     'all the input array dimensions except for the concatenation axis must match exactly'  in str(e):
                         pass
                     else:
                         raise e
-  
-                # print('\r'+str(i),end=" ")
-                # print(data_block)
-                # print(urls)
-                # print(time.time()-et)
                 i+=1
-            name_var = DBManager._get_var_name(CellUNum,CellTNum,OtherTNumm)
+  
+            name_var = DBManager._get_var_name(CellUNum,CellTNum,OtherTNum)
             name_const.extend(name_var)
             columns_name = name_const
             if i==0:
                 data_blocks = []
+
             df_all = pd.DataFrame(columns=columns_name,data=data_blocks)
+
+                
             if not df_all.empty:
                 df_all.loc[:,'时间戳'] = df_all.loc[:,'时间戳'].apply(lambda x:time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(int(x)/1000)))
             return df_all
@@ -363,6 +399,8 @@ class DBManager():
                 except Exception as e:
                     if 'Connection broken' in str(e):
                         continue
+                    elif 'Number of manager items must equal union of block items' in str(e):
+                        break
                     else:
                         raise Exception
                 else:
@@ -370,7 +408,6 @@ class DBManager():
                     gps_all_data = pd.concat([gps_all_data, gps_data], ignore_index=True)
                     system_all_data = pd.concat([system_all_data, system_data], ignore_index=True)
                     accum_all_data = pd.concat([accum_all_data, accum_data], ignore_index=True)
-
                     break
 
         bms_all_data = bms_all_data.reset_index(drop=True)