]> git.basschouten.com Git - openhab-addons.git/blob
2a5b4f2eb58e1968a161b6d9aff73f6993406155
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.lutron.internal.protocol.leap;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.lutron.internal.protocol.FanSpeedType;
17
18 /**
19  * Contains static methods for constructing LEAP messages
20  *
21  * @author Bob Adair - Initial contribution
22  */
23 @NonNullByDefault
24 public class Request {
25     public static final String BUTTON_GROUP_URL = "/buttongroup";
26
27     public static String goToLevel(int zone, int value) {
28         String request = """
29                 {"CommuniqueType": "CreateRequest",\
30                 "Header": {"Url": "/zone/%d/commandprocessor"},\
31                 "Body": {\
32                 "Command": {\
33                 "CommandType": "GoToLevel",\
34                 "Parameter": [{"Type": "Level", "Value": %d}]}}}\
35                 """;
36         return String.format(request, zone, value);
37     }
38
39     /** fadeTime must be in the format hh:mm:ss **/
40     public static String goToDimmedLevel(int zone, int value, String fadeTime) {
41         String request = """
42                 {"CommuniqueType": "CreateRequest",\
43                 "Header": {"Url": "/zone/%d/commandprocessor"},"Body": {"Command": {\
44                 "CommandType": "GoToDimmedLevel",\
45                 "DimmedLevelParameters": {"Level": %d, "FadeTime": "%s"}}}}\
46                 """;
47         return String.format(request, zone, value, fadeTime);
48     }
49
50     /** fadeTime and delayTime must be in the format hh:mm:ss **/
51     public static String goToDimmedLevel(int zone, int value, String fadeTime, String delayTime) {
52         String request = """
53                 {"CommuniqueType": "CreateRequest",\
54                 "Header": {"Url": "/zone/%d/commandprocessor"},"Body": {"Command": {\
55                 "CommandType": "GoToDimmedLevel",\
56                 "DimmedLevelParameters": {"Level": %d, "FadeTime": "%s", "DelayTime": "%s"}}}}\
57                 """;
58         return String.format(request, zone, value, fadeTime, delayTime);
59     }
60
61     public static String goToFanSpeed(int zone, FanSpeedType fanSpeed) {
62         String request = """
63                 {"CommuniqueType": "CreateRequest",\
64                 "Header": {"Url": "/zone/%d/commandprocessor"},\
65                 "Body": {\
66                 "Command": {"CommandType": "GoToFanSpeed",\
67                 "FanSpeedParameters": {"FanSpeed": "%s"}}}}\
68                 """;
69         return String.format(request, zone, fanSpeed.leapValue());
70     }
71
72     public static String buttonCommand(int button, CommandType command) {
73         String request = """
74                 {"CommuniqueType": "CreateRequest",\
75                 "Header": {"Url": "/button/%d/commandprocessor"},\
76                 "Body": {"Command": {"CommandType": "%s"}}}\
77                 """;
78         return String.format(request, button, command.toString());
79     }
80
81     public static String virtualButtonCommand(int virtualbutton, CommandType command) {
82         String request = """
83                 {"CommuniqueType": "CreateRequest",\
84                 "Header": {"Url": "/virtualbutton/%d/commandprocessor"},\
85                 "Body": {"Command": {"CommandType": "%s"}}}\
86                 """;
87         return String.format(request, virtualbutton, command.toString());
88     }
89
90     public static String zoneCommand(int zone, CommandType commandType) {
91         String request = """
92                 {"CommuniqueType": "CreateRequest",\
93                 "Header": {"Url": "/zone/%d/commandprocessor"},\
94                 "Body": {\
95                 "Command": {\
96                 "CommandType": "%s"}}}\
97                 """;
98         return String.format(request, zone, commandType.toString());
99     }
100
101     public static String request(CommuniqueType cType, String url) {
102         String request = "{\"CommuniqueType\": \"%s\",\"Header\": {\"Url\": \"%s\"}}";
103         return String.format(request, cType.toString(), url);
104     }
105
106     public static String ping() {
107         return request(CommuniqueType.READREQUEST, "/server/1/status/ping");
108     }
109
110     public static String getDevices() {
111         return getDevices("");
112     }
113
114     public static String getDevices(boolean thisDevice) {
115         String url = String.format("where=IsThisDevice:%s", (thisDevice) ? "true" : "false");
116
117         return getDevices(url);
118     }
119
120     public static String getDevices(String predicate) {
121         String url = "/device";
122         if (!predicate.isEmpty()) {
123             url = String.format("%s?%s", url, predicate);
124         }
125
126         return request(CommuniqueType.READREQUEST, url);
127     }
128
129     public static String getVirtualButtons() {
130         return request(CommuniqueType.READREQUEST, "/virtualbutton");
131     }
132
133     public static String getButtonGroups() {
134         return request(CommuniqueType.READREQUEST, BUTTON_GROUP_URL);
135     }
136
137     public static String getProject() {
138         return request(CommuniqueType.READREQUEST, "/project");
139     }
140
141     public static String getAreas() {
142         return request(CommuniqueType.READREQUEST, "/area");
143     }
144
145     public static String getOccupancyGroups() {
146         return request(CommuniqueType.READREQUEST, "/occupancygroup");
147     }
148
149     public static String getZoneStatus(int zone) {
150         return request(CommuniqueType.READREQUEST, String.format("/zone/%d/status", zone));
151     }
152
153     public static String getOccupancyGroupStatus() {
154         return request(CommuniqueType.READREQUEST, "/occupancygroup/status");
155     }
156
157     public static String subscribeOccupancyGroupStatus() {
158         return request(CommuniqueType.SUBSCRIBEREQUEST, "/occupancygroup/status");
159     }
160
161     public static String subscribeZoneStatus() {
162         return request(CommuniqueType.SUBSCRIBEREQUEST, "/zone/status");
163     }
164 }