Search in sources :

Example 76 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-core by openhab.

the class ThingManagerImpl method doRegisterHandler.

private void doRegisterHandler(final Thing thing, final ThingHandlerFactory thingHandlerFactory) {
    logger.debug("Calling '{}.registerHandler()' for thing '{}'.", thingHandlerFactory.getClass().getSimpleName(), thing.getUID());
    try {
        ThingHandler thingHandler = thingHandlerFactory.registerHandler(thing);
        thingHandler.setCallback(ThingManagerImpl.this.thingHandlerCallback);
        thing.setHandler(thingHandler);
        thingHandlers.put(thing.getUID(), thingHandler);
        synchronized (thingHandlersByFactory) {
            thingHandlersByFactory.computeIfAbsent(thingHandlerFactory, unused -> new HashSet<>()).add(thingHandler);
        }
    } catch (Exception ex) {
        ThingStatusInfo statusInfo = buildStatusInfo(ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_REGISTERING_ERROR, ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage());
        setThingStatus(thing, statusInfo);
        logger.error("Exception occurred while calling thing handler factory '{}': {}", thingHandlerFactory, ex.getMessage(), ex);
    }
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) LoggerFactory(org.slf4j.LoggerFactory) SafeCaller(org.openhab.core.common.SafeCaller) ReadyTracker(org.openhab.core.service.ReadyService.ReadyTracker) StorageService(org.openhab.core.storage.StorageService) ConfigDescriptionRegistry(org.openhab.core.config.core.ConfigDescriptionRegistry) ChannelTypeRegistry(org.openhab.core.thing.type.ChannelTypeRegistry) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Map(java.util.Map) UID(org.openhab.core.thing.UID) URI(java.net.URI) ChannelGroupUID(org.openhab.core.thing.ChannelGroupUID) EventPublisher(org.openhab.core.events.EventPublisher) ConfigDescription(org.openhab.core.config.core.ConfigDescription) BundleResolver(org.openhab.core.util.BundleResolver) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Storage(org.openhab.core.storage.Storage) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) ItemChannelLinkRegistry(org.openhab.core.thing.link.ItemChannelLinkRegistry) Deactivate(org.osgi.service.component.annotations.Deactivate) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) BridgeHandler(org.openhab.core.thing.binding.BridgeHandler) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) PrivilegedAction(java.security.PrivilegedAction) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ThingTypeRegistry(org.openhab.core.thing.type.ThingTypeRegistry) ReadyMarkerUtils(org.openhab.core.service.ReadyMarkerUtils) Channel(org.openhab.core.thing.Channel) ThingTypeMigrationService(org.openhab.core.thing.ThingTypeMigrationService) ThingEventFactory(org.openhab.core.thing.events.ThingEventFactory) List(java.util.List) ThingRegistry(org.openhab.core.thing.ThingRegistry) Provider(org.openhab.core.common.registry.Provider) ChannelGroupTypeRegistry(org.openhab.core.thing.type.ChannelGroupTypeRegistry) Entry(java.util.Map.Entry) AccessController(java.security.AccessController) ConfigValidationException(org.openhab.core.config.core.validation.ConfigValidationException) ThingManager(org.openhab.core.thing.ThingManager) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) StartLevelService(org.openhab.core.service.StartLevelService) ChannelGroupType(org.openhab.core.thing.type.ChannelGroupType) ThingType(org.openhab.core.thing.type.ThingType) ThingStatusInfoBuilder(org.openhab.core.thing.binding.builder.ThingStatusInfoBuilder) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ComponentContext(org.osgi.service.component.ComponentContext) HashMap(java.util.HashMap) Function(java.util.function.Function) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) ConfigDescriptionValidator(org.openhab.core.config.core.validation.ConfigDescriptionValidator) ThingStatusInfoI18nLocalizationService(org.openhab.core.thing.i18n.ThingStatusInfoI18nLocalizationService) HashSet(java.util.HashSet) Thing(org.openhab.core.thing.Thing) Component(org.osgi.service.component.annotations.Component) ChannelGroupTypeUID(org.openhab.core.thing.type.ChannelGroupTypeUID) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ChannelUID(org.openhab.core.thing.ChannelUID) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) Activate(org.osgi.service.component.annotations.Activate) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ThreadPoolManager(org.openhab.core.common.ThreadPoolManager) ThingStatus(org.openhab.core.thing.ThingStatus) ThingHandlerHelper(org.openhab.core.thing.util.ThingHandlerHelper) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ThingUID(org.openhab.core.thing.ThingUID) ReadyMarkerFilter(org.openhab.core.service.ReadyMarkerFilter) ThingHandler(org.openhab.core.thing.binding.ThingHandler) State(org.openhab.core.types.State) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) ReadyMarker(org.openhab.core.service.ReadyMarker) ChannelType(org.openhab.core.thing.type.ChannelType) TimeUnit(java.util.concurrent.TimeUnit) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) Lock(java.util.concurrent.locks.Lock) ReadyService(org.openhab.core.service.ReadyService) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) Identifiable(org.openhab.core.common.registry.Identifiable) Reference(org.osgi.service.component.annotations.Reference) ManagedProvider(org.openhab.core.common.registry.ManagedProvider) Bridge(org.openhab.core.thing.Bridge) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) ConfigValidationException(org.openhab.core.config.core.validation.ConfigValidationException) HashSet(java.util.HashSet)

