]> git.basschouten.com Git - openhab-addons.git/blob
f5865541e53a2e7a2a6bf705255d464e172e4f92
[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.miio.internal.robot;
14
15 import java.awt.Color;
16 import java.io.FileNotFoundException;
17 import java.io.FileReader;
18 import java.io.PrintWriter;
19 import java.lang.reflect.Type;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.slf4j.Logger;
24
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.JsonDeserializationContext;
28 import com.google.gson.JsonDeserializer;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParseException;
32 import com.google.gson.JsonSerializationContext;
33 import com.google.gson.JsonSerializer;
34 import com.google.gson.annotations.Expose;
35 import com.google.gson.annotations.SerializedName;
36
37 /**
38  * This class provides the configuration for the vacuum map drawing
39  *
40  * @author Marcel Verpaalen - Initial contribution
41  */
42 @NonNullByDefault
43 public class RRMapDrawOptions {
44
45     private static final Color COLOR_MAP_INSIDE = new Color(32, 115, 185);
46     private static final Color COLOR_MAP_OUTSIDE = new Color(19, 87, 148);
47     private static final Color COLOR_MAP_WALL = new Color(100, 196, 254);
48     private static final Color COLOR_CARPET = new Color(0xDF, 0xDF, 0xDF, 0xA0);
49     private static final Color COLOR_GREY_WALL = new Color(93, 109, 126);
50     private static final Color COLOR_PATH = new Color(147, 194, 238);
51     private static final Color COLOR_ZONES = new Color(0xAD, 0xD8, 0xFF, 0x8F);
52     private static final Color COLOR_NO_GO_ZONES = new Color(255, 33, 55, 127);
53     private static final Color COLOR_CHARGER_HALO = new Color(0x66, 0xfe, 0xda, 0x7f);
54     private static final Color COLOR_ROBO = new Color(75, 235, 149);
55     private static final Color COLOR_SCAN = new Color(0xDF, 0xDF, 0xDF);
56     private static final Color ROOM1 = new Color(240, 178, 122);
57     private static final Color ROOM2 = new Color(133, 193, 233);
58     private static final Color ROOM3 = new Color(217, 136, 128);
59     private static final Color ROOM4 = new Color(52, 152, 219);
60     private static final Color ROOM5 = new Color(205, 97, 85);
61     private static final Color ROOM6 = new Color(243, 156, 18);
62     private static final Color ROOM7 = new Color(88, 214, 141);
63     private static final Color ROOM8 = new Color(245, 176, 65);
64     private static final Color ROOM9 = new Color(0xFc, 0xD4, 0x51);
65     private static final Color ROOM10 = new Color(72, 201, 176);
66     private static final Color ROOM11 = new Color(84, 153, 199);
67     private static final Color ROOM12 = new Color(255, 213, 209);
68     private static final Color ROOM13 = new Color(228, 228, 215);
69     private static final Color ROOM14 = new Color(82, 190, 128);
70     private static final Color ROOM15 = new Color(72, 201, 176);
71     private static final Color ROOM16 = new Color(165, 105, 189);
72     private static final Color[] ROOM_COLORS = { ROOM1, ROOM2, ROOM3, ROOM4, ROOM5, ROOM6, ROOM7, ROOM8, ROOM9, ROOM10,
73             ROOM11, ROOM12, ROOM13, ROOM14, ROOM15, ROOM16 };
74
75     private static final Gson GSON = new GsonBuilder().setPrettyPrinting()
76             .registerTypeAdapter(Color.class, new JsonSerializer<Color>() {
77                 @Override
78                 public JsonElement serialize(Color src, @Nullable Type typeOfSrc,
79                         @Nullable JsonSerializationContext context) {
80                     JsonObject colorSave = new JsonObject();
81                     colorSave.addProperty("red", src.getRed());
82                     colorSave.addProperty("green", src.getGreen());
83                     colorSave.addProperty("blue", src.getBlue());
84                     colorSave.addProperty("alpha", src.getAlpha());
85                     return colorSave;
86                 }
87             }).registerTypeAdapter(Color.class, new JsonDeserializer<Color>() {
88                 @Override
89                 @Nullable
90                 public Color deserialize(@Nullable JsonElement json, @Nullable Type typeOfT,
91                         @Nullable JsonDeserializationContext context) throws JsonParseException {
92                     if (json == null) {
93                         throw new JsonParseException("missing json text");
94                     }
95                     JsonObject colorSave = json.getAsJsonObject();
96                     return new Color(colorSave.get("red").getAsInt(), colorSave.get("green").getAsInt(),
97                             colorSave.get("blue").getAsInt(), colorSave.get("alpha").getAsInt());
98                 }
99             }).create();
100
101     @SerializedName("colorMapInside")
102     @Expose
103     private Color colorMapInside = COLOR_MAP_INSIDE;
104     @SerializedName("colorMapOutside")
105     @Expose
106     private Color colorMapOutside = COLOR_MAP_OUTSIDE;
107     @SerializedName("colorMapWall")
108     @Expose
109     private Color colorMapWall = COLOR_MAP_WALL;
110     @SerializedName("colorCarpet")
111     @Expose
112     private Color colorCarpet = COLOR_CARPET;
113     @SerializedName("colorGreyWall")
114     @Expose
115     private Color colorGreyWall = COLOR_GREY_WALL;
116     @SerializedName("colorPath")
117     @Expose
118     private Color colorPath = COLOR_PATH;
119     @SerializedName("colorZones")
120     @Expose
121     private Color colorZones = COLOR_ZONES;
122     @SerializedName("colorNoGoZones")
123     @Expose
124     private Color colorNoGoZones = COLOR_NO_GO_ZONES;
125     @SerializedName("colorChargerHalo")
126     @Expose
127     private Color colorChargerHalo = COLOR_CHARGER_HALO;
128     @SerializedName("colorRobo")
129     @Expose
130     private Color colorRobo = COLOR_ROBO;
131     @SerializedName("colorScan")
132     @Expose
133     private Color colorScan = COLOR_SCAN;
134
135     @SerializedName("roomColors")
136     @Expose
137     private Color[] roomColors = ROOM_COLORS;
138
139     @SerializedName("showLogo")
140     @Expose
141     private boolean showLogo = true;
142     @SerializedName("text")
143     @Expose
144     private String text = "Openhab rocks your Xiaomi vacuum!";
145     @SerializedName("textFontSize")
146     @Expose
147     private int textFontSize = 12;
148     @SerializedName("scale")
149     @Expose
150     private float scale = 2.0f;
151     @SerializedName("cropBorder")
152     @Expose
153     private int cropBorder = 10;
154
155     public Color getColorMapInside() {
156         return colorMapInside;
157     }
158
159     public void setColorMapInside(Color colorMapInside) {
160         this.colorMapInside = colorMapInside;
161     }
162
163     public Color getColorMapOutside() {
164         return colorMapOutside;
165     }
166
167     public void setColorMapOutside(Color colorMapOutside) {
168         this.colorMapOutside = colorMapOutside;
169     }
170
171     public Color getColorMapWall() {
172         return colorMapWall;
173     }
174
175     public void setColorMapWall(Color colorMapWall) {
176         this.colorMapWall = colorMapWall;
177     }
178
179     public Color getColorCarpet() {
180         return colorCarpet;
181     }
182
183     public void setColorCarpet(Color colorCarpet) {
184         this.colorCarpet = colorCarpet;
185     }
186
187     public Color getColorGreyWall() {
188         return colorGreyWall;
189     }
190
191     public void setColorGreyWall(Color colorGreyWall) {
192         this.colorGreyWall = colorGreyWall;
193     }
194
195     public Color getColorPath() {
196         return colorPath;
197     }
198
199     public void setColorPath(Color colorPath) {
200         this.colorPath = colorPath;
201     }
202
203     public Color getColorZones() {
204         return colorZones;
205     }
206
207     public void setColorZones(Color colorZones) {
208         this.colorZones = colorZones;
209     }
210
211     public Color getColorNoGoZones() {
212         return colorNoGoZones;
213     }
214
215     public void setColorNoGoZones(Color colorNoGoZones) {
216         this.colorNoGoZones = colorNoGoZones;
217     }
218
219     public Color getColorChargerHalo() {
220         return colorChargerHalo;
221     }
222
223     public void setColorChargerHalo(Color colorChargerHalo) {
224         this.colorChargerHalo = colorChargerHalo;
225     }
226
227     public Color getColorRobo() {
228         return colorRobo;
229     }
230
231     public void setColorRobo(Color colorRobo) {
232         this.colorRobo = colorRobo;
233     }
234
235     public Color getColorScan() {
236         return colorScan;
237     }
238
239     public void setColorScan(Color colorScan) {
240         this.colorScan = colorScan;
241     }
242
243     public Color[] getRoomColors() {
244         return roomColors;
245     }
246
247     public void setRoomColors(Color[] roomColors) {
248         this.roomColors = roomColors;
249     }
250
251     public boolean isShowLogo() {
252         return showLogo;
253     }
254
255     public void setShowLogo(boolean showLogo) {
256         this.showLogo = showLogo;
257     }
258
259     public String getText() {
260         return text;
261     }
262
263     public void setText(String text) {
264         this.text = text;
265     }
266
267     public final int getTextFontSize() {
268         return textFontSize;
269     }
270
271     public final void setTextFontSize(int textFontSize) {
272         this.textFontSize = textFontSize;
273     }
274
275     public float getScale() {
276         return scale;
277     }
278
279     public void setScale(float scale) {
280         this.scale = scale;
281     }
282
283     public final int getCropBorder() {
284         return cropBorder;
285     }
286
287     public final void setCropBorder(int cropBorder) {
288         this.cropBorder = cropBorder;
289     }
290
291     public static void writeOptionsToFile(RRMapDrawOptions options, String fileName, Logger logger) {
292         String json = GSON.toJson(options, RRMapDrawOptions.class);
293         try (PrintWriter pw = new PrintWriter(fileName)) {
294             pw.println(json);
295             logger.debug("Vacuum map draw options file created: {}", fileName);
296         } catch (FileNotFoundException e) {
297             logger.info("Error writing Vacuum map draw options file: {}", e.getMessage());
298         }
299     }
300
301     public static RRMapDrawOptions getOptionsFromFile(String fileName, Logger logger) {
302         try {
303             return GSON.fromJson(new FileReader(fileName), RRMapDrawOptions.class);
304         } catch (FileNotFoundException e) {
305             logger.debug("Vacuum map draw options file {} not found. Using defaults", fileName);
306             return new RRMapDrawOptions();
307         } catch (JsonParseException e) {
308             logger.info("Error reading vacuum map draw options file {}: {}", fileName, e.getMessage());
309         }
310         logger.info("Write default map draw options to {}", fileName);
311         RRMapDrawOptions options = new RRMapDrawOptions();
312         writeOptionsToFile(options, fileName, logger);
313         return new RRMapDrawOptions();
314     }
315 }