2 * Copyright (c) 2010-2023 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.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;
26 import com.google.gson.annotations.SerializedName;
29 * The {@link HomeManager} is the Java class used to handle api requests related to home
31 * @author ben12 - Initial contribution
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";
39 private static class EndpointStateResponse extends Response<EndpointState> {
42 private static class HomeNodesResponse extends Response<HomeNode> {
45 private static enum AccessType {
52 private static enum DisplayType {
63 private static record EndpointValue<T>(T value) {
66 private static record EndpointUi(AccessType access, DisplayType display, String iconUrl, @Nullable String unit) {
69 private static enum ValueType {
78 public static record EndpointState(@Nullable String value, ValueType valueType, long refresh) {
79 public boolean asBoolean() {
81 return local != null ? Boolean.valueOf(local) : false;
86 return local != null ? Integer.valueOf(local) : Integer.MIN_VALUE;
89 public @Nullable String value() {
94 public static enum EpType {
99 public String asConfId() {
100 return name().toLowerCase();
104 private static record LogEntry(long timestamp, int value) {
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 {
117 private static enum Status {
125 public static enum Category {
133 private final ThingTypeUID thingTypeUID;
136 thingTypeUID = new ThingTypeUID(BINDING_ID, name().toLowerCase());
139 public ThingTypeUID getThingTypeUID() {
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) {
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) {
152 public HomeManager(FreeboxOsSession session) throws FreeboxException {
153 super(session, LoginManager.Permission.HOME, session.getUriBuilder().path(PATH));
156 public List<HomeNode> getHomeNodes() throws FreeboxException {
157 return get(HomeNodesResponse.class, NODES_PATH);
160 public HomeNode getHomeNode(int nodeId) throws FreeboxException {
161 return getSingle(HomeNodesResponse.class, NODES_PATH, Integer.toString(nodeId));
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));
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));