瀏覽代碼

根据在线状态更新EndpointHealth值

locky 1 年之前
父節點
當前提交
42ad34970e
共有 1 個文件被更改,包括 30 次插入11 次删除
  1. 30 11
      controller/deviceStatus.py

+ 30 - 11
controller/deviceStatus.py

@@ -528,9 +528,13 @@ class deviceStatus(TemplateView):
         # 通过app或设备打开/关闭插座电源时主动报告状态
         serial_number = request_dict.get('serial_number')
         value = request_dict.get('value')
-        cause_type = request_dict.get('cause_type')
+        online = request_dict.get('online')
+        cause_type = request_dict.get('cause_type', 'PHYSICAL_INTERACTION')
+
         logger = logging.getLogger('django')
-        logger.info('插座ChangeReport,serial_number:{}, value:{}, cause_type:{}'.format(serial_number, value, cause_type))
+        logger.info('插座ChangeReport参数,'
+                    'serial_number:{}, value:{}, cause_type:{}, online:{}'.
+                    format(serial_number, value, cause_type, online))
         if not all([serial_number, value, cause_type]):
             return JsonResponse({'code': 111, 'msg': '参数异常'})
         skill_name = 'loocam'
@@ -576,6 +580,29 @@ class deviceStatus(TemplateView):
             # 转换时间格式
             iso_string = datetime.datetime.now(timezone.utc).isoformat()
             time_of_sample = iso_string.split('.')[0] + 'Z'
+            properties = [
+                            {
+                                "namespace": "Alexa.PowerController",
+                                "name": "powerState",
+                                "value": value,
+                                "timeOfSample": time_of_sample,
+                                "uncertaintyInMilliseconds": 0
+                            }
+                        ]
+            if online is not None:
+                endpoint_health_value = 'OK' if online == '1' else 'UNREACHABLE'
+                properties.append(
+                    {
+                        "namespace": "Alexa.EndpointHealth",
+                        "name": "connectivity",
+                        "value": {
+                            "value": endpoint_health_value
+                        },
+                        "timeOfSample": "2017-02-03T16:20:50.52Z",
+                        "uncertaintyInMilliseconds": 0
+                    }
+                )
+
             payload = {
                 "event": {
                     "header": {
@@ -596,15 +623,7 @@ class deviceStatus(TemplateView):
                             "cause": {
                                 "type": cause_type
                             },
-                            "properties": [
-                                {
-                                    "namespace": "Alexa.PowerController",
-                                    "name": "powerState",
-                                    "value": value,
-                                    "timeOfSample": time_of_sample,
-                                    "uncertaintyInMilliseconds": 0
-                                }
-                            ]
+                            "properties": properties
                         }
                     }
                 }