]> git.basschouten.com Git - openhab-addons.git/blob
74020442669379cb3fd39bd08d3fe72770a5c3d1
[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 java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.TreeMap;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Represents layout of the light panels
27  *
28  * @author Martin Raepple - Initial contribution
29  * @author Stefan Höhn - further improvements
30  */
31 @NonNullByDefault
32 public class Layout {
33
34     private int numPanels;
35
36     private final Logger logger = LoggerFactory.getLogger(Layout.class);
37
38     private @Nullable List<PositionDatum> positionData = null;
39
40     public Layout() {
41     }
42
43     public Layout(List<PositionDatum> positionData) {
44         this.positionData = new ArrayList<>(positionData);
45         this.numPanels = positionData.size();
46     }
47
48     public int getNumPanels() {
49         return numPanels;
50     }
51
52     public void setNumPanels(int numPanels) {
53         this.numPanels = numPanels;
54     }
55
56     public @Nullable List<PositionDatum> getPositionData() {
57         return positionData;
58     }
59
60     public void setPositionData(List<PositionDatum> positionData) {
61         this.positionData = positionData;
62     }
63
64     /**
65      * Returns a text representation for a canvas layout.
66      *
67      * Note only canvas supported currently due to its easy geometry
68      *
69      * @return a String containing the layout
70      */
71     public String getLayoutView() {
72         List<PositionDatum> localPositionData = positionData;
73         if (localPositionData != null) {
74             String view = "";
75
76             int minx = Integer.MAX_VALUE;
77             int maxx = Integer.MIN_VALUE;
78             int miny = Integer.MAX_VALUE;
79             int maxy = Integer.MIN_VALUE;
80             int sideLength = Integer.MIN_VALUE;
81
82             final int noofDefinedPanels = localPositionData.size();
83
84             /*
85              * Since 5.0.0 sidelengths are panelspecific and not delivered per layout but only the individual panel.
86              * The only approximation we can do then is to derive the max-sidelength
87              * the other issue is that panel sidelength have become fix per paneltype which has to be retrieved in a
88              * hardcoded way.
89              */
90             for (int index = 0; index < noofDefinedPanels; index++) {
91                 PositionDatum panel = localPositionData.get(index);
92                 logger.debug("Layout: Panel position data x={} y={}", panel.getPosX(), panel.getPosY());
93
94                 if (panel.getPosX() < minx) {
95                     minx = panel.getPosX();
96                 }
97                 if (panel.getPosX() > maxx) {
98                     maxx = panel.getPosX();
99                 }
100                 if (panel.getPosY() < miny) {
101                     miny = panel.getPosY();
102                 }
103                 if (panel.getPosY() > maxy) {
104                     maxy = panel.getPosY();
105                 }
106                 if (panel.getPanelSize() > sideLength) {
107                     sideLength = panel.getPanelSize();
108                 }
109             }
110
111             int shiftWidth = sideLength / 2;
112
113             if (shiftWidth == 0) {
114                 // seems we do not have squares here
115                 return "Cannot render layout. Please note that layout views are only supported for square panels.";
116             }
117
118             int lineY = maxy;
119             Map<Integer, PositionDatum> map;
120
121             while (lineY >= miny) {
122                 map = new TreeMap<>();
123                 for (int index = 0; index < noofDefinedPanels; index++) {
124
125                     if (localPositionData != null) {
126                         PositionDatum panel = localPositionData.get(index);
127
128                         if (panel.getPosY() == lineY) {
129                             map.put(panel.getPosX(), panel);
130                         }
131                     }
132                 }
133                 lineY -= shiftWidth;
134                 for (int x = minx; x <= maxx; x += shiftWidth) {
135                     if (map.containsKey(x)) {
136                         PositionDatum panel = map.get(x);
137                         if (panel != null) {
138                             int panelId = panel.getPanelId();
139                             view += String.format("%5s ", panelId);
140                         } else {
141                             view += "      ";
142                         }
143                     } else {
144                         view += "      ";
145                     }
146                 }
147                 view += System.lineSeparator();
148             }
149
150             return view;
151         } else {
152             return "";
153         }
154     }
155
156     @Override
157     public boolean equals(@Nullable Object o) {
158         if (this == o) {
159             return true;
160         }
161
162         if (o == null || getClass() != o.getClass()) {
163             return false;
164         }
165
166         Layout l = (Layout) o;
167
168         if (numPanels != l.getNumPanels()) {
169             return false;
170         }
171
172         List<PositionDatum> pd = getPositionData();
173         List<PositionDatum> otherPd = l.getPositionData();
174         if (pd == null && otherPd == null) {
175             return true;
176         }
177
178         if (pd == null || otherPd == null) {
179             return false;
180         }
181
182         return pd.equals(otherPd);
183     }
184
185     @Override
186     public int hashCode() {
187         final int prime = 31;
188         int result = 1;
189         result = prime * result + getNumPanels();
190         List<PositionDatum> pd = getPositionData();
191         if (pd != null) {
192             for (PositionDatum p : pd) {
193                 result = prime * result + p.hashCode();
194             }
195         }
196
197         return result;
198     }
199 }