]> git.basschouten.com Git - openhab-addons.git/blob
7c717ce7a53997d7e30a0e18fc5b2977e8ea5cf3
[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.nanoleaf.internal.model;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 import com.google.gson.annotations.SerializedName;
21
22 /**
23  * Represents panel position
24  *
25  * @author Martin Raepple - Initial contribution
26  */
27 @NonNullByDefault
28 public class PositionDatum {
29
30     private int panelId;
31     @SerializedName("x")
32     private int posX;
33     @SerializedName("y")
34     private int posY;
35     @SerializedName("o")
36     private int orientation;
37     @SerializedName("shapeType")
38     private int shapeType;
39
40     private static Map<Integer, Integer> panelSizes = new HashMap<Integer, Integer>();
41
42     public PositionDatum() {
43         // initialize constant sidelengths for panels. See https://forum.nanoleaf.me/docs chapter 3.3
44         if (panelSizes.isEmpty()) {
45             panelSizes.put(0, 150); // Triangle
46             panelSizes.put(1, 0); // Rhythm N/A
47             panelSizes.put(2, 100); // Square
48             panelSizes.put(3, 100); // Control Square Master
49             panelSizes.put(4, 100); // Control Square Passive
50             panelSizes.put(7, 67); // Hexagon
51             panelSizes.put(8, 134); // Triangle Shapes
52             panelSizes.put(9, 67); // Mini Triangle Shapes
53             panelSizes.put(12, 0); // Shapes Controller (N/A)
54         }
55     }
56
57     public int getPanelId() {
58         return panelId;
59     }
60
61     public void setPanelId(int panelId) {
62         this.panelId = panelId;
63     }
64
65     public int getPosX() {
66         if (getPanelSize() != 0 && posX % getPanelSize() == 99) { // hack: check the inaccuracy of 1
67             posX = (posX / getPanelSize() + 1) * getPanelSize();
68         }
69         return posX;
70     }
71
72     public void setPosX(int x) {
73         this.posX = x;
74     }
75
76     public int getPosY() {
77         // we need to fix the positions: see
78         // https://forum.nanoleaf.me/forum/aurora-open-api/squares-send-unprecise-layout-positions
79         // unfortunately this cannot be done in the setter as gson does not access setters
80
81         if (getPanelSize() != 0 && posY % getPanelSize() == 99) { // hack: check the inaccuracy of 1
82             posY = (posY / getPanelSize() + 1) * getPanelSize();
83         }
84         return posY;
85     }
86
87     public void setPosY(int y) {
88         this.posY = y;
89     }
90
91     public int getOrientation() {
92         return orientation;
93     }
94
95     public void setOrientation(int o) {
96         this.orientation = o;
97     }
98
99     public int getShapeType() {
100         return shapeType;
101     }
102
103     public void setShapeType(int shapeType) {
104         this.shapeType = shapeType;
105     }
106
107     public Integer getPanelSize() {
108         return panelSizes.getOrDefault(shapeType, 0);
109     }
110 }