Search in sources :

Example 1 with FirmwareUID

use of org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID in project smarthome by eclipse.

the class ThingResource method updateFirmware.

@PUT
@Path("/{thingUID}/firmware/{firmwareVersion}")
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update thing firmware.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 400, message = "Firmware update preconditions not satisfied."), @ApiResponse(code = 404, message = "Thing not found.") })
public Response updateFirmware(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @PathParam("thingUID") @ApiParam(value = "thing") String thingUID, @PathParam("firmwareVersion") @ApiParam(value = "version") String firmwareVersion) throws IOException {
    Thing thing = thingRegistry.get(new ThingUID(thingUID));
    if (thing == null) {
        return getThingNotFoundResponse(thingUID);
    }
    FirmwareUID firmwareUID = new FirmwareUID(thing.getThingTypeUID(), firmwareVersion);
    try {
        firmwareUpdateService.updateFirmware(thing.getUID(), firmwareUID, LocaleUtil.getLocale(language));
    } catch (IllegalArgumentException | NullPointerException | IllegalStateException ex) {
        return JSONResponse.createResponse(Status.BAD_REQUEST, null, "Firmware update preconditions not satisfied.");
    }
    return Response.status(Status.OK).build();
}
Also used : FirmwareUID(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Thing(org.eclipse.smarthome.core.thing.Thing) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with FirmwareUID

use of org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID in project smarthome by eclipse.

the class FirmwareUpdateServiceTest method testPrerequisiteVersionCheck_illegalVersion.

@Test
public void testPrerequisiteVersionCheck_illegalVersion() {
    FirmwareProvider firmwareProvider2 = mock(FirmwareProvider.class);
    when(firmwareProvider2.getFirmware(any(FirmwareUID.class), any(Locale.class))).thenAnswer(invocation -> {
        FirmwareUID firmwareUID = (FirmwareUID) invocation.getArguments()[0];
        if (FW111_FIX_EN.getUID().equals(firmwareUID)) {
            return FW111_FIX_EN;
        } else if (FW113_EN.getUID().equals(firmwareUID)) {
            return FW113_EN;
        } else {
            return null;
        }
    });
    when(firmwareProvider2.getFirmwares(any(ThingTypeUID.class), any(Locale.class))).thenAnswer(invocation -> {
        ThingTypeUID thingTypeUID = (ThingTypeUID) invocation.getArguments()[0];
        if (THING_TYPE_UID_WITHOUT_FW.equals(thingTypeUID) || THING_TYPE_UID2.equals(thingTypeUID)) {
            return Collections.emptySet();
        } else {
            return Stream.of(FW111_FIX_EN, FW113_EN).collect(Collectors.toSet());
        }
    });
    firmwareRegistry.addFirmwareProvider(firmwareProvider2);
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw113));
    try {
        firmwareUpdateService.updateFirmware(THING1_UID, FW113_EN.getUID(), null);
        fail("Expeced an IllegalArgumentException, but it was not thrown.");
    } catch (IllegalArgumentException expected) {
        assertThat(expected.getMessage(), is(String.format("Firmware with UID %s requires at least firmware version %s to get installed. But the current firmware version of the thing with UID %s is %s.", FW113_EN.getUID(), FW113_EN.getPrerequisiteVersion(), THING1_UID, V111)));
    }
}
Also used : Locale(java.util.Locale) FirmwareUID(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 3 with FirmwareUID

use of org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID in project smarthome by eclipse.

the class FirmwareUpdateServiceTest method testPrerequisiteVersionCheck.

@Test
public void testPrerequisiteVersionCheck() {
    FirmwareProvider firmwareProvider2 = mock(FirmwareProvider.class);
    when(firmwareProvider2.getFirmware(any(FirmwareUID.class), any(Locale.class))).thenAnswer(invocation -> {
        FirmwareUID firmwareUID = (FirmwareUID) invocation.getArguments()[0];
        if (FW111_FIX_EN.getUID().equals(firmwareUID)) {
            return FW111_FIX_EN;
        } else if (FW113_EN.getUID().equals(firmwareUID)) {
            return FW113_EN;
        } else {
            return null;
        }
    });
    when(firmwareProvider2.getFirmwares(any(ThingTypeUID.class), any(Locale.class))).thenAnswer(invocation -> {
        ThingTypeUID thingTypeUID = (ThingTypeUID) invocation.getArguments()[0];
        if (THING_TYPE_UID_WITHOUT_FW.equals(thingTypeUID) || THING_TYPE_UID2.equals(thingTypeUID)) {
            return Collections.emptySet();
        } else {
            return Stream.of(FW111_FIX_EN, FW113_EN).collect(Collectors.toSet());
        }
    });
    firmwareRegistry.addFirmwareProvider(firmwareProvider2);
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw113));
    firmwareUpdateService.updateFirmware(THING1_UID, FW111_FIX_EN.getUID(), null);
    waitForAssert(() -> {
        assertThat(thing1.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V111_FIX));
        assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw113));
    });
    firmwareUpdateService.updateFirmware(THING1_UID, FW113_EN.getUID(), null);
    waitForAssert(() -> {
        assertThat(thing1.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V113));
        assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(upToDateInfo));
        assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING2_UID), is(updateExecutableInfoFw113));
    });
    firmwareUpdateService.updateFirmware(THING2_UID, FW113_EN.getUID(), null);
    waitForAssert(() -> {
        assertThat(thing2.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V113));
        assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING2_UID), is(upToDateInfo));
    });
}
Also used : Locale(java.util.Locale) FirmwareUID(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 4 with FirmwareUID

use of org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID in project smarthome by eclipse.

the class FirmwareUpdateConsoleCommandExtension method updateFirmware.

private void updateFirmware(Console console, String[] args) {
    if (args.length != 3) {
        console.println("Specify the thing id and the firmware version to update the firmware: firmware update <thingUID> <firmware version>");
        return;
    }
    ThingUID thingUID = new ThingUID(args[1]);
    FirmwareUpdateHandler firmwareUpdateHandler = getFirmwareUpdateHandler(thingUID);
    if (firmwareUpdateHandler == null) {
        console.println(String.format("No firmware update handler available for thing with UID %s.", thingUID));
        return;
    }
    FirmwareUID firmwareUID = new FirmwareUID(firmwareUpdateHandler.getThing().getThingTypeUID(), args[2]);
    firmwareUpdateService.updateFirmware(thingUID, firmwareUID, null);
    console.println("Firmware update started.");
}
Also used : FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler) FirmwareUID(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID)

Aggregations

FirmwareUID (org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID)4 Locale (java.util.Locale)2 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)2 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)2 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)2 Test (org.junit.Test)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 Thing (org.eclipse.smarthome.core.thing.Thing)1 FirmwareUpdateHandler (org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler)1