use of org.openhab.core.thing.binding.builder.ThingBuilder 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.ThingBuilder 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.ThingBuilder in project openhab-addons by openhab.
the class RemoteopenhabBridgeHandler method removeChannels.
private void removeChannels(List<RemoteopenhabItem> items) {
synchronized (updateThingLock) {
int nbRemoved = 0;
ThingBuilder thingBuilder = editThing();
for (RemoteopenhabItem item : items) {
Channel channel = getThing().getChannel(item.name);
if (channel != null) {
thingBuilder.withoutChannel(channel.getUID());
nbRemoved++;
}
}
if (nbRemoved > 0) {
updateThing(thingBuilder.build());
logger.debug("{} channels removed for the thing {} (from {} items)", nbRemoved, getThing().getUID(), items.size());
}
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class RemoteopenhabThingHandler method createTriggerChannels.
private void createTriggerChannels(RemoteopenhabThing thing, boolean addNewChannels) {
List<Channel> channels = new ArrayList<>();
for (RemoteopenhabChannel channelDTO : thing.channels) {
if (!"TRIGGER".equals(channelDTO.kind)) {
continue;
}
ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, CHANNEL_TYPE_TRIGGER);
ChannelUID channelUID = new ChannelUID(getThing().getUID(), channelDTO.uid.replaceAll("[^A-Za-z0-9_]", "_"));
Configuration channelConfig = new Configuration();
channelConfig.put(CHANNEL_UID, channelDTO.uid);
logger.trace("Create the channel {} of type {}", channelUID, channelTypeUID);
channels.add(ChannelBuilder.create(channelUID, null).withType(channelTypeUID).withKind(ChannelKind.TRIGGER).withLabel(channelDTO.label).withDescription(channelDTO.description).withConfiguration(channelConfig).build());
}
if (!channels.isEmpty()) {
ThingBuilder thingBuilder = editThing();
int nbRemoved = 0;
for (Channel channel : channels) {
if (getThing().getChannel(channel.getUID()) != null) {
thingBuilder.withoutChannel(channel.getUID());
nbRemoved++;
}
}
if (nbRemoved > 0) {
logger.debug("{} trigger channels removed for the thing {}", nbRemoved, getThing().getUID());
}
int nbAdded = 0;
if (addNewChannels) {
for (Channel channel : channels) {
thingBuilder.withChannel(channel);
}
nbAdded = channels.size();
logger.debug("{} trigger channels added for the thing {}", nbAdded, getThing().getUID());
}
if (nbRemoved > 0 || nbAdded > 0) {
updateThing(thingBuilder.build());
}
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class ResolThingHandler method packetReceived.
@Override
protected void packetReceived(Specification spec, Language lang, Packet packet) {
PacketFieldValue[] pfvs = spec.getPacketFieldValuesForHeaders(new Packet[] { packet });
for (PacketFieldValue pfv : pfvs) {
logger.trace("Id: {}, Name: {}, Raw: {}, Text: {}", pfv.getPacketFieldId(), pfv.getName(lang), pfv.getRawValueDouble(), pfv.formatTextValue(null, Locale.getDefault()));
// use English name as channel
String channelId = pfv.getName();
channelId = channelId.replace(" [", "-");
channelId = channelId.replace("]", "");
channelId = channelId.replace("(", "-");
channelId = channelId.replace(")", "");
channelId = channelId.replace(" #", "-");
channelId = channelId.replaceAll("[^A-Za-z0-9_-]+", "_");
channelId = channelId.toLowerCase(Locale.ENGLISH);
ChannelTypeUID channelTypeUID;
if (pfv.getPacketFieldSpec().getUnit().getUnitId() >= 0) {
channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, pfv.getPacketFieldSpec().getUnit().getUnitCodeText());
} else if (pfv.getPacketFieldSpec().getType() == SpecificationFile.Type.DateTime) {
channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, "datetime");
} else if (pfv.getPacketFieldSpec().getType() == SpecificationFile.Type.WeekTime) {
channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, "weektime");
} else if (pfv.getPacketFieldSpec().getType() == SpecificationFile.Type.Time) {
channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, "time");
} else {
/* used for enums and the numeric types without unit */
channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, "None");
}
String acceptedItemType;
Thing thing = getThing();
switch(pfv.getPacketFieldSpec().getType()) {
case WeekTime:
case DateTime:
acceptedItemType = "DateTime";
break;
case Number:
acceptedItemType = ResolChannelTypeProvider.itemTypeForUnit(pfv.getPacketFieldSpec().getUnit());
break;
case Time:
default:
acceptedItemType = "String";
break;
}
Channel a = thing.getChannel(channelId);
if (a == null) {
/* channel doesn't exit, let's create it */
ThingBuilder thingBuilder = editThing();
ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
if (pfv.getEnumVariant() != null) {
/* create a state option channel */
List<StateOption> options = new ArrayList<>();
PacketFieldSpec ff = pfv.getPacketFieldSpec();
Enum e = ff.getEnum();
for (long l : e.getValues()) {
EnumVariant v = e.getEnumVariantForValue(l);
options.add(new StateOption(Long.toString(l), v.getText(lang)));
}
stateDescriptionProvider.setStateOptions(channelUID, options);
Channel channel = ChannelBuilder.create(channelUID, "Number").withType(channelTypeUID).withLabel(pfv.getName(lang)).build();
thingBuilder.withChannel(channel).withLabel(thing.getLabel());
updateThing(thingBuilder.build());
} else if ("DateTime".equals(acceptedItemType)) {
/* a date channel */
Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID).withLabel(pfv.getName(lang)).build();
thingBuilder.withChannel(channel).withLabel(thing.getLabel());
updateThing(thingBuilder.build());
} else if ("String".equals(acceptedItemType)) {
/* a string channel */
Channel channel = ChannelBuilder.create(channelUID, "String").withType(channelTypeUID).withLabel(pfv.getName(lang)).build();
thingBuilder.withChannel(channel).withLabel(thing.getLabel());
updateThing(thingBuilder.build());
} else if (pfv.getRawValueDouble() != null) {
/* a number channel */
Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID).withLabel(pfv.getName(lang)).build();
thingBuilder.withChannel(channel).withLabel(thing.getLabel());
updateThing(thingBuilder.build());
} else {
/* a string channel */
Channel channel = ChannelBuilder.create(channelUID, "String").withType(channelTypeUID).withLabel(pfv.getName(lang)).build();
thingBuilder.withChannel(channel).withLabel(thing.getLabel());
updateThing(thingBuilder.build());
}
logger.debug("Creating channel: {}", channelUID);
}
if (pfv.getEnumVariant() != null) {
/* update the enum / State channel */
this.updateState(channelId, new StringType(Long.toString(pfv.getRawValueLong())));
} else {
switch(pfv.getPacketFieldSpec().getType()) {
case Number:
Double dd = pfv.getRawValueDouble();
if (dd != null) {
if (!isSpecialValue(dd)) {
/* only set the value if no error occurred */
String str = pfv.formatText();
if (str.endsWith("RH")) {
/* unit %RH for relative humidity is not known in openHAB UoM, so we remove it */
str = str.substring(0, str.length() - 2);
}
if (str.endsWith("Ω")) {
QuantityType<?> q = new QuantityType<>(dd, Units.OHM);
this.updateState(channelId, q);
} else {
try {
QuantityType<?> q = new QuantityType<>(str);
this.updateState(channelId, q);
} catch (IllegalArgumentException e) {
logger.debug("unit of '{}' unknown in openHAB", str);
QuantityType<?> q = new QuantityType<>(dd.toString());
this.updateState(channelId, q);
}
}
}
}
/*
* else {
* field not available in this packet, e. g. old firmware version not (yet) transmitting it
* }
*/
break;
case Time:
synchronized (TIME_FORMAT) {
this.updateState(channelId, new StringType(TIME_FORMAT.format(pfv.getRawValueDate())));
}
break;
case WeekTime:
synchronized (WEEK_FORMAT) {
DateTimeType d = new DateTimeType(WEEK_FORMAT.format(pfv.getRawValueDate()));
this.updateState(channelId, d);
}
break;
case DateTime:
synchronized (DATE_FORMAT) {
DateTimeType d = new DateTimeType(DATE_FORMAT.format(pfv.getRawValueDate()));
this.updateState(channelId, d);
}
break;
default:
Bridge b = getBridge();
if (b != null) {
ResolBridgeHandler handler = (ResolBridgeHandler) b.getHandler();
String value;
if (handler != null) {
value = pfv.formatTextValue(pfv.getPacketFieldSpec().getUnit(), handler.getLocale());
} else {
value = pfv.formatTextValue(pfv.getPacketFieldSpec().getUnit(), Locale.getDefault());
}
try {
QuantityType<?> q = new QuantityType<>(value);
this.updateState(channelId, q);
} catch (IllegalArgumentException e) {
this.updateState(channelId, new StringType(value));
logger.debug("unit of '{}' unknown in openHAB, using string", value);
}
}
}
}
}
}
Aggregations