2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.elroconnects.internal;
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.elroconnects.internal.util.ElroConnectsUtil;
21 import com.google.gson.annotations.SerializedName;
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.
28 * @author Mark Herwege - Initial contribution
30 @SuppressWarnings("unused") // Suppress warning on serialized fields
32 public class ElroConnectsMessage {
34 private transient boolean legacyFirmware = false; // legacy firmware uses different cmd id's and will not encode
35 // device id when sending messages
37 private static class Data {
40 @SerializedName(value = "device_ID")
41 private @Nullable Integer deviceId;
43 @SerializedName(value = "device_name")
44 private @Nullable String deviceName;
46 @SerializedName(value = "device_status")
47 private @Nullable String deviceStatus;
49 @SerializedName(value = "answer_content")
50 private @Nullable String answerContent;
52 @SerializedName(value = "sence_group")
53 private @Nullable Integer sceneGroup;
55 @SerializedName(value = "scene_type")
56 private @Nullable Integer sceneType;
58 @SerializedName(value = "scene_content")
59 private @Nullable String sceneContent;
62 private static class Params {
63 private String devTid = "";
64 private String ctrlKey = "";
65 private Data data = new Data();
69 private String action = "appSend";
70 private Params params = new Params();
72 public ElroConnectsMessage(int msgId, String devTid, String ctrlKey, int cmdId) {
73 this(msgId, devTid, ctrlKey, cmdId, false);
76 public ElroConnectsMessage(int msgId, String devTid, String ctrlKey, int cmdId, boolean legacyFirmware) {
78 params.devTid = devTid;
79 params.ctrlKey = ctrlKey;
80 params.data.cmdId = legacyFirmware ? ELRO_LEGACY_MESSAGES.getOrDefault(cmdId, cmdId) : cmdId;
82 this.legacyFirmware = legacyFirmware;
85 public ElroConnectsMessage withDeviceStatus(String deviceStatus) {
86 params.data.deviceStatus = deviceStatus;
90 public ElroConnectsMessage withDeviceId(int deviceId) {
91 params.data.deviceId = isLegacy() ? deviceId : ElroConnectsUtil.encode(deviceId);
95 public ElroConnectsMessage withSceneType(int sceneType) {
96 params.data.sceneType = isLegacy() ? sceneType : ElroConnectsUtil.encode(sceneType);
100 public ElroConnectsMessage withDeviceName(String deviceName) {
101 params.data.deviceName = deviceName;
105 public ElroConnectsMessage withSceneGroup(int sceneGroup) {
106 params.data.sceneGroup = isLegacy() ? sceneGroup : ElroConnectsUtil.encode(sceneGroup);
110 public ElroConnectsMessage withSceneContent(String sceneContent) {
111 params.data.sceneContent = sceneContent;
115 public ElroConnectsMessage withAnswerContent(String answerContent) {
116 params.data.answerContent = answerContent;
120 private boolean isLegacy() {
121 return ELRO_NEW_MESSAGES.containsKey(params.data.cmdId) || legacyFirmware;
124 public int getMsgId() {
128 public String getAction() {
132 public int getCmdId() {
133 return ELRO_NEW_MESSAGES.getOrDefault(params.data.cmdId, params.data.cmdId);
136 public String getDeviceStatus() {
137 return ElroConnectsUtil.stringOrEmpty(params.data.deviceStatus);
140 public int getSceneGroup() {
141 int sceneGroup = ElroConnectsUtil.intOrZero(params.data.sceneGroup);
142 return isLegacy() ? sceneGroup : ElroConnectsUtil.decode(sceneGroup, msgId);
145 public int getSceneType() {
146 int sceneType = ElroConnectsUtil.intOrZero(params.data.sceneType);
147 return isLegacy() ? sceneType : ElroConnectsUtil.decode(sceneType, msgId);
150 public String getSceneContent() {
151 return ElroConnectsUtil.stringOrEmpty(params.data.sceneContent);
154 public String getAnswerContent() {
155 return ElroConnectsUtil.stringOrEmpty(params.data.answerContent);
158 public int getDeviceId() {
159 int deviceId = ElroConnectsUtil.intOrZero(params.data.deviceId);
160 return isLegacy() ? deviceId : ElroConnectsUtil.decode(deviceId, msgId);
163 public String getDeviceName() {
164 return ElroConnectsUtil.stringOrEmpty(params.data.deviceName);