Browse Source

新增判断正负数方法

guanhailong 2 years ago
parent
commit
420d742798
2 changed files with 16 additions and 3 deletions
  1. 4 2
      Controller/SensorGateway/SmartSocketController.py
  2. 12 1
      Service/CommonService.py

+ 4 - 2
Controller/SensorGateway/SmartSocketController.py

@@ -137,17 +137,19 @@ class SmartSocketView(View):
             # 在线时长秒
             accumulated_time = request_dict.get('accumulatedTime', None)
             device_time = request_dict.get('deviceTime', None)
-            LOGGER.info('{}上报电量统计data:{}'.format(serial_number, request_dict))
             if not all([serial_number, watt, power, accumulated_time, device_time]):
                 return response.json(444)
             watt = float(watt)
             power = float(power)
             accumulated_time = int(accumulated_time)
             #  判断上报数据是否为负数
-            if watt < 0 or power < 0 or accumulated_time < 0:
+            number_list = [watt, power, accumulated_time]
+            number = CommonService.negative_number_judgment(number_list)
+            if not number:
                 LOGGER.info('{}插座上报负值data:{}'.format(serial_number, request_dict))
                 return response.json(177)
             now_time = int(time.time())
+            LOGGER.info('{}上报电量统计data:{}'.format(serial_number, request_dict))
             start_time, end_time = LocalDateTimeUtil.get_today_date(True)
             # 查询当前序列号当天是否有上传过电量统计
             power_qs = SocketPowerStatistics.objects.filter(serial_number=serial_number,

+ 12 - 1
Service/CommonService.py

@@ -744,4 +744,15 @@ GCqvlyw5dfxNA+EtxNE2wCW/LW7ENJlACgcfgPlBZtpLheWoZB/maw4=
         #  判断设备是否为ipc设备和是否支持云存
         if model == 2 and number in ['4', '5']:
             return True
-        return False
+        return False
+
+    @staticmethod
+    def negative_number_judgment(number_list):
+        """
+        判断正负数
+        @param number_list: float或int类型列表
+        """
+        if any(i < 0 for i in number_list):
+            return False
+        else:
+            return True