]> git.basschouten.com Git - openhab-addons.git/blob
ab018e9c006b96bcb46e043746d02a4e4702d745
[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.elroconnects.internal;
14
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.elroconnects.internal.util.ElroConnectsUtil;
20
21 import com.google.gson.annotations.SerializedName;
22
23 /**
24  * The {@link ElroConnectsMessage} represents the JSON messages exchanged with the ELRO Connects K1 Connector. This
25  * class is used to serialize/deserialize the messages. The class also maps cmdId's from older firmware to the newer
26  * codes and will encode/decode fields that are encoded in the messages with newer firmware versions.
27  *
28  * @author Mark Herwege - Initial contribution
29  */
30 @SuppressWarnings("unused") // Suppress warning on serialized fields
31 @NonNullByDefault
32 public class ElroConnectsMessage {
33
34     private transient boolean legacyFirmware = false; // legacy firmware uses different cmd id's and will not encode
35                                                       // device id when sending messages
36
37     private static class Data {
38         private int cmdId;
39
40         @SerializedName(value = "device_ID")
41         private @Nullable Integer deviceId;
42
43         @SerializedName(value = "device_name")
44         private @Nullable String deviceName;
45
46         @SerializedName(value = "device_status")
47         private @Nullable String deviceStatus;
48
49         @SerializedName(value = "answer_content")
50         private @Nullable String answerContent;
51
52         @SerializedName(value = "sence_group")
53         private @Nullable Integer sceneGroup;
54
55         @SerializedName(value = "scene_type")
56         private @Nullable Integer sceneType;
57
58         @SerializedName(value = "scene_content")
59         private @Nullable String sceneContent;
60     }
61
62     private static class Params {
63         private String devTid = "";
64         private String ctrlKey = "";
65         private Data data = new Data();
66     }
67
68     private int msgId;
69     private String action = "appSend";
70     private Params params = new Params();
71
72     public ElroConnectsMessage(int msgId, String devTid, String ctrlKey, int cmdId) {
73         this(msgId, devTid, ctrlKey, cmdId, false);
74     }
75
76     public ElroConnectsMessage(int msgId, String devTid, String ctrlKey, int cmdId, boolean legacyFirmware) {
77         this.msgId = msgId;
78         params.devTid = devTid;
79         params.ctrlKey = ctrlKey;
80         params.data.cmdId = legacyFirmware ? ELRO_LEGACY_MESSAGES.getOrDefault(cmdId, cmdId) : cmdId;
81
82         this.legacyFirmware = legacyFirmware;
83     }
84
85     public ElroConnectsMessage withDeviceStatus(String deviceStatus) {
86         params.data.deviceStatus = deviceStatus;
87         return this;
88     }
89
90     public ElroConnectsMessage withDeviceId(int deviceId) {
91         params.data.deviceId = isLegacy() ? deviceId : ElroConnectsUtil.encode(deviceId);
92         return this;
93     }
94
95     public ElroConnectsMessage withSceneType(int sceneType) {
96         params.data.sceneType = isLegacy() ? sceneType : ElroConnectsUtil.encode(sceneType);
97         return this;
98     }
99
100     public ElroConnectsMessage withDeviceName(String deviceName) {
101         params.data.deviceName = deviceName;
102         return this;
103     }
104
105     public ElroConnectsMessage withSceneGroup(int sceneGroup) {
106         params.data.sceneGroup = isLegacy() ? sceneGroup : ElroConnectsUtil.encode(sceneGroup);
107         return this;
108     }
109
110     public ElroConnectsMessage withSceneContent(String sceneContent) {
111         params.data.sceneContent = sceneContent;
112         return this;
113     }
114
115     public ElroConnectsMessage withAnswerContent(String answerContent) {
116         params.data.answerContent = answerContent;
117         return this;
118     }
119
120     private boolean isLegacy() {
121         return ELRO_NEW_MESSAGES.containsKey(params.data.cmdId) || legacyFirmware;
122     }
123
124     public int getMsgId() {
125         return msgId;
126     }
127
128     public String getAction() {
129         return action;
130     }
131
132     public int getCmdId() {
133         return ELRO_NEW_MESSAGES.getOrDefault(params.data.cmdId, params.data.cmdId);
134     }
135
136     public String getDeviceStatus() {
137         return ElroConnectsUtil.stringOrEmpty(params.data.deviceStatus);
138     }
139
140     public int getSceneGroup() {
141         int sceneGroup = ElroConnectsUtil.intOrZero(params.data.sceneGroup);
142         return isLegacy() ? sceneGroup : ElroConnectsUtil.decode(sceneGroup, msgId);
143     }
144
145     public int getSceneType() {
146         int sceneType = ElroConnectsUtil.intOrZero(params.data.sceneType);
147         return isLegacy() ? sceneType : ElroConnectsUtil.decode(sceneType, msgId);
148     }
149
150     public String getSceneContent() {
151         return ElroConnectsUtil.stringOrEmpty(params.data.sceneContent);
152     }
153
154     public String getAnswerContent() {
155         return ElroConnectsUtil.stringOrEmpty(params.data.answerContent);
156     }
157
158     public int getDeviceId() {
159         int deviceId = ElroConnectsUtil.intOrZero(params.data.deviceId);
160         return isLegacy() ? deviceId : ElroConnectsUtil.decode(deviceId, msgId);
161     }
162
163     public String getDeviceName() {
164         return ElroConnectsUtil.stringOrEmpty(params.data.deviceName);
165     }
166 }