Added Zigbee attribute reading daemon

This commit is contained in:
Ziver Koc 2021-07-23 23:35:19 +02:00
parent 68054ebf4f
commit 9e6172caeb
7 changed files with 116 additions and 25 deletions

View file

@ -0,0 +1,72 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.hal.plugin.zigbee;
import com.zsmartsystems.zigbee.zcl.ZclAttribute;
import com.zsmartsystems.zigbee.zcl.ZclCluster;
import se.hal.intf.HalAbstractControllerManager;
import se.hal.intf.HalDaemon;
import se.hal.plugin.zigbee.device.ZigbeeHalDeviceConfig;
import zutil.log.LogUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ZigbeeAttributeUpdateDaemon implements HalDaemon {
private static final Logger logger = LogUtil.getLogger();
@Override
public void initiate(ScheduledExecutorService executor) {
executor.scheduleAtFixedRate(this, 0, 10, TimeUnit.MINUTES);
}
@Override
public void run() {
logger.info("Requesting zigbee attribute updates.");
ZigbeeController controller = HalAbstractControllerManager.getController(ZigbeeController.class);
for (ZigbeeHalDeviceConfig device : controller.getRegisteredDevices()) {
try {
ZclCluster cluster = device.getZigbeeCluster(controller);
Collection<ZclAttribute> attributes = cluster.getAttributes();
List<Integer> attributeIds = new ArrayList<>();
for (ZclAttribute attr : attributes) {
attributeIds.add(attr.getId());
}
cluster.readAttributes(attributeIds);
} catch (Exception e) {
logger.log(Level.WARNING, "Was unable to read attribute for device: " + device.getZigbeeNodeAddress(), e);
}
}
}
}

View file

@ -32,8 +32,6 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.zsmartsystems.zigbee.zcl.clusters.ZclBasicCluster.*;
/**
* Controller that will connect to a Zigbee USB coordinator.
*/
@ -217,9 +215,15 @@ public class ZigbeeController implements HalSensorController,
return networkManager.getZigBeeExtendedPanId();
}
public ZigBeeNode getNode(IeeeAddress address) {
return networkManager.getNode(address);
}
public Set<ZigBeeNode> getNodes() {
return networkManager.getNodes();
}
public List<ZigbeeHalDeviceConfig> getRegisteredDevices() {
return registeredDevices;
}
// ------------------------------------------
// Zigbee Node Methods
@ -372,7 +376,7 @@ public class ZigbeeController implements HalSensorController,
@Override
public void send(HalEventConfig eventConfig, HalEventData eventData) {
if (eventConfig instanceof ZigbeeHalEventDeviceConfig) {
((ZigbeeHalEventDeviceConfig) eventConfig).sendZigbeeCommand(networkManager, eventData);
((ZigbeeHalEventDeviceConfig) eventConfig).sendZigbeeCommand(this, eventData);
}
}

View file

@ -1,6 +1,8 @@
package se.hal.plugin.zigbee.device;
import com.zsmartsystems.zigbee.IeeeAddress;
import com.zsmartsystems.zigbee.ZigBeeEndpoint;
import com.zsmartsystems.zigbee.ZigBeeNode;
import com.zsmartsystems.zigbee.zcl.ZclAttribute;
import com.zsmartsystems.zigbee.zcl.ZclCluster;
import se.hal.intf.HalAbstractController;
@ -27,6 +29,20 @@ public abstract class ZigbeeHalDeviceConfig implements HalDeviceConfig {
return new IeeeAddress(zigbeeNodeAddressStr);
}
public ZclCluster getZigbeeCluster(ZigbeeController controller) {
ZigBeeNode node = controller.getNode(getZigbeeNodeAddress());
for (ZigBeeEndpoint endpoint : node.getEndpoints()) {
ZclCluster cluster = endpoint.getInputCluster(getZigbeeClusterId());
if (cluster != null) {
return cluster;
}
}
return null;
}
// --------------------------
// Abstract Methods
// --------------------------

View file

@ -7,6 +7,7 @@ import com.zsmartsystems.zigbee.ZigBeeNode;
import com.zsmartsystems.zigbee.zcl.ZclCluster;
import com.zsmartsystems.zigbee.zcl.ZclCommand;
import se.hal.intf.HalEventData;
import se.hal.plugin.zigbee.ZigbeeController;
import zutil.log.LogUtil;
import java.lang.reflect.Method;
@ -21,22 +22,9 @@ public abstract class ZigbeeHalEventDeviceConfig extends ZigbeeHalDeviceConfig {
private static final Logger logger = LogUtil.getLogger();
public ZclCluster getZigbeeCluster(ZigBeeNetworkManager networkManager) {
ZigBeeNode node = networkManager.getNode(getZigbeeNodeAddress());
for (ZigBeeEndpoint endpoint : node.getEndpoints()) {
ZclCluster cluster = endpoint.getInputCluster(getZigbeeClusterId());
if (cluster != null) {
return cluster;
}
}
return null;
}
@SuppressWarnings("unchecked")
public void sendZigbeeCommand(ZigBeeNetworkManager networkManager, HalEventData data) {
ZclCluster cluster = getZigbeeCluster(networkManager);
public void sendZigbeeCommand(ZigbeeController controller, HalEventData data) {
ZclCluster cluster = getZigbeeCluster(controller);
if (cluster != null) {
try {

View file

@ -27,13 +27,7 @@ public class ZigbeeNodeOverviewPage extends HalWebPage {
Map<String, String> request)
throws Exception {
ZigbeeController controller = null;
for (HalAbstractController cont : HalAbstractControllerManager.getControllers()) {
if (cont instanceof ZigbeeController) {
controller = ((ZigbeeController) cont);
break;
}
}
ZigbeeController controller = HalAbstractControllerManager.getController(ZigbeeController.class);
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
tmpl.set("controller", controller);

View file

@ -11,6 +11,7 @@
{"se.hal.intf.HalSensorConfig": "se.hal.plugin.zigbee.device.ZigbeeTemperatureConfig"},
{"se.hal.intf.HalAutostartController": "se.hal.plugin.zigbee.ZigbeeController"},
{"se.hal.intf.HalDaemon": "se.hal.plugin.zigbee.ZigbeeAttributeUpdateDaemon"},
{"se.hal.intf.HalWebPage": "se.hal.plugin.zigbee.page.ZigbeeNodeOverviewPage"}
]