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