Skip to content

Instantly share code, notes, and snippets.

@splatch
Created July 5, 2016 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save splatch/657492703d452aab5214005552077cfc to your computer and use it in GitHub Desktop.
Save splatch/657492703d452aab5214005552077cfc to your computer and use it in GitHub Desktop.
@Override
public void bridgeHandlerInitialized(ThingHandler thingHandler, Bridge bridge) {
if (thingHandler instanceof BacNetBridgeHandler) {
this.client = ((BacNetBridgeHandler) thingHandler).getClient();
BacNetDeviceConfiguration configuration = getConfigAs(BacNetDeviceConfiguration.class);
// hardcode network number temporary to 0
this.device = new Device(configuration.id, configuration.ip, configuration.port, 0);
final List<Property> deviceProperties = client.getDeviceProperties(device);
List<Channel> channels = new ArrayList<>();
for (Property property : deviceProperties) {
ChannelUID channelUID = new ChannelUID(getThing().getUID(),
property.getType().name() + "#" + property.getId());
ChannelBuilder builder = null;
switch (property.getType()) {
case ANALOG_INPUT:
builder = ChannelBuilder.create(channelUID, BacNetBindingConstants.CHANNEL_ANALOG_INPUT);
break;
case ANALOG_OUTPUT:
builder = ChannelBuilder.create(channelUID, BacNetBindingConstants.CHANNEL_ANALOG_OUTPUT);
break;
case ANALOG_VALUE:
builder = ChannelBuilder.create(channelUID, BacNetBindingConstants.CHANNEL_ANALOG_VALUE);
break;
case BINARY_VALUE:
builder = ChannelBuilder.create(channelUID, BacNetBindingConstants.CHANNEL_BINARY_VALUE);
break;
case BINARY_OUTPUT:
builder = ChannelBuilder.create(channelUID, BacNetBindingConstants.CHANNEL_BINARY_OUTPUT);
break;
}
if (builder == null) {
logger.warn("Unsupported type of property {}", property.getType());
continue;
}
builder.withLabel(property.getName()).withDescription(property.getDescription())
.withProperties(ImmutableMap.of("id", Integer.toString(property.getId())));
channels.add(builder.build());
}
ThingBuilder thingBuilder = editThing().withChannels(channels);
updateThing(thingBuilder.build());
updateStatus(ThingStatus.ONLINE);
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
for (Property<?, Object> property : deviceProperties) {
String uid = property.getType().name() + "#" + property.getId();
// if (isLinked(uid.getAsString())) {
Object value = client.getPropertyValue(property);
if (value instanceof Integer) {
updateState(uid, new DecimalType((Integer) value));
} else if (value instanceof Boolean) {
updateState(uid, ((Boolean) value) ? OnOffType.ON : OnOffType.OFF);
} else if (value instanceof Float) {
updateState(uid, new DecimalType((Float) value));
} else {
logger.info("Unsupported return type {} for property {}", value, property);
}
}
}
}, 0, 30, TimeUnit.SECONDS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment