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