Finished tibber controller logic
This commit is contained in:
parent
12d3c80ba8
commit
96635381f4
7 changed files with 135 additions and 8 deletions
|
|
@ -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
|
||||
|
|
|
|||
60
hal-core/src/se/hal/util/ListenerUtil.java
Normal file
60
hal-core/src/se/hal/util/ListenerUtil.java
Normal file
|
|
@ -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<HalDeviceReportListener> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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<TibberDevice> registeredDevices = new ArrayList<>();
|
||||
private List<HalDeviceReportListener> 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);
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
||||
|
||||
|
||||
|
|
@ -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 {
|
||||
|
||||
|
||||
|
||||
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue