Пример #1
0
        def apiv1_device_command_get_post(webinterface, request, session,
                                          device_id, command_id):
            try:
                wait_time = float(request.args.get('_wait')[0])
            except:
                wait_time = 2

            arguments = args_to_dict(request.args)

            if 'inputs' in arguments:
                inputs = arguments['inputs']
            else:
                inputs = None
            if device_id in webinterface._Devices:
                device = webinterface._Devices[device_id]
            else:
                return return_not_found(request, 'device not found')
            # print("inputs: %s" % inputs)
            try:
                request_id = device.command(
                    cmd=command_id,
                    requested_by={
                        'user_id': session.user_id,
                        'component':
                        'yombo.gateway.lib.webinterface.routes.api_v1.devices.device_command',
                        'gateway': webinterface.gateway_id()
                    },
                    inputs=inputs,
                )
            except KeyError as e:
                return return_not_found(request, 'Error with command: %s' % e)

            DC = webinterface._Devices.device_commands[request_id]
            if wait_time > 0:
                exit_while = False
                start_time = time()
                while (start_time > (time() - wait_time)
                       and exit_while is False):
                    yield sleep(.075)
                    if DC.status_id >= 100:
                        exit_while = True
            if len(device.status_history) > 0:
                status_current = device.status_history[0].asdict()
            else:
                status_current = None

            if len(device.status_history) > 1:
                status_previous = device.status_history[1].asdict()
            else:
                status_previous = None

            return return_good(request,
                               payload={
                                   'device_command_id': request_id,
                                   'device_command': DC.asdict(),
                                   'status_current': status_current,
                                   'status_previous': status_previous,
                               })
 def apiv1_notifications_ack_get(webinterface, request, session, notification_id):
     session.has_access("notification", "*", "view", raise_error=True)
     try:
         webinterface._Notifications.ack(notification_id)
     except KeyError as e:
         return return_not_found(request)
     return return_good(request)
Пример #3
0
 def apiv1_notifications_ack_get(webinterface, request, session,
                                 notification_id):
     try:
         webinterface._Notifications.ack(notification_id)
     except KeyError as e:
         return return_not_found(request)
     return return_good(request)
Пример #4
0
        def apiv1_device_do_command_get_post(webinterface, request, session, device_command_id):
            if device_command_id in webinterface._Devices.device_commands:
                device_command = webinterface._Devices.device_commands[device_command_id]
                return return_good(
                    request,
                    payload=device_command.asdict()
                )

            return return_not_found(request, 'Error with device command id: %s' % device_command_id)
Пример #5
0
 def apiv1_notifications_ack_get(webinterface, request, session,
                                 notification_id):
     webinterface._Validate.id_string(notification_id)
     session.is_allowed(AUTH_PLATFORM_NOTIFICATION, "view",
                        notification_id)
     try:
         webinterface._Notifications.ack(notification_id)
     except KeyError as e:
         return return_not_found(request)
     return return_good(request)
Пример #6
0
        def apiv1_device_details_get(webinterface, request, session,
                                     device_id):
            arguments = args_to_dict(request.args)

            if device_id in webinterface._Devices:
                device = webinterface._Devices[device_id]
            else:
                return return_not_found(request, 'device not found')

            payload = device.asdict()
            if 'item' in arguments:
                payload = payload[arguments['item']]
            return return_good(request, payload=payload)
Пример #7
0
        def apiv1_permissions_details_get(webinterface, request, session,
                                          permission_id):
            webinterface._Validate.id_string(permission_id)
            session.is_allowed(AUTH_PLATFORM_PERMISSION, "view", permission_id)
            if permission_id in webinterface._Permissions:
                permission = webinterface._Permissions[permission_id]
            else:
                return return_not_found(request, "Role not found")

            return webinterface.render_api(
                request,
                data=JSONApi(permission),
                data_type="permissions",
            )
Пример #8
0
        def apiv1_device_command_getone_get(webinterface, request, session, device_command_id):
            session.has_access("device_command", device_command_id, "view", raise_error=True)
            if device_command_id in webinterface._DeviceCommands.device_commands:
                device_command = webinterface._DeviceCommands.device_commands[device_command_id]
                return webinterface.render_api(request, None,
                                               data_type="device_commands",
                                               attributes=device_command(device_command.asdict())
                                               )

                return return_good(
                    request,
                    payload=device_command.asdict()
                )

            return return_not_found(request, f"Error with device command id: {device_command_id}")
