2 * Copyright (c) 2010-2021 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.revogi.internal.api;
15 import java.util.List;
16 import java.util.Objects;
17 import java.util.concurrent.CompletableFuture;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.jetbrains.annotations.NotNull;
21 import org.openhab.binding.revogi.internal.udp.UdpResponseDTO;
22 import org.openhab.binding.revogi.internal.udp.UdpSenderService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonSyntaxException;
31 * The {@link StatusService} contains methods to get a status of a Revogi SmartStrip
33 * @author Andi Bräu - Initial contribution
36 public class StatusService {
38 private static final String UDP_DISCOVERY_QUERY = "V3{\"sn\":\"%s\", \"cmd\": 90}";
39 public static final String VERSION_STRING = "V3";
40 private final Logger logger = LoggerFactory.getLogger(StatusService.class);
42 private final Gson gson = new GsonBuilder().create();
43 private final UdpSenderService udpSenderService;
45 public StatusService(UdpSenderService udpSenderService) {
46 this.udpSenderService = udpSenderService;
49 public CompletableFuture<StatusDTO> queryStatus(String serialNumber, String ipAddress) {
50 CompletableFuture<List<UdpResponseDTO>> responses;
51 if (ipAddress.trim().isEmpty()) {
52 responses = udpSenderService.broadcastUdpDatagram(String.format(UDP_DISCOVERY_QUERY, serialNumber));
54 responses = udpSenderService.sendMessage(String.format(UDP_DISCOVERY_QUERY, serialNumber), ipAddress);
56 return responses.thenApply(this::getStatus);
60 private StatusDTO getStatus(final List<UdpResponseDTO> singleResponse) {
61 return singleResponse.stream()
62 .filter(response -> !response.getAnswer().isEmpty() && response.getAnswer().contains(VERSION_STRING))
63 .map(response -> deserializeString(response.getAnswer()))
64 .filter(statusRaw -> statusRaw.getCode() == 200 && statusRaw.getResponse() == 90)
65 .map(statusRaw -> new StatusDTO(true, statusRaw.getCode(), statusRaw.getData().getSwitchValue(),
66 statusRaw.getData().getWatt(), statusRaw.getData().getAmp()))
67 .findFirst().orElse(new StatusDTO(false, 503, null, null, null));
70 private StatusRawDTO deserializeString(String response) {
71 String extractedJsonResponse = response.substring(response.lastIndexOf(VERSION_STRING) + 2);
73 return Objects.requireNonNull(gson.fromJson(extractedJsonResponse, StatusRawDTO.class));
74 } catch (JsonSyntaxException e) {
75 logger.warn("Could not parse string \"{}\" to StatusRaw", response, e);
76 return new StatusRawDTO(503, 0, new StatusDTO(false, 503, null, null, null));