Skip to content

Commit

Permalink
lifetime and overlay channels added
Browse files Browse the repository at this point in the history
  • Loading branch information
DrRSatzteil committed Apr 10, 2024
1 parent 8a13407 commit 3851f19
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ public class AwtrixLightBindingConstants {
public static final String CHANNEL_FADE_TEXT = "fadeText";
public static final String CHANNEL_GRADIENT_COLOR = "gradientColor";
public static final String CHANNEL_ICON = "icon";
public static final String CHANNEL_LIFETIME = "lifetime";
public static final String CHANNEL_LIFETIME_MODE = "lifetimeMode";
public static final String CHANNEL_LINE = "line";
public static final String CHANNEL_OVERLAY = "overlay";
public static final String CHANNEL_PROGRESS = "progress";
public static final String CHANNEL_PROGRESSC = "progressColor";
public static final String CHANNEL_PROGRESSBC = "progressBackground";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,15 @@ private static BigDecimal[] decodeJsonPrimitiveArray(JsonArray jsonArray) {
}
return array;
}

public static BigDecimal[] leftTrim(BigDecimal[] data, int length) {
if (length < data.length) {
BigDecimal[] trimmed = new BigDecimal[length];
for (int i = data.length - length; i < data.length; i++) {
trimmed[i - (data.length - length)] = data[i];
}
return trimmed;
}
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ public class AwtrixApp {
public static final BigDecimal DEFAULT_PUSHICON = BigDecimal.ZERO;
public static final BigDecimal DEFAULT_DURATION = new BigDecimal(7);
public static final BigDecimal[] DEFAULT_LINE = {};
public static final BigDecimal DEFAULT_LIFETIME = BigDecimal.ZERO;
public static final BigDecimal DEFAULT_LIFETIME_MODE = BigDecimal.ZERO;
public static final BigDecimal[] DEFAULT_BAR = {};
public static final boolean DEFAULT_AUTOSCALE = true;
public static final String DEFAULT_OVERLAY = "Clear";
public static final BigDecimal DEFAULT_PROGRESS = MINUSONE;
public static final BigDecimal[] DEFAULT_PROGRESSC = {};
public static final BigDecimal[] DEFAULT_PROGRESSBC = {};
Expand All @@ -73,8 +76,11 @@ public class AwtrixApp {
private BigDecimal pushIcon = DEFAULT_PUSHICON;
private BigDecimal duration = DEFAULT_DURATION;
private BigDecimal[] line = DEFAULT_LINE;
private BigDecimal lifetime = DEFAULT_LIFETIME;
private BigDecimal lifetimeMode = DEFAULT_LIFETIME_MODE;
private BigDecimal[] bar = DEFAULT_BAR;
private boolean autoscale = DEFAULT_AUTOSCALE;
private String overlay = DEFAULT_OVERLAY;
private BigDecimal progress = DEFAULT_PROGRESS;
private BigDecimal[] progressC = DEFAULT_PROGRESSC;
private BigDecimal[] progressBC = DEFAULT_PROGRESSBC;
Expand Down Expand Up @@ -102,8 +108,11 @@ public void updateFields(Map<String, Object> params) {
this.pushIcon = getNumberValue(params, "pushIcon", DEFAULT_PUSHICON);
this.duration = getNumberValue(params, "duration", DEFAULT_DURATION);
this.line = getNumberArrayValue(params, "line", DEFAULT_LINE);
this.lifetime = getNumberValue(params, "lifetime", DEFAULT_LIFETIME);
this.lifetimeMode = getNumberValue(params, "lifetimeMode", DEFAULT_LIFETIME_MODE);
this.bar = getNumberArrayValue(params, "bar", DEFAULT_BAR);
this.autoscale = getBoolValue(params, "autoscale", DEFAULT_AUTOSCALE);
this.overlay = getStringValue(params, "overlay", DEFAULT_OVERLAY);
this.progress = getNumberValue(params, "progress", DEFAULT_PROGRESS);
this.progressC = getNumberArrayValue(params, "progressC", DEFAULT_PROGRESSC);
this.progressBC = getNumberArrayValue(params, "progressBC", DEFAULT_PROGRESSBC);
Expand All @@ -120,29 +129,6 @@ public void updateFields(Map<String, Object> params) {
this.effectBlend = getBoolValue(params, "effectBlend", DEFAULT_EFFECTBLEND);
}

protected Map<String, Object> getAppParams() {
Map<String, Object> fields = new HashMap<String, Object>();
fields.put("text", this.text);
fields.put("textCase", this.textCase);
fields.put("topText", this.topText);
fields.put("textOffset", this.textOffset);
fields.put("center", this.center);
fields.putAll(getColorConfig());
fields.putAll(getTextEffectConfig());
fields.putAll(getBackgroundConfig());
fields.putAll(getIconConfig());
fields.put("duration", this.duration);
fields.putAll(getGraphConfig());
fields.putAll(getProgressConfig());
if (this.scrollSpeed.compareTo(BigDecimal.ZERO) == 0) {
fields.put("noScroll", true);
} else {
fields.put("scrollSpeed", this.scrollSpeed);
}
fields.putAll(getEffectConfig());
return fields;
}

public String getAppConfig() {
Map<String, Object> fields = getAppParams();
return Helper.encodeJson(fields);
Expand Down Expand Up @@ -268,6 +254,22 @@ public void setLine(BigDecimal[] line) {
this.line = line;
}

public BigDecimal getLifetime() {
return this.lifetime;
}

public void setLifetime(BigDecimal lifetime) {
this.lifetime = lifetime;
}

public BigDecimal getLifetimeMode() {
return this.lifetimeMode;
}

public void setLifetimeMode(BigDecimal lifetimeMode) {
this.lifetimeMode = lifetimeMode;
}

public BigDecimal[] getBar() {
return this.bar;
}
Expand All @@ -284,6 +286,14 @@ public void setAutoscale(Boolean autoscale) {
this.autoscale = autoscale;
}

public String getOverlay() {
return this.overlay;
}

public void setOverlay(String overlay) {
this.overlay = overlay;
}

public BigDecimal getProgress() {
return this.progress;
}
Expand Down Expand Up @@ -358,13 +368,40 @@ public String toString() {
+ textOffset + ", center=" + center + ", color=" + Arrays.toString(color) + ", gradient="
+ Arrays.toString(gradient) + ", blinkText=" + blinkText + ", fadeText=" + fadeText + ", background="
+ Arrays.toString(background) + ", rainbow=" + rainbow + ", icon=" + icon + ", pushIcon=" + pushIcon
+ ", duration=" + duration + ", line=" + Arrays.toString(line) + ", bar=" + Arrays.toString(bar)
+ ", autoscale=" + autoscale + ", progress=" + progress + ", progressC=" + Arrays.toString(progressC)
+ ", duration=" + duration + ", line=" + Arrays.toString(line) + ", lifetime=" + lifetime
+ ", lifetimeMode=" + lifetimeMode + ", bar=" + Arrays.toString(bar) + ", autoscale=" + autoscale
+ ", overlay=" + overlay + ", progress=" + progress + ", progressC=" + Arrays.toString(progressC)
+ ", progressBC=" + Arrays.toString(progressBC) + ", scrollSpeed=" + scrollSpeed + ", effect=" + effect
+ ", effectSpeed=" + effectSpeed + ", effectPalette=" + effectPalette + ", effectBlend=" + effectBlend
+ "]";
}

protected Map<String, Object> getAppParams() {
Map<String, Object> fields = new HashMap<String, Object>();
fields.put("text", this.text);
fields.put("textCase", this.textCase);
fields.put("topText", this.topText);
fields.put("textOffset", this.textOffset);
fields.put("center", this.center);
fields.put("lifetime", this.lifetime);
fields.put("lifetimeMode", this.lifetimeMode);
fields.put("overlay", this.overlay);
fields.putAll(getColorConfig());
fields.putAll(getTextEffectConfig());
fields.putAll(getBackgroundConfig());
fields.putAll(getIconConfig());
fields.put("duration", this.duration);
fields.putAll(getGraphConfig());
fields.putAll(getProgressConfig());
if (this.scrollSpeed.compareTo(BigDecimal.ZERO) == 0) {
fields.put("noScroll", true);
} else {
fields.put("scrollSpeed", this.scrollSpeed);
}
fields.putAll(getEffectConfig());
return fields;
}

private boolean getBoolValue(Map<String, Object> params, String key, boolean defaultValue) {
if (params.containsKey(key)) {
Object value = params.get(key);
Expand Down Expand Up @@ -501,16 +538,16 @@ private Map<String, Object> getGraphConfig() {
if (this.bar.length > 0) {
graphType = "bar";
if ("None".equals(this.icon)) {
data = leftTrim(this.bar, 16);
data = Helper.leftTrim(this.bar, 16);
} else {
data = leftTrim(this.bar, 11);
data = Helper.leftTrim(this.bar, 11);
}
} else if (this.line.length > 0) {
graphType = "line";
if ("None".equals(this.icon)) {
data = leftTrim(this.line, 16);
data = Helper.leftTrim(this.line, 16);
} else {
data = leftTrim(this.line, 11);
data = Helper.leftTrim(this.line, 11);
}
}
if (graphType != null && data != null) {
Expand All @@ -520,17 +557,6 @@ private Map<String, Object> getGraphConfig() {
return fields;
}

private BigDecimal[] leftTrim(BigDecimal[] data, int length) {
if (length < data.length) {
BigDecimal[] trimmed = new BigDecimal[length];
for (int i = data.length - length; i <= data.length; i++) {
trimmed[i - data.length - length] = data[i];
}
return trimmed;
}
return data;
}

private Map<String, Object> getProgressConfig() {
Map<String, Object> fields = new HashMap<String, Object>();
if (progress.compareTo(MINUSONE) > 0 && progress.compareTo(ONEHUNDRED) <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.CHANNEL_FADE_TEXT;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.CHANNEL_GRADIENT_COLOR;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.CHANNEL_ICON;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.CHANNEL_LIFETIME;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.CHANNEL_LIFETIME_MODE;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.CHANNEL_LINE;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.CHANNEL_OVERLAY;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.CHANNEL_PROGRESS;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.CHANNEL_PROGRESSBC;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.CHANNEL_PROGRESSC;
Expand Down Expand Up @@ -285,6 +288,23 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
}
break;
case CHANNEL_LIFETIME:
if (command instanceof QuantityType) {
this.app.setLifetime(((QuantityType<?>) command).toBigDecimal());
}
break;
case CHANNEL_LIFETIME_MODE:
if (command instanceof StringType) {
switch (command.toString()) {
case "DELETE":
this.app.setLifetimeMode(new BigDecimal(0));
break;
case "STALE":
this.app.setLifetimeMode(new BigDecimal(1));
break;
}
}
break;
case CHANNEL_BAR:
if (command instanceof StringType) {
try {
Expand All @@ -305,6 +325,11 @@ public void handleCommand(ChannelUID channelUID, Command command) {
this.app.setAutoscale(command.equals(OnOffType.ON));
}
break;
case CHANNEL_OVERLAY:
if (command instanceof StringType) {
this.app.setOverlay(((StringType) command).toString());
}
break;
case CHANNEL_PROGRESS:
if (command instanceof QuantityType) {
this.app.setProgress(((QuantityType<?>) command).toBigDecimal());
Expand Down Expand Up @@ -392,12 +417,21 @@ public void channelUnlinked(ChannelUID channelUID) {
case CHANNEL_LINE:
this.app.setLine(AwtrixApp.DEFAULT_LINE);
break;
case CHANNEL_LIFETIME:
this.app.setLifetime(AwtrixApp.DEFAULT_LIFETIME);
break;
case CHANNEL_LIFETIME_MODE:
this.app.setLifetimeMode(AwtrixApp.DEFAULT_LIFETIME_MODE);
break;
case CHANNEL_BAR:
this.app.setBar(AwtrixApp.DEFAULT_BAR);
break;
case CHANNEL_AUTOSCALE:
this.app.setAutoscale(AwtrixApp.DEFAULT_AUTOSCALE);
break;
case CHANNEL_OVERLAY:
this.app.setOverlay(AwtrixApp.DEFAULT_OVERLAY);
break;
case CHANNEL_PROGRESS:
this.app.setProgress(AwtrixApp.DEFAULT_PROGRESS);
break;
Expand All @@ -414,6 +448,7 @@ public void channelUnlinked(ChannelUID channelUID) {

@Override
public void channelLinked(ChannelUID channelUID) {
// TODO: Do not update all channels when any channel is linked
initStates();
}

Expand Down Expand Up @@ -625,13 +660,21 @@ private void initStates() {
String lineString = Arrays.stream(line).map(BigDecimal::toString).collect(Collectors.joining(","));
updateState(new ChannelUID(channelPrefix + CHANNEL_LINE), new StringType(lineString));

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

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

BigDecimal[] bar = this.app.getBar().length > 0 ? this.app.getBar() : new BigDecimal[] { BigDecimal.ZERO };
String barString = Arrays.stream(bar).map(BigDecimal::toString).collect(Collectors.joining(","));
updateState(new ChannelUID(channelPrefix + CHANNEL_BAR), new StringType(barString));

updateState(new ChannelUID(channelPrefix + CHANNEL_AUTOSCALE),
this.app.getAutoscale() ? OnOffType.ON : OnOffType.OFF);

updateState(new ChannelUID(channelPrefix + CHANNEL_OVERLAY), new StringType(this.app.getOverlay()));

BigDecimal progress = this.app.getProgress().compareTo(BigDecimal.ZERO) > 0 ? this.app.getProgress()
: BigDecimal.ZERO;
updateState(new ChannelUID(channelPrefix + CHANNEL_PROGRESS), new QuantityType<>(progress, Units.PERCENT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@
<channel id="background" typeId="background"/>
<channel id="duration" typeId="duration"/>
<channel id="line" typeId="line"/>
<channel id="lifetime" typeId="lifetime"/>
<channel id="lifetimeMode" typeId="lifetimeMode"/>
<channel id="bar" typeId="bar"/>
<channel id="autoscale" typeId="autoscale"/>
<channel id="overlay" typeId="overlay"/>
<channel id="progress" typeId="progress"/>
<channel id="progressColor" typeId="progressColor"/>
<channel id="progressBackground" typeId="progressBackground"/>
Expand Down Expand Up @@ -420,20 +423,45 @@
<option value="PUSHOUTRETURN">Icon moves and reappears</option>
</options>
</state>

</channel-type>

<channel-type id="line">
<item-type>String</item-type>
<label>Line Graph</label>
<description>Draw a line graph (format: "value1,value2,value3", last 16 entries will be displayed, last 11 with icon)</description>
<category>qualityofservice</category>
<tags>
<tag>Control</tag>
</tags>
</channel-type>

<channel-type id="lifetime">
<item-type>Number:Time</item-type>
<label>App Lifetime</label>
<description>Remove the app if there was no update within specified seconds</description>
<category>time</category>
<tags>
<tag>Control</tag>
<tag>Duration</tag>
</tags>
</channel-type>

<channel-type id="lifetimeMode">
<item-type>String</item-type>
<label>App Lifetime Mode</label>
<description>Delete the app or mark as stale after lifetime</description>
<category>screen</category>
<tags>
<tag>Control</tag>
</tags>
<state>
<options>
<option value="DELETE">Delete</option>
<option value="STALE">Mark stale</option>
</options>
</state>
</channel-type>

<channel-type id="bar">
<item-type>String</item-type>
<label>Bar Graph</label>
Expand All @@ -454,6 +482,27 @@
</tags>
</channel-type>

<channel-type id="overlay">
<item-type>String</item-type>
<label>App Overlay</label>
<description>Overlay effect (overriden by global clock overlay)</description>
<category>screen</category>
<tags>
<tag>Control</tag>
</tags>
<state>
<options>
<option value="clear">Clear</option>
<option value="snow">Snow</option>
<option value="rain">Rain</option>
<option value="drizzle">Drizzle</option>
<option value="storm">Storm</option>
<option value="thunder">Thunder</option>
<option value="frost">Frost</option>
</options>
</state>
</channel-type>

<channel-type id="progress">
<item-type>Number:Dimensionless</item-type>
<label>Progress Bar</label>
Expand Down
Loading

0 comments on commit 3851f19

Please sign in to comment.