2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nanoleaf.internal.model;
15 import java.util.ArrayList;
16 import java.util.List;
18 import java.util.TreeMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Represents layout of the light panels
28 * @author Martin Raepple - Initial contribution
29 * @author Stefan Höhn - further improvements
34 private int numPanels;
36 private final Logger logger = LoggerFactory.getLogger(Layout.class);
38 private @Nullable List<PositionDatum> positionData = null;
43 public Layout(List<PositionDatum> positionData) {
44 this.positionData = new ArrayList<>(positionData);
45 this.numPanels = positionData.size();
48 public int getNumPanels() {
52 public void setNumPanels(int numPanels) {
53 this.numPanels = numPanels;
56 public @Nullable List<PositionDatum> getPositionData() {
60 public void setPositionData(List<PositionDatum> positionData) {
61 this.positionData = positionData;
65 * Returns a text representation for a canvas layout.
67 * Note only canvas supported currently due to its easy geometry
69 * @return a String containing the layout
71 public String getLayoutView() {
72 List<PositionDatum> localPositionData = positionData;
73 if (localPositionData != null) {
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;
82 final int noofDefinedPanels = localPositionData.size();
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
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());
94 if (panel.getPosX() < minx) {
95 minx = panel.getPosX();
97 if (panel.getPosX() > maxx) {
98 maxx = panel.getPosX();
100 if (panel.getPosY() < miny) {
101 miny = panel.getPosY();
103 if (panel.getPosY() > maxy) {
104 maxy = panel.getPosY();
106 if (panel.getPanelSize() > sideLength) {
107 sideLength = panel.getPanelSize();
111 int shiftWidth = sideLength / 2;
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.";
119 Map<Integer, PositionDatum> map;
121 while (lineY >= miny) {
122 map = new TreeMap<>();
123 for (int index = 0; index < noofDefinedPanels; index++) {
125 if (localPositionData != null) {
126 PositionDatum panel = localPositionData.get(index);
128 if (panel.getPosY() == lineY) {
129 map.put(panel.getPosX(), panel);
134 for (int x = minx; x <= maxx; x += shiftWidth) {
135 if (map.containsKey(x)) {
136 PositionDatum panel = map.get(x);
138 int panelId = panel.getPanelId();
139 view += String.format("%5s ", panelId);
147 view += System.lineSeparator();
157 public boolean equals(@Nullable Object o) {
162 if (o == null || getClass() != o.getClass()) {
166 Layout l = (Layout) o;
168 if (numPanels != l.getNumPanels()) {
172 List<PositionDatum> pd = getPositionData();
173 List<PositionDatum> otherPd = l.getPositionData();
174 if (pd == null && otherPd == null) {
178 if (pd == null || otherPd == null) {
182 return pd.equals(otherPd);
186 public int hashCode() {
187 final int prime = 31;
189 result = prime * result + getNumPanels();
190 List<PositionDatum> pd = getPositionData();
192 for (PositionDatum p : pd) {
193 result = prime * result + p.hashCode();