]> git.basschouten.com Git - openhab-addons.git/blob
d66bdffa31018f40aa4e2e13983986fdf1c1b46a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.revogi.internal.api;
14
15 import java.util.List;
16 import java.util.Objects;
17 import java.util.concurrent.CompletableFuture;
18
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;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonSyntaxException;
29
30 /**
31  * The {@link StatusService} contains methods to get a status of a Revogi SmartStrip
32  *
33  * @author Andi Bräu - Initial contribution
34  */
35 @NonNullByDefault
36 public class StatusService {
37
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);
41
42     private final Gson gson = new GsonBuilder().create();
43     private final UdpSenderService udpSenderService;
44
45     public StatusService(UdpSenderService udpSenderService) {
46         this.udpSenderService = udpSenderService;
47     }
48
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));
53         } else {
54             responses = udpSenderService.sendMessage(String.format(UDP_DISCOVERY_QUERY, serialNumber), ipAddress);
55         }
56         return responses.thenApply(this::getStatus);
57     }
58
59     @NotNull
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));
68     }
69
70     private StatusRawDTO deserializeString(String response) {
71         String extractedJsonResponse = response.substring(response.lastIndexOf(VERSION_STRING) + 2);
72         try {
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));
77         }
78     }
79 }