use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-modules-public by infiniteautomation.
the class DataSourceRestController method saveDataSource.
@ApiOperation(value = "Save data source")
@RequestMapping(method = { RequestMethod.POST }, produces = { "application/json" })
public ResponseEntity<AbstractDataSourceModel<?>> saveDataSource(@RequestBody(required = true) AbstractDataSourceModel<?> model, UriComponentsBuilder builder, HttpServletRequest request) {
RestProcessResult<AbstractDataSourceModel<?>> result = new RestProcessResult<AbstractDataSourceModel<?>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
try {
if (!Permissions.hasDataSourcePermission(user)) {
result.addRestMessage(this.getUnauthorizedMessage());
return result.createResponseEntity();
}
} catch (PermissionException pe) {
LOG.warn(pe.getMessage(), pe);
result.addRestMessage(this.getUnauthorizedMessage());
return result.createResponseEntity();
}
DataSourceVO<?> vo = model.getData();
// Check to see if the data source already exists
if (!StringUtils.isEmpty(vo.getXid())) {
DataSourceVO<?> existing = (DataSourceVO<?>) DataSourceDao.instance.getByXid(model.getXid());
if (existing != null) {
result.addRestMessage(HttpStatus.CONFLICT, new TranslatableMessage("rest.exception.alreadyExists", model.getXid()));
return result.createResponseEntity();
}
}
if (StringUtils.isEmpty(vo.getXid()))
vo.setXid(DataSourceDao.instance.generateUniqueXid());
if (!model.validate() || !Permissions.hasPermission(vo.getEditPermission(), user.getPermissions())) {
result.addRestMessage(this.getValidationFailedError());
return result.createResponseEntity(model);
} else {
Common.runtimeManager.saveDataSource(vo);
DataSourceVO<?> created = (DataSourceVO<?>) DataSourceDao.instance.getByXid(model.getXid());
URI location = builder.path("/v1/data-sources/{xid}").buildAndExpand(new Object[] { created.asModel().getXid() }).toUri();
result.addRestMessage(this.getResourceCreatedMessage(location));
return result.createResponseEntity(created.asModel());
}
} else {
return result.createResponseEntity();
}
}
use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-modules-public by infiniteautomation.
the class DataSourceRestController method enableDisable.
@ApiOperation(value = "Enable/disable/restart a data source")
@RequestMapping(method = RequestMethod.PUT, value = "/enable-disable/{xid}")
public ResponseEntity<DataPointModel> enableDisable(@AuthenticationPrincipal User user, @PathVariable String xid, @ApiParam(value = "Enable or disable the data source", required = true, allowMultiple = false) @RequestParam(required = true) boolean enabled, @ApiParam(value = "Restart the data source, enabled must equal true", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean restart) {
DataSourceVO<?> dsvo = DataSourceDao.instance.getByXid(xid);
if (dsvo == null)
throw new NotFoundRestException();
try {
Permissions.ensureDataSourcePermission(user, dsvo);
} catch (PermissionException e) {
throw new AccessDeniedException("User does not have permission to edit the data source", e);
}
if (enabled && restart) {
dsvo.setEnabled(true);
// saving will restart it
Common.runtimeManager.saveDataSource(dsvo);
} else if (dsvo.isEnabled() != enabled) {
dsvo.setEnabled(enabled);
Common.runtimeManager.saveDataSource(dsvo);
}
return new ResponseEntity<>(HttpStatus.OK);
}
use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-modules-public by infiniteautomation.
the class DataSourceRestController method getDataSource.
@ApiOperation(value = "Get data source by xid", notes = "Only returns data sources available to logged in user")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}", produces = { "application/json" })
public ResponseEntity<AbstractDataSourceModel<?>> getDataSource(HttpServletRequest request, @PathVariable String xid) {
RestProcessResult<AbstractDataSourceModel<?>> result = new RestProcessResult<AbstractDataSourceModel<?>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
DataSourceVO<?> vo = DataSourceDao.instance.getByXid(xid);
if (vo == null) {
return new ResponseEntity<AbstractDataSourceModel<?>>(HttpStatus.NOT_FOUND);
} else {
try {
if (Permissions.hasDataSourcePermission(user, vo))
return result.createResponseEntity(vo.asModel());
else {
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
} catch (PermissionException e) {
LOG.warn(e.getMessage(), e);
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
}
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-modules-public by infiniteautomation.
the class SerialDataSourceTestData method getCustomPoint.
public static DataPointRT getCustomPoint(String name, String xid, String valueRegex, int valueIndex, String pointIdentifier, DataSourceVO<?> ds) {
DataPointVO vo = new DataPointVO();
vo.setName(name);
vo.setXid(xid);
vo.setId(currentId++);
SerialPointLocatorVO plVo = new SerialPointLocatorVO();
plVo.setDataTypeId(DataTypes.ALPHANUMERIC);
plVo.setValueRegex(valueRegex);
plVo.setValueIndex(valueIndex);
plVo.setPointIdentifier(pointIdentifier);
vo.setPointLocator(plVo);
return new DataPointRT(vo, plVo.createRuntime(), ds, null, null);
}
use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.
the class DataSourceDwr method getAlarms.
/**
* Get the current alarms for a datasource
*
* @param id
* @return
*/
@DwrPermission(user = true)
public List<EventInstanceBean> getAlarms(int id) {
DataSourceVO<?> ds = Common.runtimeManager.getDataSource(id);
List<EventInstanceBean> beans = new ArrayList<>();
if (ds != null) {
List<EventInstance> events = EventDao.instance.getPendingEventsForDataSource(ds.getId(), Common.getUser().getId());
if (events != null) {
for (EventInstance event : events) beans.add(new EventInstanceBean(event.isActive(), event.getAlarmLevel(), Functions.getTime(event.getActiveTimestamp()), translate(event.getMessage())));
}
}
return beans;
}
Aggregations