| sidebar_position | 7 |
|---|
This examples can be used as kick-off before jumping into the bco development. They pick up the basic functions like how to query, control and access any units.
:::note Note Please make sure an mqtt broker and bco are started within your network before performing the howtos. :::
For running any java examples you only need to include the dal remote dependency in your maven or gradle project description:
<dependency>
<groupId>org.openbase</groupId>
<artifactId>bco.dal.remote</artifactId>
<version>[3.0-SNAPSHOT,3.1-alpha)</version>
</dependency>Query units
LOGGER.info("query lights");
final List<UnitConfig> lightUnitConfigList =
Registries.getUnitRegistry().getUnitConfigsByLocationIdAndUnitType(locationId, unitType);Request the unit
LOGGER.info("request the scene with the alias \"Scene-9\"");
testScene = Units.getUnitByAlias("Scene-9", true, Units.SCENE);Control the unit
LOGGER.info("activate the scene");
testScene.setActivationState(ActivationState.State.ACTIVE);Request the unit
LOGGER.info("request the light unit with the alias \"ColorableLight-0\"");
testLight = Units.getUnitByAlias("ColorableLight-7", true, Units.LIGHT_COLORABLE);Control the unit
LOGGER.info("switch the light on");
testLight.setPowerState(PowerState.State.ON);
LOGGER.info("switch light color to red");
testLight.setColor(HSBColor.newBuilder().setHue(0d).setSaturation(1d).setBrightness(1d).build());Observe a locations motion state
location.addServiceStateObserver(ServiceTempus.CURRENT, ServiceType.MOTION_STATE_SERVICE, (source, data) -> {
// we know its a motion state
final MotionState motionState = (MotionState) data;
LOGGER.info("EXAMPLE 2: "+location.getLabel("?") + " has changed its motion state to " + motionState.getValue().name());
});Resolve the label
final List<UnitConfig> targetUnitConfigs =
Registries.getUnitRegistry().getUnitConfigsByLabel(unitLabel);The infrastructure flag can be used to determine if a unit is related to any important environment services. E.g. an internet router/ water boiler / voice assistant is connected to a powerplug. If its related POWER_SWITCH unit is marked as infrastructure (MetaConfig Entry: INFRASTRUCTURE = true) it will not be affected by any location based control actions (e.g. switch off all power switches in the living room). The following code shows how to check the infrastructure flag of a unit:
// Check infrastructure flag via UnitRemote
ColorableLightRemote unit = Units.getUnit("myUnitId", false, Units.COLORABLE_LIGHT);
unit.isInfrastructure();
// Check infrastructure flag via RemoteAction
RemoteAction action = new RemoteAction(...);
action.getTargetUnit().isInfrastructure();