Example 77 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-core by openhab.

the class ThingStatusInfoI18nLocalizationService method getLocalizedThingStatusInfo.

/**
 * Localizes the {@link ThingStatusInfo} for the given thing.
 *
 * @param thing the thing whose thing status info is to be localized (must not be null)
 * @param locale the locale to be used (can be null)
 * @return the localized thing status or the original thing status if
 *         <ul>
 *         <li>there is nothing to be localized</li>
 *         <li>the thing does not have a handler</li>
 *         </ul>
 * @throws IllegalArgumentException if given thing is null
 */
public ThingStatusInfo getLocalizedThingStatusInfo(Thing thing, Locale locale) {
    if (thing == null) {
        throw new IllegalArgumentException("Thing must not be null.");
    }
    ThingHandler thingHandler = thing.getHandler();
    if (thingHandler == null) {
        return thing.getStatusInfo();
    }
    String description = thing.getStatusInfo().getDescription();
    if (description == null || !I18nUtil.isConstant(description)) {
        return thing.getStatusInfo();
    }
    String translatedDescription = translateDescription(description, locale, thingHandler);
    return new ThingStatusInfo(thing.getStatus(), thing.getStatusInfo().getStatusDetail(), translatedDescription);
}
Also used : ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo)

Example 78 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-core by openhab.

the class ThingConsoleCommandExtension method printThings.

private void printThings(Console console, Collection<Thing> things) {
    if (things.isEmpty()) {
        console.println("No things found.");
    }
    for (Thing thing : things) {
        String id = thing.getUID().toString();
        String thingType = thing instanceof Bridge ? "Bridge" : "Thing";
        ThingStatusInfo status = thingStatusInfoI18nLocalizationService.getLocalizedThingStatusInfo(thing, null);
        ThingUID bridgeUID = thing.getBridgeUID();
        String label = thing.getLabel();
        console.println(String.format("%s (Type=%s, Status=%s, Label=%s, Bridge=%s)", id, thingType, status, label, bridgeUID));
    }
}
Also used : ThingUID(org.openhab.core.thing.ThingUID) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) Thing(org.openhab.core.thing.Thing) Bridge(org.openhab.core.thing.Bridge)

Example 79 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-core by openhab.

the class ThingStatusInfoBuilderTest method testThingStatusInfoBuilderStatus.

@Test
public void testThingStatusInfoBuilderStatus() {
    ThingStatusInfo thingStatusInfo = builder.build();
    assertThat(thingStatusInfo.getStatus(), is(ThingStatus.ONLINE));
    assertThat(thingStatusInfo.getStatusDetail(), is(ThingStatusDetail.NONE));
    assertThat(thingStatusInfo.getDescription(), is(nullValue()));
}
Also used : ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) Test(org.junit.jupiter.api.Test)

Example 80 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-core by openhab.

the class ThingStatusInfoBuilderTest method testThingStatusInfoBuilderStatusDetails.

@Test
public void testThingStatusInfoBuilderStatusDetails() {
    ThingStatusInfo thingStatusInfo = builder.withStatusDetail(ThingStatusDetail.DISABLED).build();
    assertThat(thingStatusInfo.getStatus(), is(ThingStatus.ONLINE));
    assertThat(thingStatusInfo.getStatusDetail(), is(ThingStatusDetail.DISABLED));
    assertThat(thingStatusInfo.getDescription(), is(nullValue()));
}
Also used : ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) Test(org.junit.jupiter.api.Test)

Aggregations

ThingStatusInfo (org.openhab.core.thing.ThingStatusInfo)102 Test (org.junit.jupiter.api.Test)59 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)34 Thing (org.openhab.core.thing.Thing)23 ThingHandler (org.openhab.core.thing.binding.ThingHandler)23 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)20 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)18 ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)16 Nullable (org.eclipse.jdt.annotation.Nullable)14 Configuration (org.openhab.core.config.core.Configuration)13 ThingUID (org.openhab.core.thing.ThingUID)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)12 Bridge (org.openhab.core.thing.Bridge)12 JavaTest (org.openhab.core.test.java.JavaTest)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 DiscoveryResult (org.openhab.core.config.discovery.DiscoveryResult)5 BaseThingHandler (org.openhab.core.thing.binding.BaseThingHandler)5 ArrayList (java.util.ArrayList)4 ThingStatus (org.openhab.core.thing.ThingStatus)4