]> git.basschouten.com Git - openhab-addons.git/blob
15cd53343af18cc80da5da1f0b14738d7b501309
[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.nanoleaf.internal.model;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.nanoleaf.internal.layout.ShapeType;
18
19 import com.google.gson.annotations.SerializedName;
20
21 /**
22  * Represents panel position
23  *
24  * @author Martin Raepple - Initial contribution
25  */
26 @NonNullByDefault
27 public class PositionDatum {
28
29     private int panelId;
30     @SerializedName("x")
31     private int posX;
32     @SerializedName("y")
33     private int posY;
34     @SerializedName("o")
35     private int orientation;
36     @SerializedName("shapeType")
37     private int shapeType;
38
39     public PositionDatum() {
40     }
41
42     public PositionDatum(int panelId, int posX, int posY, int orientation, int shapeType) {
43         this.panelId = panelId;
44         this.posX = posX;
45         this.posY = posY;
46         this.orientation = orientation;
47         this.shapeType = shapeType;
48     }
49
50     public int getPanelId() {
51         return panelId;
52     }
53
54     public void setPanelId(int panelId) {
55         this.panelId = panelId;
56     }
57
58     public int getPosX() {
59         if (getPanelSize() != 0 && posX % getPanelSize() == 99) { // hack: check the inaccuracy of 1
60             posX = (posX / getPanelSize() + 1) * getPanelSize();
61         }
62         return posX;
63     }
64
65     public void setPosX(int x) {
66         this.posX = x;
67     }
68
69     public int getPosY() {
70         // we need to fix the positions: see
71         // https://forum.nanoleaf.me/forum/aurora-open-api/squares-send-unprecise-layout-positions
72         // unfortunately this cannot be done in the setter as gson does not access setters
73
74         if (getPanelSize() != 0 && posY % getPanelSize() == 99) { // hack: check the inaccuracy of 1
75             posY = (posY / getPanelSize() + 1) * getPanelSize();
76         }
77         return posY;
78     }
79
80     public void setPosY(int y) {
81         this.posY = y;
82     }
83
84     public int getOrientation() {
85         return orientation;
86     }
87
88     public void setOrientation(int o) {
89         this.orientation = o;
90     }
91
92     public int getShapeType() {
93         return shapeType;
94     }
95
96     public void setShapeType(int shapeType) {
97         this.shapeType = shapeType;
98     }
99
100     public Integer getPanelSize() {
101         return (int) ShapeType.valueOf(shapeType).getSideLength();
102     }
103
104     @Override
105     public boolean equals(@Nullable Object o) {
106         if (this == o) {
107             return true;
108         }
109
110         if (o == null || getClass() != o.getClass()) {
111             return false;
112         }
113
114         PositionDatum pd = (PositionDatum) o;
115         return (posX == pd.getPosX()) && (posY == pd.getPosY()) && (orientation == pd.getOrientation())
116                 && (shapeType == pd.getShapeType()) && (panelId == pd.getPanelId());
117     }
118
119     @Override
120     public int hashCode() {
121         final int prime = 31;
122         int result = 1;
123         result = prime * result + posX;
124         result = prime * result + posY;
125         result = prime * result + orientation;
126         result = prime * result + shapeType;
127         result = prime * result + panelId;
128         return result;
129     }
130 }