Пример #9
0
        def apiv1_device_details_get(webinterface, request, session,
                                     device_id):
            session.has_access("device", device_id, "view", raise_error=True)
            arguments = args_to_dict(request.args)
            if len(device_id) > 200 or isinstance(device_id, str) is False:
                return return_error(request, "invalid device_id format", 400)

            if device_id in webinterface._Devices:
                device = webinterface._Devices[device_id]
            else:
                return return_not_found(request, "Device not found")

            payload = device.asdict()
            if "item" in arguments:
                payload = payload[arguments["item"]]
            return return_good(request, payload=payload)
Пример #10
0
        def apiv1_camera_h264_get(webinterface, request, session, device_id):
            webinterface._Validate.id_string(device_id)
            session.is_allowed(AUTH_PLATFORM_DEVICE, "view", device_id)
            # print(f"auth_type: {session.auth_type}, auth_id: {session.auth.auth_id}")

            if device_id in webinterface._Devices:
                device = webinterface._Devices[device_id]
            else:
                return return_not_found(request, "Device not found")

            arguments = request_args(request)
            print("arguments: %s" % arguments)

            try:
                framerate = int(arguments["framerate"])
            except:
                framerate = 5

            try:
                video_bitrate = int(arguments["video_bitrate"])
            except:
                video_bitrate = 768

            try:
                video_profile = int(arguments["video_profile"])
            except:
                video_profile = "baseline"

            def write_to_http_h264_stream(stream_data, *args, **kwargs):
                """
                Takes a video stream from FFMPEG and spits it out to the client.
                """
                nonlocal request
                print("write_to_http_h264_stream: data size:: %s" %
                      len(stream_data))
                # print("write_to_mpeg_stream: image: %s" % image)
                request.write(bytes(stream_data))

            request.setHeader("Content-Type", "video/mp4")

            print("final framerate: %s - %s" % (framerate, type(framerate)))
            print("about to stream from: %s" % device.PLATFORM)
            yield device.stream_http_264_video(
                stream_callback=write_to_http_h264_stream,
                video_bitrate=video_bitrate,
                video_profile=video_profile)
            print("I'm finished streaming......")
Пример #11
0
        def apiv1_camera_mjpeg_get(webinterface, request, session, device_id):
            webinterface._Validate.id_string(device_id)
            session.is_allowed(AUTH_PLATFORM_DEVICE, "view", device_id)
            # print(f"auth_type: {session.auth_type}, auth_id: {session.auth.auth_id}")

            if device_id in webinterface._Devices:
                device = webinterface._Devices[device_id]
            else:
                return return_not_found(request, "Device not found")

            arguments = request_args(request)
            print("arguments: %s" % arguments)

            try:
                framerate = int(arguments["framerate"])
            except:
                framerate = 5

            try:
                quality = int(arguments["quality"])
            except:
                quality = 4

            def write_to_http_mjpeg_stream(image, *args, **kwargs):
                """
                Takes an image instance and writes it to the HTTP request instance.
                """
                # print("write_to_mpeg_stream: request: %s" % request)
                # print("write_to_mpeg_stream: image: %s" % image)
                request.write(
                    bytes(
                        '--frameboundary\r\n'
                        f'Content-Type: {image.content_type}\r\n'
                        f'Content-Length: {len(image.content)}\r\n\r\n',
                        'utf-8') + image.content + b'\r\n')

            request.setHeader(
                "Content-Type",
                "multipart/x-mixed-replace; boundary=--frameboundary")

            # print("final framerate: %s - %s" % (framerate, type(framerate)))
            # print("about to stream from: %s" % device.PLATFORM)
            yield device.stream_http_mjpeg_video(
                image_callback=write_to_http_mjpeg_stream,
                framerate=framerate,
                quality=quality)
