]> git.basschouten.com Git - openhab-addons.git/blob
c3b12b2c45c95732bba7706f411b81a273e28321
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.digitalstrom.internal.lib.sensorjobexecutor.sensorjob.impl;
14
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;
22
23 /**
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}.
26  *
27  * @author Michael Ochel - Initial contribution
28  * @author Matthias Siegele - Initial contribution
29  */
30 public class DeviceConsumptionSensorJob implements SensorJob {
31
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;
38
39     /**
40      * Creates a new {@link DeviceConsumptionSensorJob}. Through updateDevice you can set, if the {@link Device} will be
41      * updates automatically.
42      *
43      * @param device to update
44      * @param type to update
45      * @param updateDevice (true = automatically device, otherwise false)
46      * @see #DeviceConsumptionSensorJob(Device, SensorEnum)
47      */
48     public DeviceConsumptionSensorJob(Device device, SensorEnum type, boolean updateDevice) {
49         this.device = device;
50         this.sensorType = type;
51         this.meterDSID = device.getMeterDSID();
52         this.initalisationTime = System.currentTimeMillis();
53         this.updateDevice = updateDevice;
54     }
55
56     /**
57      * Creates a new {@link DeviceConsumptionSensorJob} with the given {@link SensorEnum} for the given {@link Device}
58      * and automatically {@link Device} update.
59      *
60      * @param device to update
61      * @param type to update
62      */
63     public DeviceConsumptionSensorJob(Device device, SensorEnum type) {
64         this.device = device;
65         this.sensorType = type;
66         this.meterDSID = device.getMeterDSID();
67         this.initalisationTime = System.currentTimeMillis();
68     }
69
70     @Override
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);
75         if (updateDevice) {
76             device.setDeviceSensorDsValueBySensorJob(sensorType, consumption);
77         }
78     }
79
80     @Override
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());
86         }
87         return false;
88     }
89
90     @Override
91     public int hashCode() {
92         return new String(this.device.getDSID().getValue() + this.sensorType.getSensorType()).hashCode();
93     }
94
95     @Override
96     public DSID getDSID() {
97         return device.getDSID();
98     }
99
100     @Override
101     public DSID getMeterDSID() {
102         return this.meterDSID;
103     }
104
105     @Override
106     public long getInitalisationTime() {
107         return this.initalisationTime;
108     }
109
110     @Override
111     public void setInitalisationTime(long time) {
112         this.initalisationTime = time;
113     }
114
115     @Override
116     public String toString() {
117         return "DeviceConsumptionSensorJob [sensorType=" + sensorType + ", sensorIndex="
118                 + device.getSensorIndex(sensorType) + ", deviceDSID : " + device.getDSID().getValue() + ", meterDSID="
119                 + meterDSID + ", initalisationTime=" + initalisationTime + "]";
120     }
121
122     @Override
123     public String getID() {
124         return getID(device, sensorType);
125     }
126
127     /**
128      * Returns the id for a {@link DeviceConsumptionSensorJob} with the given {@link Device} and {@link SensorEnum}.
129      *
130      * @param device to update
131      * @param sensorType to update
132      * @return id
133      */
134     public static String getID(Device device, SensorEnum sensorType) {
135         return DeviceConsumptionSensorJob.class.getSimpleName() + "-" + device.getDSID().getValue() + "-"
136                 + sensorType.toString();
137     }
138 }