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