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