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;
}
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();
}
}
}
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);
}
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.");
}
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));
}
}
Aggregations