forked from Smarteon/loxone-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockableControlState.java
More file actions
79 lines (71 loc) · 2.48 KB
/
Copy pathLockableControlState.java
File metadata and controls
79 lines (71 loc) · 2.48 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package cz.smarteon.loxone.app.state;
import cz.smarteon.loxone.Codec;
import cz.smarteon.loxone.Loxone;
import cz.smarteon.loxone.app.Control;
import cz.smarteon.loxone.app.state.events.LockedEvent;
import cz.smarteon.loxone.message.TextEvent;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
/**
* Base class for the controlStates in loxone application that supports locking.
* <p>
* This class keeps track of the state of the control based on the events of the miniserver.
* </p>
* @param <T> The type of control this class keeps track of.
*/
@Slf4j
public abstract class LockableControlState<S, T extends Control> extends ControlState<S, T> {
/**
* Current state of the lock.
*/
@Getter
@Nullable
private Locked locked;
/**
* Extra free format reason in case of lock.
*/
@Getter
@Nullable
private String lockedReason;
protected LockableControlState(Loxone loxone, T control) {
super(loxone, control);
}
/**
* Accepts TextEvents that can contain locking state updates for this control.
* @param event text event received (should not be null)
*/
@Override
void accept(@NotNull TextEvent event) {
if (event.getUuid().equals(getControl().stateLocked())) {
processLockedEvent(event);
}
}
/**
* Process the TextEvent as a locked event message and update the state of the control accordingly.
* @param event text event received
*/
private void processLockedEvent(TextEvent event) {
try {
final boolean isCurrentState;
if (event.getText().isEmpty()) {
isCurrentState = Locked.NO.equals(locked) && lockedReason == null;
this.locked = Locked.NO;
this.lockedReason = null;
} else {
final LockedEvent lockedEvent = Codec.readMessage(event.getText(), LockedEvent.class);
isCurrentState = lockedEvent.getLocked().equals(locked) && lockedEvent.getReason().equals(lockedReason);
this.locked = lockedEvent.getLocked();
this.lockedReason = lockedEvent.getReason();
}
if (!isCurrentState) {
notifyStateChanged();
}
} catch (IOException e) {
log.info("Unable to parse locked event!", e);
}
}
}