use of com.infiniteautomation.mango.regex.MatchCallback in project ma-modules-public by infiniteautomation.
the class SerialDataSourceRT method serialEvent.
@Override
public void serialEvent(SerialPortProxyEvent evt) {
// Keep a lock on the buffer while we do this
synchronized (this.buffer) {
// If our port is dead, say so
if (this.port == null) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailedPortNotSetup"));
return;
}
// String msg = null;
try {
// Don't read during timeout events as there could be no data and this would block till there is
if (!(evt instanceof TimeoutSerialEvent)) {
InputStream in = this.port.getInputStream();
int data;
// this may not be the full message, or may read multiple messages
while ((data = in.read()) > -1) {
if (buffer.size() >= this.vo.getMaxMessageSize()) {
buffer.popAll();
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailed", "Max message size reached!"));
// Give up
return;
}
buffer.push(data);
}
// Log our buffer contents
byte[] logMsg = buffer.peekAll();
if (this.vo.isLogIO()) {
if (this.vo.isHex())
this.ioLog.log(true, logMsg);
else
this.ioLog.log("I: " + new String(logMsg, Common.UTF8_CS));
}
// Serial Event so setup a Timeout Task to fire after the message timeout
if (this.buffer.size() > 0)
this.timeoutTask = new TimeoutTask(this.vo.getReadTimeout(), new SerialTimeoutClient(this));
}
// We either use a terminator and timeout OR just a Timeout
if (vo.getUseTerminator()) {
// If timeout then process the buffer
// If serial event then read input and process buffer
// "!([A-Z0-9]{3,3})([a-zA-Z])(.*);";
String messageRegex = vo.getMessageRegex();
// DS Information
int pointIdentifierIndex = vo.getPointIdentifierIndex();
// Create a String so we can use Regex and matching
String msg = null;
if (this.vo.isHex()) {
msg = convertFromHex(buffer.peekAll());
} else {
msg = new String(buffer.peekAll(), Common.UTF8_CS);
}
// Now we have a string that contains the entire contents of the buffer,
// split on terminator, keep it on the end of the message and process any full messages
// and pop them from the buffer
String[] messages = splitMessages(msg, this.vo.getMessageTerminator());
for (String message : messages) {
// potentially be one incomplete message.
if (canProcessTerminatedMessage(message, this.vo.getMessageTerminator())) {
// Pop off this message
this.buffer.pop(message.length());
if (LOG.isDebugEnabled())
LOG.debug("Matching will use String: " + message);
final AtomicBoolean matcherFailed = new AtomicBoolean(false);
pointListChangeLock.readLock().lock();
try {
for (final DataPointRT dp : this.dataPoints) {
SerialPointLocatorVO plVo = dp.getVO().getPointLocator();
MatchCallback callback = new MatchCallback() {
@Override
public void onMatch(String pointIdentifier, PointValueTime value) {
if (!updatePointValue(value, dp)) {
matcherFailed.set(true);
raiseEvent(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.invalidValue", dp.getVO().getXid()));
}
}
@Override
public void pointPatternMismatch(String message, String messageRegex) {
// Ignore as this just isn't a message we care about
}
@Override
public void messagePatternMismatch(String message, String messageRegex) {
raiseEvent(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.patternMismatch", messageRegex, message));
matcherFailed.set(true);
}
@Override
public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
// Don't Care
}
@Override
public void matchGeneralFailure(Exception e) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailed", e.getMessage()));
matcherFailed.set(true);
}
};
try {
matchPointValue(message, messageRegex, pointIdentifierIndex, plVo, vo.isHex(), LOG, callback);
} catch (Exception e) {
callback.matchGeneralFailure(e);
}
}
} finally {
pointListChangeLock.readLock().unlock();
}
// If no failures...
if (!matcherFailed.get())
returnToNormal(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis());
returnToNormal(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis());
}
if (evt instanceof TimeoutSerialEvent) {
// Clear the buffer
this.buffer.popAll();
} else {
// Check to see if we have remaining data, if not cancel timeout
if (this.buffer.size() == 0)
this.timeoutTask.cancel();
}
}
return;
} else {
// Do we have a timeout generated message?
if (evt instanceof TimeoutSerialEvent) {
String msg = null;
// We are a timeout event so we have a timeout, pop everything into the message and assume its a message
if (this.vo.isHex()) {
msg = convertFromHex(buffer.popAll());
} else {
msg = new String(buffer.popAll(), Common.UTF8_CS);
}
// Just do a match on the Entire Message because we are not using Terminator
// String messageRegex = ".*"; //Match everything
// int pointIdentifierIndex = 0; //Whole message
// "!([A-Z0-9]{3,3})([a-zA-Z])(.*);";
String messageRegex = vo.getMessageRegex();
// DS Information
int pointIdentifierIndex = vo.getPointIdentifierIndex();
if (LOG.isDebugEnabled())
LOG.debug("Matching will use String: " + msg);
final AtomicBoolean matcherFailed = new AtomicBoolean(false);
synchronized (pointListChangeLock) {
for (final DataPointRT dp : this.dataPoints) {
SerialPointLocatorVO plVo = dp.getVO().getPointLocator();
MatchCallback callback = new MatchCallback() {
@Override
public void onMatch(String pointIdentifier, PointValueTime pvt) {
if (!updatePointValue(pvt, dp)) {
matcherFailed.set(true);
raiseEvent(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.invalidValue", dp.getVO().getXid()));
}
// Ensure we clear out the buffer...
buffer.popAll();
}
@Override
public void pointPatternMismatch(String message, String messageRegex) {
// Ignore as this just isn't a message we care about
}
@Override
public void messagePatternMismatch(String message, String messageRegex) {
raiseEvent(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.patternMismatch", messageRegex, message));
matcherFailed.set(true);
}
@Override
public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
// Don't Care
}
@Override
public void matchGeneralFailure(Exception e) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailed", e.getMessage()));
matcherFailed.set(true);
}
};
try {
matchPointValue(msg, messageRegex, pointIdentifierIndex, plVo, vo.isHex(), LOG, callback);
} catch (Exception e) {
callback.matchGeneralFailure(e);
}
}
}
// If no failures...
if (!matcherFailed.get())
returnToNormal(POINT_READ_PATTERN_MISMATCH_EVENT, System.currentTimeMillis());
returnToNormal(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis());
}
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
// Ensure we clear out the buffer...
this.buffer.popAll();
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.readFailed", e.getMessage()));
}
}
// End synch
}
use of com.infiniteautomation.mango.regex.MatchCallback in project ma-modules-public by infiniteautomation.
the class AsciiFileEditDwr method testString.
@DwrPermission(user = true)
public ProcessResult testString(int dsId, String raw) {
final ProcessResult pr = new ProcessResult();
// Message we will work with
String msg;
if (dsId == -1) {
pr.addContextualMessage("testString", "dsEdit.file.test.needsSave");
return pr;
}
msg = StringEscapeUtils.unescapeJava(raw);
// Map to store the values vs the points they are for
final List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
pr.addData("results", results);
DataPointDao dpd = DataPointDao.instance;
List<DataPointVO> points = dpd.getDataPoints(dsId, null);
for (final DataPointVO vo : points) {
final Map<String, Object> result = new HashMap<String, Object>();
MatchCallback callback = new MatchCallback() {
@Override
public void onMatch(String pointIdentifier, PointValueTime value) {
result.put("success", "true");
result.put("name", vo.getName());
result.put("identifier", pointIdentifier);
result.put("value", value != null ? value.toString() : "null");
}
@Override
public void pointPatternMismatch(String message, String pointValueRegex) {
result.put("success", "false");
result.put("name", vo.getName());
result.put("error", new TranslatableMessage("dsEdit.file.test.noPointRegexMatch").translate(Common.getTranslations()));
}
@Override
public void messagePatternMismatch(String message, String messageRegex) {
}
@Override
public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
result.put("success", "false");
result.put("name", vo.getName());
result.put("error", new TranslatableMessage("dsEdit.file.test.noIdentifierFound").translate(Common.getTranslations()));
}
@Override
public void matchGeneralFailure(Exception e) {
result.put("success", "false");
result.put("name", vo.getName());
result.put("error", new TranslatableMessage("common.default", e.getMessage()).translate(Common.getTranslations()));
}
};
AsciiFilePointLocatorVO locator = vo.getPointLocator();
AsciiFileDataSourceRT.matchPointValueTime(msg, Pattern.compile(locator.getValueRegex()), locator.getPointIdentifier(), locator.getPointIdentifierIndex(), locator.getDataTypeId(), locator.getValueIndex(), locator.getHasTimestamp(), locator.getTimestampIndex(), locator.getTimestampFormat(), callback);
if (result.size() > 0) {
results.add(result);
}
}
return pr;
}
use of com.infiniteautomation.mango.regex.MatchCallback in project ma-modules-public by infiniteautomation.
the class AsciiFileDataSourceRT method fileEvent.
private void fileEvent(List<DataPointRT> dataPoints) {
// Should never happen
if (this.file == null) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("file.event.readFailedFileNotSetup"));
return;
}
if (restrictedPath) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("dsEdit.file.pathRestrictedBy", vo.getFilePath()));
return;
}
// The file is modified or we've just started, so read it.
try {
BufferedReader reader = new BufferedReader(new FileReader(this.file));
String msg;
if (!dataPoints.isEmpty()) {
// TODO optimize to be better than numLines*numPoints
while ((msg = reader.readLine()) != null) {
// Give all points the chance to find their data
for (final DataPointRT dp : dataPoints) {
AsciiFilePointLocatorRT pl = dp.getPointLocator();
final AsciiFilePointLocatorVO plVo = pl.getVo();
MatchCallback callback = new MatchCallback() {
@Override
public void onMatch(String pointIdentifier, PointValueTime value) {
if (!plVo.getHasTimestamp())
dp.updatePointValue(value);
else
dp.savePointValueDirectToCache(value, null, true, true);
}
@Override
public void pointPatternMismatch(String message, String pointValueRegex) {
// N/A
}
@Override
public void messagePatternMismatch(String message, String messageRegex) {
// N/A
}
@Override
public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), false, new TranslatableMessage("file.event.insufficientGroups", dp.getVO().getExtendedName()));
}
@Override
public void matchGeneralFailure(Exception e) {
if (e instanceof ParseException)
raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.dateParseFailed", e.getMessage()));
else if (e instanceof NumberFormatException) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.notNumber", e.getMessage()));
} else
raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.readFailed", e.getMessage()));
}
};
matchPointValueTime(msg, pl.getValuePattern(), plVo.getPointIdentifier(), plVo.getPointIdentifierIndex(), plVo.getDataTypeId(), plVo.getValueIndex(), plVo.getHasTimestamp(), plVo.getTimestampIndex(), plVo.getTimestampFormat(), callback);
}
}
reader.close();
returnToNormal(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis());
}
} catch (FileNotFoundException e) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.fileNotFound", e.getMessage()));
} catch (IOException e) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.readFailed", e.getMessage()));
} catch (NumberFormatException e) {
raiseEvent(POINT_READ_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("file.event.notNumber", e.getMessage()));
}
}
use of com.infiniteautomation.mango.regex.MatchCallback in project ma-modules-public by infiniteautomation.
the class SerialEditDwr method testString.
@DwrPermission(user = true)
public ProcessResult testString(String raw, int dsId, String messageRegex, String messageTerminator, int pointIdentifierIndex, boolean isHex, boolean useTerminator) {
final ProcessResult pr = new ProcessResult();
// Message we will work with
String msg;
if (dsId == -1) {
pr.addContextualMessage("testString", "serial.test.needsSave");
return pr;
}
msg = StringEscapeUtils.unescapeJava(raw);
messageRegex = StringEscapeUtils.unescapeJava(messageRegex);
messageTerminator = StringEscapeUtils.unescapeJava(messageTerminator);
// Are we a hex string
if (isHex) {
if (!msg.matches("[0-9A-Fa-f]+")) {
pr.addContextualMessage("testString", "serial.validate.notHex");
return pr;
}
}
// Map to store the values vs the points they are for
final List<Map<String, String>> results = new ArrayList<Map<String, String>>();
pr.addData("results", results);
DataPointDao dpd = DataPointDao.instance;
List<DataPointVO> points = dpd.getDataPoints(dsId, null);
if (useTerminator) {
// Convert the message
String[] messages = SerialDataSourceRT.splitMessages(msg, messageTerminator);
for (String message : messages) {
if (SerialDataSourceRT.canProcessTerminatedMessage(message, messageTerminator)) {
if (LOG.isDebugEnabled())
LOG.debug("Matching will use String: " + message);
// Check all the points
for (final DataPointVO vo : points) {
final Map<String, String> result = new HashMap<String, String>();
MatchCallback callback = new MatchCallback() {
@Override
public void onMatch(String pointIdentifier, PointValueTime pvt) {
result.put("name", vo.getName());
result.put("value", pvt.toString());
result.put("identifier", pointIdentifier);
result.put("success", "true");
}
@Override
public void pointPatternMismatch(String message, String messageRegex) {
result.put("success", "false");
result.put("name", vo.getName());
result.put("error", new TranslatableMessage("serial.test.noPointRegexMatch").translate(Common.getTranslations()));
}
@Override
public void messagePatternMismatch(String message, String messageRegex) {
result.put("success", "false");
result.put("name", vo.getName());
result.put("error", new TranslatableMessage("serial.test.noMessageMatch").translate(Common.getTranslations()));
}
@Override
public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
result.put("success", "false");
result.put("name", vo.getName());
result.put("error", new TranslatableMessage("serial.test.noIdentifierFound").translate(Common.getTranslations()));
}
/* (non-Javadoc)
* @see com.infiniteautomation.mango.regex.MatchCallback#matchGeneralFailure(java.lang.Exception)
*/
@Override
public void matchGeneralFailure(Exception e) {
result.put("success", "false");
result.put("name", vo.getName());
result.put("error", new TranslatableMessage("common.default", e.getMessage()).translate(Common.getTranslations()));
}
};
try {
SerialDataSourceRT.matchPointValue(message, messageRegex, pointIdentifierIndex, (SerialPointLocatorVO) vo.getPointLocator(), isHex, LOG, callback);
} catch (Exception e) {
callback.matchGeneralFailure(e);
}
if (result.size() > 0) {
result.put("message", message);
results.add(result);
}
}
} else {
Map<String, String> result = new HashMap<String, String>();
result.put("success", "false");
result.put("message", message);
result.put("error", new TranslatableMessage("serial.test.noTerminator").translate(Common.getTranslations()));
results.add(result);
}
}
} else {
if (LOG.isDebugEnabled())
LOG.debug("Matching will use String: " + msg);
// Check all the points
for (final DataPointVO vo : points) {
final Map<String, String> result = new HashMap<String, String>();
MatchCallback callback = new MatchCallback() {
@Override
public void onMatch(String pointIdentifier, PointValueTime pvt) {
result.put("name", vo.getName());
result.put("value", pvt.toString());
result.put("identifier", pointIdentifier);
result.put("success", "true");
}
@Override
public void pointPatternMismatch(String message, String messageRegex) {
result.put("success", "false");
result.put("name", vo.getName());
result.put("error", new TranslatableMessage("serial.test.noPointRegexMatch").translate(Common.getTranslations()));
}
@Override
public void messagePatternMismatch(String message, String messageRegex) {
result.put("success", "false");
result.put("name", vo.getName());
result.put("error", new TranslatableMessage("serial.test.noMessageMatch").translate(Common.getTranslations()));
}
@Override
public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
result.put("success", "false");
result.put("name", vo.getName());
result.put("error", new TranslatableMessage("serial.test.noIdentifierFound").translate(Common.getTranslations()));
}
/* (non-Javadoc)
* @see com.infiniteautomation.mango.regex.MatchCallback#matchGeneralFailure(java.lang.Exception)
*/
@Override
public void matchGeneralFailure(Exception e) {
result.put("success", "false");
result.put("name", vo.getName());
result.put("error", new TranslatableMessage("common.default", e.getMessage()).translate(Common.getTranslations()));
}
};
try {
SerialDataSourceRT.matchPointValue(msg, messageRegex, pointIdentifierIndex, (SerialPointLocatorVO) vo.getPointLocator(), isHex, LOG, callback);
} catch (Exception e) {
callback.matchGeneralFailure(e);
}
if (result.size() > 0) {
result.put("message", msg);
results.add(result);
}
}
}
return pr;
}
Aggregations