Search in sources :

Example 1 with Firmware

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

the class ThingTypeResource method getFirmwares.

@GET
@Path("/{thingTypeUID}/firmwares")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get all available firmwares for provided thingType", response = StrippedThingTypeDTO.class, responseContainer = "Set")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 204, message = "No firmwares found.") })
public Response getFirmwares(@PathParam("thingTypeUID") @ApiParam(value = "thingTypeUID") String thingTypeUID, @HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = HttpHeaders.ACCEPT_LANGUAGE) String language) {
    ThingTypeUID athingTypeUID = new ThingTypeUID(thingTypeUID);
    Collection<Firmware> firmwares = firmwareRegistry.getFirmwares(athingTypeUID, LocaleUtil.getLocale(language));
    if (firmwares.isEmpty()) {
        return Response.status(Status.NO_CONTENT).build();
    }
    Stream<FirmwareDTO> firmwareStream = firmwares.stream().map(this::convertToFirmwareDTO);
    return Response.ok(null, MediaType.TEXT_PLAIN).entity(new Stream2JSONInputStream(firmwareStream)).build();
}
Also used : FirmwareDTO(org.eclipse.smarthome.core.thing.firmware.dto.FirmwareDTO) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) Stream2JSONInputStream(org.eclipse.smarthome.io.rest.Stream2JSONInputStream) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with Firmware

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

the class FirmwareUpdateConsoleCommandExtension method listFirmwares.

private void listFirmwares(Console console, String[] args) {
    if (args.length != 2) {
        console.println("Specify the thing type id to get its available firmwares: firmware list <thingTypeUID>");
        return;
    }
    ThingTypeUID thingTypeUID = new ThingTypeUID(args[1]);
    Collection<Firmware> firmwares = firmwareRegistry.getFirmwares(thingTypeUID);
    if (firmwares.isEmpty()) {
        console.println("No firmwares found.");
    }
    for (Firmware firmware : firmwares) {
        console.println(firmware.toString());
    }
}
Also used : ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware)

Example 3 with Firmware

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

the class FirmwareUpdateServiceTest method addHandler.

private FirmwareUpdateHandler addHandler(Thing thing) {
    FirmwareUpdateHandler mockHandler = mock(FirmwareUpdateHandler.class);
    when(mockHandler.getThing()).thenReturn(thing);
    doReturn(true).when(mockHandler).isUpdateExecutable();
    doAnswer(invocation -> {
        Firmware firmware = (Firmware) invocation.getArguments()[0];
        thing.setProperty(Thing.PROPERTY_FIRMWARE_VERSION, firmware.getVersion());
        return null;
    }).when(mockHandler).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
    firmwareUpdateService.addFirmwareUpdateHandler(mockHandler);
    return mockHandler;
}
Also used : FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware)

Example 4 with Firmware

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

the class FirmwareUpdateServiceTest method testBackgroundTransfer.

