Laid foundation for pushing data via websocket.

This commit is contained in:
neviyn 2019-02-12 16:01:18 +00:00
parent fd5e5d9a55
commit d30104eafa
3 changed files with 41 additions and 0 deletions

View File

@ -60,6 +60,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>

View File

@ -9,12 +9,19 @@ 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 org.springframework.messaging.simp.SimpMessagingTemplate
import org.springframework.web.bind.annotation.CrossOrigin
import java.net.InetAddress
import javax.validation.Valid
@RestController
@RequestMapping("/api")
@CrossOrigin
class Controller {
@Autowired
lateinit var websocketMessenger: SimpMessagingTemplate
@Autowired
lateinit var siteRepository: SiteRepository
@Autowired
@ -104,6 +111,7 @@ class Controller {
it.observations.add(observation)
tutorRepository.save(it)
}
sendObservationToSocket(observation)
return observation.id
}
@ -223,6 +231,15 @@ class Controller {
}
return AfiPieChart(AfiPieChartDataset(monitoring, knowledge, control, conservatism, teamwork))
}
fun sendObservationToSocket(observation: Observation) {
websocketMessenger.convertAndSend("/ws/observations", observation)
}
@GetMapping("/address")
fun getConnectionDetails(): Map<String, String> {
return mapOf("ip" to InetAddress.getLocalHost().hostAddress)
}
}
data class AverageData(

View File

@ -0,0 +1,20 @@
package uk.co.neviyn.observationdatabase
import org.springframework.context.annotation.Configuration
import org.springframework.messaging.simp.config.MessageBrokerRegistry
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker
import org.springframework.web.socket.config.annotation.StompEndpointRegistry
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer
@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfig : WebSocketMessageBrokerConfigurer {
override fun configureMessageBroker(registry: MessageBrokerRegistry) {
registry.enableSimpleBroker("/ws")
}
override fun registerStompEndpoints(registry: StompEndpointRegistry) {
registry.addEndpoint("/websocket").withSockJS()
}
}