]> git.basschouten.com Git - openhab-addons.git/blob
933c3b0513250e4458e16808c3a3e2ec0004962e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.bosesoundtouch.internal;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Objects;
18
19 import org.apache.commons.lang3.StringEscapeUtils;
20 import org.openhab.core.types.StateOption;
21
22 import com.google.gson.annotations.Expose;
23
24 /**
25  * The {@link ContentItem} class manages a ContentItem
26  *
27  * @author Christian Niessner - Initial contribution
28  * @author Thomas Traunbauer - Initial contribution
29  */
30 public class ContentItem {
31
32     private String source;
33     private String sourceAccount;
34     private String location;
35     private boolean presetable;
36     private String itemName;
37     private int presetID;
38     private String containerArt;
39     @Expose
40     private final Map<String, String> additionalAttributes;
41
42     /**
43      * Creates a new instance of this class
44      */
45     public ContentItem() {
46         source = "";
47         sourceAccount = null;
48         location = null;
49         presetable = false;
50         itemName = null;
51         presetID = 0;
52         containerArt = null;
53         additionalAttributes = new HashMap<>();
54     }
55
56     /**
57      * Returns true if this ContentItem is defined as Preset
58      *
59      * @return true if this ContentItem is defined as Preset
60      */
61     public boolean isPreset() {
62         if (presetable) {
63             return presetID > 0;
64         } else {
65             return false;
66         }
67     }
68
69     /**
70      * Returns true if all necessary stats are set
71      *
72      * @return true if all necessary stats are set
73      */
74     public boolean isValid() {
75         if (getOperationMode() == OperationModeType.STANDBY) {
76             return true;
77         }
78         if (itemName == null || source == null || itemName.isEmpty() || source.isEmpty()) {
79             return false;
80         } else {
81             return true;
82         }
83     }
84
85     /**
86      * Returns true if source, sourceAccount, location, itemName, and presetable are equal
87      *
88      * @return true if source, sourceAccount, location, itemName, and presetable are equal
89      */
90     @Override
91     public boolean equals(Object obj) {
92         if (obj instanceof ContentItem) {
93             ContentItem other = (ContentItem) obj;
94             if (!Objects.equals(other.source, this.source)) {
95                 return false;
96             }
97             if (!Objects.equals(other.sourceAccount, this.sourceAccount)) {
98                 return false;
99             }
100             if (other.presetable != this.presetable) {
101                 return false;
102             }
103             if (!Objects.equals(other.location, this.location)) {
104                 return false;
105             }
106             if (!Objects.equals(other.itemName, this.itemName)) {
107                 return false;
108             }
109             return true;
110         }
111         return super.equals(obj);
112     }
113
114     /**
115      * Returns the operation Mode, depending on the stats that are set
116      *
117      * @return the operation Mode, depending on the stats that are set
118      */
119     public OperationModeType getOperationMode() {
120         OperationModeType operationMode = OperationModeType.OTHER;
121         if (source == null || source.equals("")) {
122             return OperationModeType.OTHER;
123         }
124         if (source.contains("PRODUCT")) {
125             if (sourceAccount.contains("TV")) {
126                 operationMode = OperationModeType.TV;
127             }
128             if (sourceAccount.contains("HDMI")) {
129                 operationMode = OperationModeType.HDMI1;
130             }
131             return operationMode;
132         }
133         try {
134             operationMode = OperationModeType.valueOf(source);
135             return operationMode;
136         } catch (IllegalArgumentException iae) {
137             return OperationModeType.OTHER;
138         }
139     }
140
141     public void setSource(String source) {
142         this.source = source;
143     }
144
145     public void setSourceAccount(String sourceAccount) {
146         this.sourceAccount = sourceAccount;
147     }
148
149     public void setLocation(String location) {
150         this.location = location;
151     }
152
153     public void setItemName(String itemName) {
154         this.itemName = itemName;
155     }
156
157     public void setAdditionalAttribute(String name, String value) {
158         this.additionalAttributes.put(name, value);
159     }
160
161     public void setPresetable(boolean presetable) {
162         this.presetable = presetable;
163     }
164
165     public void setPresetID(int presetID) {
166         this.presetID = presetID;
167     }
168
169     public void setContainerArt(String containerArt) {
170         this.containerArt = containerArt;
171     }
172
173     public String getSource() {
174         return source;
175     }
176
177     public String getSourceAccount() {
178         return sourceAccount;
179     }
180
181     public String getLocation() {
182         return location;
183     }
184
185     public String getItemName() {
186         return itemName;
187     }
188
189     public boolean isPresetable() {
190         return presetable;
191     }
192
193     public int getPresetID() {
194         return presetID;
195     }
196
197     public String getContainerArt() {
198         return containerArt;
199     }
200
201     /**
202      * Returns the XML Code that is needed to switch to this ContentItem
203      *
204      * @return the XML Code that is needed to switch to this ContentItem
205      */
206     public String generateXML() {
207         String xml;
208         switch (getOperationMode()) {
209             case BLUETOOTH:
210                 xml = "<ContentItem source=\"BLUETOOTH\"></ContentItem>";
211                 break;
212             case AUX:
213             case AUX1:
214             case AUX2:
215             case AUX3:
216                 xml = "<ContentItem source=\"AUX\" sourceAccount=\"" + sourceAccount + "\"></ContentItem>";
217                 break;
218             case TV:
219                 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"TV\" isPresetable=\"false\" />";
220                 break;
221             case HDMI1:
222                 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"HDMI_1\" isPresetable=\"false\" />";
223                 break;
224             default:
225                 StringBuilder sbXml = new StringBuilder("<ContentItem");
226                 if (source != null) {
227                     sbXml.append(" source=\"").append(StringEscapeUtils.escapeXml(source)).append("\"");
228                 }
229                 if (location != null) {
230                     sbXml.append(" location=\"").append(StringEscapeUtils.escapeXml(location)).append("\"");
231                 }
232                 if (sourceAccount != null) {
233                     sbXml.append(" sourceAccount=\"").append(StringEscapeUtils.escapeXml(sourceAccount)).append("\"");
234                 }
235                 sbXml.append(" isPresetable=\"").append(presetable).append("\"");
236                 for (Map.Entry<String, String> aae : additionalAttributes.entrySet()) {
237                     sbXml.append(" ").append(aae.getKey()).append("=\"")
238                             .append(StringEscapeUtils.escapeXml(aae.getValue())).append("\"");
239                 }
240                 sbXml.append(">");
241                 if (itemName != null) {
242                     sbXml.append("<itemName>").append(itemName).append("</itemName>");
243                 }
244                 if (containerArt != null) {
245                     sbXml.append("<containerArt>").append(containerArt).append("</containerArt>");
246                 }
247                 sbXml.append("</ContentItem>");
248                 xml = sbXml.toString();
249                 break;
250         }
251         return xml;
252     }
253
254     public StateOption toStateOption() {
255         String stateOptionLabel = String.valueOf(presetID) + ": " + itemName;
256         return new StateOption(String.valueOf(presetID), stateOptionLabel);
257     }
258
259     @Override
260     public String toString() {
261         // if (presetID >= 1 && presetID <= 6) {
262         // StringBuilder buffer = new StringBuilder();
263         // buffer.append("PRESET_");
264         // buffer.append(presetID);
265         // return buffer.toString();
266         // }
267         return itemName;
268     }
269 }