@Test
public void testBackgroundTransfer() throws Exception {
    Map<String, String> props = new HashMap<>();
    props.put(Thing.PROPERTY_FIRMWARE_VERSION, V111);
    Thing thing4 = ThingBuilder.create(THING_TYPE_UID3, THING4_ID).withProperties(props).build();
    FirmwareUpdateBackgroundTransferHandler handler4 = mock(FirmwareUpdateBackgroundTransferHandler.class);
    when(handler4.getThing()).thenReturn(thing4);
    doAnswer(invocation -> {
        return updateExecutable.get();
    }).when(handler4).isUpdateExecutable();
    doAnswer(invocation -> {
        Firmware firmware = (Firmware) invocation.getArguments()[0];
        thing4.setProperty(Thing.PROPERTY_FIRMWARE_VERSION, firmware.getVersion());
        updateExecutable.set(false);
        return null;
    }).when(handler4).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
    firmwareUpdateService.addFirmwareUpdateHandler(handler4);
    doAnswer(invocation -> {
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        updateExecutable.set(true);
        return null;
    }).when(handler4).transferFirmware(any(Firmware.class));
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING4_UID), is(unknownInfo));
    ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(mockPublisher, times(1)).post(eventCaptor.capture());
    assertFirmwareStatusInfoEvent(THING4_UID, eventCaptor.getAllValues().get(eventCaptor.getAllValues().size() - 1), unknownInfo);
    FirmwareProvider firmwareProvider2 = mock(FirmwareProvider.class);
    when(firmwareProvider2.getFirmware(eq(FW120_EN.getUID()), any(Locale.class))).thenReturn(FW120_EN);
    when(firmwareProvider2.getFirmwares(any(ThingTypeUID.class), any(Locale.class))).thenAnswer(invocation -> {
        ThingTypeUID thingTypeUID = (ThingTypeUID) invocation.getArguments()[0];
        if (THING_TYPE_UID3.equals(thingTypeUID)) {
            return Collections.singleton(FW120_EN);
        } else {
            return Collections.emptySet();
        }
    });
    firmwareRegistry.addFirmwareProvider(firmwareProvider2);
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING4_UID), is(updateAvailableInfo));
    verify(mockPublisher, times(2)).post(eventCaptor.capture());
    assertFirmwareStatusInfoEvent(THING4_UID, eventCaptor.getAllValues().get(eventCaptor.getAllValues().size() - 1), updateAvailableInfo);
    waitForAssert(() -> {
        assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING4_UID), is(updateExecutableInfoFw120));
        verify(mockPublisher, times(3)).post(eventCaptor.capture());
        assertFirmwareStatusInfoEvent(THING4_UID, eventCaptor.getAllValues().get(eventCaptor.getAllValues().size() - 1), updateExecutableInfoFw120);
    });
    firmwareUpdateService.updateFirmware(THING4_UID, FW120_EN.getUID(), null);
    waitForAssert(() -> {
        assertThat(thing4.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V120));
    });
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING4_UID), is(upToDateInfo));
    verify(mockPublisher, times(4)).post(eventCaptor.capture());
    assertFirmwareStatusInfoEvent(THING4_UID, eventCaptor.getAllValues().get(eventCaptor.getAllValues().size() - 1), upToDateInfo);
    assertThat(handler4.isUpdateExecutable(), is(false));
}
Also used : Locale(java.util.Locale) FirmwareUpdateBackgroundTransferHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler) HashMap(java.util.HashMap) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) Event(org.eclipse.smarthome.core.events.Event) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 5 with Firmware

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

the class ModelRestrictedFirmwareUpdateServiceOSGiTest method testUpdateModelRestrictedFirmware.

@Test
public void testUpdateModelRestrictedFirmware() {
    // given two things of the same thing type but with different models, both with firmware 1.0.0 installed
    ThingUID thingUID1 = createAndRegisterThing(THING_ID_1, MODEL_ID_1, VERSION_1_0_0).getUID();
    ThingUID thingUID2 = createAndRegisterThing(THING_ID_2, MODEL_ID_2, VERSION_1_0_0).getUID();
    // given a firmware provider that provides different firmwares for the different models
    Firmware firmwareModelA = createModelRestrictedFirmware(MODEL_ID_1, VERSION_1_0_1);
    Firmware firmwareModelB = createModelRestrictedFirmware(MODEL_ID_2, VERSION_1_0_2);
    registerService(createFirmwareProvider(firmwareModelA, firmwareModelB));
    // when the firmware on thing1 is updated to the firmware for model1
    firmwareUpdateService.updateFirmware(thingUID1, firmwareModelA.getUID(), null);
    // then the new firmware is installed on thing 1 and no more update is available
    waitForAssert(() -> {
        assertThatThingHasFirmware(thingUID1, VERSION_1_0_1);
        assertThatThingHasFirmwareStatus(thingUID1, UP_TO_DATE);
    });
    // and when the firmware on thing2 is updated to the firmware for model2
    firmwareUpdateService.updateFirmware(thingUID2, firmwareModelB.getUID(), null);
    // then the new firmware is installed on thing2 and no more update is available
    waitForAssert(() -> {
        assertThatThingHasFirmware(thingUID2, VERSION_1_0_2);
        assertThatThingHasFirmwareStatus(thingUID2, UP_TO_DATE);
    });
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Firmware (org.eclipse.smarthome.core.thing.binding.firmware.Firmware)12 Locale (java.util.Locale)6 FirmwareUpdateHandler (org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler)5 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)4 ProgressCallback (org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback)4 Event (org.eclipse.smarthome.core.events.Event)3 Thing (org.eclipse.smarthome.core.thing.Thing)3 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)3 FirmwareUpdateBackgroundTransferHandler (org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler)3 FirmwareStatusInfo (org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Test (org.junit.Test)3 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 TimeUnit (java.util.concurrent.TimeUnit)2 ConfigDescriptionValidator (org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator)2 SafeCaller (org.eclipse.smarthome.core.common.SafeCaller)2 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)2