From 203bb67f7f34b085f139393867775f112aad68a5 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Mon, 19 Jul 2021 18:06:00 +0200 Subject: [PATCH] Added hal prefix to all config properties --- hal-core/src/se/hal/HalContext.java | 28 ++++----- hal-core/src/se/hal/HalServer.java | 2 +- hal-core/src/se/hal/page/MapWebPage.java | 4 +- hal.conf.example | 24 ++++---- .../assistant/google/SmartHomeDaemon.java | 12 ++-- .../hal/plugin/nutups/NutUpsController.java | 12 ++-- .../daemon/PCDataSynchronizationDaemon.java | 2 +- .../plugin/tellstick/TellstickSerialComm.java | 2 +- .../resource/web/zigbee_node_overview.tmpl | 30 ++++++++++ .../hal/plugin/zigbee/ZigbeeController.java | 57 ++++++++++++++----- .../device/ZigbeeHalEventDeviceConfig.java | 8 +-- .../hal/plugin/zwave/HalZWaveController.java | 4 +- 12 files changed, 123 insertions(+), 62 deletions(-) diff --git a/hal-core/src/se/hal/HalContext.java b/hal-core/src/se/hal/HalContext.java index 3c77bb0e..c114598b 100644 --- a/hal-core/src/se/hal/HalContext.java +++ b/hal-core/src/se/hal/HalContext.java @@ -1,10 +1,7 @@ package se.hal; -import se.hal.struct.User; import zutil.ObjectUtil; import zutil.db.DBConnection; -import zutil.db.DBUpgradeHandler; -import zutil.db.SQLResultHandler; import zutil.db.handler.PropertiesSQLResult; import zutil.io.file.FileUtil; import zutil.log.LogUtil; @@ -12,12 +9,11 @@ import zutil.log.LogUtil; import java.io.File; import java.io.FileReader; import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.util.HashMap; import java.util.Map; import java.util.Properties; +import java.util.logging.Level; import java.util.logging.Logger; @@ -25,8 +21,8 @@ public class HalContext { private static final Logger logger = LogUtil.getLogger(); // Constants - public static final String PROPERTY_HTTP_PORT = "hal.http_port"; - public static final String PROPERTY_MAP_BACKGROUND_IMAGE = "hal.map_bgimage"; + public static final String CONFIG_HTTP_PORT = "hal_core.http_port"; + public static final String CONFIG_MAP_BACKGROUND_IMAGE = "hal_core.map_bgimage"; public static final String RESOURCE_ROOT; static { @@ -53,7 +49,7 @@ public class HalContext { static { // Set default values to get Hal up and running - fileConf.setProperty(PROPERTY_HTTP_PORT, "" + 8080); + fileConf.setProperty(CONFIG_HTTP_PORT, "" + 8080); } @@ -141,12 +137,16 @@ public class HalContext { return getBooleanProperty(key); } - public static void setProperty(String key, String value) throws SQLException { - PreparedStatement stmt = db.getPreparedStatement("REPLACE INTO conf (key, value) VALUES (?, ?)"); - stmt.setObject(1, key); - stmt.setObject(2, value); - DBConnection.exec(stmt); - dbConf.setProperty(key, value); + public static void setProperty(String key, String value) { + try { + PreparedStatement stmt = db.getPreparedStatement("REPLACE INTO conf (key, value) VALUES (?, ?)"); + stmt.setObject(1, key); + stmt.setObject(2, value); + DBConnection.exec(stmt); + dbConf.setProperty(key, value); + } catch (SQLException e) { + logger.log(Level.SEVERE, "Was unable to save property: " + key + " = " + value, e); + } } diff --git a/hal-core/src/se/hal/HalServer.java b/hal-core/src/se/hal/HalServer.java index 6ad82c76..2bb7a771 100644 --- a/hal-core/src/se/hal/HalServer.java +++ b/hal-core/src/se/hal/HalServer.java @@ -52,7 +52,7 @@ public class HalServer { // init variables pluginManager = new PluginManager(); daemonExecutor = Executors.newScheduledThreadPool(1); // We set only one thread for easier troubleshooting - http = new HttpServer(HalContext.getIntegerProperty(HalContext.PROPERTY_HTTP_PORT)); + http = new HttpServer(HalContext.getIntegerProperty(HalContext.CONFIG_HTTP_PORT)); // Upgrade database HalDatabaseUpgradeManager.initialize(pluginManager); diff --git a/hal-core/src/se/hal/page/MapWebPage.java b/hal-core/src/se/hal/page/MapWebPage.java index 8d74f71d..1bf801a3 100644 --- a/hal-core/src/se/hal/page/MapWebPage.java +++ b/hal-core/src/se/hal/page/MapWebPage.java @@ -77,7 +77,7 @@ public class MapWebPage extends HalWebPage { private void loadBgImage() { - String property = HalContext.getStringProperty(HalContext.PROPERTY_MAP_BACKGROUND_IMAGE); + String property = HalContext.getStringProperty(HalContext.CONFIG_MAP_BACKGROUND_IMAGE); if (property != null) { String[] split = property.split(",", 2); bgType = split[0]; @@ -86,6 +86,6 @@ public class MapWebPage extends HalWebPage { } private void saveBgImage(String type, byte[] data) throws SQLException { - HalContext.setProperty(HalContext.PROPERTY_MAP_BACKGROUND_IMAGE, type + "," + Base64Encoder.encode(data)); + HalContext.setProperty(HalContext.CONFIG_MAP_BACKGROUND_IMAGE, type + "," + Base64Encoder.encode(data)); } } diff --git a/hal.conf.example b/hal.conf.example index c7f1e2f4..57b84471 100644 --- a/hal.conf.example +++ b/hal.conf.example @@ -2,7 +2,7 @@ # Core settings # ------------------------------------ -hal.http_port=8080 +hal_core.http_port=8080 # ------------------------------------------------------------------------ # Plugin configurations @@ -13,31 +13,31 @@ hal.http_port=8080 # ------------------------------------ ## Tellstick plugin -#tellstick.com_port=COM5 -#tellstick.com_port=/dev/serial/by-id/usb-Telldus_TellStick_Duo_A6XNNE6Z-if00-port0 +#hal_tellstick.com_port=COM5 +#hal_tellstick.com_port=/dev/serial/by-id/usb-Telldus_TellStick_Duo_A6XNNE6Z-if00-port0 ## NetUPS plugin -#nutups.host= -#nutups.port= +#hal_nutups.host= +#hal_nutups.port= ## NetScan plugin # Network scanning should probably be disabled in some networks (default on) -#netscan.ipscan=false +#hal_netscan.ipscan=false ## Zigbee plugin -#zigbee.com_port=COM4 -#zigbee.dongle=CC2531|CONBEE|XBEE +#hal_zigbee.com_port=COM4 +#hal_zigbee.dongle=CC2531|CONBEE|XBEE ## ZWave plugin -#zwave.com_port=COM4 +#hal_zwave.com_port=COM4 ## Power Challenge Plugin -powerchallenge.sync_port=6666 +hal_powerchallenge.sync_port=6666 # ------------------------------------ # Assistants # ------------------------------------ ## Google Assistant -assistant.google.port=8081 -assistant.google.client_id=https://oauth-redirect.googleusercontent.com/r/optimal-comfort-XXXXX +hal_assistant.google.port=8081 +hal_assistant.google.client_id=https://oauth-redirect.googleusercontent.com/r/optimal-comfort-XXXXX diff --git a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeDaemon.java b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeDaemon.java index 8e9dcfdc..30ef44bc 100644 --- a/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeDaemon.java +++ b/plugins/hal-assistant-google/src/se/hal/plugin/assistant/google/SmartHomeDaemon.java @@ -43,8 +43,8 @@ public class SmartHomeDaemon implements HalDaemon { public static final String ENDPOINT_TOKEN = "api/assistant/google/auth/token"; public static final String ENDPOINT_SMARTHOME = "api/assistant/google/smarthome"; - private static final String PARAM_PORT = "assistant.google.port"; - private static final String PARAM_CLIENT_ID = "assistant.google.client_id"; + private static final String CONFIG_PORT = "hal_assistant.google.port"; + private static final String CONFIG_CLIENT_ID = "hal_assistant.google.client_id"; private SmartHomeImpl smartHome; private OAuth2Registry oAuth2Registry; @@ -53,8 +53,8 @@ public class SmartHomeDaemon implements HalDaemon { @Override public void initiate(ScheduledExecutorService executor) { if (smartHome == null) { - if (!HalContext.containsProperty(PARAM_PORT) || - !HalContext.containsProperty(PARAM_CLIENT_ID)) { + if (!HalContext.containsProperty(CONFIG_PORT) || + !HalContext.containsProperty(CONFIG_CLIENT_ID)) { logger.severe("Missing configuration, abort initializations."); return; } @@ -62,10 +62,10 @@ public class SmartHomeDaemon implements HalDaemon { smartHome = new SmartHomeImpl(); oAuth2Registry = new OAuth2Registry(); - oAuth2Registry.addWhitelist(HalContext.getStringProperty(PARAM_CLIENT_ID)); + oAuth2Registry.addWhitelist(HalContext.getStringProperty(CONFIG_CLIENT_ID)); oAuth2Registry.setTokenListener(smartHome); - httpServer = new HttpServer(HalContext.getIntegerProperty(PARAM_PORT)); + httpServer = new HttpServer(HalContext.getIntegerProperty(CONFIG_PORT)); httpServer.setPage(ENDPOINT_AUTH, new OAuth2AuthorizationPage(oAuth2Registry)); httpServer.setPage(ENDPOINT_TOKEN, new OAuth2TokenPage(oAuth2Registry)); httpServer.setPage(ENDPOINT_SMARTHOME, new SmartHomePage(smartHome)); diff --git a/plugins/hal-nutups/src/se/hal/plugin/nutups/NutUpsController.java b/plugins/hal-nutups/src/se/hal/plugin/nutups/NutUpsController.java index efbaebb3..52bf723a 100644 --- a/plugins/hal-nutups/src/se/hal/plugin/nutups/NutUpsController.java +++ b/plugins/hal-nutups/src/se/hal/plugin/nutups/NutUpsController.java @@ -70,8 +70,8 @@ public class NutUpsController implements HalSensorController, HalAutostartContro public static Logger logger = LogUtil.getLogger(); private static final int SYNC_INTERVAL = 60 * 1000; - public static final String PROPERTY_HOST = "nutups.host"; - public static final String PROPERTY_PORT = "nutups.port"; + public static final String CONFIG_HOST = "hal_nutups.host"; + public static final String CONFIG_PORT = "hal_nutups.port"; private HashMap registeredDevices = new HashMap<>(); private NutUPSClient client; @@ -82,16 +82,16 @@ public class NutUpsController implements HalSensorController, HalAutostartContro @Override public boolean isAvailable() { - return HalContext.containsProperty(PROPERTY_HOST); + return HalContext.containsProperty(CONFIG_HOST); } @Override public void initialize() throws Exception { if (client == null) { int port = NutUPSClient.DEFAULT_PORT; - if (HalContext.containsProperty(PROPERTY_PORT)) - port = HalContext.getIntegerProperty(PROPERTY_PORT); + if (HalContext.containsProperty(CONFIG_PORT)) + port = HalContext.getIntegerProperty(CONFIG_PORT); - client = new NutUPSClient(HalContext.getStringProperty(PROPERTY_HOST), port); + client = new NutUPSClient(HalContext.getStringProperty(CONFIG_HOST), port); executor = Executors.newScheduledThreadPool(1); executor.scheduleAtFixedRate(this, 5000, SYNC_INTERVAL, TimeUnit.MILLISECONDS); diff --git a/plugins/hal-powerchallenge/src/se/hal/plugin/powerchallenge/daemon/PCDataSynchronizationDaemon.java b/plugins/hal-powerchallenge/src/se/hal/plugin/powerchallenge/daemon/PCDataSynchronizationDaemon.java index a40ef489..4a69d942 100644 --- a/plugins/hal-powerchallenge/src/se/hal/plugin/powerchallenge/daemon/PCDataSynchronizationDaemon.java +++ b/plugins/hal-powerchallenge/src/se/hal/plugin/powerchallenge/daemon/PCDataSynchronizationDaemon.java @@ -54,7 +54,7 @@ import java.util.logging.Logger; public class PCDataSynchronizationDaemon extends ThreadedTCPNetworkServer implements HalDaemon { private static final Logger logger = LogUtil.getLogger(); - public static final String PROPERTY_SYNC_PORT = "powerchallenge.sync_port"; + public static final String PROPERTY_SYNC_PORT = "hal_powerchallenge.sync_port"; public static final int PROTOCOL_VERSION = 5; // Increment for protocol changes diff --git a/plugins/hal-tellstick/src/se/hal/plugin/tellstick/TellstickSerialComm.java b/plugins/hal-tellstick/src/se/hal/plugin/tellstick/TellstickSerialComm.java index 028eb072..62223157 100644 --- a/plugins/hal-tellstick/src/se/hal/plugin/tellstick/TellstickSerialComm.java +++ b/plugins/hal-tellstick/src/se/hal/plugin/tellstick/TellstickSerialComm.java @@ -50,7 +50,7 @@ public class TellstickSerialComm implements Runnable, HalSensorController, HalEventController, HalAutostartController { private static final Logger logger = LogUtil.getLogger(); - private static String CONFIG_TELLSTICK_COM_PORT = "tellstick.com_port"; + private static String CONFIG_TELLSTICK_COM_PORT = "hal_tellstick.com_port"; private static final long TRANSMISSION_UNIQUENESS_TTL = 1000; // milliseconds private static TellstickSerialComm instance; // Todo: Don't like this but it is the best I could come up with diff --git a/plugins/hal-zigbee/resource/resource/web/zigbee_node_overview.tmpl b/plugins/hal-zigbee/resource/resource/web/zigbee_node_overview.tmpl index 275e76e7..cc200e91 100644 --- a/plugins/hal-zigbee/resource/resource/web/zigbee_node_overview.tmpl +++ b/plugins/hal-zigbee/resource/resource/web/zigbee_node_overview.tmpl @@ -1,5 +1,35 @@

Zigbee Node Overview

+

Network info

+ +
+
+
+
Node Description
+
+ + + + + + + + + + + + + + + +
PAN ID:{{controller.getZigbeePanId()}}
Extended PAN ID:{{controller.getZigbeeExtendedPanId()}}
Channel:{{controller.getZigbeeChannel()}}
+
+
+
+
+ +

Connected Nodes

+ {{#nodes}}
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 13d6cc71..4e1093bb 100644 --- a/plugins/hal-zigbee/src/se/hal/plugin/zigbee/ZigbeeController.java +++ b/plugins/hal-zigbee/src/se/hal/plugin/zigbee/ZigbeeController.java @@ -51,8 +51,13 @@ public class ZigbeeController implements HalSensorController, public static final String ZIGBEE_DONGLE_CONBEE = "CONBEE"; public static final String ZIGBEE_DONGLE_XBEE = "XBEE"; - private static final String CONFIG_ZIGBEE_PORT = "zigbee.com_port"; - private static final String CONFIG_ZIGBEE_DONGLE = "zigbee.dongle"; + private static final String CONFIG_ZIGBEE_DONGLE = "hal_zigbee.dongle"; + private static final String CONFIG_ZIGBEE_PORT = "hal_zigbee.com_port"; + private static final String CONFIG_ZIGBEE_NETWORK_CHANNEL = "hal_zigbee.network.channel"; + private static final String CONFIG_ZIGBEE_NETWORK_PANID = "hal_zigbee.network.panid"; + private static final String CONFIG_ZIGBEE_NETWORK_EPANID = "hal_zigbee.network.epanid"; + private static final String CONFIG_ZIGBEE_NETWORK_NWKKEY = "hal_zigbee.network.nwkey"; + private static final String CONFIG_ZIGBEE_NETWORK_LINKKEY = "hal_zigbee.network.linkkey"; private ZigBeePort serialPort; protected ZigBeeNetworkManager networkManager; @@ -140,16 +145,28 @@ public class ZigbeeController implements HalSensorController, networkManager.addSupportedServerCluster(ZclWindowCoveringCluster.CLUSTER_ID); networkManager.addSupportedServerCluster(ZclBinaryInputBasicCluster.CLUSTER_ID); + // Prepare defaults + + if (!HalContext.containsProperty(CONFIG_ZIGBEE_NETWORK_CHANNEL)) + HalContext.setProperty(CONFIG_ZIGBEE_NETWORK_CHANNEL, "11"); + if (!HalContext.containsProperty(CONFIG_ZIGBEE_NETWORK_PANID)) + HalContext.setProperty(CONFIG_ZIGBEE_NETWORK_PANID, "" + (int) Math.floor((Math.random() * 65534))); + if (!HalContext.containsProperty(CONFIG_ZIGBEE_NETWORK_EPANID)) + HalContext.setProperty(CONFIG_ZIGBEE_NETWORK_EPANID, "" + ExtendedPanId.createRandom()); + if (!HalContext.containsProperty(CONFIG_ZIGBEE_NETWORK_NWKKEY)) + HalContext.setProperty(CONFIG_ZIGBEE_NETWORK_NWKKEY, "" + ZigBeeKey.createRandom()); + if (!HalContext.containsProperty(CONFIG_ZIGBEE_NETWORK_LINKKEY)) + HalContext.setProperty(CONFIG_ZIGBEE_NETWORK_LINKKEY, "" + new ZigBeeKey(new int[] { + 0x5A, 0x69, 0x67, 0x42, 0x65, 0x65, 0x41, 0x6C, 0x6C, 0x69, 0x61, 0x6E, 0x63, 0x65, 0x30, 0x39 })); // Add the default ZigBeeAlliance09 HA link key + // Configure network + networkManager.setZigBeeChannel(ZigBeeChannel.create(HalContext.getIntegerProperty(CONFIG_ZIGBEE_NETWORK_CHANNEL, 11))); + networkManager.setZigBeePanId(HalContext.getIntegerProperty(CONFIG_ZIGBEE_NETWORK_PANID)); + networkManager.setZigBeeExtendedPanId(new ExtendedPanId(HalContext.getStringProperty(CONFIG_ZIGBEE_NETWORK_EPANID))); + networkManager.setZigBeeNetworkKey(new ZigBeeKey(HalContext.getStringProperty(CONFIG_ZIGBEE_NETWORK_NWKKEY))); + networkManager.setZigBeeLinkKey(new ZigBeeKey(HalContext.getStringProperty(CONFIG_ZIGBEE_NETWORK_LINKKEY))); networkManager.setDefaultProfileId(ZigBeeProfileType.ZIGBEE_HOME_AUTOMATION.getKey()); - networkManager.setZigBeeNetworkKey(ZigBeeKey.createRandom());//new ZigBeeKey("552FAAF9B5F49E75F1ADDA12215C2CA1")); // ZigBeeKey.createRandom(); - networkManager.setZigBeeLinkKey(new ZigBeeKey(new int[] { // Add the default ZigBeeAlliance09 HA link key - 0x5A, 0x69, 0x67, 0x42, 0x65, 0x65, 0x41, 0x6C, 0x6C, 0x69, 0x61, 0x6E, 0x63, 0x65, 0x30, 0x39 })); - networkManager.setZigBeeChannel(ZigBeeChannel.create(11)); - networkManager.setZigBeePanId(65534); // (int) Math.floor((Math.random() * 65534)); - networkManager.setZigBeeExtendedPanId(new ExtendedPanId("00124B001CCE1B5F")); // ExtendedPanId.createRandom(); - //transportOptions.addOption(TransportConfigOption.TRUST_CENTRE_JOIN_MODE, TrustCentreJoinMode.TC_JOIN_INSECURE); // TC_JOIN_SECURE dongle.updateTransportConfig(transportOptions); @@ -186,6 +203,24 @@ public class ZigbeeController implements HalSensorController, serialPort.close(); } + // ------------------------------------------ + // Getters + // ------------------------------------------ + + public ZigBeeChannel getZigbeeChannel() { + return networkManager.getZigBeeChannel(); + } + public int getZigbeePanId() { + return networkManager.getZigBeePanId(); + } + public ExtendedPanId getZigbeeExtendedPanId() { + return networkManager.getZigBeeExtendedPanId(); + } + + public Set getNodes() { + return networkManager.getNodes(); + } + // ------------------------------------------ // Zigbee Node Methods // ------------------------------------------ @@ -237,10 +272,6 @@ public class ZigbeeController implements HalSensorController, logger.fine("[Node: " + node.getIeeeAddress() + "]: Node registration has been removed."); } - public Set getNodes() { - return networkManager.getNodes(); - } - // ------------------------------------------ // Zigbee Endpoint 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 1ed47db7..9fe086d8 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 @@ -42,15 +42,15 @@ public abstract class ZigbeeHalEventDeviceConfig extends ZigbeeHalDeviceConfig { (CommandResult) ZclCluster.class.getMethod("sendCommand", ZclCommand.class).invoke(command); if (result.isError() || result.isTimeout()) { - logger.warning("[Endpoint: " + cluster.getZigBeeAddress() + "] Command failed with error: " + result.isError() + " (timeout=" + result.isTimeout() + ")"); + logger.warning("[Node: " + getZigbeeNodeAddress() + ", Endpoint: " + cluster.getZigBeeAddress() + "] Command failed with error: " + result.isError() + " (timeout=" + result.isTimeout() + ")"); } else { - logger.info("[Endpoint: " + cluster.getZigBeeAddress() + "] Command has been successfully sent"); + logger.info("[Node: " + getZigbeeNodeAddress() + ", Endpoint: " + cluster.getZigBeeAddress() + "] Command has been successfully sent."); } } catch (Exception e) { - logger.warning("[Endpoint: " + cluster.getZigBeeAddress() + "] Failed to send command [" + e.getMessage() + "]"); + logger.warning("[Node: " + getZigbeeNodeAddress() + ", Endpoint: " + cluster.getZigBeeAddress() + "] Failed to send command: " + e.getMessage()); } } else { - logger.warning("[Node: " + getZigbeeNodeAddress() + "] Unable to find cluster."); + logger.warning("[Node: " + getZigbeeNodeAddress() + "] Unable to find cluster: " + getZigbeeClusterId()); } } diff --git a/plugins/hal-zwave/src/se/hal/plugin/zwave/HalZWaveController.java b/plugins/hal-zwave/src/se/hal/plugin/zwave/HalZWaveController.java index ca3319df..20629919 100644 --- a/plugins/hal-zwave/src/se/hal/plugin/zwave/HalZWaveController.java +++ b/plugins/hal-zwave/src/se/hal/plugin/zwave/HalZWaveController.java @@ -19,8 +19,8 @@ import java.util.logging.Logger; public class HalZWaveController implements HalSensorController, HalEventController, HalAutostartController, NotificationWatcher { private static final Logger logger = LogUtil.getLogger(); - public static final String CONFIG_ZWAVE_PORT = "zwave.com_port"; - public static final String CONFIG_ZWAVE_CFG_PATH = "zwave.cfg_path"; + public static final String CONFIG_ZWAVE_PORT = "hal_zwave.com_port"; + public static final String CONFIG_ZWAVE_CFG_PATH = "hal_zwave.cfg_path"; private String serialPort; private long homeId;