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