use of com.serotonin.m2m2.rt.RTException in project ma-modules-public by infiniteautomation.
the class PointValueRestController method setPointValue.
/**
* Helper method for setting a point value
*
* @param xid
* @param data
* @param unitConversion
* @return
*/
private RestProcessResult<PointValueTimeModel> setPointValue(User user, String xid, PointValueTimeModel model, boolean unitConversion, UriComponentsBuilder builder) {
RestProcessResult<PointValueTimeModel> result = new RestProcessResult<PointValueTimeModel>(HttpStatus.OK);
DataPointVO existingDp = DataPointDao.instance.getByXid(xid);
if (existingDp == null) {
result.addRestMessage(getDoesNotExistMessage());
return result;
}
try {
if (Permissions.hasDataPointSetPermission(user, existingDp)) {
// Set the time to now if it is not present
if (model.getTimestamp() == 0) {
model.setTimestamp(Common.timer.currentTimeMillis());
}
// Validate the model's data type for compatibility
if (DataTypeEnum.convertFrom(model.getType()) != existingDp.getPointLocator().getDataTypeId()) {
result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("event.ds.dataType"));
return result;
}
// Validate the timestamp for future dated
if (model.getTimestamp() > Common.timer.currentTimeMillis() + SystemSettingsDao.getFutureDateLimit()) {
result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "Future dated points not acceptable."));
return result;
}
// Are we converting from the rendered Unit?
if (unitConversion) {
if ((model.getType() == DataTypeEnum.NUMERIC) && (model.getValue() instanceof Number)) {
double value;
if (model.getValue() instanceof Integer) {
value = (double) ((Integer) model.getValue());
} else {
value = (double) ((Double) model.getValue());
}
model.setValue(existingDp.getRenderedUnit().getConverterTo(existingDp.getUnit()).convert(value));
} else {
result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "[" + xid + "]Cannot perform unit conversion on Non Numeric data types."));
return result;
}
}
// to convert it
if ((model.getType() == DataTypeEnum.MULTISTATE) && (model.getValue() instanceof String)) {
try {
DataValue value = existingDp.getTextRenderer().parseText((String) model.getValue(), existingDp.getPointLocator().getDataTypeId());
model.setValue(value.getObjectValue());
} catch (Exception e) {
// Lots can go wrong here so let the user know
result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "[" + xid + "]Unable to convert Multistate String representation to any known value."));
}
}
final PointValueTime pvt;
try {
pvt = model.getData();
} catch (Exception e) {
result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "[" + xid + "]Invalid Format"));
return result;
}
// one last check to ensure we are inserting the correct data type
if (DataTypes.getDataType(pvt.getValue()) != existingDp.getPointLocator().getDataTypeId()) {
result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("event.ds.dataType"));
return result;
}
final int dataSourceId = existingDp.getDataSourceId();
SetPointSource source = null;
if (model.getAnnotation() != null) {
source = new SetPointSource() {
@Override
public String getSetPointSourceType() {
return "REST";
}
@Override
public int getSetPointSourceId() {
return dataSourceId;
}
@Override
public TranslatableMessage getSetPointSourceMessage() {
return ((AnnotatedPointValueTime) pvt).getSourceMessage();
}
@Override
public void raiseRecursionFailureEvent() {
LOG.error("Recursive failure while setting point via REST");
}
};
}
try {
Common.runtimeManager.setDataPointValue(existingDp.getId(), pvt, source);
// This URI may not always be accurate if the Data Source doesn't use the
// provided time...
URI location = builder.path("/v1/point-values/{xid}/{time}").buildAndExpand(xid, pvt.getTime()).toUri();
result.addRestMessage(getResourceCreatedMessage(location));
return result;
} catch (RTException e) {
// Ok its probably not enabled or settable
result.addRestMessage(new RestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("common.default", "[" + xid + "]" + e.getMessage())));
return result;
} catch (Exception e) {
LOG.error(e.getMessage(), e);
result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
return result;
}
} else {
result.addRestMessage(getUnauthorizedMessage());
return result;
}
} catch (PermissionException e) {
LOG.error(e.getMessage(), e);
result.addRestMessage(getUnauthorizedMessage());
return result;
}
}
use of com.serotonin.m2m2.rt.RTException in project ma-core-public by infiniteautomation.
the class RuntimeManagerImpl method forcePointRead.
/* (non-Javadoc)
* @see com.serotonin.m2m2.rt.RuntimeManager#forcePointRead(int)
*/
@Override
public void forcePointRead(int dataPointId) {
DataPointRT dataPoint = dataPoints.get(dataPointId);
if (dataPoint == null)
throw new RTException("Point is not enabled");
// Tell the data source to read the point value;
DataSourceRT<? extends DataSourceVO<?>> ds = getRunningDataSource(dataPoint.getDataSourceId());
if (ds != null)
// The data source may have been disabled. Just make sure.
ds.forcePointRead(dataPoint);
}
use of com.serotonin.m2m2.rt.RTException in project ma-core-public by infiniteautomation.
the class RuntimeManagerImpl method relinquish.
/* (non-Javadoc)
* @see com.serotonin.m2m2.rt.RuntimeManager#relinquish(int)
*/
@Override
public void relinquish(int dataPointId) {
DataPointRT dataPoint = dataPoints.get(dataPointId);
if (dataPoint == null)
throw new RTException("Point is not enabled");
if (!dataPoint.getPointLocator().isSettable())
throw new RTException("Point is not settable");
if (!dataPoint.getPointLocator().isRelinquishable())
throw new RTException("Point is not relinquishable");
// Tell the data source to relinquish value of the point.
DataSourceRT<? extends DataSourceVO<?>> ds = getRunningDataSource(dataPoint.getDataSourceId());
// The data source may have been disabled. Just make sure.
if (ds != null)
ds.relinquish(dataPoint);
}
use of com.serotonin.m2m2.rt.RTException in project ma-core-public by infiniteautomation.
the class RuntimeManagerImpl method setDataPointValue.
/* (non-Javadoc)
* @see com.serotonin.m2m2.rt.RuntimeManager#setDataPointValue(int, com.serotonin.m2m2.rt.dataImage.PointValueTime, com.serotonin.m2m2.rt.dataImage.SetPointSource)
*/
@Override
public void setDataPointValue(int dataPointId, PointValueTime valueTime, SetPointSource source) {
DataPointRT dataPoint = dataPoints.get(dataPointId);
if (dataPoint == null)
throw new RTException("Point is not enabled");
if (!dataPoint.getPointLocator().isSettable())
throw new RTException("Point is not settable");
// Tell the data source to set the value of the point.
DataSourceRT<? extends DataSourceVO<?>> ds = getRunningDataSource(dataPoint.getDataSourceId());
// The data source may have been disabled. Just make sure.
if (ds != null)
ds.setPointValue(dataPoint, valueTime, source);
}
Aggregations