Search in sources :

Example 76 with ThingUID

use of org.eclipse.smarthome.core.thing.ThingUID in project smarthome by eclipse.

the class ThingConfigDescriptionAliasProvider method getChannelConfigDescriptionURI.

@Nullable
private URI getChannelConfigDescriptionURI(URI uri) {
    String stringUID = uri.getSchemeSpecificPart();
    if (uri.getFragment() != null) {
        stringUID = stringUID + "#" + uri.getFragment();
    }
    ChannelUID channelUID = new ChannelUID(stringUID);
    ThingUID thingUID = channelUID.getThingUID();
    // First, get the thing so we get access to the channel type via the channel
    Thing thing = thingRegistry.get(thingUID);
    if (thing == null) {
        return null;
    }
    Channel channel = thing.getChannel(channelUID.getId());
    if (channel == null) {
        return null;
    }
    ChannelType channelType = channelTypeRegistry.getChannelType(channel.getChannelTypeUID());
    if (channelType == null) {
        return null;
    }
    // Get the config description URI for this channel type
    URI configURI = channelType.getConfigDescriptionURI();
    return configURI;
}
Also used : ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Channel(org.eclipse.smarthome.core.thing.Channel) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) URI(java.net.URI) Thing(org.eclipse.smarthome.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 77 with ThingUID

use of org.eclipse.smarthome.core.thing.ThingUID in project smarthome by eclipse.

the class ThingManager method thingUpdated.

@Override
public void thingUpdated(final Thing thing, ThingTrackerEvent thingTrackerEvent) {
    ThingUID thingUID = thing.getUID();
    if (thingUpdatedLock.contains(thingUID)) {
        // called from the thing handler itself, therefore
        // it exists, is initializing/initialized and
        // must not be informed (in order to prevent infinite loops)
        replaceThing(getThing(thingUID), thing);
    } else {
        Lock lock1 = getLockForThing(thing.getUID());
        try {
            lock1.lock();
            Thing oldThing = getThing(thingUID);
            ThingHandler thingHandler = replaceThing(oldThing, thing);
            if (thingHandler != null) {
                if (ThingHandlerHelper.isHandlerInitialized(thing) || isInitializing(thing)) {
                    if (oldThing != null) {
                        oldThing.setHandler(null);
                    }
                    thing.setHandler(thingHandler);
                    safeCaller.create(thingHandler, ThingHandler.class).build().thingUpdated(thing);
                } else {
                    logger.debug("Cannot notify handler about updated thing '{}', because handler is not initialized (thing must be in status UNKNOWN, ONLINE or OFFLINE).", thing.getThingTypeUID());
                    if (thingHandler.getThing() == thing) {
                        logger.debug("Initializing handler of thing '{}'", thing.getThingTypeUID());
                        if (oldThing != null) {
                            oldThing.setHandler(null);
                        }
                        thing.setHandler(thingHandler);
                        initializeHandler(thing);
                    } else {
                        logger.debug("Replacing uninitialized handler for updated thing '{}'", thing.getThingTypeUID());
                        ThingHandlerFactory thingHandlerFactory = getThingHandlerFactory(thing);
                        unregisterHandler(thingHandler.getThing(), thingHandlerFactory);
                        registerAndInitializeHandler(thing, thingHandlerFactory);
                    }
                }
            } else {
                registerAndInitializeHandler(thing, getThingHandlerFactory(thing));
            }
        } finally {
            lock1.unlock();
        }
    }
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) Thing(org.eclipse.smarthome.core.thing.Thing) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 78 with ThingUID

use of org.eclipse.smarthome.core.thing.ThingUID in project smarthome by eclipse.

the class ThingManager method migrateThingType.

@Override
public void migrateThingType(final Thing thing, final ThingTypeUID thingTypeUID, final Configuration configuration) {
    final ThingType thingType = thingTypeRegistry.getThingType(thingTypeUID);
    if (thingType == null) {
        throw new RuntimeException(MessageFormat.format("No thing type {0} registered, cannot change thing type for thing {1}", thingTypeUID.getAsString(), thing.getUID().getAsString()));
    }
    scheduler.schedule(new Runnable() {

        @Override
        public void run() {
            Lock lock = getLockForThing(thing.getUID());
            try {
                lock.lock();
                ThingUID thingUID = thing.getUID();
                waitForRunningHandlerRegistrations(thingUID);
                // Remove the ThingHandler, if any
                final ThingHandlerFactory oldThingHandlerFactory = findThingHandlerFactory(thing.getThingTypeUID());
                if (oldThingHandlerFactory != null) {
                    ThingHandler thingHandler = thing.getHandler();
                    unregisterAndDisposeHandler(oldThingHandlerFactory, thing, thingHandler);
                    waitUntilHandlerUnregistered(thing, 60 * 1000);
                } else {
                    logger.debug("No ThingHandlerFactory available that can handle {}", thing.getThingTypeUID());
                }
                // Set the new channels
                List<Channel> channels = ThingFactoryHelper.createChannels(thingType, thingUID, configDescriptionRegistry);
                ((ThingImpl) thing).setChannels(channels);
                // Set the given configuration
                ThingFactoryHelper.applyDefaultConfiguration(configuration, thingType, configDescriptionRegistry);
                ((ThingImpl) thing).setConfiguration(configuration);
                // Change the ThingType
                ((ThingImpl) thing).setThingTypeUID(thingTypeUID);
                // Register the new Handler - ThingManager.updateThing() is going to take care of that
                thingRegistry.update(thing);
                ThingHandler handler = thing.getHandler();
                String handlerString = "NO HANDLER";
                if (handler != null) {
                    handlerString = handler.toString();
                }
                logger.debug("Changed ThingType of Thing {} to {}. New ThingHandler is {}.", thing.getUID().toString(), thing.getThingTypeUID(), handlerString);
            } finally {
                lock.unlock();
            }
        }

        private void waitUntilHandlerUnregistered(final Thing thing, int timeout) {
            for (int i = 0; i < timeout / 100; i++) {
                if (thing.getHandler() == null && thingHandlers.get(thing.getUID()) == null) {
                    return;
                }
                try {
                    Thread.sleep(100);
                    logger.debug("Waiting for handler deregistration to complete for thing {}. Took already {}ms.", thing.getUID().getAsString(), (i + 1) * 100);
                } catch (InterruptedException e) {
                    return;
                }
            }
            String message = MessageFormat.format("Thing type migration failed for {0}. The handler deregistration did not complete within {1}ms.", thing.getUID().getAsString(), timeout);
            logger.error(message);
            throw new RuntimeException(message);
        }

        private void waitForRunningHandlerRegistrations(ThingUID thingUID) {
            for (int i = 0; i < 10 * 10; i++) {
                if (!registerHandlerLock.contains(thingUID)) {
                    return;
                }
                try {
                    // Wait a little to give running handler registrations a chance to complete...
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    return;
                }
            }
            String message = MessageFormat.format("Thing type migration failed for {0}. Could not obtain lock for hander registration.", thingUID.getAsString());
            logger.error(message);
            throw new RuntimeException(message);
        }
    }, 0, TimeUnit.MILLISECONDS);
}
Also used : ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingType(org.eclipse.smarthome.core.thing.type.ThingType) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 79 with ThingUID

use of org.eclipse.smarthome.core.thing.ThingUID in project smarthome by eclipse.

the class FirmwareUpdateConsoleCommandExtension method updateFirmware.

private void updateFirmware(Console console, String[] args) {
    if (args.length != 3) {
        console.println("Specify the thing id and the firmware version to update the firmware: firmware update <thingUID> <firmware version>");
        return;
    }
    ThingUID thingUID = new ThingUID(args[1]);
    FirmwareUpdateHandler firmwareUpdateHandler = getFirmwareUpdateHandler(thingUID);
    if (firmwareUpdateHandler == null) {
        console.println(String.format("No firmware update handler available for thing with UID %s.", thingUID));
        return;
    }
    FirmwareUID firmwareUID = new FirmwareUID(firmwareUpdateHandler.getThing().getThingTypeUID(), args[2]);
    firmwareUpdateService.updateFirmware(thingUID, firmwareUID, null);
    console.println("Firmware update started.");
}
Also used : FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler) FirmwareUID(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID)

