use of org.openhab.core.thing.events.ThingStatusInfoEvent 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)));
}
use of org.openhab.core.thing.events.ThingStatusInfoEvent in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerPostsThingStatusEventsIfTheStatusOfAThingIsUpdated.
@Test
public void thingManagerPostsThingStatusEventsIfTheStatusOfAThingIsUpdated() {
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<Event> receivedEvents = new ArrayList<>();
@NonNullByDefault EventSubscriber thingStatusEventSubscriber = 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) {
receivedEvents.add(event);
}
};
registerService(thingStatusEventSubscriber);
// set status to INITIALIZING
ThingStatusInfo initializingNone = ThingStatusInfoBuilder.create(ThingStatus.INITIALIZING, ThingStatusDetail.NONE).build();
ThingStatusInfoEvent event = ThingEventFactory.createStatusInfoEvent(thing.getUID(), initializingNone);
managedThingProvider.add(thing);
waitForAssert(() -> assertThat(receivedEvents.size(), is(1)));
assertThat(receivedEvents.get(0).getType(), is(event.getType()));
assertThat(receivedEvents.get(0).getPayload(), is(event.getPayload()));
assertThat(receivedEvents.get(0).getTopic(), is(event.getTopic()));
receivedEvents.clear();
// set status to ONLINE
ThingStatusInfo onlineNone = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build();
event = ThingEventFactory.createStatusInfoEvent(thing.getUID(), onlineNone);
state.callback.statusUpdated(thing, onlineNone);
waitForAssert(() -> assertThat(receivedEvents.size(), is(1)));
assertThat(receivedEvents.get(0).getType(), is(event.getType()));
assertThat(receivedEvents.get(0).getPayload(), is(event.getPayload()));
assertThat(receivedEvents.get(0).getTopic(), is(event.getTopic()));
receivedEvents.clear();
// set status to OFFLINE
ThingStatusInfo offlineCommError = ThingStatusInfoBuilder.create(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR).build();
event = ThingEventFactory.createStatusInfoEvent(thing.getUID(), offlineCommError);
state.callback.statusUpdated(thing, offlineCommError);
waitForAssert(() -> assertThat(receivedEvents.size(), is(1)));
assertThat(receivedEvents.get(0).getType(), is(event.getType()));
assertThat(receivedEvents.get(0).getPayload(), is(event.getPayload()));
assertThat(receivedEvents.get(0).getTopic(), is(event.getTopic()));
receivedEvents.clear();
// set status to UNINITIALIZED
ThingStatusInfo uninitializedError = ThingStatusInfoBuilder.create(ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_MISSING_ERROR).build();
final Event uninitializedEvent = ThingEventFactory.createStatusInfoEvent(thing.getUID(), uninitializedError);
unregisterService(thingHandlerFactory);
waitForAssert(() -> {
assertThat(receivedEvents.size(), is(2));
assertThat(receivedEvents.get(1).getType(), is(uninitializedEvent.getType()));
assertThat(receivedEvents.get(1).getPayload(), is(uninitializedEvent.getPayload()));
assertThat(receivedEvents.get(1).getTopic(), is(uninitializedEvent.getTopic()));
});
}
use of org.openhab.core.thing.events.ThingStatusInfoEvent in project openhab-core by openhab.
the class ThingStatusTriggerHandler method receive.
@Override
public void receive(Event event) {
final ModuleHandlerCallback callback = this.callback;
if (!(callback instanceof TriggerHandlerCallback)) {
return;
}
TriggerHandlerCallback thCallback = (TriggerHandlerCallback) callback;
logger.trace("Received Event: Source: {} Topic: {} Type: {} Payload: {}", event.getSource(), event.getTopic(), event.getType(), event.getPayload());
Map<String, Object> values = new HashMap<>();
if (event instanceof ThingStatusInfoEvent && UPDATE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
ThingStatus status = ((ThingStatusInfoEvent) event).getStatusInfo().getStatus();
if (statusMatches(this.status, status)) {
values.put(OUT_STATUS, status);
}
} else if (event instanceof ThingStatusInfoChangedEvent && CHANGE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
ThingStatus newStatus = ((ThingStatusInfoChangedEvent) event).getStatusInfo().getStatus();
ThingStatus oldStatus = ((ThingStatusInfoChangedEvent) event).getOldStatusInfo().getStatus();
if (statusMatches(this.status, newStatus) && statusMatches(this.previousStatus, oldStatus)) {
values.put(OUT_NEW_STATUS, newStatus);
values.put(OUT_OLD_STATUS, oldStatus);
}
}
if (!values.isEmpty()) {
values.put(OUT_EVENT, event);
thCallback.triggered(module, values);
}
}
Aggregations