]> git.basschouten.com Git - openhab-addons.git/blob
109ad1efcba150f55027140477b567c3094a1f9d
[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.unifi.internal.api.dto;
14
15 import java.time.Instant;
16
17 import org.openhab.binding.unifi.internal.api.cache.UniFiControllerCache;
18 import org.openhab.binding.unifi.internal.api.util.UniFiTidyLowerCaseStringDeserializer;
19 import org.openhab.binding.unifi.internal.api.util.UniFiTimestampDeserializer;
20
21 import com.google.gson.annotations.JsonAdapter;
22 import com.google.gson.annotations.SerializedName;
23
24 /**
25  * The {@link UniFiClient} is the base data model for any (wired or wireless) connected to a UniFi network.
26  *
27  * @author Matthew Bowman - Initial contribution
28  * @author Patrik Wimnell - Blocking / Unblocking client support
29  */
30 public abstract class UniFiClient implements HasId {
31
32     private final transient UniFiControllerCache cache;
33
34     @SerializedName("_id")
35     private String id;
36
37     private String siteId;
38
39     @JsonAdapter(UniFiTidyLowerCaseStringDeserializer.class)
40     private String mac;
41
42     private String ip;
43
44     @JsonAdapter(UniFiTidyLowerCaseStringDeserializer.class)
45     private String hostname;
46
47     @SerializedName("name")
48     @JsonAdapter(UniFiTidyLowerCaseStringDeserializer.class)
49     private String alias;
50
51     private Integer uptime;
52
53     @JsonAdapter(UniFiTimestampDeserializer.class)
54     private Instant lastSeen;
55
56     private boolean blocked;
57
58     @SerializedName("is_guest")
59     private boolean guest;
60
61     @SerializedName("fixed_ip")
62     private String fixedIp;
63
64     @SerializedName("satisfaction")
65     private Integer experience;
66
67     protected UniFiClient(final UniFiControllerCache cache) {
68         this.cache = cache;
69     }
70
71     @Override
72     public String getId() {
73         return id;
74     }
75
76     public String getMac() {
77         return mac;
78     }
79
80     public String getIp() {
81         return this.ip == null || this.ip.isBlank() ? this.fixedIp : this.ip;
82     }
83
84     public String getHostname() {
85         return hostname;
86     }
87
88     public String getAlias() {
89         return alias;
90     }
91
92     public Integer getUptime() {
93         return uptime;
94     }
95
96     public Instant getLastSeen() {
97         return lastSeen;
98     }
99
100     public boolean isBlocked() {
101         return blocked;
102     }
103
104     public abstract boolean isWired();
105
106     public final boolean isWireless() {
107         return !isWired();
108     }
109
110     protected abstract String getDeviceMac();
111
112     public UniFiSite getSite() {
113         return cache.getSite(siteId);
114     }
115
116     public UniFiDevice getDevice() {
117         return cache.getDevice(getDeviceMac());
118     }
119
120     public boolean isGuest() {
121         return guest;
122     }
123
124     public Integer getExperience() {
125         return experience;
126     }
127
128     @Override
129     public String toString() {
130         return String.format(
131                 "UniFiClient{id: '%s', mac: '%s', ip: '%s', hostname: '%s', alias: '%s', wired: %b, guest: %b, blocked: %b, experience: %d, device: %s}",
132                 id, mac, getIp(), hostname, alias, isWired(), guest, blocked, experience, getDevice());
133     }
134 }