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> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId> <artifactId>spring-boot-starter-cache</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.module</groupId> <groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId> <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.RequestBody
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController 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 import javax.validation.Valid
@RestController @RestController
@RequestMapping("/api") @RequestMapping("/api")
@CrossOrigin
class Controller { class Controller {
@Autowired
lateinit var websocketMessenger: SimpMessagingTemplate
@Autowired @Autowired
lateinit var siteRepository: SiteRepository lateinit var siteRepository: SiteRepository
@Autowired @Autowired
@ -104,6 +111,7 @@ class Controller {
it.observations.add(observation) it.observations.add(observation)
tutorRepository.save(it) tutorRepository.save(it)
} }
sendObservationToSocket(observation)
return observation.id return observation.id
} }
@ -223,6 +231,15 @@ class Controller {
} }
return AfiPieChart(AfiPieChartDataset(monitoring, knowledge, control, conservatism, teamwork)) 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( 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()
}
}