]> git.basschouten.com Git - openhab-addons.git/blob
de430978ae258e7a9dfa3bf141ca11ce2176ca72
[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.impl.DSID;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * The {@link SceneOutputValueReadingJob} is the implementation of a {@link SensorJob}
24  * for reading out a scene configuration of a digitalSTROM-Device and store it into the {@link Device}.
25  *
26  * @author Michael Ochel - Initial contribution
27  * @author Matthias Siegele - Initial contribution
28  */
29 public class SceneOutputValueReadingJob implements SensorJob {
30
31     private final Logger logger = LoggerFactory.getLogger(SceneOutputValueReadingJob.class);
32
33     private final Device device;
34     private short sceneID = 0;
35     private final DSID meterDSID;
36     private long initalisationTime = 0;
37
38     /**
39      * Creates a new {@link SceneOutputValueReadingJob} for the given {@link Device} and the given sceneID.
40      *
41      * @param device to update
42      * @param sceneID to update
43      */
44     public SceneOutputValueReadingJob(Device device, short sceneID) {
45         this.device = device;
46         this.sceneID = sceneID;
47         this.meterDSID = device.getMeterDSID();
48         this.initalisationTime = System.currentTimeMillis();
49     }
50
51     @Override
52     public void execute(DsAPI digitalSTROM, String token) {
53         int[] sceneValue = digitalSTROM.getSceneValue(token, this.device.getDSID(), null, null, this.sceneID);
54
55         if (sceneValue[0] != -1) {
56             if (device.isBlind()) {
57                 device.setSceneOutputValue(this.sceneID, sceneValue[0], sceneValue[1]);
58             } else {
59                 device.setSceneOutputValue(this.sceneID, sceneValue[0]);
60             }
61             logger.debug("UPDATED sceneOutputValue for dsid: {}, sceneID: {}, value: {}, angle: {}",
62                     this.device.getDSID(), sceneID, sceneValue[0], sceneValue[1]);
63         }
64     }
65
66     @Override
67     public boolean equals(Object obj) {
68         if (obj instanceof SceneOutputValueReadingJob other) {
69             String str = other.device.getDSID().getValue() + "-" + other.sceneID;
70             return (this.device.getDSID().getValue() + "-" + this.sceneID).equals(str);
71         }
72         return false;
73     }
74
75     @Override
76     public int hashCode() {
77         return new String(this.device.getDSID().getValue() + this.sceneID).hashCode();
78     }
79
80     @Override
81     public DSID getDSID() {
82         return device.getDSID();
83     }
84
85     @Override
86     public DSID getMeterDSID() {
87         return this.meterDSID;
88     }
89
90     @Override
91     public long getInitalisationTime() {
92         return this.initalisationTime;
93     }
94
95     @Override
96     public void setInitalisationTime(long time) {
97         this.initalisationTime = time;
98     }
99
100     @Override
101     public String toString() {
102         return "SceneOutputValueReadingJob [sceneID: " + sceneID + ", deviceDSID : " + device.getDSID().getValue()
103                 + ", meterDSID=" + meterDSID + ", initalisationTime=" + initalisationTime + "]";
104     }
105
106     @Override
107     public String getID() {
108         return getID(device, sceneID);
109     }
110
111     /**
112      * Returns the id for a {@link SceneOutputValueReadingJob} with the given {@link Device} and sceneID.
113      *
114      * @param device to update
115      * @param sceneID to update
116      * @return id
117      */
118     public static String getID(Device device, Short sceneID) {
119         return DeviceOutputValueSensorJob.class.getSimpleName() + "-" + device.getDSID().getValue() + "-" + sceneID;
120     }
121 }