]> git.basschouten.com Git - openhab-addons.git/blob
85a819496411ab7a2493f8f190fb840a592140e3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.model;
14
15 import java.util.Calendar;
16
17 import org.openhab.binding.unifi.internal.api.UniFiException;
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 {
31
32     protected final transient UniFiController controller;
33
34     @SerializedName("_id")
35     protected String id;
36
37     protected String siteId;
38
39     @JsonAdapter(UniFiTidyLowerCaseStringDeserializer.class)
40     protected String mac;
41
42     protected String ip;
43
44     @JsonAdapter(UniFiTidyLowerCaseStringDeserializer.class)
45     protected String hostname;
46
47     @SerializedName("name")
48     @JsonAdapter(UniFiTidyLowerCaseStringDeserializer.class)
49     protected String alias;
50
51     protected Integer uptime;
52
53     @JsonAdapter(UniFiTimestampDeserializer.class)
54     protected Calendar lastSeen;
55
56     protected boolean blocked;
57
58     protected UniFiClient(UniFiController controller) {
59         this.controller = controller;
60     }
61
62     public String getId() {
63         return id;
64     }
65
66     public String getMac() {
67         return mac;
68     }
69
70     public String getIp() {
71         return this.ip;
72     }
73
74     public String getHostname() {
75         return hostname;
76     }
77
78     public String getAlias() {
79         return alias;
80     }
81
82     public Integer getUptime() {
83         return uptime;
84     }
85
86     public Calendar getLastSeen() {
87         return lastSeen;
88     }
89
90     public boolean isBlocked() {
91         return blocked;
92     }
93
94     public abstract Boolean isWired();
95
96     public final Boolean isWireless() {
97         return isWired() == null ? null : (isWired().booleanValue() ? Boolean.FALSE : Boolean.TRUE);
98     }
99
100     protected abstract String getDeviceMac();
101
102     public UniFiSite getSite() {
103         return controller.getSite(siteId);
104     }
105
106     public UniFiDevice getDevice() {
107         return controller.getDevice(getDeviceMac());
108     }
109
110     // Functional API
111
112     public void block(boolean blocked) throws UniFiException {
113         controller.block(this, blocked);
114     }
115
116     public void reconnect() throws UniFiException {
117         controller.reconnect(this);
118     }
119
120     @Override
121     public String toString() {
122         return String.format(
123                 "UniFiClient{id: '%s', mac: '%s', ip: '%s', hostname: '%s', alias: '%s', wired: %b, blocked: %b, device: %s}",
124                 id, mac, ip, hostname, alias, isWired(), blocked, getDevice());
125     }
126 }