Search in sources :

Example 6 with Firmware

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

the class ModelRestrictedFirmwareUpdateServiceOSGiTest method testUpdateModelRestrictedFirmwareMultipleFirmwareProviders.

@Test
public void testUpdateModelRestrictedFirmwareMultipleFirmwareProviders() {
    // given two things of the same thing type but with different models and with different firmwares installed
    ThingUID thingUID1 = createAndRegisterThing(THING_ID_1, MODEL_ID_1, VERSION_1_0_1).getUID();
    ThingUID thingUID2 = createAndRegisterThing(THING_ID_2, MODEL_ID_2, VERSION_1_0_2).getUID();
    // given a firmware provider that provides the current firmware for both things,
    // and another firmware provider that provides a more recent firmware for the first model
    Firmware firmwareModelA = createModelRestrictedFirmware(MODEL_ID_1, VERSION_1_0_1);
    Firmware firmwareModelB = createModelRestrictedFirmware(MODEL_ID_2, VERSION_1_0_2);
    Firmware firmwareModelC = createModelRestrictedFirmware(MODEL_ID_1, VERSION_1_0_3);
    registerService(createFirmwareProvider(firmwareModelA, firmwareModelB));
    registerService(createFirmwareProvider(firmwareModelC));
    // then there is an update to 1.0.3 available for thing1
    FirmwareStatusInfo firmwareStatusInfoA = firmwareUpdateService.getFirmwareStatusInfo(thingUID1);
    assertThat(firmwareStatusInfoA.getFirmwareStatus(), is(UPDATE_EXECUTABLE));
    assertThat(firmwareStatusInfoA.getUpdatableFirmwareUID().getFirmwareVersion(), is(VERSION_1_0_3));
    // but there is no update available for thing2
    assertThatThingHasFirmwareStatus(thingUID2, UP_TO_DATE);
    // and when the firmware on thing1 is updated
    firmwareUpdateService.updateFirmware(thingUID1, firmwareModelC.getUID(), null);
    // then the new firmware is installed on thing 1 and no more update is available
    waitForAssert(() -> {
        assertThatThingHasFirmware(thingUID1, VERSION_1_0_3);
        assertThatThingHasFirmwareStatus(thingUID1, 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)

Example 7 with Firmware

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

the class FirmwareUpdateServiceTest method testEvents.

@Test
public void testEvents() {
    doAnswer(invocation -> {
        Firmware firmware = (Firmware) invocation.getArguments()[0];
        ProgressCallback progressCallback = (ProgressCallback) invocation.getArguments()[1];
        progressCallback.defineSequence(SEQUENCE);
        progressCallback.next();
        progressCallback.next();
        progressCallback.next();
        progressCallback.next();
        thing1.setProperty(Thing.PROPERTY_FIRMWARE_VERSION, firmware.getVersion());
        return null;
    }).when(handler1).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
    // getFirmwareStatusInfo() method will internally generate and post one FirmwareStatusInfoEvent event.
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
    firmwareUpdateService.updateFirmware(THING1_UID, FW112_EN.getUID(), null);
    AtomicReference<List<Event>> events = new AtomicReference<>(new ArrayList<>());
    ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
    waitForAssert(() -> {
        // Wait for four FirmwareUpdateProgressInfoEvents plus one FirmwareStatusInfoEvent event.
        verify(mockPublisher, atLeast(SEQUENCE.length + 1)).post(eventCaptor.capture());
    });
    events.get().addAll(eventCaptor.getAllValues());
    List<Event> list = events.get().stream().filter(event -> event instanceof FirmwareUpdateProgressInfoEvent).collect(Collectors.toList());
    assertTrue(list.size() >= SEQUENCE.length);
    for (int i = 0; i < SEQUENCE.length; i++) {
        FirmwareUpdateProgressInfoEvent event = (FirmwareUpdateProgressInfoEvent) list.get(i);
        assertThat(event.getTopic(), containsString(THING1_UID.getAsString()));
        assertThat(event.getThingUID(), is(THING1_UID));
        assertThat(event.getProgressInfo().getProgressStep(), is(SEQUENCE[i]));
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CoreMatchers(org.hamcrest.CoreMatchers) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) AtomicReference(java.util.concurrent.atomic.AtomicReference) FirmwareUpdateBackgroundTransferHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) FirmwareStatusInfo(org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo) SafeCaller(org.eclipse.smarthome.core.common.SafeCaller) Locale(java.util.Locale) FirmwareUID(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) After(org.junit.After) FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler) NoSuchElementException(java.util.NoSuchElementException) Bundle(org.osgi.framework.Bundle) ExpectedException(org.junit.rules.ExpectedException) Hashtable(java.util.Hashtable) Before(org.junit.Before) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) TranslationProvider(org.eclipse.smarthome.core.i18n.TranslationProvider) ConfigDescriptionValidator(org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) IOException(java.io.IOException) Test(org.junit.Test) Constants(org.eclipse.smarthome.core.thing.firmware.Constants) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) ThingBuilder(org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder) LocaleProvider(org.eclipse.smarthome.core.i18n.LocaleProvider) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ProgressStep(org.eclipse.smarthome.core.thing.binding.firmware.ProgressStep) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) Event(org.eclipse.smarthome.core.events.Event) ArrayList(java.util.ArrayList) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 8 with Firmware

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

