]> git.basschouten.com Git - openhab-addons.git/blob
0ea6ddfcca557858234dea178e2f2c827a084d0d
[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                     Color color = new Color(colorSave.get("red").getAsInt(), colorSave.get("green").getAsInt(),
97                             colorSave.get("blue").getAsInt(), colorSave.get("alpha").getAsInt());
98                     return color;
99                 }
100             }).create();
101
102     @SerializedName("colorMapInside")
103     @Expose
104     private Color colorMapInside = COLOR_MAP_INSIDE;
105     @SerializedName("colorMapOutside")
106     @Expose
107     private Color colorMapOutside = COLOR_MAP_OUTSIDE;
108     @SerializedName("colorMapWall")
109     @Expose
110     private Color colorMapWall = COLOR_MAP_WALL;
111     @SerializedName("colorCarpet")
112     @Expose
113     private Color colorCarpet = COLOR_CARPET;
114     @SerializedName("colorGreyWall")
115     @Expose
116     private Color colorGreyWall = COLOR_GREY_WALL;
117     @SerializedName("colorPath")
118     @Expose
119     private Color colorPath = COLOR_PATH;
120     @SerializedName("colorZones")
121     @Expose
122     private Color colorZones = COLOR_ZONES;
123     @SerializedName("colorNoGoZones")
124     @Expose
125     private Color colorNoGoZones = COLOR_NO_GO_ZONES;
126     @SerializedName("colorChargerHalo")
127     @Expose
128     private Color colorChargerHalo = COLOR_CHARGER_HALO;
129     @SerializedName("colorRobo")
130     @Expose
131     private Color colorRobo = COLOR_ROBO;
132     @SerializedName("colorScan")
133     @Expose
134     private Color colorScan = COLOR_SCAN;
135
136     @SerializedName("roomColors")
137     @Expose
138     private Color[] roomColors = ROOM_COLORS;
139
140     @SerializedName("showLogo")
141     @Expose
142     private boolean showLogo = true;
143     @SerializedName("text")
144     @Expose
145     private String text = "Openhab rocks your Xiaomi vacuum!";
146     @SerializedName("textFontSize")
147     @Expose
148     private int textFontSize = 12;
149     @SerializedName("scale")
150     @Expose
151     private float scale = 2.0f;
152     @SerializedName("cropBorder")
153     @Expose
154     private int cropBorder = 10;
155
156     public Color getColorMapInside() {
157         return colorMapInside;
158     }
159
160     public void setColorMapInside(Color colorMapInside) {
161         this.colorMapInside = colorMapInside;
162     }
163
164     public Color getColorMapOutside() {
165         return colorMapOutside;
166     }
167
168     public void setColorMapOutside(Color colorMapOutside) {
169         this.colorMapOutside = colorMapOutside;
170     }
171
172     public Color getColorMapWall() {
173         return colorMapWall;
174     }
175
176     public void setColorMapWall(Color colorMapWall) {
177         this.colorMapWall = colorMapWall;
178     }
179
180     public Color getColorCarpet() {
181         return colorCarpet;
182     }
183
184     public void setColorCarpet(Color colorCarpet) {
185         this.colorCarpet = colorCarpet;
186     }
187
188     public Color getColorGreyWall() {
189         return colorGreyWall;
190     }
191
192     public void setColorGreyWall(Color colorGreyWall) {
193         this.colorGreyWall = colorGreyWall;
194     }
195
196     public Color getColorPath() {
197         return colorPath;
198     }
199
200     public void setColorPath(Color colorPath) {
201         this.colorPath = colorPath;
202     }
203
204     public Color getColorZones() {
205         return colorZones;
206     }
207
208     public void setColorZones(Color colorZones) {
209         this.colorZones = colorZones;
210     }
211
212     public Color getColorNoGoZones() {
213         return colorNoGoZones;
214     }
215
216     public void setColorNoGoZones(Color colorNoGoZones) {
217         this.colorNoGoZones = colorNoGoZones;
218     }
219
220     public Color getColorChargerHalo() {
221         return colorChargerHalo;
222     }
223
224     public void setColorChargerHalo(Color colorChargerHalo) {
225         this.colorChargerHalo = colorChargerHalo;
226     }
227
228     public Color getColorRobo() {
229         return colorRobo;
230     }
231
232     public void setColorRobo(Color colorRobo) {
233         this.colorRobo = colorRobo;
234     }
235
236     public Color getColorScan() {
237         return colorScan;
238     }
239
240     public void setColorScan(Color colorScan) {
241         this.colorScan = colorScan;
242     }
243
244     public Color[] getRoomColors() {
245         return roomColors;
246     }
247
248     public void setRoomColors(Color[] roomColors) {
249         this.roomColors = roomColors;
250     }
251
252     public boolean isShowLogo() {
253         return showLogo;
254     }
255
256     public void setShowLogo(boolean showLogo) {
257         this.showLogo = showLogo;
258     }
259
260     public String getText() {
261         return text;
262     }
263
264     public void setText(String text) {
265         this.text = text;
266     }
267
268     public final int getTextFontSize() {
269         return textFontSize;
270     }
271
272     public final void setTextFontSize(int textFontSize) {
273         this.textFontSize = textFontSize;
274     }
275
276     public float getScale() {
277         return scale;
278     }
279
280     public void setScale(float scale) {
281         this.scale = scale;
282     }
283
284     public final int getCropBorder() {
285         return cropBorder;
286     }
287
288     public final void setCropBorder(int cropBorder) {
289         this.cropBorder = cropBorder;
290     }
291
292     public static void writeOptionsToFile(RRMapDrawOptions options, String fileName, Logger logger) {
293         String json = GSON.toJson(options, RRMapDrawOptions.class);
294         try (PrintWriter pw = new PrintWriter(fileName)) {
295             pw.println(json);
296             logger.debug("Vacuum map draw options file created: {}", fileName);
297         } catch (FileNotFoundException e) {
298             logger.info("Error writing Vacuum map draw options file: {}", e.getMessage());
299         }
300     }
301
302     public static RRMapDrawOptions getOptionsFromFile(String fileName, Logger logger) {
303         try {
304             RRMapDrawOptions options = GSON.fromJson(new FileReader(fileName), RRMapDrawOptions.class);
305             return options;
306         } catch (FileNotFoundException e) {
307             logger.debug("Vacuum map draw options file {} not found. Using defaults", fileName);
308             return new RRMapDrawOptions();
309         } catch (JsonParseException e) {
310             logger.info("Error reading vacuum map draw options file {}: {}", fileName, e.getMessage());
311         }
312         logger.info("Write default map draw options to {}", fileName);
313         RRMapDrawOptions options = new RRMapDrawOptions();
314         writeOptionsToFile(options, fileName, logger);
315         return new RRMapDrawOptions();
316     }
317 }