Search in sources :

Example 1 with ThingStatusInfoChangedEvent

use of org.openhab.core.thing.events.ThingStatusInfoChangedEvent in project addons by smarthomej.

the class CompilerService method receive.

@Override
public void receive(Event event) {
    String eventType = event.getType();
    if (ACTION_EVENTS.contains(eventType)) {
        ThingStatusInfoChangedEvent event1 = (ThingStatusInfoChangedEvent) event;
        if ((ThingStatus.INITIALIZING.equals(event1.getOldStatusInfo().getStatus()) && INITIALIZED.contains(event1.getStatusInfo().getStatus())) || (ThingStatus.UNINITIALIZED.equals(event1.getStatusInfo().getStatus()) && INITIALIZED.contains(event1.getOldStatusInfo().getStatus()))) {
            // only regenerate jar if things are changing to or from an initialized
            try {
                if (classGenerator.generateThingActions()) {
                    buildJavaRuleDependenciesJar();
                }
            } catch (IOException e) {
                logger.warn("Failed to (re-)build thing action classes: {}", e.getMessage());
            }
        }
    } else if (ITEM_EVENTS.contains(eventType)) {
        logger.debug("Added/updated item: {}", event);
        try {
            classGenerator.generateItems();
            buildJavaRuleDependenciesJar();
        } catch (IOException e) {
            logger.warn("Failed to (re-)build item class: {}", e.getMessage());
        }
    } else if (THING_EVENTS.contains(eventType)) {
        logger.debug("Added/updated thing: {}", event);
        try {
            classGenerator.generateThings();
            buildJavaRuleDependenciesJar();
        } catch (IOException e) {
            logger.warn("Failed to (re-)build thing class: {}", e.getMessage());
        }
    }
}
Also used : ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) IOException(java.io.IOException)

Example 2 with ThingStatusInfoChangedEvent

use of org.openhab.core.thing.events.ThingStatusInfoChangedEvent in project openhab-core by openhab.

the class DSLScriptEngine method createEvaluationContext.

private DefaultEvaluationContext createEvaluationContext(Script script, IEvaluationContext specificContext) {
    IEvaluationContext parentContext = specificContext;
    if (specificContext == null && script instanceof ScriptImpl) {
        XExpression xExpression = ((ScriptImpl) script).getXExpression();
        if (xExpression != null) {
            Resource resource = xExpression.eResource();
            if (resource instanceof XtextResource) {
                IResourceServiceProvider provider = ((XtextResource) resource).getResourceServiceProvider();
                parentContext = provider.get(IEvaluationContext.class);
            }
        }
    }
    DefaultEvaluationContext evalContext = new DefaultEvaluationContext(parentContext);
    for (Map.Entry<String, String> entry : IMPLICIT_VARS.entrySet()) {
        Object value = context.getAttribute(entry.getKey());
        if (value != null) {
            QualifiedName qn = QualifiedName.create(entry.getValue());
            if (evalContext.getValue(qn) == null) {
                evalContext.newValue(qn, value);
            } else {
                evalContext.assignValue(qn, value);
            }
        }
    }
    // now add specific implicit vars, where we have to map the right content
    Object value = context.getAttribute(OUTPUT_EVENT);
    if (value instanceof ChannelTriggeredEvent) {
        ChannelTriggeredEvent event = (ChannelTriggeredEvent) value;
        evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_RECEIVED_EVENT), event.getEvent());
        evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_TRIGGERING_CHANNEL), event.getChannel().getAsString());
    }
    if (value instanceof ItemEvent) {
        ItemEvent event = (ItemEvent) value;
        evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_TRIGGERING_ITEM_NAME), event.getItemName());
    }
    if (value instanceof ThingStatusInfoChangedEvent) {
        ThingStatusInfoChangedEvent event = (ThingStatusInfoChangedEvent) value;
        evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_TRIGGERING_THING), event.getThingUID().toString());
        evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_PREVIOUS_STATUS), event.getOldStatusInfo().getStatus().toString());
        evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_NEW_STATUS), event.getStatusInfo().getStatus().toString());
    }
    return evalContext;
}
Also used : ItemEvent(org.openhab.core.items.events.ItemEvent) DefaultEvaluationContext(org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IEvaluationContext(org.eclipse.xtext.xbase.interpreter.IEvaluationContext) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ChannelTriggeredEvent(org.openhab.core.thing.events.ChannelTriggeredEvent) XtextResource(org.eclipse.xtext.resource.XtextResource) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) XExpression(org.eclipse.xtext.xbase.XExpression) Map(java.util.Map)