the class FirmwareUpdateService method getFirmwareStatusInfo.

/**
 * Returns the {@link FirmwareStatusInfo} for the thing having the given thing UID.
 *
 * @param thingUID the UID of the thing (must not be null)
 * @return the firmware status info (is null if there is no {@link FirmwareUpdateHandler} for the thing
 *         available)
 * @throws NullPointerException if the given thing UID is null
 */
public FirmwareStatusInfo getFirmwareStatusInfo(ThingUID thingUID) {
    Objects.requireNonNull(thingUID, "Thing UID must not be null.");
    FirmwareUpdateHandler firmwareUpdateHandler = getFirmwareUpdateHandler(thingUID);
    if (firmwareUpdateHandler == null) {
        logger.trace("No firmware update handler available for thing with UID {}.", thingUID);
        return null;
    }
    Firmware latestFirmware = getLatestSuitableFirmware(firmwareUpdateHandler.getThing());
    FirmwareStatusInfo firmwareStatusInfo = getFirmwareStatusInfo(firmwareUpdateHandler, latestFirmware);
    processFirmwareStatusInfo(firmwareUpdateHandler, firmwareStatusInfo, latestFirmware);
    return firmwareStatusInfo;
}
Also used : FirmwareStatusInfo(org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo) FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware)

Example 9 with Firmware

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

the class FirmwareUpdateService method cancelFirmwareUpdate.

/**
 * Cancels the firmware update of the thing having the given thing UID by invoking the operation
 * {@link FirmwareUpdateHandler#cancel()} of the thing´s firmware update handler.
 *
 * @param thingUID the thing UID (must not be null)
 */
public void cancelFirmwareUpdate(final ThingUID thingUID) {
    Objects.requireNonNull(thingUID, "Thing UID must not be null.");
    final FirmwareUpdateHandler firmwareUpdateHandler = getFirmwareUpdateHandler(thingUID);
    if (firmwareUpdateHandler == null) {
        throw new IllegalArgumentException(String.format("There is no firmware update handler for thing with UID %s.", thingUID));
    }
    final ProgressCallbackImpl progressCallback = getProgressCallback(thingUID);
    logger.debug("Cancelling firmware update for thing with UID {}.", thingUID);
    safeCaller.create(firmwareUpdateHandler, FirmwareUpdateHandler.class).withTimeout(timeout).withAsync().onTimeout(() -> {
        logger.error("Timeout occurred while cancelling firmware update of thing with UID {}.", thingUID);
        progressCallback.failedInternal("timeout-error-during-cancel");
    }).onException(e -> {
        logger.error("Unexpected exception occurred while cancelling firmware update of thing with UID {}.", thingUID, e.getCause());
        progressCallback.failedInternal("unexpected-handler-error-during-cancel");
    }).withIdentifier(new Object()).build().cancel();
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) URISyntaxException(java.net.URISyntaxException) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) LoggerFactory(org.slf4j.LoggerFactory) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) FirmwareUpdateBackgroundTransferHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler) ThingStatusInfoChangedEvent(org.eclipse.smarthome.core.thing.events.ThingStatusInfoChangedEvent) Component(org.osgi.service.component.annotations.Component) FirmwareStatusInfo(org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo) SafeCaller(org.eclipse.smarthome.core.common.SafeCaller) Locale(java.util.Locale) FirmwareUID(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Activate(org.osgi.service.component.annotations.Activate) URI(java.net.URI) FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler) ThreadPoolManager(org.eclipse.smarthome.core.common.ThreadPoolManager) TranslationProvider(org.eclipse.smarthome.core.i18n.TranslationProvider) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) Deactivate(org.osgi.service.component.annotations.Deactivate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ConfigDescriptionValidator(org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) List(java.util.List) Modified(org.osgi.service.component.annotations.Modified) LocaleProvider(org.eclipse.smarthome.core.i18n.LocaleProvider) Event(org.eclipse.smarthome.core.events.Event) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler)

