]> git.basschouten.com Git - openhab-addons.git/blob
a46a28b0aa4f6daf0bf573b68f75b98ba04fc9c0
[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.List;
16 import java.util.Map;
17 import java.util.TreeMap;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Represents layout of the light panels
26  *
27  * @author Martin Raepple - Initial contribution
28  * @author Stefan Höhn - further improvements
29  */
30 @NonNullByDefault
31 public class Layout {
32
33     private int numPanels;
34
35     private final Logger logger = LoggerFactory.getLogger(Layout.class);
36
37     private @Nullable List<PositionDatum> positionData = null;
38
39     public int getNumPanels() {
40         return numPanels;
41     }
42
43     public void setNumPanels(int numPanels) {
44         this.numPanels = numPanels;
45     }
46
47     public @Nullable List<PositionDatum> getPositionData() {
48         return positionData;
49     }
50
51     public void setPositionData(List<PositionDatum> positionData) {
52         this.positionData = positionData;
53     }
54
55     /**
56      * Returns an text representation for a canvas layout.
57      *
58      * Note only canvas supported currently due to its easy geometry
59      *
60      * @return a String containing the layout
61      */
62     public String getLayoutView() {
63         List<PositionDatum> localPositionData = positionData;
64         if (localPositionData != null) {
65             String view = "";
66
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;
72
73             final int noofDefinedPanels = localPositionData.size();
74
75             /*
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
79              * hardcoded way.
80              */
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());
84
85                 if (panel.getPosX() < minx) {
86                     minx = panel.getPosX();
87                 }
88                 if (panel.getPosX() > maxx) {
89                     maxx = panel.getPosX();
90                 }
91                 if (panel.getPosY() < miny) {
92                     miny = panel.getPosY();
93                 }
94                 if (panel.getPosY() > maxy) {
95                     maxy = panel.getPosY();
96                 }
97                 if (panel.getPanelSize() > sideLength) {
98                     sideLength = panel.getPanelSize();
99                 }
100             }
101
102             int shiftWidth = sideLength / 2;
103
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.";
107             }
108
109             int lineY = maxy;
110             Map<Integer, PositionDatum> map;
111
112             while (lineY >= miny) {
113                 map = new TreeMap<>();
114                 for (int index = 0; index < noofDefinedPanels; index++) {
115
116                     if (localPositionData != null) {
117                         PositionDatum panel = localPositionData.get(index);
118
119                         if (panel.getPosY() == lineY) {
120                             map.put(panel.getPosX(), panel);
121                         }
122                     }
123                 }
124                 lineY -= shiftWidth;
125                 for (int x = minx; x <= maxx; x += shiftWidth) {
126                     if (map.containsKey(x)) {
127                         PositionDatum panel = map.get(x);
128                         if (panel != null) {
129                             int panelId = panel.getPanelId();
130                             view += String.format("%5s ", panelId);
131                         } else {
132                             view += "      ";
133                         }
134                     } else {
135                         view += "      ";
136                     }
137                 }
138                 view += System.lineSeparator();
139             }
140
141             return view;
142         } else {
143             return "";
144         }
145     }
146 }