]> git.basschouten.com Git - openhab-addons.git/blob
2b9aff1f91632c756c0489df2ab1f29a954f0c10
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.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     private StatusDTO getStatus(final List<UdpResponseDTO> singleResponse) {
59         return singleResponse.stream()
60                 .filter(response -> !response.getAnswer().isEmpty() && response.getAnswer().contains(VERSION_STRING))
61                 .map(response -> deserializeString(response.getAnswer()))
62                 .filter(statusRaw -> statusRaw.getCode() == 200 && statusRaw.getResponse() == 90)
63                 .map(statusRaw -> new StatusDTO(true, statusRaw.getCode(), statusRaw.getData().getSwitchValue(),
64                         statusRaw.getData().getWatt(), statusRaw.getData().getAmp()))
65                 .findFirst().orElse(new StatusDTO(false, 503, null, null, null));
66     }
67
68     private StatusRawDTO deserializeString(String response) {
69         String extractedJsonResponse = response.substring(response.lastIndexOf(VERSION_STRING) + 2);
70         try {
71             return Objects.requireNonNull(gson.fromJson(extractedJsonResponse, StatusRawDTO.class));
72         } catch (JsonSyntaxException e) {
73             logger.warn("Could not parse string \"{}\" to StatusRaw", response, e);
74             return new StatusRawDTO(503, 0, new StatusDTO(false, 503, null, null, null));
75         }
76     }
77 }