Search in sources :

Example 1 with XidPointValueTimeModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel in project ma-modules-public by infiniteautomation.

the class PointValueImportResult method saveValue.

public void saveValue(XidPointValueTimeModel model) {
    if (valid) {
        // Validate the model against our point
        long timestamp = model.getTimestamp();
        if (timestamp == 0)
            timestamp = Common.timer.currentTimeMillis();
        int dataTypeId = DataTypeEnum.convertFrom(model.getType());
        if (dataTypeId != vo.getPointLocator().getDataTypeId()) {
            result.addContextualMessage("dataType", "event.ds.dataType");
            return;
        }
        DataValue value;
        switch(model.getType()) {
            case ALPHANUMERIC:
                value = new AlphanumericValue((String) model.getValue());
                break;
            case BINARY:
                value = new BinaryValue((Boolean) model.getValue());
                break;
            case MULTISTATE:
                if (model.getValue() instanceof String) {
                    try {
                        value = vo.getTextRenderer().parseText((String) model.getValue(), dataTypeId);
                    } catch (Exception e) {
                        // Lots can go wrong here so let the user know
                        result.addContextualMessage("value", "event.valueParse.textParse", e.getMessage());
                        return;
                    }
                } else {
                    value = new MultistateValue(((Number) model.getValue()).intValue());
                }
                break;
            case NUMERIC:
                value = new NumericValue(((Number) model.getValue()).doubleValue());
                break;
            case IMAGE:
            default:
                result.addContextualMessage("dataType", "common.default", model.getType() + " data type not supported yet");
                return;
        }
        PointValueTime pvt;
        if (model.getAnnotation() == null) {
            pvt = new PointValueTime(value, timestamp);
        } else {
            pvt = new AnnotatedPointValueTime(value, timestamp, new TranslatableMessage("common.default", model.getAnnotation()));
        }
        if (rt == null) {
            dao.savePointValueAsync(vo.getId(), pvt, null);
        } else {
            rt.savePointValueDirectToCache(pvt, null, true, true);
        }
        total++;
    }
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Example 2 with XidPointValueTimeModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel in project ma-modules-public by infiniteautomation.

the class PointValueRestController method savePointsValues.

@ApiOperation(value = "Import Point Values for one or many Data Points", notes = "Data Point must exist and user must have write access")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Collection<PointValueImportResult>> savePointsValues(HttpServletRequest request, @RequestBody(required = true) List<XidPointValueTimeModel> models, @AuthenticationPrincipal User user) {
    // Map of XIDs to results
    Map<String, PointValueImportResult> results = new HashMap<String, PointValueImportResult>();
    for (XidPointValueTimeModel model : models) {
        PointValueImportResult result = results.get(model.getXid());
        if (result == null) {
            result = new PointValueImportResult(model.getXid(), dao, user);
            results.put(model.getXid(), result);
        }
        // Attempt to save it
        result.saveValue(model);
    }
    return ResponseEntity.ok(results.values());
}
Also used : XidPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel) HashMap(java.util.HashMap) PointValueImportResult(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueImportResult) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with XidPointValueTimeModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel in project ma-modules-public by infiniteautomation.

the class PointValueRestController method putPointsValues.

@ApiOperation(value = "Update one or many data point's current value", notes = "Each data point must exist and be enabled")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json" }, consumes = { "application/json" })
public ResponseEntity<List<XidPointValueTimeModel>> putPointsValues(HttpServletRequest request, @RequestBody(required = true) List<XidPointValueTimeModel> models, @ApiParam(value = "Return converted value using displayed unit", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean unitConversion) throws RestValidationFailedException {
    RestProcessResult<List<XidPointValueTimeModel>> result = new RestProcessResult<List<XidPointValueTimeModel>>(HttpStatus.OK);
    List<XidPointValueTimeModel> setValues = new ArrayList<XidPointValueTimeModel>();
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        for (XidPointValueTimeModel model : models) {
            RestProcessResult<PointValueTimeModel> pointResult = setPointValue(user, model.getXid(), model, unitConversion, ServletUriComponentsBuilder.fromContextPath(request));
            if (pointResult.getHighestStatus().value() == HttpStatus.CREATED.value()) {
                // Save the model for later
                setValues.add(model);
            }
            for (RestMessage message : pointResult.getRestMessages()) {
                result.addRestMessage(message);
            }
        }
        if (setValues.size() > 0)
            return result.createResponseEntity(setValues);
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) XidPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel) User(com.serotonin.m2m2.vo.User) 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) RestMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestMessage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

XidPointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 PointValueImportResult (com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueImportResult)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)1 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)1 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)1 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)1 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)1 User (com.serotonin.m2m2.vo.User)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 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1