2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.digitalstrom.internal.lib.sensorjobexecutor.sensorjob.impl;
15 import org.openhab.binding.digitalstrom.internal.lib.sensorjobexecutor.sensorjob.SensorJob;
16 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.DsAPI;
17 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.Device;
18 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum;
19 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.impl.DSID;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * The {@link DeviceConsumptionSensorJob} is the implementation of a {@link SensorJob}
25 * for reading out the current value of the of a digitalSTROM-Device sensor and updates the {@link Device}.
27 * @author Michael Ochel - Initial contribution
28 * @author Matthias Siegele - Initial contribution
30 public class DeviceConsumptionSensorJob implements SensorJob {
32 private final Logger logger = LoggerFactory.getLogger(DeviceConsumptionSensorJob.class);
33 private final Device device;
34 private final SensorEnum sensorType;
35 private final DSID meterDSID;
36 private long initalisationTime = 0;
37 private boolean updateDevice = true;
40 * Creates a new {@link DeviceConsumptionSensorJob}. Through updateDevice you can set, if the {@link Device} will be
41 * updates automatically.
43 * @param device to update
44 * @param type to update
45 * @param updateDevice (true = automatically device, otherwise false)
46 * @see #DeviceConsumptionSensorJob(Device, SensorEnum)
48 public DeviceConsumptionSensorJob(Device device, SensorEnum type, boolean updateDevice) {
50 this.sensorType = type;
51 this.meterDSID = device.getMeterDSID();
52 this.initalisationTime = System.currentTimeMillis();
53 this.updateDevice = updateDevice;
57 * Creates a new {@link DeviceConsumptionSensorJob} with the given {@link SensorEnum} for the given {@link Device}
58 * and automatically {@link Device} update.
60 * @param device to update
61 * @param type to update
63 public DeviceConsumptionSensorJob(Device device, SensorEnum type) {
65 this.sensorType = type;
66 this.meterDSID = device.getMeterDSID();
67 this.initalisationTime = System.currentTimeMillis();
71 public void execute(DsAPI digitalSTROM, String token) {
72 int consumption = digitalSTROM.getDeviceSensorValue(token, this.device.getDSID(), null, null,
73 device.getSensorIndex(sensorType));
74 logger.debug("Executes {} new device consumption is {}", this.toString(), consumption);
76 device.setDeviceSensorDsValueBySensorJob(sensorType, consumption);
81 public boolean equals(Object obj) {
82 if (obj instanceof DeviceConsumptionSensorJob other) {
83 String device = this.device.getDSID().getValue() + this.sensorType.getSensorType();
84 return device.equals(other.device.getDSID().getValue() + other.sensorType.getSensorType());
90 public int hashCode() {
91 return new String(this.device.getDSID().getValue() + this.sensorType.getSensorType()).hashCode();
95 public DSID getDSID() {
96 return device.getDSID();
100 public DSID getMeterDSID() {
101 return this.meterDSID;
105 public long getInitalisationTime() {
106 return this.initalisationTime;
110 public void setInitalisationTime(long time) {
111 this.initalisationTime = time;
115 public String toString() {
116 return "DeviceConsumptionSensorJob [sensorType=" + sensorType + ", sensorIndex="
117 + device.getSensorIndex(sensorType) + ", deviceDSID : " + device.getDSID().getValue() + ", meterDSID="
118 + meterDSID + ", initalisationTime=" + initalisationTime + "]";
122 public String getID() {
123 return getID(device, sensorType);
127 * Returns the id for a {@link DeviceConsumptionSensorJob} with the given {@link Device} and {@link SensorEnum}.
129 * @param device to update
130 * @param sensorType to update
133 public static String getID(Device device, SensorEnum sensorType) {
134 return DeviceConsumptionSensorJob.class.getSimpleName() + "-" + device.getDSID().getValue() + "-"
135 + sensorType.toString();