-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LswLoggerHandler.java
142 lines (130 loc) · 7.28 KB
/
LswLoggerHandler.java
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
* <p>
* See the NOTICE file(s) distributed with this work for additional
* information.
* <p>
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
* <p>
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.lswlogger.internal.protocolv5.lsw3;
import org.eclipse.jdt.annotation.NonNull;
import org.openhab.binding.lswlogger.internal.LoggerThingConfiguration;
import org.openhab.binding.lswlogger.internal.LswLoggerBindingConstants;
import org.openhab.binding.lswlogger.internal.LswLoggerBindingConstants.InitialDataValues;
import org.openhab.binding.lswlogger.internal.protocolv5.AbstractLoggerHandler;
import org.openhab.binding.lswlogger.internal.protocolv5.ResponseDispatcher;
import org.openhab.binding.lswlogger.internal.protocolv5.UnknownResponseHandler;
import org.openhab.binding.lswlogger.internal.protocolv5.states.ConnectingState;
import org.openhab.binding.lswlogger.internal.protocolv5.states.ProtocolState;
import org.openhab.binding.lswlogger.internal.protocolv5.states.ReadingResponseState;
import org.openhab.binding.lswlogger.internal.protocolv5.states.ReconnectingState;
import org.openhab.binding.lswlogger.internal.protocolv5.states.SendingRequestState;
import org.openhab.binding.lswlogger.internal.protocolv5.states.StateBuilder;
import org.openhab.binding.lswlogger.internal.protocolv5.states.StateMachine;
import org.openhab.binding.lswlogger.internal.protocolv5.states.UnrecoverableErrorState;
import org.openhab.binding.lswlogger.internal.protocolv5.states.WaitingForNextSendRequestState;
import org.openhab.binding.lswlogger.internal.protocolv5.states.WaitingToWakeupInverterReconnectingState;
import org.openhab.core.thing.Thing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The {@link LswLoggerHandler} is responsible for handling commands, which are
* sent to one of the channels.
*
* @author Piotr Bojko - Initial contribution
*/
public class LswLoggerHandler extends AbstractLoggerHandler {
private static final Logger logger = LoggerFactory.getLogger(LswLoggerHandler.class);
private static final int FROM_REGISTER = 0x0000;
private static final int TO_REGISTER = 0x0027;
public LswLoggerHandler(Thing thing) {
super(thing);
}
@Override
protected StateMachine<?> createStateMachine() {
return createStateMachine(
new LswLoggerHandlerContext(),
this.getConfigAs(LoggerThingConfiguration.class));
}
private StateMachine<LswLoggerHandlerContext> createStateMachine(
@NonNull LswLoggerHandlerContext lswLoggerHandlerContext,
@NonNull LoggerThingConfiguration configAs) {
ProtocolState<LswLoggerHandlerContext> initial = new ConnectingState<>();
UnrecoverableErrorState<LswLoggerHandlerContext> unrecoverableErrorState = new UnrecoverableErrorState<>();
SendingRequestState<LswLoggerHandlerContext> sendingRequestState = new SendingRequestState<>(
FROM_REGISTER,
TO_REGISTER);
ReadingResponseState<LswLoggerHandlerContext> readingResponseState = new ReadingResponseState<>(
createResponseDispatcher());
WaitingForNextSendRequestState<LswLoggerHandlerContext> cycleWaitingState = new WaitingForNextSendRequestState<>();
ReconnectingState<LswLoggerHandlerContext> reconnectingState = new ReconnectingState<>();
WaitingToWakeupInverterReconnectingState<LswLoggerHandlerContext> waitingToWakeupInverterReconnectingState = new WaitingToWakeupInverterReconnectingState<>();
StateBuilder<LswLoggerHandlerContext> builder = new StateBuilder<>();
builder
.addContext(lswLoggerHandlerContext)
.addConfiguration(configAs)
.setInitial(initial)
.addState(initial,
routes -> routes.addNextRoute(sendingRequestState)
.addErrorRoute(unrecoverableErrorState))
.addState(sendingRequestState,
routes -> routes.addNextRoute(readingResponseState)
.addExceptionRoute(reconnectingState))
.addState(readingResponseState,
routes -> routes.addNextRoute(cycleWaitingState)
.addExceptionRoute(reconnectingState))
.addState(reconnectingState,
routes -> routes.addNextRoute(sendingRequestState)
.addAlternativeRoute(
waitingToWakeupInverterReconnectingState)
.addExceptionRoute(reconnectingState))
.addState(cycleWaitingState, routes -> routes.addNextRoute(sendingRequestState))
.addState(waitingToWakeupInverterReconnectingState,
route -> route.addNextRoute(sendingRequestState)
.addExceptionRoute(
waitingToWakeupInverterReconnectingState)
.addErrorRoute(unrecoverableErrorState));
return builder.build();
}
private ResponseDispatcher createResponseDispatcher() {
return ResponseDispatcher.create(
new DataResponseHandler(this::updateState),
new ExtendedDataResponseHandler(this::updateState),
new UnknownResponseHandler());
}
private class LswLoggerHandlerContext extends AbstractLoggerHandler.AbstractContext {
public LswLoggerHandlerContext() {
super(LswLoggerHandler.this);
}
@Override
public void notifyLoggerIsOffline() {
super.notifyLoggerIsOffline();
updateState(LswLoggerBindingConstants.LSWLoggerV5.gridAVoltageChannel,
InitialDataValues.ZERO_VOLTS);
updateState(LswLoggerBindingConstants.LSWLoggerV5.gridACurrentChannel,
InitialDataValues.ZERO_AMPERES);
updateState(LswLoggerBindingConstants.LSWLoggerV5.gridAPowerChannel,
InitialDataValues.ZERO_WATTS);
updateState(LswLoggerBindingConstants.LSWLoggerV5.gridBVoltageChannel,
InitialDataValues.ZERO_VOLTS);
updateState(LswLoggerBindingConstants.LSWLoggerV5.gridBCurrentChannel,
InitialDataValues.ZERO_AMPERES);
updateState(LswLoggerBindingConstants.LSWLoggerV5.gridBPowerChannel,
InitialDataValues.ZERO_WATTS);
updateState(LswLoggerBindingConstants.LSWLoggerV5.outputActivePowerChannel,
InitialDataValues.ZERO_WATTS);
updateState(LswLoggerBindingConstants.LSWLoggerV5.outputReactivePowerChannel,
InitialDataValues.ZERO_WATTS);
updateState(LswLoggerBindingConstants.LSWLoggerV5.Phase1CurrentChannel,
InitialDataValues.ZERO_AMPERES);
updateState(LswLoggerBindingConstants.LSWLoggerV5.Phase2CurrentChannel,
InitialDataValues.ZERO_AMPERES);
updateState(LswLoggerBindingConstants.LSWLoggerV5.Phase3CurrentChannel,
InitialDataValues.ZERO_AMPERES);
}
}
}