]> git.basschouten.com Git - openhab-addons.git/blob
d84e34783395eee5550aff67d7e9c8acce1587fa
[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) {
69             SceneOutputValueReadingJob other = (SceneOutputValueReadingJob) obj;
70             String str = other.device.getDSID().getValue() + "-" + other.sceneID;
71             return (this.device.getDSID().getValue() + "-" + this.sceneID).equals(str);
72         }
73         return false;
74     }
75
76     @Override
77     public int hashCode() {
78         return new String(this.device.getDSID().getValue() + this.sceneID).hashCode();
79     }
80
81     @Override
82     public DSID getDSID() {
83         return device.getDSID();
84     }
85
86     @Override
87     public DSID getMeterDSID() {
88         return this.meterDSID;
89     }
90
91     @Override
92     public long getInitalisationTime() {
93         return this.initalisationTime;
94     }
95
96     @Override
97     public void setInitalisationTime(long time) {
98         this.initalisationTime = time;
99     }
100
101     @Override
102     public String toString() {
103         return "SceneOutputValueReadingJob [sceneID: " + sceneID + ", deviceDSID : " + device.getDSID().getValue()
104                 + ", meterDSID=" + meterDSID + ", initalisationTime=" + initalisationTime + "]";
105     }
106
107     @Override
108     public String getID() {
109         return getID(device, sceneID);
110     }
111
112     /**
113      * Returns the id for a {@link SceneOutputValueReadingJob} with the given {@link Device} and sceneID.
114      *
115      * @param device to update
116      * @param sceneID to update
117      * @return id
118      */
119     public static String getID(Device device, Short sceneID) {
120         return DeviceOutputValueSensorJob.class.getSimpleName() + "-" + device.getDSID().getValue() + "-" + sceneID;
121     }
122 }