]> git.basschouten.com Git - openhab-addons.git/blob
fbb64ecaf6dc24e9808296f28433e797f9674f2d
[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.freeboxos.internal.api.rest;
14
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.BINDING_ID;
16
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
23 import org.openhab.binding.freeboxos.internal.api.Response;
24 import org.openhab.core.thing.ThingTypeUID;
25
26 import com.google.gson.annotations.SerializedName;
27
28 /**
29  * The {@link HomeManager} is the Java class used to handle api requests related to home
30  *
31  * @author ben12 - Initial contribution
32  */
33 @NonNullByDefault
34 public class HomeManager extends RestManager {
35     private static final String PATH = "home";
36     private static final String NODES_PATH = "nodes";
37     private static final String ENDPOINTS_PATH = "endpoints";
38
39     private static class EndpointStateResponse extends Response<EndpointState> {
40     }
41
42     private static class HomeNodesResponse extends Response<HomeNode> {
43     }
44
45     private static enum AccessType {
46         R,
47         W,
48         RW,
49         UNKNOWN;
50     }
51
52     private static enum DisplayType {
53         TEXT,
54         ICON,
55         BUTTON,
56         SLIDER,
57         TOGGLE,
58         COLOR,
59         WARNING,
60         UNKNOWN;
61     }
62
63     private static record EndpointValue<T> (T value) {
64     }
65
66     private static record EndpointUi(AccessType access, DisplayType display, String iconUrl, @Nullable String unit) {
67     }
68
69     private static enum ValueType {
70         BOOL,
71         INT,
72         FLOAT,
73         VOID,
74         STRING,
75         UNKNOWN;
76     }
77
78     public static record EndpointState(@Nullable String value, ValueType valueType, long refresh) {
79         public boolean asBoolean() {
80             String local = value;
81             return local != null ? Boolean.valueOf(local) : false;
82         }
83
84         public int asInt() {
85             String local = value;
86             return local != null ? Integer.valueOf(local) : Integer.MIN_VALUE;
87         }
88
89         public @Nullable String value() {
90             return value;
91         }
92     }
93
94     public static enum EpType {
95         SIGNAL,
96         SLOT,
97         UNKNOWN;
98
99         public String asConfId() {
100             return name().toLowerCase();
101         }
102     }
103
104     private static record LogEntry(long timestamp, int value) {
105     }
106
107     public static record Endpoint(int id, String name, String label, EpType epType, Visibility visibility, int refresh,
108             ValueType valueType, EndpointUi ui, @Nullable String category, Object value, List<LogEntry> history) {
109         private static enum Visibility {
110             INTERNAL,
111             NORMAL,
112             DASHBOARD,
113             UNKNOWN;
114         }
115     }
116
117     private static enum Status {
118         UNREACHABLE,
119         DISABLED,
120         ACTIVE,
121         UNPAIRED,
122         UNKNOWN;
123     }
124
125     public static enum Category {
126         BASIC_SHUTTER,
127         SHUTTER,
128         ALARM,
129         KFB,
130         CAMERA,
131         UNKNOWN;
132
133         private final ThingTypeUID thingTypeUID;
134
135         Category() {
136             thingTypeUID = new ThingTypeUID(BINDING_ID, name().toLowerCase());
137         }
138
139         public ThingTypeUID getThingTypeUID() {
140             return thingTypeUID;
141         }
142     }
143
144     public static record NodeType(@SerializedName("abstract") boolean _abstract, List<Endpoint> endpoints,
145             boolean generic, String icon, String inherit, String label, String name, boolean physical) {
146     }
147
148     public static record HomeNode(int id, @Nullable String name, @Nullable String label, Category category,
149             Status status, List<Endpoint> showEndpoints, Map<String, String> props, NodeType type) {
150     }
151
152     public HomeManager(FreeboxOsSession session) throws FreeboxException {
153         super(session, LoginManager.Permission.HOME, session.getUriBuilder().path(PATH));
154     }
155
156     public List<HomeNode> getHomeNodes() throws FreeboxException {
157         return get(HomeNodesResponse.class, NODES_PATH);
158     }
159
160     public HomeNode getHomeNode(int nodeId) throws FreeboxException {
161         return getSingle(HomeNodesResponse.class, NODES_PATH, Integer.toString(nodeId));
162     }
163
164     public <T> @Nullable EndpointState getEndpointsState(int nodeId, int stateSignalId) throws FreeboxException {
165         return getSingle(EndpointStateResponse.class, ENDPOINTS_PATH, String.valueOf(nodeId),
166                 String.valueOf(stateSignalId));
167     }
168
169     public <T> boolean putCommand(int nodeId, int stateSignalId, T value) throws FreeboxException {
170         put(new EndpointValue<T>(value), ENDPOINTS_PATH, String.valueOf(nodeId), String.valueOf(stateSignalId));
171         return true;
172     }
173 }