This repository has been archived by the owner on May 17, 2021. It is now read-only.
/
ExpireGenericBindingProvider.java
203 lines (174 loc) · 7.16 KB
/
ExpireGenericBindingProvider.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* 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
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.expire.internal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.openhab.binding.expire.ExpireBindingProvider;
import org.openhab.core.binding.BindingConfig;
import org.openhab.core.items.Item;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.TypeParser;
import org.openhab.core.types.UnDefType;
import org.openhab.model.item.binding.AbstractGenericBindingProvider;
import org.openhab.model.item.binding.BindingConfigParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class is responsible for parsing the binding configuration.
*
* @author Michael Wyraz -- initial implementation
* @author John Cocula -- added command support, tweaks
* @since 1.9.0
*/
public class ExpireGenericBindingProvider extends AbstractGenericBindingProvider implements ExpireBindingProvider {
private static final Logger logger = LoggerFactory.getLogger(ExpireGenericBindingProvider.class);
/**
* {@inheritDoc}
*/
@Override
public String getBindingType() {
return "expire";
}
/**
* {@inheritDoc}
*/
@Override
public void validateItemType(Item item, String bindingConfig) throws BindingConfigParseException {
// valid for any item type
}
/**
* {@inheritDoc}
*/
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig)
throws BindingConfigParseException {
ExpireBindingConfig config = new ExpireBindingConfig(item, bindingConfig);
logger.debug("Processed binding config for item {}: {}", item.getName(), config);
super.processBindingConfiguration(context, item, bindingConfig);
addBindingConfig(item, config);
}
@Override
public long getDuration(String itemName) {
return getItemConfig(itemName).duration;
}
@Override
public String getDurationString(String itemName) {
return getItemConfig(itemName).durationString;
}
@Override
public State getExpireState(String itemName) {
return getItemConfig(itemName).expireState;
}
@Override
public Command getExpireCommand(String itemName) {
return getItemConfig(itemName).expireCommand;
}
private ExpireBindingConfig getItemConfig(String itemName) {
return (ExpireBindingConfig) bindingConfigs.get(itemName);
}
/**
* This is a helper class holding binding specific configuration details
*
* @author Michael Wyraz
* @since 1.9.0
*/
private static class ExpireBindingConfig implements BindingConfig {
protected static final Pattern durationPattern = Pattern
.compile("(?:([0-9]+)H)?\\s*(?:([0-9]+)M)?\\s*(?:([0-9]+)S)?", Pattern.CASE_INSENSITIVE);
protected static final String COMMAND_PREFIX = "command=";
protected static final String STATE_PREFIX = "state=";
/**
* Human readable textual representation of duration (e.g. "13h 42m 12s")
*/
protected String durationString;
/**
* duration in ms
*/
protected long duration;
/**
* state to post if item is expired.
*/
protected State expireState;
/**
* command to post if item is expired.
*/
protected Command expireCommand;
private long parseDuration(String duration) throws BindingConfigParseException {
Matcher m = durationPattern.matcher(duration);
if (!m.matches() || (m.group(1) == null && m.group(2) == null && m.group(3) == null)) {
throw new BindingConfigParseException(
"Invalid duration: " + duration + ". Expected something like: '1h 15m 30s'");
}
long ms = 0;
if (m.group(1) != null) {
ms += Long.parseLong(m.group(1)) * 60 * 60 * 1000;
}
if (m.group(2) != null) {
ms += Long.parseLong(m.group(2)) * 60 * 1000;
}
if (m.group(3) != null) {
ms += Long.parseLong(m.group(3)) * 1000;
}
return ms;
}
/**
* Construct an ExpireBindingConfig from the {@code bindingConfig} string.
*
* Valid syntax:
*
* {@code <duration>[,[state=|command=|]<stateOrCommand>]}<br>
* if neither state= or command= is present, assume state
*
* @param item the item to which we are bound
* @param bindingConfig the string that the user specified in the .items file
* @throws BindingConfigParseException if it is ill-formatted
*/
ExpireBindingConfig(Item item, String bindingConfig) throws BindingConfigParseException {
int commaPos = bindingConfig.indexOf(',');
durationString = (commaPos >= 0) ? bindingConfig.substring(0, commaPos).trim() : bindingConfig.trim();
duration = parseDuration(durationString);
String stateOrCommand = ((commaPos >= 0) && (bindingConfig.length() - 1) > commaPos)
? bindingConfig.substring(commaPos + 1).trim() : null;
if ((stateOrCommand != null) && (stateOrCommand.length() > 0)) {
if (stateOrCommand.startsWith(COMMAND_PREFIX)) {
String commandString = stateOrCommand.substring(COMMAND_PREFIX.length());
expireCommand = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandString);
if (expireCommand == null) {
throw new BindingConfigParseException("The string '" + commandString
+ "' does not represent a valid command for item " + item.getName());
}
} else {
if (stateOrCommand.startsWith(STATE_PREFIX)) {
stateOrCommand = stateOrCommand.substring(STATE_PREFIX.length());
}
String stateString = stateOrCommand;
expireState = TypeParser.parseState(item.getAcceptedDataTypes(), stateString);
if (expireState == null) {
throw new BindingConfigParseException("The string '" + stateString
+ "' does not represent a valid state for item " + item.getName());
}
}
} else {
// default is to post Undefined state
expireCommand = null;
expireState = UnDefType.UNDEF;
}
}
@Override
public String toString() {
return "duration='" + durationString + "', ms=" + duration + ", state='" + expireState + "', command='"
+ expireCommand + "'";
}
}
}