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);
});
}
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]));
}
}
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;
}
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();
}
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);
}
Aggregations