-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSyncCommandGuard.java
More file actions
56 lines (45 loc) · 1.81 KB
/
SyncCommandGuard.java
File metadata and controls
56 lines (45 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cz.smarteon.loxone;
import cz.smarteon.loxone.message.LoxoneMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
class SyncCommandGuard<T> {
private static final Logger LOG = LoggerFactory.getLogger(SyncCommandGuard.class);
private final CountDownLatch latch;
private final Command<T> command;
private Object response;
SyncCommandGuard(final Command<T> command) {
this.command = command;
latch = new CountDownLatch(1);
}
@SuppressWarnings("unchecked")
T waitForResponse(int seconds) {
try {
if (latch.await(seconds, TimeUnit.SECONDS)) {
try {
return (T) response;
} catch (ClassCastException cce) {
if (response instanceof LoxoneMessage<?>) {
LoxoneMessage<?> error = (LoxoneMessage<?>) response;
throw new LoxoneException("Error received of " + error.getControl() + " code " + error.getCode());
} else {
throw new LoxoneException("Unrecognizable error received to " + command.getCommand());
}
}
} else {
throw new LoxoneException("Timeout waiting for sync command response " + command.getCommand());
}
} catch (InterruptedException e) {
LOG.error("Interrupted while waiting for sync command request completion", e);
throw new LoxoneException("Interrupted while waiting for sync command request completion");
}
}
void receive(final Object response) {
this.response = response;
latch.countDown();
}
Command<T> getCommand() {
return command;
}
}