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