-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
161 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/no/oyvindis/kafkaproducerclimate/KafkaProducerClimateApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/no/oyvindis/kafkaproducerclimate/config/AirthingsWebClientConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package no.oyvindis.kafkaproducerclimate.config | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.security.oauth2.client.AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager | ||
import org.springframework.security.oauth2.client.InMemoryReactiveOAuth2AuthorizedClientService | ||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository | ||
import org.springframework.security.oauth2.client.registration.InMemoryReactiveClientRegistrationRepository | ||
import org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction | ||
import org.springframework.web.reactive.function.client.WebClient | ||
|
||
|
||
private val logger: Logger = LoggerFactory.getLogger(AirthingsWebClientConfig::class.java) | ||
|
||
@Configuration | ||
open class AirthingsWebClientConfig { | ||
@Bean | ||
open fun webClient(clientRegistrationRepository: ClientRegistrationRepository): WebClient? { | ||
val registrationRepository = | ||
InMemoryReactiveClientRegistrationRepository(clientRegistrationRepository.findByRegistrationId("airthings")) | ||
val clientService = InMemoryReactiveOAuth2AuthorizedClientService(registrationRepository) | ||
val clientManager = | ||
AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(registrationRepository, clientService) | ||
return WebClient.builder() | ||
.filter(ServerOAuth2AuthorizedClientExchangeFilterFunction(clientManager)) | ||
.build() | ||
} | ||
} |
18 changes: 12 additions & 6 deletions
18
src/main/kotlin/no/oyvindis/kafkaproducerclimate/entities/Sensor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
package no.oyvindis.kafkaproducerclimate.entities | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
|
||
@JsonIgnoreProperties | ||
data class Sensor( | ||
@JsonProperty("temperature") | ||
val temperature: String, | ||
@JsonProperty("humidity") | ||
val humidity: String? | ||
) | ||
var location: String?, | ||
val battery: String? = null, | ||
val co2: String? = null, | ||
val humidity: String? = null, | ||
val pm1: String? = null, | ||
val pm25: String? = null, | ||
val pressure: String? = null, | ||
val temp: String? = null, | ||
val voc: String? = null | ||
) |
61 changes: 61 additions & 0 deletions
61
src/main/kotlin/no/oyvindis/kafkaproducerclimate/service/ClimateService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package no.oyvindis.kafkaproducerclimate.service | ||
|
||
import no.oyvindis.kafkaproducerclimate.entities.Sensor | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.kafka.core.KafkaTemplate | ||
import org.springframework.kafka.support.KafkaHeaders | ||
import org.springframework.messaging.Message | ||
import org.springframework.messaging.support.MessageBuilder | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import org.springframework.web.reactive.function.client.bodyToMono | ||
|
||
private val logger: Logger = LoggerFactory.getLogger(ClimateService::class.java) | ||
|
||
class AirthingsResponse { | ||
val data: Sensor? = null | ||
} | ||
|
||
@Service | ||
class ClimateService( | ||
private val webClient: WebClient, | ||
@Autowired | ||
val kafkaTemplate: KafkaTemplate<String, Any>, | ||
@Value("\${kafka.topics.sensor}") | ||
val topic: String, | ||
) { | ||
@Scheduled(fixedDelay = 900000) | ||
fun postReadingFromAirthings() { | ||
try { | ||
logger.debug("postReadingFromAirthings") | ||
val data = webClient | ||
.get() | ||
.uri("https://ext-api.airthings.com/v1/devices/2960009475/latest-samples") | ||
.attributes( | ||
ServerOAuth2AuthorizedClientExchangeFilterFunction | ||
.clientRegistrationId("airthings") | ||
) | ||
.retrieve() | ||
.bodyToMono<AirthingsResponse>() | ||
val responseStr = data.block() | ||
|
||
val sensorReading: Sensor? = responseStr?.data | ||
sensorReading?.location = "2960009475" | ||
sensorReading?.let { | ||
val message: Message<Sensor?> = MessageBuilder | ||
.withPayload(sensorReading) | ||
.setHeader(KafkaHeaders.TOPIC, topic) | ||
.build() | ||
kafkaTemplate.send(message) | ||
logger.info("Message sent with success") | ||
} | ||
} catch (e: Exception) { | ||
logger.error("Exception: {}", e) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 1 addition & 8 deletions
9
src/test/kotlin/no/oyvindis/kafkaproducerclimate/KafkaProducerClimateApplicationTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,6 @@ | ||
package no.oyvindis.kafkaproducerclimate | ||
|
||
import org.junit.jupiter.api.Test | ||
import org.springframework.boot.test.context.SpringBootTest | ||
|
||
@SpringBootTest | ||
class KafkaProducerClimateApplicationTests { | ||
|
||
@Test | ||
fun contextLoads() { | ||
} | ||
|
||
} | ||
class KafkaProducerClimateApplicationTests {} |