From 96635381f48002a207fd91f854e882e10d576d46 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sun, 29 Dec 2024 00:38:07 +0100 Subject: [PATCH] Finished tibber controller logic --- .../se/hal/intf/HalDeviceReportListener.java | 25 +++++++- hal-core/src/se/hal/util/ListenerUtil.java | 60 +++++++++++++++++++ .../se/hal/plugin/dummy/DummyController.java | 29 ++++++++- .../hal/plugin/tibber/TibberController.java | 23 ++++++- ....java => TibberElectricityCostSensor.java} | 2 +- ...java => TibberElectricityPriceSensor.java} | 2 +- ...java => TibberPowerConsumptionSensor.java} | 2 +- 7 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 hal-core/src/se/hal/util/ListenerUtil.java rename plugins/hal-tibber/src/se/hal/plugin/tibber/device/{ElectricityCostSensor.java => TibberElectricityCostSensor.java} (96%) rename plugins/hal-tibber/src/se/hal/plugin/tibber/device/{ElectricityPriceSensor.java => TibberElectricityPriceSensor.java} (96%) rename plugins/hal-tibber/src/se/hal/plugin/tibber/device/{PowerConsumptionSensor.java => TibberPowerConsumptionSensor.java} (96%) diff --git a/hal-core/src/se/hal/intf/HalDeviceReportListener.java b/hal-core/src/se/hal/intf/HalDeviceReportListener.java index 78f81ed0..ce7e91e2 100644 --- a/hal-core/src/se/hal/intf/HalDeviceReportListener.java +++ b/hal-core/src/se/hal/intf/HalDeviceReportListener.java @@ -1,5 +1,28 @@ -package se.hal.intf; +/* + * The MIT License (MIT) + * + * Copyright (c) 2024 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.intf; /** * A listener interface that will be called when the diff --git a/hal-core/src/se/hal/util/ListenerUtil.java b/hal-core/src/se/hal/util/ListenerUtil.java new file mode 100644 index 00000000..5c5f61bf --- /dev/null +++ b/hal-core/src/se/hal/util/ListenerUtil.java @@ -0,0 +1,60 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2024 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.util; + +import se.hal.intf.HalDeviceConfig; +import se.hal.intf.HalDeviceData; +import se.hal.intf.HalDeviceReportListener; +import zutil.log.LogUtil; + +import java.util.List; +import java.util.logging.Logger; +import java.util.logging.Level; + +/** + * Utility class that contains general code for interacting with listeners. + */ +public class ListenerUtil { + private static final Logger logger = LogUtil.getLogger(); + + /** + * Helper function to call {@link HalDeviceReportListener#reportReceived(HalDeviceConfig, HalDeviceData)} on all provided + * listeners with the same input parameters. Exceptions will be handled individually per listener and will not prevent + * other listeners to be called. + * + * @param deviceListeners a list of listeners to be called. + * @param deviceConfig the device config that will be provided on each call. + * @param deviceData the data config that will be provided on each call. + */ + public static void callReportReceived(List deviceListeners, HalDeviceConfig deviceConfig, HalDeviceData deviceData) { + for (HalDeviceReportListener deviceListener : deviceListeners) { + try { + deviceListener.reportReceived(deviceConfig, deviceData); + } catch (Exception e) { + logger.log(Level.WARNING, "Device listener threw exception during new device report, exception is ignored.", e); + } + } + } +} diff --git a/plugins/hal-dummy/src/se/hal/plugin/dummy/DummyController.java b/plugins/hal-dummy/src/se/hal/plugin/dummy/DummyController.java index 5b2eee90..9d37cbf8 100644 --- a/plugins/hal-dummy/src/se/hal/plugin/dummy/DummyController.java +++ b/plugins/hal-dummy/src/se/hal/plugin/dummy/DummyController.java @@ -1,7 +1,32 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2024 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.dummy; import se.hal.HalServer; import se.hal.intf.*; +import se.hal.util.ListenerUtil; import zutil.log.LogUtil; import java.util.ArrayList; @@ -39,9 +64,7 @@ public class DummyController implements HalSensorController, HalEventController, for (DummyDevice device : registeredDevices) { HalDeviceData data = device.generateData(); - for (HalDeviceReportListener deviceListener : deviceListeners) { - deviceListener.reportReceived(device, data); - } + ListenerUtil.callReportReceived(deviceListeners, device, data); } } catch (Exception e) { logger.log(Level.SEVERE, "Dummy device data generation has crashed.", e); diff --git a/plugins/hal-tibber/src/se/hal/plugin/tibber/TibberController.java b/plugins/hal-tibber/src/se/hal/plugin/tibber/TibberController.java index 704e8dc2..8366d82a 100644 --- a/plugins/hal-tibber/src/se/hal/plugin/tibber/TibberController.java +++ b/plugins/hal-tibber/src/se/hal/plugin/tibber/TibberController.java @@ -27,6 +27,11 @@ package se.hal.plugin.tibber; import se.hal.HalContext; import se.hal.HalServer; import se.hal.intf.*; +import se.hal.plugin.tibber.device.TibberElectricityCostSensor; +import se.hal.plugin.tibber.device.TibberElectricityPriceSensor; +import se.hal.plugin.tibber.device.TibberPowerConsumptionSensor; +import se.hal.struct.devicedata.PriceSensorData; +import se.hal.util.ListenerUtil; import zutil.ObjectUtil; import zutil.log.LogUtil; import zutil.net.http.HttpClient; @@ -35,6 +40,7 @@ import zutil.parser.DataNode; import zutil.parser.json.JSONParser; import zutil.ui.UserMessageManager; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -51,6 +57,7 @@ public class TibberController implements HalSensorController, Runnable, HalDaemo private static final String TIBBER_API_ENDPOINT = "https://api.tibber.com/v1-beta/gql"; private static final int POLL_TIME = 60*60; // poll every 1h defined in sec + private static SimpleDateFormat DATE_PARSER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); // 2024-12-28T23:00:00.000+01:00 private List registeredDevices = new ArrayList<>(); private List deviceListeners = new CopyOnWriteArrayList<>(); @@ -124,7 +131,7 @@ public class TibberController implements HalSensorController, Runnable, HalDaemo HttpHeader header = client.send(); JSONParser parser = new JSONParser(header.getInputStream()); DataNode json = parser.read(); - logger.finest("Response: " + json); + logger.finest("Received data from Tibber API: " + json); if (header.getResponseStatusCode() != 200) { logger.severe("There was a error fetching data from tibber: StatusCode=" + header.getResponseStatusCode() + ", Response=" + header.getResponseStatusString()); @@ -147,6 +154,20 @@ public class TibberController implements HalSensorController, Runnable, HalDaemo } DataNode data = json.get("data").get("viewer").get("homes").get(0).get("consumption").get("nodes").get(0); + long timestamp = DATE_PARSER.parse(data.getString("from")).getTime(); + + // Data example: + // unitPrice=0.1507875, cost=0.3131856375, from=2024-12-28T21:00:00.000+01:00, consumption=2.077, to=2024-12-28T22:00:00.000+01:00, unitPriceVAT=0.0301575, consumptionUnit=kWh + + if (data.get("cost") != null) { + ListenerUtil.callReportReceived(deviceListeners, new TibberElectricityCostSensor(), new PriceSensorData(data.getDouble("cost"), timestamp)); + } + if (data.get("unitPriceVAT") != null) { + ListenerUtil.callReportReceived(deviceListeners, new TibberElectricityPriceSensor(), new PriceSensorData(data.getDouble("unitPriceVAT"), timestamp)); + } + if (data.get("consumption") != null) { + ListenerUtil.callReportReceived(deviceListeners, new TibberPowerConsumptionSensor(), new PriceSensorData(data.getDouble("consumption"), timestamp)); + } } catch (Exception e) { logger.log(Level.SEVERE, "Tibber API querying failed.", e); diff --git a/plugins/hal-tibber/src/se/hal/plugin/tibber/device/ElectricityCostSensor.java b/plugins/hal-tibber/src/se/hal/plugin/tibber/device/TibberElectricityCostSensor.java similarity index 96% rename from plugins/hal-tibber/src/se/hal/plugin/tibber/device/ElectricityCostSensor.java rename to plugins/hal-tibber/src/se/hal/plugin/tibber/device/TibberElectricityCostSensor.java index a1013fbc..ee003516 100644 --- a/plugins/hal-tibber/src/se/hal/plugin/tibber/device/ElectricityCostSensor.java +++ b/plugins/hal-tibber/src/se/hal/plugin/tibber/device/TibberElectricityCostSensor.java @@ -33,7 +33,7 @@ import se.hal.struct.devicedata.CostSensorData; /** * A sensor that calculate current electricity bil */ -public class ElectricityCostSensor implements HalSensorConfig { +public class TibberElectricityCostSensor implements HalSensorConfig { diff --git a/plugins/hal-tibber/src/se/hal/plugin/tibber/device/ElectricityPriceSensor.java b/plugins/hal-tibber/src/se/hal/plugin/tibber/device/TibberElectricityPriceSensor.java similarity index 96% rename from plugins/hal-tibber/src/se/hal/plugin/tibber/device/ElectricityPriceSensor.java rename to plugins/hal-tibber/src/se/hal/plugin/tibber/device/TibberElectricityPriceSensor.java index 97b47465..ad251936 100644 --- a/plugins/hal-tibber/src/se/hal/plugin/tibber/device/ElectricityPriceSensor.java +++ b/plugins/hal-tibber/src/se/hal/plugin/tibber/device/TibberElectricityPriceSensor.java @@ -33,7 +33,7 @@ import se.hal.struct.devicedata.PriceSensorData; /** * A sensor that shows the price of electricity at a specific time */ -public class ElectricityPriceSensor implements HalSensorConfig { +public class TibberElectricityPriceSensor implements HalSensorConfig { diff --git a/plugins/hal-tibber/src/se/hal/plugin/tibber/device/PowerConsumptionSensor.java b/plugins/hal-tibber/src/se/hal/plugin/tibber/device/TibberPowerConsumptionSensor.java similarity index 96% rename from plugins/hal-tibber/src/se/hal/plugin/tibber/device/PowerConsumptionSensor.java rename to plugins/hal-tibber/src/se/hal/plugin/tibber/device/TibberPowerConsumptionSensor.java index 4786e357..de912d8e 100644 --- a/plugins/hal-tibber/src/se/hal/plugin/tibber/device/PowerConsumptionSensor.java +++ b/plugins/hal-tibber/src/se/hal/plugin/tibber/device/TibberPowerConsumptionSensor.java @@ -31,7 +31,7 @@ import se.hal.struct.devicedata.PowerConsumptionSensorData; import se.hal.plugin.tibber.TibberController; -public class PowerConsumptionSensor implements HalSensorConfig { +public class TibberPowerConsumptionSensor implements HalSensorConfig { @Override