Skip to content

Commit

Permalink
Remove explicit oauth calls in applications
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdgeisler committed Dec 7, 2022
1 parent f716c26 commit b1a9aa0
Show file tree
Hide file tree
Showing 15 changed files with 9 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package de.dvdgeisler.iot.dirigera.client.examples.discolights;

import de.dvdgeisler.iot.dirigera.client.api.DirigeraApi;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -27,16 +25,8 @@ public DiscoLights(final DirigeraApi api) {
this.api = api;
}

@Bean
public CommandLineRunner run() {
return (String... args) -> this.api.pairIfRequired().block();
}

@Scheduled(fixedDelay = 500)
public void changeState() {
if (!this.api.isPaired())
return;

this.api.device.light.all() // fetch all light devices from hub
.flatMapMany(Flux::fromIterable)
.flatMap(device ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public Dump(final ObjectMapper objectMapper) {
@Bean
public CommandLineRunner run(final DirigeraApi api) {
return (String... args) -> {
api.pairIfRequired().block();

api.dump()
.flatMap(this::toJSON)
.doOnSuccess(log::info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public CommandLineRunner runListDevices(final DirigeraApi api) {
return (String... args) -> {
final GatewayStatus status;

api.pairIfRequired().block();

status = api.status()
.filter(s -> s.attributes.ota.state == READY_TO_CHECK)
.switchIfEmpty(Mono.error(new RuntimeException("Gateway not ready")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public CommandLineRunner run(final DirigeraApi api) {
final List<GatewayDevice> gateways;
final Device device;

// pair gateway if required
api.pairIfRequired().block();

// fetch all known devices
devices = api.device.all()
.flatMapMany(Flux::fromIterable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import de.dvdgeisler.iot.dirigera.client.api.model.device.light.LightDevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -29,16 +27,8 @@ public LightMonitor(final DirigeraApi api) {
this.api = api;
}

@Bean
public CommandLineRunner run() {
return (String... args) -> this.api.pairIfRequired().block();
}

@Scheduled(fixedRate = 1000)
public void monitor() {
if(!this.api.isPaired())
return;

this.api.device.light.all()
.flatMapMany(Flux::fromIterable)
.filter(d -> d.deviceType == DeviceType.LIGHT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public CommandLineRunner run(final DirigeraApi api) {
final int receivingDeviceIndex;
final int controllingDeviceIndex;

api.pairIfRequired().block();

in = new Scanner(System.in);

receivingDevices = api.device.all()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public ListDevices(final ObjectMapper objectMapper) {
@Bean
public CommandLineRunner runListDevices(final DirigeraApi api) {
return (String... args) -> {
api.pairIfRequired().block();

api.device.all() // fetch all devices from hub
.flatMapMany(Flux::fromIterable)
.map(device -> Map.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public class RedlightDistrict {
@Bean
public CommandLineRunner run(final DirigeraApi api) {
return (String... args) -> {
api.pairIfRequired().block();

api.device.light.all()
.flatMapMany(Flux::fromIterable)
.doOnNext(d -> log.info("Found light '{}': isOn={}, hue={}, saturation={}, lightLevel={}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public Rooms(final DirigeraApi api) {
public CommandLineRunner run() {
return (String... args) -> {
Room room;
this.api.pairIfRequired().block();

room = this.api.room.create("My-First-Room", "Icon", "Color")
.doOnSuccess(r -> log.info("Created Room {}: name={}, icon={}, color={}", r.id, r.attributes.name, r.attributes.icon, r.attributes.color))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public Scenes(final DirigeraApi api) {
public CommandLineRunner run() {
return (String... args) -> {
Scene scene;
this.api.pairIfRequired().block();

scene = this.api.scene.create("My-First-Scene", "Icon")
.doOnSuccess(s -> log.info("Created Scene {}: name={}, icon={}", s.id, s.attributes.info.name, s.attributes.info.icon))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ private Stream<Scene> createDummyScenes(final Device device) {
public CommandLineRunner run() {
return (String... args) -> {
List<Scene> scenes;
this.dapi.pairIfRequired().block();


scenes = new ArrayList<>();


try {
this.dapi.device.controller.light // create dummy scenes for light controller
.all()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.dvdgeisler.iot.dirigera.client.api.DirigeraApi;
import de.dvdgeisler.iot.dirigera.client.api.http.ClientApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
Expand All @@ -21,19 +20,15 @@ public class WebSockets {
private final static Logger log = LoggerFactory.getLogger(WebSockets.class);

@Bean
public CommandLineRunner run(final DirigeraApi api, final ClientApi capi, final ObjectMapper json) {

return (String... args) -> {
api.pairIfRequired().block(); // pair gateway if required

api.websocket(event -> {
try {
log.debug("Received websocket message: {}", json.writeValueAsString(event));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
};
public CommandLineRunner run(final DirigeraApi api, final ObjectMapper json) {

return (String... args) -> api.websocket(event -> {
try {
log.info("Received websocket message: {}", json.writeValueAsString(event));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public MqttClient getMqttClient(@Value("${dirigera.mqtt.hostname:localhost}") fi
final String publisherId;
final String uri;

api.pairIfRequired().block();
publisherId = api.status().map(s -> s.id).block();
uri = String.format("tcp://%s:%d", host, port);
client = new MqttClient(uri, publisherId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public MqttEventHandler(
this.api = api;
this.objectMapper = objectMapper;

this.api.pairIfRequired().block();

log.info("Subscribe {} to Dirigera websocket: event={}", this.getClass().getSimpleName(), eventType.getSimpleName());
this.api.websocket(this::onDirigeraEvent, eventType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ protected String getTopic(final D device, final String component, final String..
protected String getGatewayId() {
synchronized (this) {
if (this.gatewayId == null) {
this.getApi().pairIfRequired().block();
this.gatewayId = this.getApi().getHome()
.map(h -> h.hub)
.map(h -> h.id)
Expand Down

0 comments on commit b1a9aa0

Please sign in to comment.