Skip to content

Commit

Permalink
Merge pull request #1 from presslab-us/zigbee
Browse files Browse the repository at this point in the history
Zigbee
  • Loading branch information
cdjackson committed Mar 22, 2015
2 parents 27d4876 + 54f51ad commit 0b608d6
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,14 @@
import org.bubblecloud.zigbee.ZigBeeApi;
import org.bubblecloud.zigbee.api.Device;
import org.bubblecloud.zigbee.api.DeviceListener;
import org.bubblecloud.zigbee.api.ZigBeeDeviceException;
import org.bubblecloud.zigbee.api.cluster.Cluster;
import org.bubblecloud.zigbee.api.cluster.general.ColorControl;
import org.bubblecloud.zigbee.api.cluster.general.LevelControl;
import org.bubblecloud.zigbee.api.cluster.general.OnOff;
import org.bubblecloud.zigbee.api.cluster.impl.api.core.Attribute;
import org.bubblecloud.zigbee.api.cluster.impl.api.core.ReportListener;
import org.bubblecloud.zigbee.api.cluster.impl.api.core.ZigBeeClusterException;
import org.bubblecloud.zigbee.api.cluster.impl.attribute.AttributeDescriptor;
import org.bubblecloud.zigbee.network.model.DiscoveryMode;
import org.bubblecloud.zigbee.network.port.ZigBeePort;
import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.core.library.types.HSBType;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
Expand Down Expand Up @@ -141,8 +134,6 @@ protected void browsingComplete() {
logger.debug("ZigBee network READY. Found "
+ zigbeeApi.getDevices().size() + " nodes.");

updateStatus(ThingStatus.ONLINE);

final List<Device> devices = zigbeeApi.getDevices();
for (int i = 0; i < devices.size(); i++) {
final Device device = devices.get(i);
Expand Down Expand Up @@ -197,95 +188,6 @@ private Device getDeviceByIndexOrEndpointId(ZigBeeApi zigbeeApi,
return device;
}

public boolean LightPower(String lightAddress, OnOffType state) {
final Device device = getDeviceByIndexOrEndpointId(zigbeeApi,
lightAddress);
if (device == null) {
return false;
}
final OnOff onOff = device.getCluster(OnOff.class);
try {
if (state == OnOffType.ON) {
onOff.on();
} else {
onOff.off();
}
} catch (ZigBeeDeviceException e) {
e.printStackTrace();
}

return true;
}

public boolean LightBrightness(String lightAddress, int state) {
final Device device = getDeviceByIndexOrEndpointId(zigbeeApi,
lightAddress);
if (device == null) {
return false;
}

try {
final LevelControl levelControl = device
.getCluster(LevelControl.class);
levelControl.moveToLevelWithOnOff((short)(state * 254.0 / 100.0 + 0.5), 10);
} catch (ZigBeeDeviceException e) {
e.printStackTrace();
}

return true;
}

public boolean LightColor(String lightAddress, HSBType state) {
final Device device = getDeviceByIndexOrEndpointId(zigbeeApi,
lightAddress);
if (device == null) {
return false;
}

final ColorControl colorControl = device.getCluster(ColorControl.class);
if (colorControl == null) {
logger.debug("Device {} does not support color control.",
lightAddress);
return false;
}

try {
int hue = state.getHue().intValue();
int saturation = state.getSaturation().intValue();
colorControl.moveToHue((int)(hue * 254.0 / 360.0 + 0.5), 0, 10);
colorControl.movetoSaturation((int)(saturation * 254.0 / 100.0 + 0.5), 10);
} catch (ZigBeeDeviceException e) {
e.printStackTrace();
}

return true;
}

public boolean LightColorTemp(String lightAddress, PercentType state) {
final Device device = getDeviceByIndexOrEndpointId(zigbeeApi,
lightAddress);
if (device == null) {
return false;
}

final ColorControl colorControl = device.getCluster(ColorControl.class);
if (colorControl == null) {
logger.debug("Device {} does not support color control.",
lightAddress);
return false;
}

try {
// Range of 2000K to 6000K, gain = 4000K, offset = 2000K
double kelvin = state.intValue() * 4000.0 / 100.0 + 2000.0;
colorControl.moveToColorTemperature((short)(1e6 / kelvin + 0.5), 10);
} catch (ZigBeeDeviceException e) {
e.printStackTrace();
}

return true;
}

public Object attributeRead(String zigbeeAddress, int clusterId, int attributeIndex) {
final Device device = getDeviceByIndexOrEndpointId(zigbeeApi,
zigbeeAddress);
Expand Down Expand Up @@ -319,7 +221,7 @@ public Object readAttribute(Device device, int clusterId, int attributeIndex) {
}
}

public Attribute openAttribute(String zigbeeAddress, short clusterId, AttributeDescriptor attributeId, ZigBeeEventListener listener) {
public <T extends Cluster> Attribute openAttribute(String zigbeeAddress, Class<T> clusterId, AttributeDescriptor attributeId, ZigBeeEventListener listener) {
final Device device = getDeviceByIndexOrEndpointId(zigbeeApi, zigbeeAddress);
Cluster cluster = device.getCluster(clusterId);
if (cluster == null) {
Expand All @@ -342,6 +244,15 @@ public void closeAttribute(Attribute attribute, ZigBeeEventListener listener) {
}
}

public <T extends Cluster> T openCluster(String zigbeeAddress, Class<T> clusterId) {
final Device device = getDeviceByIndexOrEndpointId(zigbeeApi, zigbeeAddress);
return device.getCluster(clusterId);
}

public void closeCluster(Cluster cluster) {

}

/**
* Returns a list of all known devices
* @return list of devices
Expand Down Expand Up @@ -448,8 +359,9 @@ public void run(Device device) {
// Signal to the handlers that they are known...
ZigBeeEventListener listener = eventListeners.get(device.getEndpointId());
if (listener != null) {
listener.openDevice();
listener.onEndpointStateChange();
if (listener.openDevice()) {
listener.onEndpointStateChange();
}
}

discoveryService.deviceAdded(device, description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public interface ZigBeeEventListener {
public void onEndpointStateChange();

public void openDevice();
public boolean openDevice();
public void closeDevice();

/**
Expand Down
Loading

0 comments on commit 0b608d6

Please sign in to comment.