Example 10 with Firmware

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

the class FirmwareUpdateService method updateFirmware.

/**
 * Updates the firmware of the thing having the given thing UID by invoking the operation
 * {@link FirmwareUpdateHandler#updateFirmware(Firmware, ProgressCallback)} of the thing´s firmware update handler.
 * <p>
 * This operation is a non-blocking operation by spawning a new thread around the invocation of the firmware update
 * handler. The time out of the thread is 30 minutes.
 *
 * @param thingUID the thing UID (must not be null)
 * @param firmwareUID the UID of the firmware to be updated (must not be null)
 * @param locale the locale to be used to internationalize error messages (if null then the locale provided by the
 *            {@link LocaleProvider} is used)
 * @throws NullPointerException if given thing UID or firmware UID is null
 * @throws IllegalStateException if
 *             <ul>
 *             <li>there is no firmware update handler for the thing</li>
 *             <li>the firmware update handler is not able to execute the firmware update</li>
 *             </ul>
 * @throws IllegalArgumentException if
 *             <ul>
 *             <li>the firmware cannot be found</li>
 *             <li>the firmware is not suitable for the thing</li>
 *             <li>the firmware requires another prerequisite firmware version</li>
 *             </ul>
 */
public void updateFirmware(final ThingUID thingUID, final FirmwareUID firmwareUID, final Locale locale) {
    Objects.requireNonNull(thingUID, "Thing UID must not be null.");
    Objects.requireNonNull(firmwareUID, "Firmware UID must not be null.");
    final FirmwareUpdateHandler firmwareUpdateHandler = getFirmwareUpdateHandler(thingUID);
    if (firmwareUpdateHandler == null) {
        throw new IllegalArgumentException(String.format("There is no firmware update handler for thing with UID %s.", thingUID));
    }
    final Firmware firmware = getFirmware(firmwareUID);
    validateFirmwareUpdateConditions(firmware, firmwareUpdateHandler);
    final Locale loc = locale != null ? locale : localeProvider.getLocale();
    final ProgressCallbackImpl progressCallback = new ProgressCallbackImpl(firmwareUpdateHandler, eventPublisher, i18nProvider, thingUID, firmwareUID, loc);
    progressCallbackMap.put(thingUID, progressCallback);
    logger.debug("Starting firmware update for thing with UID {} and firmware with UID {}", thingUID, firmwareUID);
    safeCaller.create(firmwareUpdateHandler, FirmwareUpdateHandler.class).withTimeout(timeout).withAsync().onTimeout(() -> {
        logger.error("Timeout occurred for firmware update of thing with UID {} and firmware with UID {}.", thingUID, firmwareUID);
        progressCallback.failedInternal("timeout-error");
    }).onException(e -> {
        logger.error("Unexpected exception occurred for firmware update of thing with UID {} and firmware with UID {}.", thingUID, firmwareUID, e.getCause());
        progressCallback.failedInternal("unexpected-handler-error");
    }).build().updateFirmware(firmware, progressCallback);
}
Also used : Locale(java.util.Locale) FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware)

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