Search in sources :

Example 6 with ChannelBuilder

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);
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelUID(org.openhab.core.thing.ChannelUID) PLCDateTimeConfiguration(org.openhab.binding.plclogo.internal.config.PLCDateTimeConfiguration) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) Thing(org.openhab.core.thing.Thing) Bridge(org.openhab.core.thing.Bridge)

Example 7 with ChannelBuilder

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);
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelUID(org.openhab.core.thing.ChannelUID) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) PLCDigitalConfiguration(org.openhab.binding.plclogo.internal.config.PLCDigitalConfiguration) Thing(org.openhab.core.thing.Thing) Bridge(org.openhab.core.thing.Bridge)

Example 8 with ChannelBuilder

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);
    }
}
Also used : PLCPulseConfiguration(org.openhab.binding.plclogo.internal.config.PLCPulseConfiguration) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelUID(org.openhab.core.thing.ChannelUID) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) Thing(org.openhab.core.thing.Thing) Bridge(org.openhab.core.thing.Bridge)

Example 9 with ChannelBuilder

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());
        }
    });
}
Also used : ActionType(org.openhab.binding.tr064.internal.dto.config.ActionType) XMLInputFactory(javax.xml.stream.XMLInputFactory) SOAPException(javax.xml.soap.SOAPException) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) Tr064RootHandler(org.openhab.binding.tr064.internal.Tr064RootHandler) HttpClient(org.eclipse.jetty.client.HttpClient) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Nullable(org.eclipse.jdt.annotation.Nullable) Duration(java.time.Duration) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Tr064RootConfiguration(org.openhab.binding.tr064.internal.config.Tr064RootConfiguration) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) Set(java.util.Set) ChannelTypeDescriptions(org.openhab.binding.tr064.internal.dto.config.ChannelTypeDescriptions) Tr064ChannelConfig(org.openhab.binding.tr064.internal.config.Tr064ChannelConfig) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) Tr064SubConfiguration(org.openhab.binding.tr064.internal.config.Tr064SubConfiguration) List(java.util.List) SCPDActionType(org.openhab.binding.tr064.internal.dto.scpd.service.SCPDActionType) Stream(java.util.stream.Stream) SCPDScpdType(org.openhab.binding.tr064.internal.dto.scpd.service.SCPDScpdType) Optional(java.util.Optional) SCPDStateVariableType(org.openhab.binding.tr064.internal.dto.scpd.service.SCPDStateVariableType) ChannelTypeDescription(org.openhab.binding.tr064.internal.dto.config.ChannelTypeDescription) SCPDArgumentType(org.openhab.binding.tr064.internal.dto.scpd.service.SCPDArgumentType) SCPDServiceType(org.openhab.binding.tr064.internal.dto.scpd.root.SCPDServiceType) StreamSource(javax.xml.transform.stream.StreamSource) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Thing(org.openhab.core.thing.Thing) Tr064BaseThingConfiguration(org.openhab.binding.tr064.internal.config.Tr064BaseThingConfiguration) Tr064BindingConstants(org.openhab.binding.tr064.internal.Tr064BindingConstants) UIDUtils(org.openhab.core.util.UIDUtils) ChannelUID(org.openhab.core.thing.ChannelUID) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) ParameterType(org.openhab.binding.tr064.internal.dto.config.ParameterType) Logger(org.slf4j.Logger) NodeList(org.w3c.dom.NodeList) JAXBElement(javax.xml.bind.JAXBElement) ExpiringCacheMap(org.openhab.core.cache.ExpiringCacheMap) Field(java.lang.reflect.Field) SCPDDirection(org.openhab.binding.tr064.internal.dto.scpd.service.SCPDDirection) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpMethod(org.eclipse.jetty.http.HttpMethod) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) SOAPMessage(javax.xml.soap.SOAPMessage) ChannelConfigException(org.openhab.binding.tr064.internal.ChannelConfigException) Collections(java.util.Collections) InputStream(java.io.InputStream) SCPDStateVariableType(org.openhab.binding.tr064.internal.dto.scpd.service.SCPDStateVariableType) Tr064SubConfiguration(org.openhab.binding.tr064.internal.config.Tr064SubConfiguration) SCPDServiceType(org.openhab.binding.tr064.internal.dto.scpd.root.SCPDServiceType) Tr064RootConfiguration(org.openhab.binding.tr064.internal.config.Tr064RootConfiguration) ActionType(org.openhab.binding.tr064.internal.dto.config.ActionType) SCPDActionType(org.openhab.binding.tr064.internal.dto.scpd.service.SCPDActionType) SCPDScpdType(org.openhab.binding.tr064.internal.dto.scpd.service.SCPDScpdType) HashMap(java.util.HashMap) SCPDArgumentType(org.openhab.binding.tr064.internal.dto.scpd.service.SCPDArgumentType) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) Tr064BaseThingConfiguration(org.openhab.binding.tr064.internal.config.Tr064BaseThingConfiguration) ChannelUID(org.openhab.core.thing.ChannelUID) Tr064ChannelConfig(org.openhab.binding.tr064.internal.config.Tr064ChannelConfig) ChannelConfigException(org.openhab.binding.tr064.internal.ChannelConfigException) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) SCPDActionType(org.openhab.binding.tr064.internal.dto.scpd.service.SCPDActionType) HashSet(java.util.HashSet)

Example 10 with ChannelBuilder

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;
}
Also used : ChannelUID(org.openhab.core.thing.ChannelUID) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder)

Aggregations

ChannelBuilder (org.openhab.core.thing.binding.builder.ChannelBuilder)25 ChannelUID (org.openhab.core.thing.ChannelUID)17 ThingBuilder (org.openhab.core.thing.binding.builder.ThingBuilder)10 ChannelTypeUID (org.openhab.core.thing.type.ChannelTypeUID)10 Channel (org.openhab.core.thing.Channel)9 Thing (org.openhab.core.thing.Thing)8 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)8 Configuration (org.openhab.core.config.core.Configuration)6 Nullable (org.eclipse.jdt.annotation.Nullable)5 Bridge (org.openhab.core.thing.Bridge)5 ArrayList (java.util.ArrayList)4 BigDecimal (java.math.BigDecimal)3 ChannelType (org.openhab.core.thing.type.ChannelType)3 Duration (java.time.Duration)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Test (org.junit.jupiter.api.Test)2