Example 3 with ThingStatusInfoChangedEvent

use of org.openhab.core.thing.events.ThingStatusInfoChangedEvent in project openhab-core by openhab.

the class FirmwareUpdateServiceImpl method receive.

@Override
public void receive(Event event) {
    if (event instanceof ThingStatusInfoChangedEvent) {
        ThingStatusInfoChangedEvent changedEvent = (ThingStatusInfoChangedEvent) event;
        if (changedEvent.getStatusInfo().getStatus() != ThingStatus.ONLINE) {
            return;
        }
        ThingUID thingUID = changedEvent.getThingUID();
        FirmwareUpdateHandler firmwareUpdateHandler = getFirmwareUpdateHandler(thingUID);
        if (firmwareUpdateHandler != null && !firmwareStatusInfoMap.containsKey(thingUID)) {
            initializeFirmwareStatus(firmwareUpdateHandler);
        }
    }
}
Also used : ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) FirmwareUpdateHandler(org.openhab.core.thing.binding.firmware.FirmwareUpdateHandler) ThingUID(org.openhab.core.thing.ThingUID)

Example 4 with ThingStatusInfoChangedEvent

use of org.openhab.core.thing.events.ThingStatusInfoChangedEvent in project openhab-core by openhab.

the class ThingManagerOSGiTest method thingManagerPostsLocalizedThingStatusInfoAndThingStatusInfoChangedEvents.

