-
Notifications
You must be signed in to change notification settings - Fork 3
/
CommandController.kt
42 lines (38 loc) · 1.62 KB
/
CommandController.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.msd.command.application
import com.msd.robot.application.RobotApplicationService
import mu.KotlinLogging
import org.springframework.core.env.Environment
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import kotlin.concurrent.thread
@RestController
@RequestMapping("/commands")
class CommandController(
val robotService: RobotApplicationService,
val commandService: CommandApplicationService,
val environment: Environment
) {
private val logger = KotlinLogging.logger {}
/**
* Receives a list of commands in a string representation and executes them asynchronously.
*
* @param commandDto A DTO containing the list of commands
* @return ResponseEntity stating the success of the Command Queueing
*/
@PostMapping(consumes = ["application/json"], produces = ["application/json"])
fun receiveCommand(@RequestBody commandDto: CommandDTO): ResponseEntity<Any> {
if (commandDto.commands.isNotEmpty()) {
val commands = commandService.parseCommandsFromStrings(commandDto.commands)
if (!environment.acceptsProfiles("no-async")) {
thread(start = true, isDaemon = false) {
robotService.executeCommands(commands)
}
} else
robotService.executeCommands(commands)
}
return ResponseEntity.accepted().body("Command batch accepted")
}
}