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) {
83 DeviceConsumptionSensorJob other = (DeviceConsumptionSensorJob) obj;
84 String device = this.device.getDSID().getValue() + this.sensorType.getSensorType();
85 return device.equals(other.device.getDSID().getValue() + other.sensorType.getSensorType());
91 public int hashCode() {
92 return new String(this.device.getDSID().getValue() + this.sensorType.getSensorType()).hashCode();
96 public DSID getDSID() {
97 return device.getDSID();
101 public DSID getMeterDSID() {
102 return this.meterDSID;
106 public long getInitalisationTime() {
107 return this.initalisationTime;
111 public void setInitalisationTime(long time) {
112 this.initalisationTime = time;
116 public String toString() {
117 return "DeviceConsumptionSensorJob [sensorType=" + sensorType + ", sensorIndex="
118 + device.getSensorIndex(sensorType) + ", deviceDSID : " + device.getDSID().getValue() + ", meterDSID="
119 + meterDSID + ", initalisationTime=" + initalisationTime + "]";
123 public String getID() {
124 return getID(device, sensorType);
128 * Returns the id for a {@link DeviceConsumptionSensorJob} with the given {@link Device} and {@link SensorEnum}.
130 * @param device to update
131 * @param sensorType to update
134 public static String getID(Device device, SensorEnum sensorType) {
135 return DeviceConsumptionSensorJob.class.getSimpleName() + "-" + device.getDSID().getValue() + "-"
136 + sensorType.toString();