@Test
@SuppressWarnings("null")
public void thingManagerPostsLocalizedThingStatusInfoAndThingStatusInfoChangedEvents() throws Exception {
    registerThingTypeProvider();
    class ThingHandlerState {

        @Nullable
        ThingHandlerCallback callback;
    }
    final ThingHandlerState state = new ThingHandlerState();
    ThingHandler thingHandler = mock(ThingHandler.class);
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.callback = (ThingHandlerCallback) invocation.getArgument(0);
            return null;
        }
    }).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
    when(thingHandler.getThing()).thenReturn(thing);
    ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
    when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
    registerService(thingHandlerFactory);
    BundleResolver bundleResolver = mock(BundleResolver.class);
    when(bundleResolver.resolveBundle(any())).thenReturn(bundleContext.getBundle());
    ThingStatusInfoI18nLocalizationService thingStatusInfoI18nLocalizationService = getService(ThingStatusInfoI18nLocalizationService.class);
    thingStatusInfoI18nLocalizationService.setBundleResolver(bundleResolver);
    final List<ThingStatusInfoEvent> infoEvents = new ArrayList<>();
    @NonNullByDefault EventSubscriber thingStatusInfoEventSubscriber = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Set.of(ThingStatusInfoEvent.TYPE);
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event event) {
            infoEvents.add((ThingStatusInfoEvent) event);
        }
    };
    registerService(thingStatusInfoEventSubscriber);
    final List<ThingStatusInfoChangedEvent> infoChangedEvents = new ArrayList<>();
    @NonNullByDefault EventSubscriber thingStatusInfoChangedEventSubscriber = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Set.of(ThingStatusInfoChangedEvent.TYPE);
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event event) {
            infoChangedEvents.add((ThingStatusInfoChangedEvent) event);
        }
    };
    registerService(thingStatusInfoChangedEventSubscriber);
    // add thing (UNINITIALIZED -> INITIALIZING)
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertThat(infoEvents.size(), is(1));
        assertThat(infoChangedEvents.size(), is(1));
    });
    assertThat(infoEvents.get(0).getType(), is(ThingStatusInfoEvent.TYPE));
    assertThat(infoEvents.get(0).getTopic(), is("openhab/things/binding:type:id/status"));
    assertThat(infoEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
    assertThat(infoEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoEvents.get(0).getStatusInfo().getDescription(), is(nullValue()));
    assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
    assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getDescription(), is(nullValue()));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.UNINITIALIZED));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getDescription(), is(nullValue()));
    infoEvents.clear();
    infoChangedEvents.clear();
    LocaleProvider localeProvider = getService(LocaleProvider.class);
    assertThat(localeProvider, is(notNullValue()));
    Locale defaultLocale = localeProvider.getLocale();
    // set status to ONLINE (INITIALIZING -> ONLINE)
    new DefaultLocaleSetter(configurationAdmin).setDefaultLocale(Locale.ENGLISH);
    waitForAssert(() -> assertThat(localeProvider.getLocale(), is(Locale.ENGLISH)));
    ThingStatusInfo onlineNone = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).withDescription("@text/online").build();
    state.callback.statusUpdated(thing, onlineNone);
    waitForAssert(() -> {
        assertThat(infoEvents.size(), is(1));
        assertThat(infoChangedEvents.size(), is(1));
    });
    assertThat(infoEvents.get(0).getType(), is(ThingStatusInfoEvent.TYPE));
    assertThat(infoEvents.get(0).getTopic(), is("openhab/things/binding:type:id/status"));
    assertThat(infoEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.ONLINE));
    assertThat(infoEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoEvents.get(0).getStatusInfo().getDescription(), is("Thing is online."));
    assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
    assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.ONLINE));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getDescription(), is("Thing is online."));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getDescription(), is(nullValue()));
    infoEvents.clear();
    infoChangedEvents.clear();
    // set status to OFFLINE (ONLINE -> OFFLINE)
    new DefaultLocaleSetter(configurationAdmin).setDefaultLocale(Locale.GERMAN);
    waitForAssert(() -> assertThat(localeProvider.getLocale(), is(Locale.GERMAN)));
    ThingStatusInfo offlineNone = ThingStatusInfoBuilder.create(ThingStatus.OFFLINE, ThingStatusDetail.NONE).withDescription("@text/offline.without-param").build();
    state.callback.statusUpdated(thing, offlineNone);
    waitForAssert(() -> {
        assertThat(infoEvents.size(), is(1));
        assertThat(infoChangedEvents.size(), is(1));
    });
    assertThat(infoEvents.get(0).getType(), is(ThingStatusInfoEvent.TYPE));
    assertThat(infoEvents.get(0).getTopic(), is("openhab/things/binding:type:id/status"));
    assertThat(infoEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.OFFLINE));
    assertThat(infoEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoEvents.get(0).getStatusInfo().getDescription(), is("Thing ist offline."));
    assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
    assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.OFFLINE));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getDescription(), is("Thing ist offline."));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.ONLINE));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getDescription(), is("Thing ist online."));
    new DefaultLocaleSetter(configurationAdmin).setDefaultLocale(defaultLocale);
    waitForAssert(() -> assertThat(localeProvider.getLocale(), is(defaultLocale)));
}
Also used : Locale(java.util.Locale) ThingStatusInfoEvent(org.openhab.core.thing.events.ThingStatusInfoEvent) ArrayList(java.util.ArrayList) DefaultLocaleSetter(org.openhab.core.thing.testutil.i18n.DefaultLocaleSetter) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) Thing(org.openhab.core.thing.Thing) EventSubscriber(org.openhab.core.events.EventSubscriber) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatusInfoI18nLocalizationService(org.openhab.core.thing.i18n.ThingStatusInfoI18nLocalizationService) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) BundleResolver(org.openhab.core.util.BundleResolver) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LocaleProvider(org.openhab.core.i18n.LocaleProvider) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent) ThingStatusInfoEvent(org.openhab.core.thing.events.ThingStatusInfoEvent) Event(org.openhab.core.events.Event) ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Nullable(org.eclipse.jdt.annotation.Nullable) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 5 with ThingStatusInfoChangedEvent

