use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class Util method checkAvailableChannels.
/**
* check and add available channels on a thing
*
* @param thing the Thing
* @param thingBuilder the ThingBuilder (needs to be passed as editThing is only available in the handler)
* @param scpdUtil the SCPDUtil instance for this thing
* @param deviceId the device id for this thing
* @param deviceType the (SCPD) device-type for this thing
* @param channels a (mutable) channel list for storing all channels
*/
public static void checkAvailableChannels(Thing thing, ThingBuilder thingBuilder, SCPDUtil scpdUtil, String deviceId, String deviceType, Map<ChannelUID, Tr064ChannelConfig> channels) {
Tr064BaseThingConfiguration thingConfig = Tr064RootHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID()) ? thing.getConfiguration().as(Tr064RootConfiguration.class) : thing.getConfiguration().as(Tr064SubConfiguration.class);
channels.clear();
CHANNEL_TYPES.stream().filter(channel -> deviceType.equals(channel.getService().getDeviceType())).forEach(channelTypeDescription -> {
String channelId = channelTypeDescription.getName();
String serviceId = channelTypeDescription.getService().getServiceId();
String typeId = channelTypeDescription.getTypeId();
Map<String, String> channelProperties = new HashMap<String, String>();
if (typeId != null) {
channelProperties.put("typeId", typeId);
}
Set<String> parameters = new HashSet<>();
try {
SCPDServiceType deviceService = scpdUtil.getDevice(deviceId).flatMap(device -> device.getServiceList().stream().filter(service -> service.getServiceId().equals(serviceId)).findFirst()).orElseThrow(() -> new ChannelConfigException("Service '" + serviceId + "' not found"));
SCPDScpdType serviceRoot = scpdUtil.getService(deviceService.getServiceId()).orElseThrow(() -> new ChannelConfigException("Service definition for '" + serviceId + "' not found"));
Tr064ChannelConfig channelConfig = new Tr064ChannelConfig(channelTypeDescription, deviceService);
// get
ActionType getAction = channelTypeDescription.getGetAction();
if (getAction != null) {
String actionName = getAction.getName();
String argumentName = getAction.getArgument();
SCPDActionType scpdAction = getAction(serviceRoot, actionName, "Get-Action");
SCPDArgumentType scpdArgument = getArgument(scpdAction, argumentName, SCPDDirection.OUT);
SCPDStateVariableType relatedStateVariable = getStateVariable(serviceRoot, scpdArgument);
parameters.addAll(getAndCheckParameters(channelId, getAction, scpdAction, serviceRoot, thingConfig));
channelConfig.setGetAction(scpdAction);
channelConfig.setDataType(relatedStateVariable.getDataType());
}
// check set action
ActionType setAction = channelTypeDescription.getSetAction();
if (setAction != null) {
String actionName = setAction.getName();
String argumentName = setAction.getArgument();
SCPDActionType scpdAction = getAction(serviceRoot, actionName, "Set-Action");
if (argumentName != null) {
SCPDArgumentType scpdArgument = getArgument(scpdAction, argumentName, SCPDDirection.IN);
SCPDStateVariableType relatedStateVariable = getStateVariable(serviceRoot, scpdArgument);
if (channelConfig.getDataType().isEmpty()) {
channelConfig.setDataType(relatedStateVariable.getDataType());
} else if (!channelConfig.getDataType().equals(relatedStateVariable.getDataType())) {
throw new ChannelConfigException("dataType of set and get action are different");
}
}
}
// everything is available, create the channel
ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, channelTypeDescription.getName());
if (parameters.isEmpty()) {
// we have no parameters, so create a single channel
ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
ChannelBuilder channelBuilder = ChannelBuilder.create(channelUID, channelTypeDescription.getItem().getType()).withType(channelTypeUID).withProperties(channelProperties);
thingBuilder.withChannel(channelBuilder.build());
channels.put(channelUID, channelConfig);
} else {
// create a channel for each parameter
parameters.forEach(parameter -> {
// remove comment: split parameter at '#', discard everything after that and remove
// trailing spaces
String rawParameter = parameter.split("#")[0].trim();
String normalizedParameter = UIDUtils.encode(rawParameter);
ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId + "_" + normalizedParameter);
ChannelBuilder channelBuilder = ChannelBuilder.create(channelUID, channelTypeDescription.getItem().getType()).withType(channelTypeUID).withProperties(channelProperties).withLabel(channelTypeDescription.getLabel() + " " + parameter);
thingBuilder.withChannel(channelBuilder.build());
Tr064ChannelConfig channelConfig1 = new Tr064ChannelConfig(channelConfig);
channelConfig1.setParameter(rawParameter);
channels.put(channelUID, channelConfig1);
});
}
} catch (ChannelConfigException e) {
LOGGER.debug("Channel {} not available: {}", channelId, e.getMessage());
}
});
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class VigiCruesHandler method discoverAttributes.
private Map<String, String> discoverAttributes(StationConfiguration config) {
Map<String, String> properties = new HashMap<>();
ThingBuilder thingBuilder = editThing();
List<Channel> channels = new ArrayList<>(getThing().getChannels());
try {
HubEauResponse stationDetails = apiHandler.discoverStations(config.id);
stationDetails.stations.stream().findFirst().ifPresent(station -> {
PointType stationLocation = new PointType(String.format(Locale.US, "%f,%f", station.latitudeStation, station.longitudeStation));
properties.put(LOCATION, stationLocation.toString());
PointType serverLocation = locationProvider.getLocation();
if (serverLocation != null) {
DecimalType distance = serverLocation.distanceFrom(stationLocation);
properties.put(DISTANCE, new QuantityType<>(distance, SIUnits.METRE).toString());
}
properties.put(RIVER, station.libelleCoursEau);
});
} catch (VigiCruesException e) {
logger.info("Unable to retrieve station location details {} : {}", config.id, e.getMessage());
}
try {
CdStationHydro refineStation = apiHandler.getStationDetails(config.id);
if (refineStation.vigilanceCrues.cruesHistoriques == null) {
throw new VigiCruesException("No historical data available");
}
refineStation.vigilanceCrues.cruesHistoriques.stream().forEach(crue -> properties.putAll(crue.getDescription()));
String codeTerritoire = refineStation.vigilanceCrues.pereBoitEntVigiCru.cdEntVigiCru;
TerEntVigiCru territoire = apiHandler.getTerritoire(codeTerritoire);
for (VicANMoinsUn troncon : territoire.vicTerEntVigiCru.vicANMoinsUn) {
TronEntVigiCru detail = apiHandler.getTroncon(troncon.vicCdEntVigiCru);
if (detail.getStations().anyMatch(s -> config.id.equalsIgnoreCase(s.vicCdEntVigiCru))) {
properties.put(TRONCON, troncon.vicCdEntVigiCru);
break;
}
}
} catch (VigiCruesException e) {
logger.info("Historical flooding data are not available {} : {}", config.id, e.getMessage());
channels.removeIf(channel -> channel.getUID().getId().contains(RELATIVE_PREFIX));
}
if (!properties.containsKey(TRONCON)) {
channels.removeIf(channel -> channel.getUID().getId().contains(ALERT));
channels.removeIf(channel -> channel.getUID().getId().contains(COMMENT));
}
try {
OpenDatasoftResponse measures = apiHandler.getMeasures(config.id);
measures.getFirstRecord().ifPresent(field -> {
if (field.getHeight() == -1) {
channels.removeIf(channel -> (channel.getUID().getId().contains(HEIGHT)));
}
if (field.getFlow() == -1) {
channels.removeIf(channel -> (channel.getUID().getId().contains(FLOW)));
}
});
} catch (VigiCruesException e) {
logger.warn("Unable to read measurements data {} : {}", config.id, e.getMessage());
}
thingBuilder.withChannels(channels);
updateThing(thingBuilder.build());
return properties;
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class NUTHandler method initDynamicChannels.
/**
* Initializes any channels configured by the user by creating complementary channel types and recreate the channels
* of the thing.
*/
private void initDynamicChannels() {
final NUTChannelTypeProvider localChannelTypeProvider = channelTypeProvider;
final NUTDynamicChannelFactory localDynamicChannelFactory = dynamicChannelFactory;
if (localChannelTypeProvider == null || localDynamicChannelFactory == null) {
return;
}
final List<Channel> updatedChannels = new ArrayList<>();
boolean rebuildChannels = false;
for (final Channel channel : thing.getChannels()) {
if (channel.getConfiguration().getProperties().isEmpty()) {
updatedChannels.add(channel);
} else {
// If the channel has a custom created channel type id the channel should be recreated.
// This is specific for Quantity type channels created in thing files.
final boolean customChannel = channel.getChannelTypeUID() == null;
final NUTDynamicChannelConfiguration channelConfig = channel.getConfiguration().as(NUTDynamicChannelConfiguration.class);
final Channel dynamicChannel;
rebuildChannels = customChannel;
if (customChannel) {
dynamicChannel = localDynamicChannelFactory.createChannel(channel, channelConfig);
if (dynamicChannel == null) {
logger.debug("Could not initialize the dynamic channel '{}'. This channel will be ignored ", channel.getUID());
continue;
} else {
logger.debug("Updating channel '{}' with dynamic channelType settings: {}", channel.getUID(), dynamicChannel.getChannelTypeUID());
}
} else {
logger.debug("Mapping standard dynamic channel '{}' with dynamic channelType settings: {}", channel.getUID(), channel.getChannelTypeUID());
dynamicChannel = channel;
}
userChannelToNutMap.put(channel.getUID(), channelConfig);
updatedChannels.add(dynamicChannel);
}
}
if (rebuildChannels) {
final ThingBuilder thingBuilder = editThing();
thingBuilder.withChannels(updatedChannels);
updateThing(thingBuilder.build());
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class SomfyTahomaBaseThingHandler method createChannel.
private void createChannel(String name, String type, String label, ChannelTypeUID channelType) {
ThingBuilder thingBuilder = editThing();
Channel channel = ChannelBuilder.create(new ChannelUID(thing.getUID(), name), type).withLabel(label).withType(channelType).build();
thingBuilder.withChannel(channel);
updateThing(thingBuilder.build());
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class SimpleDeviceHandler method assertChannel.
/**
* Assert that all channels inside of our thing are well defined.
*
* Only channels which can not be found are created.
*
* @param solarwattChannel channel description with name and unit
*/
protected void assertChannel(SolarwattChannel solarwattChannel) {
ChannelUID channelUID = new ChannelUID(this.getThing().getUID(), solarwattChannel.getChannelName());
ChannelTypeUID channelType = this.channelTypeProvider.assertChannelType(solarwattChannel);
if (this.getThing().getChannel(channelUID) == null) {
ThingBuilder thingBuilder = this.editThing();
thingBuilder.withChannel(getChannelBuilder(solarwattChannel, channelUID, channelType).build());
this.updateThing(thingBuilder.build());
}
}
Aggregations