]> git.basschouten.com Git - openhab-addons.git/blob
6f15b4f2634573f2393b35bc8bf643f33138a716
[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.DeviceConstants;
19 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.DeviceStateUpdate;
20 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.impl.DSID;
21 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.impl.DeviceStateUpdateImpl;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The {@link DeviceOutputValueSensorJob} is the implementation of a {@link SensorJob}
27  * for reading out the current device output value of a digitalSTROM-Device and update the {@link Device}.
28  *
29  * @author Michael Ochel - Initial contribution
30  * @author Matthias Siegele - Initial contribution
31  */
32 public class DeviceOutputValueSensorJob implements SensorJob {
33
34     private final Logger logger = LoggerFactory.getLogger(DeviceOutputValueSensorJob.class);
35     private final Device device;
36     private short index = 0;
37     private final DSID meterDSID;
38     private long initalisationTime = 0;
39
40     /**
41      * Creates a new {@link DeviceOutputValueSensorJob} for the given {@link Device}.
42      *
43      * @param device to update
44      */
45     public DeviceOutputValueSensorJob(Device device) {
46         this.device = device;
47         if (device.isShade()) {
48             this.index = DeviceConstants.DEVICE_SENSOR_SLAT_POSITION_OUTPUT;
49         } else {
50             this.index = DeviceConstants.DEVICE_SENSOR_OUTPUT;
51         }
52         this.meterDSID = device.getMeterDSID();
53         this.initalisationTime = System.currentTimeMillis();
54     }
55
56     @Override
57     public void execute(DsAPI digitalSTROM, String token) {
58         int value = digitalSTROM.getDeviceOutputValue(token, this.device.getDSID(), null, null, index);
59         logger.debug("Device output value on Demand : {}, dSID: {}", value, this.device.getDSID().getValue());
60
61         if (value != 1) {
62             switch (this.index) {
63                 case 0:
64                     this.device.updateInternalDeviceState(new DeviceStateUpdateImpl(DeviceStateUpdate.OUTPUT, value));
65                     return;
66                 case 2:
67                     this.device.updateInternalDeviceState(
68                             new DeviceStateUpdateImpl(DeviceStateUpdate.SLATPOSITION, value));
69                     if (device.isBlind()) {
70                         value = digitalSTROM.getDeviceOutputValue(token, this.device.getDSID(), null, null,
71                                 DeviceConstants.DEVICE_SENSOR_SLAT_ANGLE_OUTPUT);
72                         logger.debug("Device angle output value on Demand : {}, dSID: {}", value,
73                                 this.device.getDSID().getValue());
74                         if (value != 1) {
75                             this.device.updateInternalDeviceState(
76                                     new DeviceStateUpdateImpl(DeviceStateUpdate.SLAT_ANGLE, value));
77                         }
78                     }
79                     return;
80                 default:
81                     return;
82             }
83         }
84     }
85
86     @Override
87     public boolean equals(Object obj) {
88         if (obj instanceof DeviceOutputValueSensorJob other) {
89             String key = this.device.getDSID().getValue() + this.index;
90             return key.equals((other.device.getDSID().getValue() + other.index));
91         }
92         return false;
93     }
94
95     @Override
96     public int hashCode() {
97         return new String(this.device.getDSID().getValue() + this.index).hashCode();
98     }
99
100     @Override
101     public DSID getDSID() {
102         return device.getDSID();
103     }
104
105     @Override
106     public DSID getMeterDSID() {
107         return this.meterDSID;
108     }
109
110     @Override
111     public long getInitalisationTime() {
112         return this.initalisationTime;
113     }
114
115     @Override
116     public void setInitalisationTime(long time) {
117         this.initalisationTime = time;
118     }
119
120     @Override
121     public String toString() {
122         return "DeviceOutputValueSensorJob [deviceDSID : " + device.getDSID().getValue() + ", meterDSID=" + meterDSID
123                 + ", initalisationTime=" + initalisationTime + "]";
124     }
125
126     @Override
127     public String getID() {
128         return getID(device);
129     }
130
131     /**
132      * Returns the id for a {@link DeviceOutputValueSensorJob} with the given {@link Device}.
133      *
134      * @param device to update
135      * @return id
136      */
137     public static String getID(Device device) {
138         return DeviceOutputValueSensorJob.class.getSimpleName() + "-" + device.getDSID().getValue();
139     }
140 }