Search in sources :

Example 1 with RTException

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;
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) XidPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel) RecentPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel) PointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) SetPointSource(com.serotonin.m2m2.rt.dataImage.SetPointSource) URI(java.net.URI) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) RTException(com.serotonin.m2m2.rt.RTException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) RTException(com.serotonin.m2m2.rt.RTException) RestMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestMessage) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 2 with RTException

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);
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT)

Example 3 with RTException

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);
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT)

Example 4 with RTException

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);
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT)

Aggregations

DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)3 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)1 ValidationFailedRestException (com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 RTException (com.serotonin.m2m2.rt.RTException)1 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 SetPointSource (com.serotonin.m2m2.rt.dataImage.SetPointSource)1 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)1 RestMessage (com.serotonin.m2m2.web.mvc.rest.v1.message.RestMessage)1 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)1 PointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel)1 RecentPointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel)1 XidPointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel)1 URI (java.net.URI)1