-
Notifications
You must be signed in to change notification settings - Fork 12
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
1 parent
e60b7d0
commit 55643a1
Showing
9 changed files
with
220 additions
and
22 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
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
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: 5 additions & 4 deletions
9
...-api/src/main/java/de/dvdgeisler/iot/dirigera/client/api/model/scene/SceneTriggerApp.java
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,17 +1,18 @@ | ||
package de.dvdgeisler.iot.dirigera.client.api.model.scene; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import static de.dvdgeisler.iot.dirigera.client.api.model.scene.SceneTriggerType.APPLICATION; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class SceneTriggerApp extends SceneTrigger { | ||
public LocalDateTime triggeredAt; | ||
|
||
public SceneTriggerApp() { | ||
} | ||
|
||
public SceneTriggerApp(final String id, final Boolean disabled, final LocalDateTime triggeredAt) { | ||
super(id, APPLICATION, disabled); | ||
this.triggeredAt = triggeredAt; | ||
public SceneTriggerApp(final String id, final Boolean disabled, final LocalDateTime triggeredAt, final SceneEndTrigger endTrigger) { | ||
super(id, APPLICATION, disabled, triggeredAt, endTrigger); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...c/main/java/de/dvdgeisler/iot/dirigera/client/api/model/scene/SceneTriggerController.java
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,20 @@ | ||
package de.dvdgeisler.iot.dirigera.client.api.model.scene; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import static de.dvdgeisler.iot.dirigera.client.api.model.scene.SceneTriggerType.CONTROLLER; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class SceneTriggerController extends SceneTrigger { | ||
public SceneTriggerControllerTrigger trigger; | ||
|
||
public SceneTriggerController() { | ||
} | ||
|
||
public SceneTriggerController(final String id, final Boolean disabled, final LocalDateTime triggeredAt, final SceneEndTrigger endTrigger, final SceneTriggerControllerTrigger trigger) { | ||
super(id, CONTROLLER, disabled, triggeredAt, endTrigger); | ||
this.trigger = trigger; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...java/de/dvdgeisler/iot/dirigera/client/api/model/scene/SceneTriggerControllerTrigger.java
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,24 @@ | ||
package de.dvdgeisler.iot.dirigera.client.api.model.scene; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import de.dvdgeisler.iot.dirigera.client.api.model.device.DeviceType; | ||
|
||
import java.util.List; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class SceneTriggerControllerTrigger { | ||
public List<String> days; | ||
public DeviceType controllerType; | ||
public Integer buttonIndex; | ||
public String deviceId; | ||
|
||
public SceneTriggerControllerTrigger(final List<String> days, final DeviceType controllerType, final Integer buttonIndex, final String deviceId) { | ||
this.days = days; | ||
this.controllerType = controllerType; | ||
this.buttonIndex = buttonIndex; | ||
this.deviceId = deviceId; | ||
} | ||
|
||
public SceneTriggerControllerTrigger() { | ||
} | ||
} |
9 changes: 4 additions & 5 deletions
9
...ain/java/de/dvdgeisler/iot/dirigera/client/api/model/scene/SceneTriggerSunriseSunset.java
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
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
151 changes: 151 additions & 0 deletions
151
...src/main/java/de/dvdgeisler/iot/dirigera/client/examples/scenetriggers/SceneTriggers.java
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,151 @@ | ||
package de.dvdgeisler.iot.dirigera.client.examples.scenetriggers; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import de.dvdgeisler.iot.dirigera.client.api.DirigeraApi; | ||
import de.dvdgeisler.iot.dirigera.client.api.http.ClientApi; | ||
import de.dvdgeisler.iot.dirigera.client.api.model.device.Device; | ||
import de.dvdgeisler.iot.dirigera.client.api.model.device.DeviceType; | ||
import de.dvdgeisler.iot.dirigera.client.api.model.scene.Scene; | ||
import de.dvdgeisler.iot.dirigera.client.api.model.scene.SceneTriggerApp; | ||
import de.dvdgeisler.iot.dirigera.client.api.model.scene.SceneTriggerController; | ||
import de.dvdgeisler.iot.dirigera.client.api.model.scene.SceneTriggerControllerTrigger; | ||
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 reactor.core.publisher.Mono; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* | ||
*/ | ||
@SpringBootApplication | ||
@ComponentScan(basePackageClasses = {DirigeraApi.class}) | ||
public class SceneTriggers { | ||
private final static Logger log = LoggerFactory.getLogger(SceneTriggers.class); | ||
private final DirigeraApi dapi; | ||
private final ClientApi capi; | ||
private final ObjectMapper json; | ||
|
||
public SceneTriggers(final DirigeraApi dapi, final ClientApi capi, final ObjectMapper json) { | ||
this.dapi = dapi; | ||
this.capi = capi; | ||
this.json = json; | ||
} | ||
|
||
private static SceneTriggerController createTrigger(final Device device, final int buttonIndex) { | ||
return new SceneTriggerController( // trigger for button 0 (turn light on) | ||
null, false, null, null, | ||
new SceneTriggerControllerTrigger( | ||
null, | ||
DeviceType.SHORTCUT_CONTROLLER, | ||
buttonIndex, | ||
device.id)); | ||
} | ||
|
||
private Scene createDummyScene(final Device device, final int button) { | ||
final Scene scene; | ||
final String name; | ||
|
||
name = String.format("%s button %d", device.attributes.state.customName, button); | ||
|
||
scene = this.dapi.scene.create(name, "Icon") | ||
.doOnSuccess(s -> log.info("Created Scene {}: name={}, icon={}", s.id, s.attributes.info.name, s.attributes.info.icon)) | ||
.block(); // create dummy scene | ||
|
||
return this.dapi.scene.setTrigger(scene, List.of( | ||
new SceneTriggerApp(null, null, null, null), | ||
createTrigger(device, button)) | ||
).block(); | ||
} | ||
|
||
private Stream<Scene> createDummyScenes(final Device device) { | ||
return Stream.of( | ||
this.createDummyScene(device, 0), | ||
this.createDummyScene(device, 1), | ||
this.createDummyScene(device, 2)); | ||
} | ||
|
||
@Bean | ||
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() | ||
.block() | ||
.stream() | ||
.flatMap(this::createDummyScenes) | ||
.forEach(scenes::add); | ||
this.dapi.device.controller.shortcut // create dummy scenes for shortcut controller | ||
.all() | ||
.block() | ||
.stream() | ||
.flatMap(this::createDummyScenes) | ||
.forEach(scenes::add); | ||
this.dapi.device.controller.sound // create dummy scenes for sound controller | ||
.all() | ||
.block() | ||
.stream() | ||
.flatMap(this::createDummyScenes) | ||
.forEach(scenes::add); | ||
this.dapi.device.motionSensor // create dummy scenes for motion sensors | ||
.all() | ||
.block() | ||
.stream() | ||
.flatMap(this::createDummyScenes) | ||
.forEach(scenes::add); | ||
log.info("Press button of any connected controller"); | ||
this.capi.websocket(this::logButtonPress) | ||
.block(Duration.ofSeconds(60)); | ||
} catch (Exception e) { | ||
log.error(e.getMessage()); | ||
} | ||
|
||
scenes.stream() | ||
.peek(scene -> log.info("Delete scene {}", scene.attributes.info.name)) | ||
.map(this.dapi.scene::delete) | ||
.forEach(Mono::block); | ||
}; | ||
} | ||
|
||
private void logButtonPress(final String s) { | ||
final Map json, data, info; | ||
final String type; | ||
|
||
try { | ||
json = this.json.readValue(s, Map.class); | ||
type = json.get("type").toString(); | ||
if(!Objects.equals(type, "sceneUpdated")) { | ||
log.info("Received: {}", s); | ||
return; | ||
} | ||
data = (Map) json.get("data"); | ||
info = (Map) data.get("info"); | ||
log.info("Received: {}", info.get("name")); | ||
} catch (Throwable e) { | ||
log.error(e.getMessage()); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SceneTriggers.class, args).close(); | ||
} | ||
|
||
|
||
} |