]> git.basschouten.com Git - openhab-addons.git/blob
bc2fd2c492b4223478c467e8d9690b3cf8c73605
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.doorbird.internal.api;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.doorbird.internal.model.DoorbirdInfoDTO;
21 import org.openhab.binding.doorbird.internal.model.DoorbirdInfoDTO.DoorbirdInfoBha;
22 import org.openhab.binding.doorbird.internal.model.DoorbirdInfoDTO.DoorbirdInfoBha.DoorbirdInfoArray;
23
24 import com.google.gson.JsonSyntaxException;
25
26 /**
27  * The {@link DoorbirdInfo} holds information about the Doorbird.
28  *
29  * @author Mark Hilbush - Initial contribution
30  */
31 @NonNullByDefault
32 public class DoorbirdInfo {
33     private @Nullable String returnCode;
34     private @Nullable String firmwareVersion;
35     private @Nullable String buildNumber;
36     private @Nullable String primaryMacAddress;
37     private @Nullable String wifiMacAddress;
38     private @Nullable String deviceType;
39     private ArrayList<String> relays = new ArrayList<>();
40
41     @SuppressWarnings("null")
42     public DoorbirdInfo(String infoJson) throws JsonSyntaxException {
43         DoorbirdInfoDTO info = DoorbirdAPI.fromJson(infoJson, DoorbirdInfoDTO.class);
44         if (info != null) {
45             DoorbirdInfoBha bha = info.bha;
46             returnCode = bha.returnCode;
47             if (bha.doorbirdInfoArray.length == 1) {
48                 DoorbirdInfoArray doorbirdInfo = bha.doorbirdInfoArray[0];
49                 firmwareVersion = doorbirdInfo.firmwareVersion;
50                 buildNumber = doorbirdInfo.buildNumber;
51                 primaryMacAddress = doorbirdInfo.primaryMacAddress;
52                 wifiMacAddress = doorbirdInfo.wifiMacAddress;
53                 deviceType = doorbirdInfo.deviceType;
54                 relays.addAll(Arrays.asList(doorbirdInfo.relays));
55             }
56         }
57     }
58
59     public @Nullable String getReturnCode() {
60         return returnCode;
61     }
62
63     public @Nullable String getFirmwareVersion() {
64         return firmwareVersion;
65     }
66
67     public @Nullable String getBuildNumber() {
68         return buildNumber;
69     }
70
71     public @Nullable String getPrimaryMacAddress() {
72         return primaryMacAddress;
73     }
74
75     public @Nullable String getWifiMacAddress() {
76         return wifiMacAddress;
77     }
78
79     public @Nullable String getDeviceType() {
80         return deviceType;
81     }
82
83     public @Nullable String getControllerId(@Nullable String configId) {
84         return relays.stream().map(relay -> relay.split("@")).filter(parts -> parts.length == 2).map(parts -> parts[0])
85                 .filter(id -> configId == null || id.equals(configId)).reduce((first, second) -> second).orElse(null);
86     }
87
88     public ArrayList<String> getRelays() {
89         return relays;
90     }
91 }