Skip to content

Commit

Permalink
Improve channel init
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Lauterbach <thomas@pop-os.amfthome.org>
  • Loading branch information
Thomas Lauterbach committed Feb 18, 2025
1 parent a424544 commit 421e819
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -34,11 +35,11 @@ public class AwtrixApp {
public static final boolean DEFAULT_TOPTEXT = false;
public static final int DEFAULT_TEXTOFFSET = 0;
public static final boolean DEFAULT_CENTER = true;
public static final int[] DEFAULT_COLOR = {};
public static final int[] DEFAULT_COLOR = { 255, 255, 255 };
public static final int[][] DEFAULT_GRADIENT = {};
public static final int DEFAULT_BLINKTEXT = 0;
public static final int DEFAULT_FADETEXT = 0;
public static final int[] DEFAULT_BACKGROUND = {};
public static final int[] DEFAULT_BACKGROUND = { 0, 0, 0 };
public static final boolean DEFAULT_RAINBOW = false;
public static final String DEFAULT_ICON = "None";
public static final int DEFAULT_PUSHICON = 0;
Expand All @@ -50,8 +51,8 @@ public class AwtrixApp {
public static final boolean DEFAULT_AUTOSCALE = true;
public static final String DEFAULT_OVERLAY = "Clear";
public static final int DEFAULT_PROGRESS = -1;
public static final int[] DEFAULT_PROGRESSC = {};
public static final int[] DEFAULT_PROGRESSBC = {};
public static final int[] DEFAULT_PROGRESSC = { 0, 255, 0 };
public static final int[] DEFAULT_PROGRESSBC = { 255, 255, 255 };
public static final int DEFAULT_SCROLLSPEED = 100;
public static final String DEFAULT_EFFECT = "None";
public static final int DEFAULT_EFFECTSPEED = 100;
Expand Down Expand Up @@ -179,30 +180,14 @@ public int[] getColor() {

public void setColor(int[] color) {
this.color = color;
if (this.gradient.length == 2) {
this.gradient[0] = color;
}
}

public int[][] getGradient() {
return this.gradient;
}

public void setGradient(int[][] gradient) {
if (gradient.length != 2) {
this.gradient = DEFAULT_GRADIENT;
} else {
this.gradient = gradient;
this.color = gradient[0];
}
}

public void setGradient(int[] gradient) {
if (gradient.length != 3) {
this.gradient = DEFAULT_GRADIENT;
} else {
this.gradient = new int[][] { this.color, gradient };
}
this.gradient = gradient;
}

public int getBlinkText() {
Expand Down Expand Up @@ -359,8 +344,9 @@ public void setEffectSettings(Map<String, Object> effectSettings) {

protected String propertiesAsString() {
return "text=" + text + ", textCase=" + textCase + ", topText=" + topText + ", textOffset=" + textOffset
+ ", center=" + center + ", color=" + Arrays.toString(color) + ", gradient=" + Arrays.toString(gradient)
+ ", blinkText=" + blinkText + ", fadeText=" + fadeText + ", background=" + Arrays.toString(background)
+ ", center=" + center + ", color=" + Arrays.toString(color) + ", gradient=["
+ Arrays.stream(gradient).map(color -> Arrays.toString(color)).collect(Collectors.joining(", "))
+ "], blinkText=" + blinkText + ", fadeText=" + fadeText + ", background=" + Arrays.toString(background)
+ ", rainbow=" + rainbow + ", icon=" + icon + ", pushIcon=" + pushIcon + ", duration=" + duration
+ ", line=" + Arrays.toString(line) + ", lifetime=" + lifetime + ", lifetimeMode=" + lifetimeMode
+ ", bar=" + Arrays.toString(bar) + ", autoscale=" + autoscale + ", overlay=" + overlay + ", progress="
Expand Down Expand Up @@ -521,7 +507,7 @@ private Map<String, Object> getColorConfig() {

private Map<String, Object> getTextEffectConfig() {
Map<String, Object> fields = new HashMap<String, Object>();
if (this.color.length == 0 || this.gradient.length == 0) {
if (Arrays.equals(this.color, DEFAULT_COLOR) && Arrays.equals(this.gradient, DEFAULT_GRADIENT)) {
if (this.blinkText > 0) {
fields.put("blinkText", this.blinkText);
} else if (this.fadeText > 0) {
Expand Down Expand Up @@ -580,12 +566,8 @@ private Map<String, Object> getProgressConfig() {
Map<String, Object> fields = new HashMap<String, Object>();
if (progress > -1 && progress <= 100) {
fields.put("progress", this.progress);
if (this.progressC.length == 3) {
fields.put("progressC", this.progressC);
}
if (this.progressBC.length == 3) {
fields.put("progressBC", this.progressBC);
}
fields.put("progressC", this.progressC);
fields.put("progressBC", this.progressBC);
}
return fields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,17 @@ public void handleCommand(ChannelUID channelUID, Command command) {
break;
case CHANNEL_COLOR:
if (command instanceof HSBType) {
this.app.setColor(ColorUtil.hsbToRgb((HSBType) command));
int[] rgb = ColorUtil.hsbToRgb((HSBType) command);
this.app.setColor(rgb);
if (this.app.getGradient().length != 0) {
this.app.setGradient(new int[][] { rgb, this.app.getGradient()[1] });
}
}
break;
case CHANNEL_GRADIENT_COLOR:
if (command instanceof HSBType) {
this.app.setGradient(ColorUtil.hsbToRgb((HSBType) command));
int[] rgb = ColorUtil.hsbToRgb((HSBType) command);
this.app.setGradient(new int[][] { this.app.getColor(), rgb });
}
break;
case CHANNEL_SCROLLSPEED:
Expand Down Expand Up @@ -533,13 +538,10 @@ private void updateApp() {
private void initStates() {
updateState(new ChannelUID(channelPrefix + CHANNEL_ACTIVE), this.active ? OnOffType.ON : OnOffType.OFF);

int[] color = this.app.getColor().length == 3 ? this.app.getColor() : new int[] { 0, 0, 0 };
int[] color = this.app.getColor();
updateState(new ChannelUID(channelPrefix + CHANNEL_COLOR), HSBType.fromRGB(color[0], color[1], color[2]));

int[] gradient = this.app.getGradient().length == 2 && this.app.getGradient()[1] != null
&& this.app.getGradient()[1].length == 3 ? this.app.getGradient()[1] : new int[] { 0, 0, 0 };
updateState(new ChannelUID(channelPrefix + CHANNEL_GRADIENT_COLOR),
HSBType.fromRGB(gradient[0], gradient[1], gradient[2]));
HSBType.fromRGB(color[0], color[1], color[2]));

updateState(new ChannelUID(channelPrefix + CHANNEL_SCROLLSPEED),
new QuantityType<>(this.app.getScrollSpeed(), Units.PERCENT));
Expand Down Expand Up @@ -585,31 +587,31 @@ private void initStates() {

updateState(new ChannelUID(channelPrefix + CHANNEL_ICON), new StringType(this.app.getIcon()));

String param = "";
if (this.app.getPushIcon() == 0) {
param = PUSH_ICON_OPTION_0;
} else if (this.app.getPushIcon() == 1) {
param = PUSH_ICON_OPTION_1;
} else if (this.app.getPushIcon() == 2) {
param = PUSH_ICON_OPTION_2;
switch (this.app.getPushIcon()) {
case 0:
updateState(new ChannelUID(channelPrefix + CHANNEL_PUSH_ICON), new StringType(PUSH_ICON_OPTION_0));
break;
case 1:
updateState(new ChannelUID(channelPrefix + CHANNEL_PUSH_ICON), new StringType(PUSH_ICON_OPTION_1));
break;
case 2:
updateState(new ChannelUID(channelPrefix + CHANNEL_PUSH_ICON), new StringType(PUSH_ICON_OPTION_2));
break;
}
updateState(new ChannelUID(channelPrefix + CHANNEL_PUSH_ICON), new StringType(param));

int[] background = this.app.getBackground().length == 3 ? this.app.getBackground() : new int[] { 0, 0, 0 };
int[] background = this.app.getBackground();
updateState(new ChannelUID(channelPrefix + CHANNEL_BACKGROUND),
HSBType.fromRGB(background[0], background[1], background[2]));

String line = this.app.getLine().length > 0 ? Arrays.toString(this.app.getLine()) : "";
updateState(new ChannelUID(channelPrefix + CHANNEL_LINE), new StringType(line));
updateState(new ChannelUID(channelPrefix + CHANNEL_LINE), new StringType(Arrays.toString(this.app.getLine())));

updateState(new ChannelUID(channelPrefix + CHANNEL_LIFETIME),
new QuantityType<>(this.app.getLifetime(), Units.SECOND));

String lifetimeMode = this.app.getLifetimeMode() == 0 ? "DELETE" : "STALE";
updateState(new ChannelUID(channelPrefix + CHANNEL_LIFETIME_MODE), new StringType(lifetimeMode));

String bar = this.app.getBar().length > 0 ? Arrays.toString(this.app.getBar()) : "";
updateState(new ChannelUID(channelPrefix + CHANNEL_BAR), new StringType(bar));
updateState(new ChannelUID(channelPrefix + CHANNEL_BAR), new StringType(Arrays.toString(this.app.getBar())));

updateState(new ChannelUID(channelPrefix + CHANNEL_AUTOSCALE),
this.app.getAutoscale() ? OnOffType.ON : OnOffType.OFF);
Expand All @@ -619,11 +621,11 @@ private void initStates() {
int progress = Math.max(this.app.getProgress(), 0);
updateState(new ChannelUID(channelPrefix + CHANNEL_PROGRESS), new QuantityType<>(progress, Units.PERCENT));

int[] progressC = this.app.getProgressC().length == 3 ? this.app.getProgressC() : new int[] { 0, 0, 0 };
int[] progressC = this.app.getProgressC();
updateState(new ChannelUID(channelPrefix + CHANNEL_PROGRESSC),
HSBType.fromRGB(progressC[0], progressC[1], progressC[2]));

int[] progressBC = this.app.getProgressBC().length == 3 ? this.app.getProgressBC() : new int[] { 0, 0, 0 };
int[] progressBC = this.app.getProgressBC();
updateState(new ChannelUID(channelPrefix + CHANNEL_PROGRESSBC),
HSBType.fromRGB(progressBC[0], progressBC[1], progressBC[2]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down Expand Up @@ -58,14 +59,17 @@ public void convertJson() {
String[] stringProperties = { FIELD_BRIDGE_APP, FIELD_BRIDGE_FIRMWARE };
String[] booleanProperties = { FIELD_BRIDGE_INDICATOR1, FIELD_BRIDGE_INDICATOR2, FIELD_BRIDGE_INDICATOR3 };
if (Arrays.stream(stringProperties).anyMatch(s::equals)) {
@Nullable
Object prop = convertedJson.get(s);
assertNotNull(prop);
assertEquals(String.class, prop.getClass());
} else if (Arrays.stream(booleanProperties).anyMatch(s::equals)) {
@Nullable
Object prop = convertedJson.get(s);
assertNotNull(prop);
assertEquals(Boolean.class, prop.getClass());
} else {
@Nullable
Object prop = convertedJson.get(s);
assertNotNull(prop);
assertEquals(Double.class, prop.getClass());
Expand Down Expand Up @@ -115,7 +119,7 @@ private AwtrixApp getTestAppWithGradient() {
AwtrixApp app = new AwtrixApp();
app.setText("Test");
app.setColor(new int[] { 255, 255, 255 });
app.setGradient(new int[] { 255, 0, 0 });
app.setGradient(new int[][] { { 255, 255, 255 }, { 255, 0, 0 } });
return app;
}

Expand Down

0 comments on commit 421e819

Please sign in to comment.