Sfoglia il codice sorgente

获取设备电量显示

linhaohong 3 mesi fa
parent
commit
b6b9492167
1 ha cambiato i file con 81 aggiunte e 2 eliminazioni
  1. 81 2
      AdminController/DeviceManagementController.py

+ 81 - 2
AdminController/DeviceManagementController.py

@@ -1,5 +1,6 @@
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
+import datetime
 import json
 import operator
 import os
@@ -13,6 +14,7 @@ from django.core.paginator import Paginator
 from django.db import transaction
 from django.db.models import Q, F, Sum, OuterRef, Min, Subquery
 from django.forms.models import model_to_dict
+from django.utils import timezone
 from django.views.generic.base import View
 
 from Ansjer.config import LOGGER, SERIAL_DOMAIN_NAME, HUAWEICLOUD_AK, HUAWEICLOUD_SK, HUAWEICLOUD_OBS_SERVER, \
@@ -23,7 +25,8 @@ from Ansjer.config import SERVER_DOMAIN_TEST, SERVER_DOMAIN_CN, SERVER_DOMAIN_US
 from Model.models import Device_Info, UidSetModel, LogModel, UID_Bucket, Unused_Uid_Meal, StsCrdModel, \
     VodHlsModel, ExperienceContextModel, DeviceTypeModel, UidUserModel, ExperienceAiModel, AiService, \
     AppBundle, App_Info, AppDeviceType, DeviceNameLanguage, UIDCompanySerialModel, UidPushModel, \
-    CustomCustomerOrderInfo, CustomCustomerDevice, DeviceVersionInfo, VoicePromptModel, DeviceAlgorithmExplain, BaiduBigModelLicense
+    CustomCustomerOrderInfo, CustomCustomerDevice, DeviceVersionInfo, VoicePromptModel, DeviceAlgorithmExplain, BaiduBigModelLicense, \
+    DeviceDailyReport
 from Object.AWS.AmazonS3Util import AmazonS3Util
 from Object.Enums.RedisKeyConstant import RedisKeyConstant
 from Object.RedisObject import RedisObject
@@ -143,6 +146,9 @@ class DeviceManagement(View):
                 return self.edit_baidu_big_model_license(request, request_dict, response)
             elif operation == 'delBaiduBigModelLicense':   # 删除大模型许可证
                 return self.del_baidu_big_model_license(request_dict, response)
+
+            elif operation == 'devicePowerDisplay':
+                return self.device_power_display(request_dict, response)
             else:
                 return response.json(444, 'operation')
 
@@ -2064,4 +2070,77 @@ class DeviceManagement(View):
             return response.json(0)
         except Exception as e:
             return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
-        
+
+    @staticmethod
+    def device_power_display(request_dict, response):
+        """
+        获取设备电量显示
+        @param request_dict: 包含查询参数的字典
+        @param response: 响应对象
+        @return: 分页后的设备日报数据,包含聚合信息(如果条件满足)
+        """
+        device_id = request_dict.get('deviceId')
+        start_time = request_dict.get('startTime')
+        end_time = request_dict.get('endTime')
+        page = int(request_dict.get('page', 1))  # 默认第1页
+        page_size = int(request_dict.get('pageSize', 10))  # 默认每页10条
+
+        device_daily_report_qs = DeviceDailyReport.objects.filter(type=1)
+
+        # 应用过滤条件
+        if device_id:
+            device_daily_report_qs = device_daily_report_qs.filter(device_id=device_id)
+
+        if start_time and end_time:
+            start_time = int(start_time)
+            end_time = int(end_time)
+
+            device_daily_report_qs = device_daily_report_qs.filter(
+                report_time__gte=start_time,
+                report_time__lte=end_time
+            )
+        else:
+            device_daily_report_qs = device_daily_report_qs.exclude(report_time=0)
+
+        # 计算总数(用于分页)
+        total_count = device_daily_report_qs.count()
+
+        device_daily_report_qs = device_daily_report_qs.order_by('-report_time')
+
+        # 应用分页
+        paginator = Paginator(device_daily_report_qs, page_size)
+        device_daily_report_page = paginator.page(page)
+
+        # 序列化分页数据
+        device_daily_report = list(device_daily_report_page.object_list.values(
+            'device_id',
+            'battery_level',
+            'report_time',
+            'human_detection',
+            'working_hours',
+            'wake_sleep',
+            'pir_wakeup_count',
+            'mqtt_wakeup_count'
+        ))
+
+        # 构建返回数据
+        data = {
+            "list": device_daily_report,
+            "total": total_count,
+        }
+
+        # 如果满足条件(有设备ID和时间范围),计算聚合数据
+        if device_id and start_time and end_time:
+            aggregates = device_daily_report_qs.aggregate(
+                total_human_detection=Sum('human_detection'),
+                total_working_hours=Sum('working_hours'),
+                total_wake_sleep=Sum('wake_sleep')
+            )
+
+            data['data_statistics'] = {
+                'total_human_detection': aggregates.get('total_human_detection', 0),
+                'total_working_hours': aggregates.get('total_working_hours', 0),
+                'total_wake_sleep': aggregates.get('total_wake_sleep', 0)
+            }
+
+        return response.json(0, data)