2 * Copyright (c) 2010-2021 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.List;
17 import java.util.TreeMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Represents layout of the light panels
27 * @author Martin Raepple - Initial contribution
28 * @author Stefan Höhn - further improvements
33 private int numPanels;
35 private final Logger logger = LoggerFactory.getLogger(Layout.class);
37 private @Nullable List<PositionDatum> positionData = null;
39 public int getNumPanels() {
43 public void setNumPanels(int numPanels) {
44 this.numPanels = numPanels;
47 public @Nullable List<PositionDatum> getPositionData() {
51 public void setPositionData(List<PositionDatum> positionData) {
52 this.positionData = positionData;
56 * Returns an text representation for a canvas layout.
58 * Note only canvas supported currently due to its easy geometry
60 * @return a String containing the layout
62 public String getLayoutView() {
63 List<PositionDatum> localPositionData = positionData;
64 if (localPositionData != null) {
67 int minx = Integer.MAX_VALUE;
68 int maxx = Integer.MIN_VALUE;
69 int miny = Integer.MAX_VALUE;
70 int maxy = Integer.MIN_VALUE;
71 int sideLength = Integer.MIN_VALUE;
73 final int noofDefinedPanels = localPositionData.size();
76 * Since 5.0.0 sidelengths are panelspecific and not delivered per layout but only the individual panel.
77 * The only approximation we can do then is to derive the max-sidelength
78 * the other issue is that panel sidelength have become fix per paneltype which has to be retrieved in a
81 for (int index = 0; index < noofDefinedPanels; index++) {
82 PositionDatum panel = localPositionData.get(index);
83 logger.debug("Layout: Panel position data x={} y={}", panel.getPosX(), panel.getPosY());
85 if (panel.getPosX() < minx) {
86 minx = panel.getPosX();
88 if (panel.getPosX() > maxx) {
89 maxx = panel.getPosX();
91 if (panel.getPosY() < miny) {
92 miny = panel.getPosY();
94 if (panel.getPosY() > maxy) {
95 maxy = panel.getPosY();
97 if (panel.getPanelSize() > sideLength) {
98 sideLength = panel.getPanelSize();
102 int shiftWidth = sideLength / 2;
104 if (shiftWidth == 0) {
105 // seems we do not have squares here
106 return "Cannot render layout. Please note that layout views are only supported for square panels.";
110 Map<Integer, PositionDatum> map;
112 while (lineY >= miny) {
113 map = new TreeMap<>();
114 for (int index = 0; index < noofDefinedPanels; index++) {
116 if (localPositionData != null) {
117 PositionDatum panel = localPositionData.get(index);
119 if (panel.getPosY() == lineY) {
120 map.put(panel.getPosX(), panel);
125 for (int x = minx; x <= maxx; x += shiftWidth) {
126 if (map.containsKey(x)) {
127 PositionDatum panel = map.get(x);
129 int panelId = panel.getPanelId();
130 view += String.format("%5s ", panelId);
138 view += System.lineSeparator();