async def test_list_with_two_offline_device(app, client, devices_repo) -> None:
    device_id = uuid.uuid4()
    device_id2 = uuid.uuid4()

    await devices_repo.create(
        Device(id=device_id, name="my device 1", properties={}))
    await devices_repo.create(
        Device(id=device_id2, name="my device 2", properties={}))

    expected_device = {
        "id": str(device_id),
        "online": False,
        "name": "my device 1",
    }
    expected_device2 = {
        "id": str(device_id2),
        "online": False,
        "name": "my device 2",
    }

    url = app.url_path_for("devices:list")
    response = await client.get(url)
    items = response.json()

    assert response.status_code == 200
    assert [expected_device, expected_device2] == items
async def test_list_with_one_online_device_and_one_offline_device(
        app, client, devices_repo, device_factory) -> None:
    online_device_id = uuid.uuid4()
    offline_device_id = uuid.uuid4()

    await devices_repo.create(
        Device(id=offline_device_id, name="laptop", properties={}))

    expected_online_device = {
        "id": str(online_device_id),
        "online": True,
        "name": "desktop",
    }
    expected_offline_device = {
        "id": str(offline_device_id),
        "online": False,
        "name": "laptop",
    }
    async with device_factory(device_id=online_device_id,
                              name=expected_online_device["name"]):
        url = app.url_path_for("devices:list")
        response = await client.get(url)
        items = response.json()

        assert response.status_code == 200
        assert [expected_offline_device, expected_online_device] == items
示例#3
0
    async def create_device(
        *,
        device_id: Optional[str] = None,
        name: Optional[str] = None,
        response_payload: Optional[dict] = None,
    ):
        device_id = device_id or uuid.uuid4()
        name = name or f"Device-{device_id}"
        device = Device(id=device_id, name=name, properties={})
        await devices_repo.create(device)

        url = app.url_path_for("devices:connect")
        content = {"device": {"id": str(device_id)}}
        token = create_jwt_token(jwt_content=content,
                                 secret_key=secret_key,
                                 expires_delta=timedelta(hours=1))
        async with client.websocket_connect(
                path=url, headers={"Authorization": f"Bearer {token}"}) as ws:
            device = FakeDevice(ws, device_id, event_loop, printer)
            device.start_listen(response_payload=response_payload)
            yield device
async def get_device_hardware(
    device: Device = Depends(get_registered_device),
    broker_repo: DeviceBrokerRepo = Depends(get_repository(DeviceBrokerRepo)),
) -> GetDeviceHardwareResponse:
    hardware = device.get_hardware()  # type:ignore

    if hardware:
        return GetDeviceHardwareResponse.parse_obj(hardware)

    command = SyncDeviceHardwareCommand(device_id=device.id,
                                        correlation_id=uuid.uuid4())
    await broker_repo.send_command(command)
    try:
        event = await broker_repo.get(device.id,
                                      command.correlation_id)  # type: ignore
    except RuntimeError:
        raise HTTPException(status_code=status.HTTP_504_GATEWAY_TIMEOUT,
                            detail="device unavailable")
    return GetDeviceHardwareResponse(**event["event_attributes"])
async def get_device_system(
    device: Device = Depends(get_registered_device),
    broker_repo: DeviceBrokerRepo = Depends(get_repository(DeviceBrokerRepo)),
) -> GetDeviceSystemResponse:
    system = device.get_system()  # type: ignore

    if system:
        return GetDeviceSystemResponse(system_attributes=system)

    command = SyncDeviceSystemCommand(device_id=device.id, correlation_id=uuid.uuid4())
    await broker_repo.send_command(command)

    try:
        event = await broker_repo.get(device.id, command.correlation_id)  # type: ignore
    except RuntimeError:
        raise HTTPException(
            status_code=status.HTTP_504_GATEWAY_TIMEOUT, detail="device unavailable"
        )
    return GetDeviceSystemResponse(system_attributes=event["event_attributes"])
示例#6
0
async def get_device_software(
    device: Device = Depends(get_registered_device),
    broker_repo: DeviceBrokerRepo = Depends(get_repository(DeviceBrokerRepo)),
) -> GetListDeviceSoftware:
    software = device.get_software()  # type: ignore

    if software:
        return GetListDeviceSoftware.parse_obj(software)

    command = SyncDeviceSoftwareCommand(device_id=device.id,
                                        correlation_id=uuid.uuid4())
    await broker_repo.send_command(command)

    try:
        event = await broker_repo.get(device.id,
                                      command.correlation_id)  # type: ignore
    except RuntimeError:
        raise HTTPException(status_code=status.HTTP_504_GATEWAY_TIMEOUT,
                            detail="device unavailable")
    else:
        return GetListDeviceSoftware(
            __root__=event["event_attributes"]["installed_programs"])
 def to_entity(self) -> Device:  # type: ignore
     return Device(id=self.id, name=self.name, properties=self.properties)
 def from_entity(cls, device: Device) -> "DeviceInfraModel":  # type: ignore
     return cls.parse_obj(device.dict())