]> git.basschouten.com Git - openhab-addons.git/blob
1c77a557243bfa07d341d395c3541757f428009f
[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     private String hostname;
45
46     private String name;
47
48     private Integer uptime;
49
50     @JsonAdapter(UniFiTimestampDeserializer.class)
51     private Instant lastSeen;
52
53     private boolean blocked;
54
55     @SerializedName("is_guest")
56     private boolean guest;
57
58     @SerializedName("fixed_ip")
59     private String fixedIp;
60
61     @SerializedName("satisfaction")
62     private Integer experience;
63
64     protected UniFiClient(final UniFiControllerCache cache) {
65         this.cache = cache;
66     }
67
68     @Override
69     public String getId() {
70         return id;
71     }
72
73     public String getMac() {
74         return mac;
75     }
76
77     public String getIp() {
78         return this.ip == null || this.ip.isBlank() ? this.fixedIp : this.ip;
79     }
80
81     public String getHostname() {
82         return hostname;
83     }
84
85     public String getName() {
86         return name;
87     }
88
89     public Integer getUptime() {
90         return uptime;
91     }
92
93     public Instant getLastSeen() {
94         return lastSeen;
95     }
96
97     public boolean isBlocked() {
98         return blocked;
99     }
100
101     public abstract boolean isWired();
102
103     public final boolean isWireless() {
104         return !isWired();
105     }
106
107     protected abstract String getDeviceMac();
108
109     public UniFiSite getSite() {
110         return cache.getSite(siteId);
111     }
112
113     public UniFiDevice getDevice() {
114         return cache.getDevice(getDeviceMac());
115     }
116
117     public boolean isGuest() {
118         return guest;
119     }
120
121     public Integer getExperience() {
122         return experience;
123     }
124
125     @Override
126     public String toString() {
127         return String.format(
128                 "UniFiClient{id: '%s', mac: '%s', ip: '%s', hostname: '%s', name: '%s', wired: %b, guest: %b, blocked: %b, experience: %d, device: %s}",
129                 id, mac, getIp(), hostname, name, isWired(), guest, blocked, experience, getDevice());
130     }
131 }