use of org.openhab.core.thing.binding.builder.ChannelBuilder in project openhab-addons by openhab.
the class PLCDateTimeHandler method doInitialization.
@Override
protected void doInitialization() {
Thing thing = getThing();
logger.debug("Initialize LOGO! date/time handler.");
config.set(getConfigAs(PLCDateTimeConfiguration.class));
super.doInitialization();
if (ThingStatus.OFFLINE != thing.getStatus()) {
String block = config.get().getBlockType();
String text = "Time".equalsIgnoreCase(block) ? "Time" : "Date";
ThingBuilder tBuilder = editThing();
String label = thing.getLabel();
if (label == null) {
Bridge bridge = getBridge();
label = (bridge == null) || (bridge.getLabel() == null) ? "Siemens Logo!" : bridge.getLabel();
label += (": " + text.toLowerCase() + " in/output");
}
tBuilder.withLabel(label);
String name = config.get().getBlockName();
String type = config.get().getChannelType();
ChannelUID uid = new ChannelUID(thing.getUID(), "Time".equalsIgnoreCase(block) ? "time" : "date");
ChannelBuilder cBuilder = ChannelBuilder.create(uid, type);
cBuilder.withType(new ChannelTypeUID(BINDING_ID, type.toLowerCase()));
cBuilder.withLabel(name);
cBuilder.withDescription(text + " block parameter " + name);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
tBuilder.withChannel(cBuilder.build());
cBuilder = ChannelBuilder.create(new ChannelUID(thing.getUID(), VALUE_CHANNEL), ANALOG_ITEM);
cBuilder.withType(new ChannelTypeUID(BINDING_ID, ANALOG_ITEM.toLowerCase()));
cBuilder.withLabel(name);
cBuilder.withDescription(text + " block parameter " + name);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
tBuilder.withChannel(cBuilder.build());
setOldValue(name, null);
updateThing(tBuilder.build());
updateStatus(ThingStatus.ONLINE);
}
}
use of org.openhab.core.thing.binding.builder.ChannelBuilder in project openhab-addons by openhab.
the class PLCDigitalHandler method doInitialization.
@Override
protected void doInitialization() {
Thing thing = getThing();
logger.debug("Initialize LOGO! digital input blocks handler.");
config.set(getConfigAs(PLCDigitalConfiguration.class));
super.doInitialization();
if (ThingStatus.OFFLINE != thing.getStatus()) {
String kind = getBlockKind();
String type = config.get().getChannelType();
String text = DIGITAL_INPUT_ITEM.equalsIgnoreCase(type) ? "input" : "output";
ThingBuilder tBuilder = editThing();
String label = thing.getLabel();
if (label == null) {
Bridge bridge = getBridge();
label = (bridge == null) || (bridge.getLabel() == null) ? "Siemens Logo!" : bridge.getLabel();
label += (": digital " + text + "s");
}
tBuilder.withLabel(label);
for (int i = 0; i < getNumberOfChannels(); i++) {
String name = kind + String.valueOf(i + 1);
ChannelUID uid = new ChannelUID(thing.getUID(), name);
ChannelBuilder cBuilder = ChannelBuilder.create(uid, type);
cBuilder.withType(new ChannelTypeUID(BINDING_ID, type.toLowerCase()));
cBuilder.withLabel(name);
cBuilder.withDescription("Digital " + text + " block " + name);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
tBuilder.withChannel(cBuilder.build());
setOldValue(name, null);
}
updateThing(tBuilder.build());
updateStatus(ThingStatus.ONLINE);
}
}
use of org.openhab.core.thing.binding.builder.ChannelBuilder in project openhab-addons by openhab.
the class PLCPulseHandler method doInitialization.
@Override
protected void doInitialization() {
Thing thing = getThing();
logger.debug("Initialize LOGO! pulse handler.");
config.set(getConfigAs(PLCPulseConfiguration.class));
super.doInitialization();
if (ThingStatus.OFFLINE != thing.getStatus()) {
ThingBuilder tBuilder = editThing();
String label = thing.getLabel();
if (label == null) {
Bridge bridge = getBridge();
label = (bridge == null) || (bridge.getLabel() == null) ? "Siemens Logo!" : bridge.getLabel();
label += (": digital pulse in/output");
}
tBuilder.withLabel(label);
String bName = config.get().getBlockName();
String bType = config.get().getChannelType();
ChannelUID uid = new ChannelUID(thing.getUID(), STATE_CHANNEL);
ChannelBuilder cBuilder = ChannelBuilder.create(uid, bType);
cBuilder.withType(new ChannelTypeUID(BINDING_ID, bType.toLowerCase()));
cBuilder.withLabel(bName);
cBuilder.withDescription("Control block " + bName);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, bName));
tBuilder.withChannel(cBuilder.build());
setOldValue(STATE_CHANNEL, null);
String oName = config.get().getObservedBlock();
String oType = config.get().getObservedChannelType();
cBuilder = ChannelBuilder.create(new ChannelUID(thing.getUID(), OBSERVE_CHANNEL), oType);
cBuilder.withType(new ChannelTypeUID(BINDING_ID, oType.toLowerCase()));
cBuilder.withLabel(oName);
cBuilder.withDescription("Observed block " + oName);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, oName));
tBuilder.withChannel(cBuilder.build());
setOldValue(OBSERVE_CHANNEL, null);
updateThing(tBuilder.build());
updateStatus(ThingStatus.ONLINE);
}
}
use of org.openhab.core.thing.binding.builder.ChannelBuilder 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.ChannelBuilder in project openhab-addons by openhab.
the class LxControl method addChannel.
/**
* Create a new channel and add it to the control. Channel ID is assigned automatically in the order of calls to
* this method, see (@link LxControl#getChannelId}.
*
* @param itemType item type for the channel
* @param typeId channel type ID for the channel
* @param channelLabel channel label
* @param channelDescription channel description
* @param tags tags for the channel or null if no tags needed
* @param commandCallback {@link LxControl} child class method that will be called when command is received
* @param stateCallback {@link LxControl} child class method that will be called to get state value
* @return channel ID of the added channel (can be used to later set state description to it)
*/
ChannelUID addChannel(String itemType, ChannelTypeUID typeId, String channelLabel, String channelDescription, Set<String> tags, CommandCallback commandCallback, StateCallback stateCallback) {
if (channelLabel == null || channelDescription == null) {
logger.error("Attempt to add channel with not finalized configuration!: {}", channelLabel);
return null;
}
ChannelUID channelId = getChannelId(numberOfChannels++);
ChannelBuilder builder = ChannelBuilder.create(channelId, itemType).withType(typeId).withLabel(channelLabel).withDescription(channelDescription + " : " + channelLabel);
if (tags != null) {
builder.withDefaultTags(tags);
}
channels.add(builder.build());
if (commandCallback != null || stateCallback != null) {
callbacks.put(channelId, new Callbacks(commandCallback, stateCallback));
}
return channelId;
}
Aggregations