Skip to content

Commit

Permalink
Bi-directional global brightness updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels van de Weem committed Nov 17, 2024
1 parent bf442b3 commit 336f600
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void buildSubscriptions(Device device, LightingConfig lighting) {
var newBrightness = NumberUtils.toInt(payload, 100);
lighting.setGlobalBrightness(newBrightness);
andThen.run();
applicationEventPublisher.publishEvent(new HomePage.GlobalBrightnessChangedEvent(newBrightness));
applicationEventPublisher.publishEvent(new HomePage.GlobalBrightnessChangedEvent(this, device.getSerialNumber(), newBrightness));
});
subscribeToColors(lighting.getKnobConfigs(), topicHelper, dial, knobOverride, idx -> device.getLightingConfig().getKnobConfigs()[idx].getColor1());
subscribeToColors(lighting.getSliderConfigs(), topicHelper, slider, sliderOverride, idx -> device.getLightingConfig().getSliderConfigs()[idx].getColor1());
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/getpcpanel/mqtt/MqttDeviceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.getpcpanel.profile.SingleKnobLightingConfig;
import com.getpcpanel.profile.SingleSliderLabelLightingConfig;
import com.getpcpanel.profile.SingleSliderLightingConfig;
import com.getpcpanel.ui.HomePage.GlobalBrightnessChangedEvent;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
Expand Down Expand Up @@ -116,6 +117,11 @@ public void buttonPress(ButtonClickEvent btn) {
});
}

@EventListener
public void globalBrightnessChange(GlobalBrightnessChangedEvent event) {
mqtt.send(mqttTopicHelper.valueTopic(event.serialNr(), brightness, 0), String.valueOf(event.brightness()), false);
}

private void initialize(Device device) {
if (initializedDevices.contains(device)) {
return;
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/getpcpanel/ui/HomePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -45,6 +46,7 @@ public class HomePage extends Application {
private final SaveService saveService;
private final DeviceScanner deviceScanner;
private final DeviceHolder devices;
private final ApplicationEventPublisher applicationEventPublisher;

@Value("${application.version}") private String version;
@Value("${application.build}") private String build;
Expand Down Expand Up @@ -105,9 +107,11 @@ public void start(Stage stage) throws Exception {
deviceScanner.init();
}

@EventListener(GlobalBrightnessChangedEvent.class)
public void globalBrightnessChanged() {
globalBrightness.setValue(connectedDeviceList.getSelectionModel().getSelectedItem().getLightingConfig().getGlobalBrightness());
@EventListener
public void globalBrightnessChanged(GlobalBrightnessChangedEvent event) {
if (!event.isSource(this)) {
globalBrightness.setValue(connectedDeviceList.getSelectionModel().getSelectedItem().getLightingConfig().getGlobalBrightness());
}
}

private void addBrightnessListener() {
Expand All @@ -130,6 +134,7 @@ private void addBrightnessListener() {
var light = device.getLightingConfig();
light.setGlobalBrightness(newValue.byteValue());
device.setLighting(light, false);
applicationEventPublisher.publishEvent(new GlobalBrightnessChangedEvent(this, serialNumber, newValue.intValue()));
});
}

Expand Down Expand Up @@ -251,6 +256,10 @@ public void onSaveEvent(SaveService.SaveEvent event) {
public record ShowMainEvent() {
}

public record GlobalBrightnessChangedEvent(int brightness) {
public record GlobalBrightnessChangedEvent(Object source, String serialNr, int brightness) {
public boolean isSource(Object other) {
//noinspection ObjectEquality
return source == other;
}
}
}

0 comments on commit 336f600

Please sign in to comment.