2 * Copyright (c) 2010-2024 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.freeboxos.internal.api.rest;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.BINDING_ID;
17 import java.util.List;
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;
27 import com.google.gson.annotations.SerializedName;
30 * The {@link HomeManager} is the Java class used to handle api requests related to home
32 * @author ben12 - Initial contribution
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";
40 private static class EndpointStateResponse extends Response<EndpointState> {
43 private static class HomeNodesResponse extends Response<HomeNode> {
46 private enum AccessType {
53 private enum DisplayType {
64 private static record EndpointValue<T> (T value) {
67 private static record EndpointUi(AccessType access, DisplayType display, String iconUrl, @Nullable String unit) {
70 private enum ValueType {
79 public static record EndpointState(@Nullable String value, ValueType valueType, long refresh) {
80 public boolean asBoolean() {
82 return local != null ? Boolean.valueOf(local) : false;
87 return local != null ? Integer.valueOf(local) : Integer.MIN_VALUE;
90 public @Nullable String value() {
100 public String asConfId() {
101 return name().toLowerCase();
105 private static record LogEntry(long timestamp, int value) {
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 {
118 private enum Status {
126 public enum Category {
134 private final ThingTypeUID thingTypeUID;
137 thingTypeUID = new ThingTypeUID(BINDING_ID, name().toLowerCase());
140 public ThingTypeUID getThingTypeUID() {
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) {
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) {
153 public HomeManager(FreeboxOsSession session) throws FreeboxException {
154 super(session, LoginManager.Permission.HOME, session.getUriBuilder().path(PATH));
157 public List<HomeNode> getHomeNodes() throws FreeboxException {
158 return get(HomeNodesResponse.class, NODES_PATH);
161 public HomeNode getHomeNode(int nodeId) throws FreeboxException {
162 return getSingle(HomeNodesResponse.class, NODES_PATH, Integer.toString(nodeId));
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));
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));