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++;
}
}
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());
}
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();
}
Aggregations