use of org.openhab.core.thing.events.ThingStatusInfoChangedEvent in project openhab-core by openhab.

the class ThingManagerOSGiTest method thingManagerPostsThingStatusChangedEventsIfTheStatusOfAThingIsChanged.

@Test
public void thingManagerPostsThingStatusChangedEventsIfTheStatusOfAThingIsChanged() throws Exception {
    registerThingTypeProvider();
    class ThingHandlerState {

        @Nullable
        ThingHandlerCallback callback;
    }
    final ThingHandlerState state = new ThingHandlerState();
    ThingHandler thingHandler = mock(ThingHandler.class);
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.callback = (ThingHandlerCallback) invocation.getArgument(0);
            return null;
        }
    }).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
    when(thingHandler.getThing()).thenReturn(thing);
    ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
    when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
    registerService(thingHandlerFactory);
    final List<ThingStatusInfoChangedEvent> infoChangedEvents = new ArrayList<>();
    @NonNullByDefault EventSubscriber thingStatusEventSubscriber = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Set.of(ThingStatusInfoChangedEvent.TYPE);
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event event) {
            infoChangedEvents.add((ThingStatusInfoChangedEvent) event);
        }
    };
    registerService(thingStatusEventSubscriber);
    // add thing (UNINITIALIZED -> INITIALIZING)
    managedThingProvider.add(thing);
    waitForAssert(() -> assertThat(infoChangedEvents.size(), is(1)));
    assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
    assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.UNINITIALIZED));
    infoChangedEvents.clear();
    // set status to ONLINE (INITIALIZING -> ONLINE)
    ThingStatusInfo onlineNone = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build();
    state.callback.statusUpdated(thing, onlineNone);
    waitForAssert(() -> assertThat(infoChangedEvents.size(), is(1)));
    assertThat(infoChangedEvents.get(0).getType(), is(ThingStatusInfoChangedEvent.TYPE));
    assertThat(infoChangedEvents.get(0).getTopic(), is("openhab/things/binding:type:id/statuschanged"));
    assertThat(infoChangedEvents.get(0).getStatusInfo().getStatus(), is(ThingStatus.ONLINE));
    assertThat(infoChangedEvents.get(0).getOldStatusInfo().getStatus(), is(ThingStatus.INITIALIZING));
    infoChangedEvents.clear();
    // set status to ONLINE again
    state.callback.statusUpdated(thing, onlineNone);
    // make sure no event has been sent
    Thread.sleep(500);
    assertThat(infoChangedEvents.size(), is(0));
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ArrayList(java.util.ArrayList) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent) ThingStatusInfoEvent(org.openhab.core.thing.events.ThingStatusInfoEvent) Event(org.openhab.core.events.Event) ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

ThingStatusInfoChangedEvent (org.openhab.core.thing.events.ThingStatusInfoChangedEvent)6 ThingStatusInfoEvent (org.openhab.core.thing.events.ThingStatusInfoEvent)3 ArrayList (java.util.ArrayList)2 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 Test (org.junit.jupiter.api.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Event (org.openhab.core.events.Event)2 EventSubscriber (org.openhab.core.events.EventSubscriber)2 ItemStateEvent (org.openhab.core.items.events.ItemStateEvent)2 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)2 Thing (org.openhab.core.thing.Thing)2 ThingStatusInfo (org.openhab.core.thing.ThingStatusInfo)2 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)2 ThingHandler (org.openhab.core.thing.binding.ThingHandler)2 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)2 ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1