Skip to content

Commit ea14c5d

Browse files
committed
Allow characteristic setters to get the username of the triggering connection
1 parent 1b20ab0 commit ea14c5d

16 files changed

+101
-17
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# HAP-Java 2.0.7
2+
* Add overloads to characteristics so that the username can be passed through.
3+
14
# HAP-Java 2.0.6
25
* Several methods allowing library users to manipulate characteristics themselves
36
* Allow library users to provider their own implementation of AccessoryInformationService

src/main/java/io/github/hapjava/characteristics/Characteristic.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ public interface Characteristic {
4444
* @param jsonValue the JSON serialized value to set.
4545
*/
4646
void setValue(JsonValue jsonValue);
47+
48+
/**
49+
* Invoked by the remote client, this updates the current value of the characteristic.
50+
*
51+
* @param jsonValue the JSON serialized value to set.
52+
* @param username the authenticated username making the request
53+
*/
54+
default void setValue(JsonValue jsonValue, String username) {
55+
setValue(jsonValue);
56+
}
4757
}

src/main/java/io/github/hapjava/characteristics/ExceptionalConsumer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33
public interface ExceptionalConsumer<T> {
44
void accept(T t) throws Exception;
5+
6+
default void accept(T t, String username) throws Exception {
7+
accept(t);
8+
}
59
}

src/main/java/io/github/hapjava/characteristics/impl/base/BaseCharacteristic.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,20 @@ protected CompletableFuture<JsonObjectBuilder> makeBuilder(int instanceId) {
128128
/** {@inheritDoc} */
129129
@Override
130130
public final void setValue(JsonValue jsonValue) {
131+
setValue(jsonValue, null);
132+
}
133+
134+
/** {@inheritDoc} */
135+
@Override
136+
public final void setValue(JsonValue jsonValue, String username) {
131137
try {
132-
setValue(convert(jsonValue));
138+
setValue(convert(jsonValue), username);
133139
} catch (Exception e) {
134140
logger.warn(
135-
"Error while setting JSON value {} for characteristic {}",
141+
"Error while setting JSON value {} for characteristic {} from user {}",
136142
jsonValue,
137143
getClass().getName(),
144+
username,
138145
e);
139146
}
140147
}
@@ -185,6 +192,17 @@ public void unsubscribe() {
185192
*/
186193
public abstract void setValue(T value) throws Exception;
187194

195+
/**
196+
* Update the characteristic value using a new value supplied by the connected client.
197+
*
198+
* @param value the new value to set.
199+
* @param username the authenticated username making the request
200+
* @throws Exception if the value cannot be set.
201+
*/
202+
public void setValue(T value, String username) throws Exception {
203+
setValue(value);
204+
}
205+
188206
/**
189207
* Retrieves the current value of the characteristic.
190208
*

src/main/java/io/github/hapjava/characteristics/impl/base/BooleanCharacteristic.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ public CompletableFuture<Boolean> getValue() {
6666

6767
@Override
6868
public void setValue(Boolean value) throws Exception {
69-
if (setter.isPresent()) setter.get().accept(value);
69+
setValue(value, null);
70+
}
71+
72+
@Override
73+
public void setValue(Boolean value, String username) throws Exception {
74+
if (setter.isPresent()) setter.get().accept(value, username);
7075
}
7176

7277
/** {@inheritDoc} */

src/main/java/io/github/hapjava/characteristics/impl/base/EnumCharacteristic.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,24 @@ public CompletableFuture<Integer> getValue() {
113113
}
114114

115115
public void setValue(T value) throws Exception {
116+
setValue(value, null);
117+
}
118+
119+
public void setValue(T value, String username) throws Exception {
116120
if (!setter.isPresent()) {
117121
return;
118122
}
119123

120-
setter.get().accept(value);
124+
setter.get().accept(value, username);
121125
}
122126

123127
@Override
124128
public void setValue(Integer value) throws Exception {
129+
setValue(value, null);
130+
}
131+
132+
@Override
133+
public void setValue(Integer value, String username) throws Exception {
125134
if (!setter.isPresent()) {
126135
return;
127136
}
@@ -130,7 +139,7 @@ public void setValue(Integer value) throws Exception {
130139
if (validValues != null && value != null) {
131140
for (T valid : validValues) {
132141
if (valid.getCode() == value) {
133-
setValue(valid);
142+
setValue(valid, username);
134143
return;
135144
}
136145
}

src/main/java/io/github/hapjava/characteristics/impl/base/FloatCharacteristic.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ public final CompletableFuture<Double> getValue() {
130130

131131
@Override
132132
public void setValue(Double value) throws Exception {
133-
if (setter.isPresent()) setter.get().accept(value);
133+
setValue(value, null);
134+
}
135+
136+
@Override
137+
public void setValue(Double value, String username) throws Exception {
138+
if (setter.isPresent()) setter.get().accept(value, username);
134139
}
135140

136141
/** {@inheritDoc} */

src/main/java/io/github/hapjava/characteristics/impl/base/IntegerCharacteristic.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ public CompletableFuture<Integer> getValue() {
7777

7878
@Override
7979
public void setValue(Integer value) throws Exception {
80-
if (setter.isPresent()) setter.get().accept(value);
80+
setValue(value, null);
81+
}
82+
83+
@Override
84+
public void setValue(Integer value, String username) throws Exception {
85+
if (setter.isPresent()) setter.get().accept(value, username);
8186
}
8287

8388
/** {@inheritDoc} */

src/main/java/io/github/hapjava/characteristics/impl/base/StringCharacteristic.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ public String convert(JsonValue jsonValue) {
6363
/** {@inheritDoc} */
6464
@Override
6565
public void setValue(String value) throws Exception {
66-
if (setter.isPresent()) setter.get().accept(value);
66+
setValue(value, null);
67+
}
68+
69+
/** {@inheritDoc} */
70+
@Override
71+
public void setValue(String value, String username) throws Exception {
72+
if (setter.isPresent()) setter.get().accept(value, username);
6773
}
6874

6975
/** {@inheritDoc} */

src/main/java/io/github/hapjava/server/impl/HomekitServer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,15 @@ public HomekitStandaloneAccessoryServer createStandaloneAccessory(
117117
return new HomekitStandaloneAccessoryServer(accessory, http, localAddress, authInfo);
118118
}
119119
}
120+
120121
public HomekitStandaloneAccessoryServer createStandaloneAccessory(
121122
HomekitAuthInfo authInfo, HomekitAccessory accessory, int category)
122123
throws IOException, ExecutionException, InterruptedException {
123124
if (jmdns != null) {
124125
return new HomekitStandaloneAccessoryServer(accessory, http, jmdns, authInfo, category);
125126
} else {
126-
return new HomekitStandaloneAccessoryServer(accessory, http, localAddress, authInfo, category);
127+
return new HomekitStandaloneAccessoryServer(
128+
accessory, http, localAddress, authInfo, category);
127129
}
128130
}
129131

0 commit comments

Comments
 (0)