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