[Serial binding] OH2 Compatibility issue with RegexTransformation service #5184
Changes from 3 commits
defa8ae
cb21796
30d13f1
4c0e8cf
f691479
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* Copyright (c) 2010-2016 by the respective copyright holders. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.openhab.binding.serial.internal; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.openhab.core.transform.TransformationException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Pattern matcher to match and cache patterns | ||
* | ||
* @author Marek Halmo | ||
* @see RegexPatternMatcher#getMatches getMatches() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
* | ||
*/ | ||
public class RegexPatternMatcher { | ||
private static final Logger logger = LoggerFactory.getLogger(RegexPatternMatcher.class); | ||
private static HashMap<String, Pattern> patternCache = new HashMap<String, Pattern>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new HashMap<>(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point |
||
private static final Pattern SUBST_PATTERN = Pattern.compile("^s/(.*?[^\\\\])/(.*?[^\\\\])/(.*)$"); | ||
|
||
/** | ||
* Returns compiled pattern for specific regeExpression that is stored in patternCache | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "regeExpression" should say "regExpression", or possibly "regular expression". |
||
* | ||
* @param regExpression | ||
* @return | ||
*/ | ||
private static Pattern getPattern(String regExpression) { | ||
cache(regExpression); | ||
return patternCache.get(regExpression); | ||
} | ||
|
||
/** | ||
* Returns true if given regExpression is a substitution pattern | ||
* This fact is indicated by giving | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "indicated by giving" ... what? Please complete this sentence. |
||
* | ||
* @param regExpression | ||
* @return | ||
*/ | ||
private static boolean isSubtitution(String regExpression) { | ||
cache(regExpression); | ||
return patternCache.get(regExpression) == null; | ||
} | ||
|
||
/** | ||
* Caches given regexpression, if the expression is substitution pattern it marks is as substitution | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Caches given regExpression. If the expression is a substitution pattern, it marks it as a substitution." |
||
* | ||
* @return | ||
*/ | ||
private static void cache(String regExpression) { | ||
if (!patternCache.containsKey(regExpression)) { | ||
// Check if this pattern is a substitution. If so ignore it | ||
Matcher substMatcher = SUBST_PATTERN.matcher(regExpression); | ||
if (substMatcher.matches()) { | ||
patternCache.put(regExpression, null); | ||
} else { | ||
Pattern pattern = Pattern.compile(regExpression, Pattern.DOTALL); | ||
patternCache.put(regExpression, pattern); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns array of strings that match given regular expression. | ||
* If expression is not known the compiled pattern is cached in pattern cache | ||
* | ||
* @param regExpression regular expression to match or a substitution in form of "s/<regex>/result/g" (replace all) | ||
* or "s/<regex>/result/" (replace first) | ||
* @param source text to search in | ||
* @return | ||
* @throws TransformationException if regExpression or source is null | ||
* @see org.eclipse.smarthome.transform.regex.internal.RegExTranformationService RegExTranformationService | ||
*/ | ||
public static String[] getMatches(String regExpression, String source) throws TransformationException { | ||
if (regExpression == null || source == null) { | ||
throw new TransformationException("the given parameters 'regex' and 'source' must not be null"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "The given" ... |
||
} | ||
|
||
logger.debug("about to transform '{}' by the function '{}'", source, regExpression); | ||
|
||
// Check if RegEx is a substitution (s/<regex>/result/g) or (s/<regex>/result/) | ||
if (isSubtitution(regExpression)) { | ||
Matcher substMatcher = SUBST_PATTERN.matcher(regExpression); | ||
if (substMatcher.matches()) { | ||
logger.debug("Using substitution form of regex transformation"); | ||
String regex = substMatcher.group(1); | ||
String substitution = substMatcher.group(2); | ||
String options = substMatcher.group(3); | ||
|
||
String result; | ||
if (options.equals("g")) { | ||
result = source.trim().replaceAll(regex, substitution); | ||
} else { | ||
result = source.trim().replaceFirst(regex, substitution); | ||
} | ||
|
||
return new String[] { result }; | ||
} | ||
} | ||
|
||
Matcher matcher = getPattern(regExpression).matcher(source.trim()); | ||
List<String> results = new ArrayList<String>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. = new ArrayList<>(); |
||
|
||
while (matcher.find()) { | ||
results.add(matcher.group(1)); | ||
} | ||
|
||
return results.toArray(new String[results.size()]); | ||
} | ||
|
||
/** | ||
* Removes pattern from cache | ||
* | ||
* @param regExpression | ||
*/ | ||
public static void removePattern(String regExpression) { | ||
patternCache.remove(regExpression); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ | |
import org.openhab.core.library.types.StopMoveType; | ||
import org.openhab.core.library.types.StringType; | ||
import org.openhab.core.library.types.UpDownType; | ||
import org.openhab.core.transform.TransformationService; | ||
import org.openhab.core.types.Command; | ||
import org.openhab.core.types.State; | ||
import org.openhab.model.item.binding.BindingConfigParseException; | ||
|
@@ -69,8 +68,6 @@ public class SerialBinding extends AbstractEventSubscriber implements BindingCon | |
|
||
private EventPublisher eventPublisher = null; | ||
|
||
private TransformationService transformationService; | ||
|
||
public void setEventPublisher(EventPublisher eventPublisher) { | ||
this.eventPublisher = eventPublisher; | ||
|
||
|
@@ -87,20 +84,6 @@ public void unsetEventPublisher(EventPublisher eventPublisher) { | |
} | ||
} | ||
|
||
public void setTransformationService(TransformationService transformationService) { | ||
this.transformationService = transformationService; | ||
for (SerialDevice serialDevice : serialDevices.values()) { | ||
serialDevice.setTransformationService(transformationService); | ||
} | ||
} | ||
|
||
public void unsetTransformationService(TransformationService transformationService) { | ||
this.transformationService = null; | ||
for (SerialDevice serialDevice : serialDevices.values()) { | ||
serialDevice.setTransformationService(null); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
|
@@ -174,31 +157,39 @@ public void processBindingConfiguration(String context, Item item, String bindin | |
String downCommand = null; | ||
String stopCommand = null; | ||
|
||
String[] split = bindingConfig.split(","); | ||
if (split.length > 0) { | ||
for (int i = 1; i < split.length; i++) { | ||
String part = split[i]; | ||
String substring = part.substring(0, part.length()); | ||
|
||
if (substring.startsWith("REGEX(")) { | ||
pattern = substring.substring(6, substring.length() - 1); | ||
} else if (substring.equals("BASE64")) { | ||
base64 = true; | ||
} else if (substring.startsWith("ON(")) { | ||
onCommand = substring.substring(3, substring.length() - 1); | ||
} else if (substring.startsWith("OFF(")) { | ||
offCommand = substring.substring(4, substring.length() - 1); | ||
} else if (substring.startsWith("UP(")) { | ||
upCommand = substring.substring(3, substring.length() - 1); | ||
} else if (substring.startsWith("DOWN(")) { | ||
downCommand = substring.substring(5, substring.length() - 1); | ||
} else if (substring.startsWith("STOP(")) { | ||
stopCommand = substring.substring(5, substring.length() - 1); | ||
int parameterSplitterAt = bindingConfig.indexOf(","); | ||
|
||
if (parameterSplitterAt > 0) { | ||
String[] split = bindingConfig.substring(parameterSplitterAt + 1, bindingConfig.length() - 1).split("\\),"); | ||
if (split.length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant. Should remove this if block. |
||
for (int i = 0; i < split.length; i++) { | ||
String substring = split[i]; | ||
|
||
if (substring.startsWith("REGEX(")) { | ||
pattern = substring.substring(6, substring.length()); | ||
} else if (substring.equals("BASE64")) { | ||
base64 = true; | ||
} else if (substring.startsWith("ON(")) { | ||
onCommand = substring.substring(3, substring.length()); | ||
} else if (substring.startsWith("OFF(")) { | ||
offCommand = substring.substring(4, substring.length()); | ||
} else if (substring.startsWith("UP(")) { | ||
upCommand = substring.substring(3, substring.length()); | ||
} else if (substring.startsWith("DOWN(")) { | ||
downCommand = substring.substring(5, substring.length()); | ||
} else if (substring.startsWith("STOP(")) { | ||
stopCommand = substring.substring(5, substring.length()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
String portConfig[] = split[0].split("@"); | ||
String portConfig[]; | ||
if (parameterSplitterAt > 0) { | ||
portConfig = bindingConfig.substring(0, parameterSplitterAt).split("@"); | ||
} else { | ||
portConfig = bindingConfig.split("@"); | ||
} | ||
|
||
String port = portConfig[0]; | ||
int baudRate = 0; | ||
|
@@ -215,7 +206,6 @@ public void processBindingConfiguration(String context, Item item, String bindin | |
serialDevice = new SerialDevice(port); | ||
} | ||
|
||
serialDevice.setTransformationService(transformationService); | ||
serialDevice.setEventPublisher(eventPublisher); | ||
try { | ||
serialDevice.initialize(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,6 @@ | |
import org.openhab.core.library.types.PercentType; | ||
import org.openhab.core.library.types.StringType; | ||
import org.openhab.core.transform.TransformationException; | ||
import org.openhab.core.transform.TransformationService; | ||
import org.openhab.core.types.State; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
@@ -42,7 +41,7 @@ | |
import gnu.io.UnsupportedCommOperationException; | ||
|
||
/** | ||
* This class represents a serial device that is linked to exactly one String item and/or Switch item. | ||
* This class represents a serial device that is linked to one or many String, Number, Switch or Rollershutter items | ||
* | ||
* @author Kai Kreuzer | ||
* | ||
|
@@ -55,7 +54,6 @@ public class SerialDevice implements SerialPortEventListener { | |
private int baud = 9600; | ||
|
||
private EventPublisher eventPublisher; | ||
private TransformationService transformationService; | ||
|
||
private CommPortIdentifier portId; | ||
private SerialPort serialPort; | ||
|
@@ -102,6 +100,12 @@ public void addConfig(String itemName, Class<?> type, String pattern, boolean ba | |
|
||
public void removeConfig(String itemName) { | ||
if (configMap != null) { | ||
ItemType type = configMap.get(itemName); | ||
if (type.pattern != null) { | ||
// For duplicate patterns - it will be added to cache next time it is required | ||
RegexPatternMatcher.removePattern(type.pattern); | ||
} | ||
|
||
configMap.remove(itemName); | ||
} | ||
} | ||
|
@@ -123,10 +127,6 @@ public void unsetEventPublisher(EventPublisher eventPublisher) { | |
this.eventPublisher = null; | ||
} | ||
|
||
public void setTransformationService(TransformationService transformationService) { | ||
this.transformationService = transformationService; | ||
} | ||
|
||
public String getPort() { | ||
return port; | ||
} | ||
|
@@ -280,39 +280,35 @@ public void serialEvent(SerialPortEvent event) { | |
if (eventPublisher != null) { | ||
if (configMap != null && !configMap.isEmpty()) { | ||
for (Entry<String, ItemType> entry : configMap.entrySet()) { | ||
|
||
String pattern = entry.getValue().pattern; | ||
// use pattern | ||
if (entry.getValue().pattern != null) { | ||
|
||
if (transformationService == null) { | ||
logger.error("No transformation service available!"); | ||
if (pattern != null) { | ||
try { | ||
String[] matches = RegexPatternMatcher.getMatches(pattern, result); | ||
|
||
} else { | ||
try { | ||
String value = transformationService.transform(entry.getValue().pattern, | ||
result); | ||
for (int i = 0; i < matches.length; i++) { | ||
String match = matches[i]; | ||
|
||
try { | ||
State state = null; | ||
|
||
if (entry.getValue().type.equals(NumberItem.class)) { | ||
state = new DecimalType(value); | ||
state = new DecimalType(match); | ||
} else if (entry.getValue().type == RollershutterItem.class) { | ||
state = new PercentType(value); | ||
state = new PercentType(match); | ||
} else { | ||
state = new StringType(value); | ||
state = new StringType(match); | ||
} | ||
|
||
eventPublisher.postUpdate(entry.getKey(), state); | ||
} catch (NumberFormatException e) { | ||
logger.warn("Unable to convert regex result '{}' for item {} to number", | ||
new String[] { result, entry.getKey() }); | ||
} | ||
} catch (TransformationException e) { | ||
logger.error("Unable to transform!", e); | ||
} | ||
} catch (TransformationException e) { | ||
logger.error("Unable to transform!", e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a warn instead of an error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not shure, TransformationException is thrown only if you provide null pattern or result. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the logging guidelines. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, based on guidelines you are right.. changing to warn |
||
} | ||
|
||
} else if (entry.getValue().type == StringItem.class) { | ||
if (entry.getValue().base64) { | ||
result = Base64.encodeBase64String(result.getBytes()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2017