Search in sources :

Example 1 with FirmwareUpdateBackgroundTransferHandler

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

the class FirmwareUpdateService method processFirmwareStatusInfo.

private synchronized void processFirmwareStatusInfo(FirmwareUpdateHandler firmwareUpdateHandler, FirmwareStatusInfo newFirmwareStatusInfo, Firmware latestFirmware) {
    ThingUID thingUID = firmwareUpdateHandler.getThing().getUID();
    FirmwareStatusInfo previousFirmwareStatusInfo = firmwareStatusInfoMap.put(thingUID, newFirmwareStatusInfo);
    if (previousFirmwareStatusInfo == null || !previousFirmwareStatusInfo.equals(newFirmwareStatusInfo)) {
        eventPublisher.post(FirmwareEventFactory.createFirmwareStatusInfoEvent(newFirmwareStatusInfo, thingUID));
        if (newFirmwareStatusInfo.getFirmwareStatus() == FirmwareStatus.UPDATE_AVAILABLE && firmwareUpdateHandler instanceof FirmwareUpdateBackgroundTransferHandler && !firmwareUpdateHandler.isUpdateExecutable()) {
            transferLatestFirmware((FirmwareUpdateBackgroundTransferHandler) firmwareUpdateHandler, latestFirmware, previousFirmwareStatusInfo);
        }
    }
}
Also used : FirmwareStatusInfo(org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo) FirmwareUpdateBackgroundTransferHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler) ThingUID(org.eclipse.smarthome.core.thing.ThingUID)

Example 2 with FirmwareUpdateBackgroundTransferHandler

use of org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler 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)

Aggregations

FirmwareUpdateBackgroundTransferHandler (org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler)2 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Event (org.eclipse.smarthome.core.events.Event)1 Thing (org.eclipse.smarthome.core.thing.Thing)1 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)1 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)1 Firmware (org.eclipse.smarthome.core.thing.binding.firmware.Firmware)1 ProgressCallback (org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback)1 FirmwareStatusInfo (org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo)1 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)1 Test (org.junit.Test)1