Пример #12
0
        def apiv1_device_command_get_post(webinterface, request, session,
                                          device_id, command_id):
            session.has_access("device",
                               device_id,
                               "control",
                               raise_error=True)
            if len(device_id) > 200 or isinstance(device_id, str) is False:
                return return_error(request, "invalid device_id format", 400)
            if len(command_id) > 200 or isinstance(command_id, str) is False:
                return return_error(request, "invalid command_id format", 400)

            try:
                wait_time = float(request.args.get("_wait")[0])
            except:
                wait_time = 2

            arguments = args_to_dict(request.args)

            pin_code = arguments.get("pin_code", None)
            delay = arguments.get("delay", None)
            max_delay = arguments.get("max_delay", None)
            not_before = arguments.get("not_before", None)
            not_after = arguments.get("not_after", None)
            inputs = arguments.get("inputs", None)
            if device_id in webinterface._Devices:
                device = webinterface._Devices[device_id]
            else:
                return return_not_found(request, "Device not found")
            try:
                device_command_id = device.command(
                    cmd=command_id,
                    auth=session,
                    pin=pin_code,
                    delay=delay,
                    max_delay=max_delay,
                    not_before=not_before,
                    not_after=not_after,
                    inputs=inputs,
                    idempotence=request.idempotence,
                )
            except KeyError as e:
                print(
                    f"error with apiv1_device_command_get_post keyerror: {e}")
                return return_not_found(
                    request, f"Error with command, it is not found: {e}")
            except YomboWarning as e:
                print(f"error with apiv1_device_command_get_post warning: {e}")
                return return_error(request, f"Error with command: {e}")

            DC = webinterface._DeviceCommands.device_commands[
                device_command_id]
            if wait_time > 0:
                exit_while = False
                start_time = time()
                while (start_time > (time() - wait_time)
                       and exit_while is False):
                    yield sleep(.075)
                    if DC.status_id >= 100:
                        exit_while = True
            if len(device.status_history) > 0:
                status_current = device.status_history[0].asdict()
            else:
                status_current = None

            if len(device.status_history) > 1:
                status_previous = device.status_history[1].asdict()
            else:
                status_previous = None

            return return_good(request,
                               payload={
                                   "device_command_id": device_command_id,
                                   "device_command": DC.asdict(),
                                   "status_current": status_current,
                                   "status_previous": status_previous,
                               })
Пример #13
0
        def apiv1_devices_command_get_post(webinterface, request, session,
                                           device_id, command_id):
            webinterface._Validate.id_string(device_id)
            webinterface._Validate.id_string(command_id)
            session.is_allowed(AUTH_PLATFORM_DEVICE, "control", device_id)

            try:
                wait_time = float(request.args.get("_wait")[0])
            except:
                wait_time = 2

            print(f"rrequest.content.read(): {request.content.read()}")
            print(f"request.processed_body: {request.processed_body}")
            print(
                f"request.processed_body_encoding: {request.processed_body_encoding}"
            )
            print(f"request.args: {request.args}")
            if request.processed_body is not None:
                arguments = request.processed_body
            else:
                arguments = request_args(request)

            pin_code = arguments.get("pin_code", None)
            delay = arguments.get("delay", None)
            max_delay = arguments.get("max_delay", None)
            not_before = arguments.get("not_before", None)
            not_after = arguments.get("not_after", None)
            inputs = arguments.get("inputs", None)

            if device_id in webinterface._Devices:
                device = webinterface._Devices[device_id]
            else:
                return return_not_found(request, "Device id not found")

            if command_id in webinterface._Commands:
                command = webinterface._Commands[command_id]
            else:
                return return_not_found(request, "Command id not found")
            print(f"device control, input: {inputs}")
            try:
                device_command_id = yield device.command(
                    command=command,
                    authentication=session,
                    pin=pin_code,
                    delay=delay,
                    max_delay=max_delay,
                    not_before=not_before,
                    not_after=not_after,
                    inputs=inputs,
                    request_context=f"api/v1:{request.getClientIP()}"
                    # idempotence=request.idempotence,
                )
            except KeyError as e:
                print(
                    f"error with apiv1_device_command_get_post keyerror: {e}")
                return return_not_found(
                    request, f"Error with command, it is not found: {e}")
            except YomboWarning as e:
                print(f"error with apiv1_device_command_get_post warning: {e}")
                return return_error(request, f"Error with command: {e}")

            DC = webinterface._DeviceCommands.device_commands[
                device_command_id]
            if wait_time > 0:
                exit_while = False
                start_time = time()
                while (start_time > (time() - wait_time)
                       and exit_while is False):
                    yield sleep(.075)
                    if DC.status_id >= 100:
                        exit_while = True

            if len(device.state_history) > 0:
                status_current = device.state_history[0].to_dict(
                    include_meta=False)
            else:
                status_current = None

            if len(device.state_history) > 1:
                status_previous = device.state_history[1].to_dict(
                    include_meta=False)
            else:
                status_previous = None

            return webinterface.render_api(
                request,
                data=JSONApi(
                    data={
                        "type": device_command_id,
                        "id": device_command_id,
                        "attributes": {
                            "id": device_command_id,
                            "device_id": device.device_id,
                            "command_id": DC.command_id,
                            "device_command_id": device_command_id,
                            "device_command": DC.to_dict(include_meta=False),
                            "status_current": status_current,
                            "status_previous": status_previous,
                        }
                    }),
                data_type="device_commands",
            )