]> git.basschouten.com Git - openhab-addons.git/blob
59fb21bcf295cc9cb4744d0a68bae94950e80749
[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 other) {
83             String device = this.device.getDSID().getValue() + this.sensorType.getSensorType();
84             return device.equals(other.device.getDSID().getValue() + other.sensorType.getSensorType());
85         }
86         return false;
87     }
88
89     @Override
90     public int hashCode() {
91         return new String(this.device.getDSID().getValue() + this.sensorType.getSensorType()).hashCode();
92     }
93
94     @Override
95     public DSID getDSID() {
96         return device.getDSID();
97     }
98
99     @Override
100     public DSID getMeterDSID() {
101         return this.meterDSID;
102     }
103
104     @Override
105     public long getInitalisationTime() {
106         return this.initalisationTime;
107     }
108
109     @Override
110     public void setInitalisationTime(long time) {
111         this.initalisationTime = time;
112     }
113
114     @Override
115     public String toString() {
116         return "DeviceConsumptionSensorJob [sensorType=" + sensorType + ", sensorIndex="
117                 + device.getSensorIndex(sensorType) + ", deviceDSID : " + device.getDSID().getValue() + ", meterDSID="
118                 + meterDSID + ", initalisationTime=" + initalisationTime + "]";
119     }
120
121     @Override
122     public String getID() {
123         return getID(device, sensorType);
124     }
125
126     /**
127      * Returns the id for a {@link DeviceConsumptionSensorJob} with the given {@link Device} and {@link SensorEnum}.
128      *
129      * @param device to update
130      * @param sensorType to update
131      * @return id
132      */
133     public static String getID(Device device, SensorEnum sensorType) {
134         return DeviceConsumptionSensorJob.class.getSimpleName() + "-" + device.getDSID().getValue() + "-"
135                 + sensorType.toString();
136     }
137 }