use of com.serotonin.m2m2.rt.dataImage.PointValueTime 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;
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-modules-public by infiniteautomation.
the class SerialDataSourceRT method setPointValueImpl.
@Override
public void setPointValueImpl(DataPointRT dataPoint, PointValueTime valueTime, SetPointSource source) {
// Are we connected?
if (this.port == null && !connect()) {
raiseEvent(POINT_WRITE_EXCEPTION_EVENT, Common.timer.currentTimeMillis(), true, new TranslatableMessage("event.serial.writeFailedPortNotSetup"));
return;
}
try {
setPointValueImplTransport(dataPoint, valueTime);
returnToNormal(POINT_WRITE_EXCEPTION_EVENT, System.currentTimeMillis());
} catch (IOException e) {
if (vo.getRetries() == 0)
LOG.error("Failed setting serial point " + dataPoint.getVO().getExtendedName() + ": " + e.getMessage(), e);
// Try and reset the connection
Exception ex = e;
int retries = vo.getRetries();
while (retries > 0) {
try {
retries -= 1;
if (this.port != null)
Common.serialPortManager.close(this.port);
if (this.connect()) {
setPointValueImplTransport(dataPoint, valueTime);
returnToNormal(POINT_WRITE_EXCEPTION_EVENT, System.currentTimeMillis());
return;
}
} catch (Exception e2) {
ex = e2;
}
}
if (ex != null) {
raiseEvent(POINT_WRITE_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.writeFailed", e.getMessage()));
LOG.error("Error re-connecting to serial port.", ex);
}
}
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-modules-public by infiniteautomation.
the class SerialDataSourceRT method setPointValueImplTransport.
private void setPointValueImplTransport(DataPointRT dataPoint, PointValueTime valueTime) throws IOException {
OutputStream os = this.port.getOutputStream();
if (os == null)
throw new IOException("Port is closed.");
// Create Message from Message Start
SerialPointLocatorRT pl = dataPoint.getPointLocator();
byte[] data;
if (this.vo.isHex()) {
// Convert to Hex
try {
switch(dataPoint.getDataTypeId()) {
case DataTypes.ALPHANUMERIC:
data = convertToHex(valueTime.getStringValue());
break;
case DataTypes.BINARY:
if (valueTime.getBooleanValue())
data = convertToHex("00");
else
data = convertToHex("01");
break;
case DataTypes.MULTISTATE:
String intValue = Integer.toString(valueTime.getIntegerValue());
if (intValue.length() % 2 != 0)
intValue = "0" + intValue;
data = convertToHex(intValue);
break;
case DataTypes.NUMERIC:
String numValue = Integer.toString(valueTime.getIntegerValue());
if (numValue.length() % 2 != 0)
numValue = "0" + numValue;
data = convertToHex(numValue);
break;
default:
throw new ShouldNeverHappenException("Unsupported data type" + dataPoint.getDataTypeId());
}
if (this.vo.isLogIO())
this.ioLog.log(false, data);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
raiseEvent(POINT_WRITE_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.serial.notHex"));
return;
}
} else {
// Pin the terminator on the end
String messageTerminator = ((SerialDataSourceVO) this.getVo()).getMessageTerminator();
// Do we need to or is it already on the end?
String identifier = pl.getVo().getPointIdentifier();
String fullMsg = identifier + valueTime.getStringValue();
if (!fullMsg.endsWith(messageTerminator)) {
fullMsg += messageTerminator;
}
// String output = newValue.getStringValue();
data = fullMsg.getBytes();
if (vo.isLogIO())
this.ioLog.log("O: " + fullMsg);
}
for (byte b : data) {
os.write(b);
}
os.flush();
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class BaseDwr method prepareBasePointState.
/**
* Base method for preparing information in a state object and returning a
* point value.
*
* @param componentId
* a unique id for the browser side component. Required for set
* point snippets.
* @param state
* @param point
* @param status
* @param model
* @return
*/
protected static PointValueTime prepareBasePointState(String componentId, BasePointState state, DataPointVO pointVO, DataPointRT point, Map<String, Object> model) {
model.clear();
model.put("componentId", componentId);
model.put("point", pointVO);
model.put("pointRT", point);
model.put(MODEL_ATTR_TRANSLATIONS, getTranslations());
PointValueTime pointValue = null;
if (point == null)
model.put("disabled", "true");
else {
pointValue = point.getPointValue();
if (pointValue != null)
model.put("pointValue", pointValue);
}
return pointValue;
}
use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.
the class DataPointDetailsDwr method getPointData.
public static PointDetailsState getPointData() {
// Get the point from the user's session. It should have been set by the controller.
HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
User user = Common.getUser(request);
DataPointVO pointVO = user.getEditPoint();
// Create the watch list state.
Map<String, Object> model = new HashMap<String, Object>();
// Get the data point status from the data image.
DataPointRT pointRT = Common.runtimeManager.getDataPoint(pointVO.getId());
PointDetailsState state = new PointDetailsState();
state.setId(Integer.toString(pointVO.getId()));
PointValueTime pointValue = prepareBasePointState(Integer.toString(pointVO.getId()), state, pointVO, pointRT, model);
setPrettyText(request, state, pointVO, model, pointValue);
setChange(pointVO, state, pointRT, request, model, user);
setEvents(pointVO, user, model, pointEventsLimit);
setMessages(state, request, "dataPointMessages.jsp", model);
return state;
}
Aggregations