Skip to content

Commit

Permalink
* Set QOS to 1 and Retained as default
Browse files Browse the repository at this point in the history
* Pass subscription topic suffix as parameter (i.e. "set")
* Fix mixed up bound checks for color temperature
  • Loading branch information
dvdgeisler committed Nov 24, 2022
1 parent eb9cade commit 32355a4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,35 @@ protected String topic(final MqttBridge mqtt, final _Device device, final String
.collect(Collectors.joining("/"));
}

protected <T> MqttBridgeMessage<_Device> build(final MqttBridge mqtt, final _Device device, final String topic, final T payload) {
protected <T> MqttBridgeMessage<_Device> build(
final MqttBridge mqtt,
final _Device device,
final String topic,
final T payload) {
try {
return new MqttBridgeMessage<>(
device,
this,
this.topic(mqtt, device, topic),
new MqttMessage(this.objectMapper.writeValueAsBytes(payload)));
new MqttMessage(this.objectMapper.writeValueAsBytes(payload)){{
this.setQos(1);
this.setRetained(true);
}});
} catch (JsonProcessingException e) {
log.error(e.getMessage());
}
return null;
}

protected void subscribe(final MqttBridge mqtt, final DirigeraApi api, final _Device device) {
protected void subscribe(final MqttBridge mqtt, final DirigeraApi api, final _Device device, final String topicSuffix) {
final String topic;

try {
mqtt.subscribe(this.topic(mqtt, device, "set"), (topic, message) -> {
topic = this.topic(mqtt, device, topicSuffix);
log.info("Subscribe to device: topic={}, id={}, name={}, category={}, type={}",
topic, device.id, device.attributes.state.customName, device.deviceType, device.type);

mqtt.subscribe(this.topic(mqtt, device, topicSuffix), (t, message) -> {
final _DeviceState state;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public void setDevice(final MqttBridge mqtt, final DirigeraApi api, final LightD

Optional.ofNullable(status.color_temp)
.filter(t -> canReceive(device, "colorTemperature"))
.map(t -> max(t, getMaxTemperatureMireds(device))) // todo: getMaxTemperatureMireds may return null
.map(t -> min(t, getMinTemperatureMireds(device))) // todo: getMinTemperatureMireds may return null
.flatMap(t -> Optional.ofNullable(getMaxTemperatureMireds(device)).map(maxT -> min(t, maxT)))
.flatMap(t -> Optional.ofNullable(getMinTemperatureMireds(device)).map(minT -> max(t, minT)))
.map(LightUtils::miredsToKelvin)
.map(t -> api.device.light.setTemperature(device, t))
.ifPresent(Mono::block);
Expand Down Expand Up @@ -81,7 +81,7 @@ public Stream<MqttBridgeMessage<LightDevice>> addDevice(
final List<LightColorMode> colorModes;

colorModes = colorModes(device);
this.subscribe(mqtt, api, device);
this.subscribe(mqtt, api, device, TOPIC_SET);
return Stream.of(this.build(mqtt, device, TOPIC_CONFIG,
new LightConfig(
device.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Stream<MqttBridgeMessage<OutletDevice>> addDevice(
final DirigeraApi api,
final OutletDevice device) {

this.subscribe(mqtt, api, device);
this.subscribe(mqtt, api, device, TOPIC_SET);
return Stream.of(this.build(mqtt, device, TOPIC_CONFIG,
new OutletConfig(
device.id,
Expand Down

0 comments on commit 32355a4

Please sign in to comment.