Search in sources :

Example 1 with SystemActionDefinition

use of com.serotonin.m2m2.module.SystemActionDefinition in project ma-modules-public by infiniteautomation.

the class SystemActionRestV2Controller method list.

@ApiOperation(value = "List Available Actions", notes = "", response = List.class, responseContainer = "Array")
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<List<String>> list() {
    Map<String, SystemActionDefinition> defs = ModuleRegistry.getSystemActionDefinitions();
    List<String> model = new ArrayList<String>();
    for (SystemActionDefinition def : defs.values()) model.add(def.getKey());
    return new ResponseEntity<>(model, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ArrayList(java.util.ArrayList) SystemActionDefinition(com.serotonin.m2m2.module.SystemActionDefinition) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with SystemActionDefinition

use of com.serotonin.m2m2.module.SystemActionDefinition in project ma-modules-public by infiniteautomation.

the class SystemActionRestV2Controller method performAction.

@ApiOperation(value = "Perform an Action", notes = "Kicks off action and returns temporary URL for status")
@ApiResponses({ @ApiResponse(code = 500, message = "Internal error", response = ResponseEntity.class), @ApiResponse(code = 404, message = "Not Found", response = ResponseEntity.class) })
@RequestMapping(method = RequestMethod.PUT, value = "/trigger/{action}")
public ResponseEntity<SystemActionTemporaryResource> performAction(@ApiParam(value = "Valid System Action", required = true, allowMultiple = false) @PathVariable String action, @ApiParam(value = "Input for task", required = false, allowMultiple = false) @RequestBody(required = false) JsonNode input, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
    // Kick off action
    SystemActionDefinition def = ModuleRegistry.getSystemActionDefinition(action);
    if (def == null)
        throw new NotFoundRestException();
    String resourceId = resources.generateResourceId();
    SystemActionTemporaryResource resource = new SystemActionTemporaryResource(resourceId, def.getTask(user, input), resources, new Date(System.currentTimeMillis() + 600000));
    // Resource can live for up to 10 minutes (TODO Configurable?)
    resources.put(resourceId, resource);
    URI location = builder.path("/v2/actions/status/{resourceId}").buildAndExpand(resourceId).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) SystemActionTemporaryResource(com.infiniteautomation.mango.rest.v2.util.SystemActionTemporaryResource) URI(java.net.URI) Date(java.util.Date) SystemActionDefinition(com.serotonin.m2m2.module.SystemActionDefinition) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

SystemActionDefinition (com.serotonin.m2m2.module.SystemActionDefinition)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)1 SystemActionTemporaryResource (com.infiniteautomation.mango.rest.v2.util.SystemActionTemporaryResource)1 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HttpHeaders (org.springframework.http.HttpHeaders)1