From 9e6172caeb3f82cbe4246e5503761baa9041884b Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Fri, 23 Jul 2021 23:35:19 +0200 Subject: [PATCH] Added Zigbee attribute reading daemon --- .../intf/HalAbstractControllerManager.java | 16 +++++ .../zigbee/ZigbeeAttributeUpdateDaemon.java | 72 +++++++++++++++++++ .../hal/plugin/zigbee/ZigbeeController.java | 10 ++- .../zigbee/device/ZigbeeHalDeviceConfig.java | 16 +++++ .../device/ZigbeeHalEventDeviceConfig.java | 18 +---- .../zigbee/page/ZigbeeNodeOverviewPage.java | 8 +-- .../src/se/hal/plugin/zigbee/plugin.json | 1 + 7 files changed, 116 insertions(+), 25 deletions(-) create mode 100644 plugins/hal-zigbee/src/se/hal/plugin/zigbee/ZigbeeAttributeUpdateDaemon.java diff --git a/hal-core/src/se/hal/intf/HalAbstractControllerManager.java b/hal-core/src/se/hal/intf/HalAbstractControllerManager.java index 416add9b..48c49ad4 100644 --- a/hal-core/src/se/hal/intf/HalAbstractControllerManager.java +++ b/hal-core/src/se/hal/intf/HalAbstractControllerManager.java @@ -117,6 +117,22 @@ public abstract class HalAbstractControllerManager(controllerMap.values()); } + /** + * @param controllerClass the class of the wanted controller + * @return the first controller matching the given class, null if no controller was found. + */ + public static T getController(Class controllerClass) { + for (HalAbstractController controller : HalAbstractControllerManager.getControllers()) { + if (controllerClass.isAssignableFrom(controller.getClass())) { + return (T) controller; + } + } + + return null; + } + + + /** * Will return a singleton controller instance of the given class. * If a instance does not exist yet the a new instance will be allocated diff --git a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/ZigbeeAttributeUpdateDaemon.java b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/ZigbeeAttributeUpdateDaemon.java new file mode 100644 index 00000000..2462e2b4 --- /dev/null +++ b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/ZigbeeAttributeUpdateDaemon.java @@ -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 attributes = cluster.getAttributes(); + + List 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); + } + } + } +} diff --git a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/ZigbeeController.java b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/ZigbeeController.java index c454dd39..0fb9ac09 100644 --- a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/ZigbeeController.java +++ b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/ZigbeeController.java @@ -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 getNodes() { return networkManager.getNodes(); } + public List 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); } } diff --git a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/device/ZigbeeHalDeviceConfig.java b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/device/ZigbeeHalDeviceConfig.java index 5f473bb3..c28bce30 100644 --- a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/device/ZigbeeHalDeviceConfig.java +++ b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/device/ZigbeeHalDeviceConfig.java @@ -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 // -------------------------- diff --git a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/device/ZigbeeHalEventDeviceConfig.java b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/device/ZigbeeHalEventDeviceConfig.java index 25d0497a..2b03c3ad 100644 --- a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/device/ZigbeeHalEventDeviceConfig.java +++ b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/device/ZigbeeHalEventDeviceConfig.java @@ -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 { diff --git a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/page/ZigbeeNodeOverviewPage.java b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/page/ZigbeeNodeOverviewPage.java index a0deffd1..79ea01bc 100644 --- a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/page/ZigbeeNodeOverviewPage.java +++ b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/page/ZigbeeNodeOverviewPage.java @@ -27,13 +27,7 @@ public class ZigbeeNodeOverviewPage extends HalWebPage { Map 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); diff --git a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/plugin.json b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/plugin.json index acdb1f73..c80d4a69 100644 --- a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/plugin.json +++ b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/plugin.json @@ -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"} ]