Example 80 with ThingUID

use of org.eclipse.smarthome.core.thing.ThingUID in project smarthome by eclipse.

the class FirmwareUpdateConsoleCommandExtension method listFirmwareStatus.

private void listFirmwareStatus(Console console, String[] args) {
    if (args.length != 2) {
        console.println("Specify the thing id to get its firmware status: firmware status <thingUID>");
        return;
    }
    ThingUID thingUID = new ThingUID(args[1]);
    FirmwareStatusInfo firmwareStatusInfo = firmwareUpdateService.getFirmwareStatusInfo(thingUID);
    if (firmwareStatusInfo != null) {
        StringBuffer sb = new StringBuffer();
        sb.append(String.format("Firmware status for thing with UID %s is %s.", thingUID, firmwareStatusInfo.getFirmwareStatus()));
        if (firmwareStatusInfo.getUpdatableFirmwareUID() != null) {
            sb.append(String.format(" The latest updatable firmware version is %s.", firmwareStatusInfo.getUpdatableFirmwareUID().getFirmwareVersion()));
        }
        console.println(sb.toString());
    } else {
        console.println(String.format("The firmware status for thing with UID %s could not be determined.", thingUID));
    }
}
Also used : FirmwareStatusInfo(org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo) ThingUID(org.eclipse.smarthome.core.thing.ThingUID)

Aggregations

ThingUID (org.eclipse.smarthome.core.thing.ThingUID)99 DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)29 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)27 Thing (org.eclipse.smarthome.core.thing.Thing)26 Test (org.junit.Test)25 HashMap (java.util.HashMap)21 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)14 ApiOperation (io.swagger.annotations.ApiOperation)10 ApiResponses (io.swagger.annotations.ApiResponses)10 Path (javax.ws.rs.Path)9 JsonObject (com.google.gson.JsonObject)8 JsonParser (com.google.gson.JsonParser)7 RolesAllowed (javax.annotation.security.RolesAllowed)7 Locale (java.util.Locale)6 Nullable (org.eclipse.jdt.annotation.Nullable)6 Consumes (javax.ws.rs.Consumes)5 Configuration (org.eclipse.smarthome.config.core.Configuration)5 Collection (java.util.Collection)4 GET (javax.ws.rs.GET)4 InboxPredicates.forThingUID (org.eclipse.smarthome.config.discovery.inbox.InboxPredicates.forThingUID)4