Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DrRSatzteil committed Sep 19, 2023
1 parent 751d506 commit e26c371
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@
@Component(service = ThingHandlerFactory.class)
@NonNullByDefault
public class AwtrixLightHandlerFactory extends BaseThingHandlerFactory {
private final ThingRegistry thingRegistry;

@Activate
public AwtrixLightHandlerFactory(final @Reference ThingRegistry thingRegistry) {
this.thingRegistry = thingRegistry;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/
package org.openhab.binding.mqtt.awtrixlight.internal;

import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.*;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.SCREEN_HEIGHT;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.SCREEN_WIDTH;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -61,16 +62,12 @@ public static HashMap<String, Object> decodeJson(String messageJSON) {
JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();
if (jsonPrimitive.isString()) {
String s = jsonPrimitive.getAsString();
if (s != null) {
results.put(keyString, s);
}
results.put(keyString, s);
} else if (jsonPrimitive.isBoolean()) {
results.put(keyString, jsonPrimitive.getAsBoolean());
} else if (jsonPrimitive.isNumber()) {
BigDecimal bd = jsonPrimitive.getAsBigDecimal();
if (bd != null) {
results.put(keyString, bd);
}
results.put(keyString, bd);
}
} else if (jsonElement.isJsonArray()) {
JsonArray jsonArray = jsonElement.getAsJsonArray();
Expand All @@ -95,10 +92,7 @@ public static HashMap<String, Object> decodeJson(String messageJSON) {

}
} else if (jsonElement.isJsonObject()) {
String jsonString = jsonElement.toString();
if (jsonString != null) {
results.put(keyString, decodeJson(jsonString));
}
results.put(keyString, decodeJson(jsonElement.toString()));
}
}
}
Expand All @@ -107,20 +101,13 @@ public static HashMap<String, Object> decodeJson(String messageJSON) {
}

public static String encodeJson(Map<String, Object> params) {
String json = new Gson().toJson(params);
return json == null ? "" : json;
return new Gson().toJson(params);
}

public static byte[] decodeImage(String messageJSON) {
String cutBrackets = messageJSON.substring(1, messageJSON.length() - 1);

if (messageJSON != null) {
String cutBrackets = messageJSON.substring(1, messageJSON.length() - 1);
if (cutBrackets != null) {
messageJSON = cutBrackets;
}
}

String[] pixelStrings = messageJSON.split(",");
String[] pixelStrings = cutBrackets.split(",");
int[] values = Arrays.stream(pixelStrings).mapToInt(Integer::parseInt).toArray();
int[][] pixels = new int[SCREEN_HEIGHT][SCREEN_WIDTH];
for (int i = 0; i < 256; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
package org.openhab.binding.mqtt.awtrixlight.internal.discovery;

import static org.openhab.binding.mqtt.MqttBindingConstants.BINDING_ID;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.*;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.AWTRIX_APP;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.PROP_APPID;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.PROP_APPNAME;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.PROP_UNIQUEID;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.THING_TYPE_APP;

import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -84,12 +88,10 @@ protected void startScan() {
}

void publishApp(ThingUID connectionBridgeUid, String bridgeHardwareId, String basetopic, String appName) {
if (bridgeHardwareId != null && appName != null) {
String appId = bridgeHardwareId + "-" + appName;
thingDiscovered(DiscoveryResultBuilder
.create(new ThingUID(new ThingTypeUID(BINDING_ID, AWTRIX_APP), connectionBridgeUid, appName))
.withBridge(connectionBridgeUid).withProperty(PROP_APPID, appId).withProperty(PROP_APPNAME, appName)
.withRepresentationProperty(PROP_APPID).withLabel("Awtrix App " + appName).build());
}
String appId = bridgeHardwareId + "-" + appName;
thingDiscovered(DiscoveryResultBuilder
.create(new ThingUID(new ThingTypeUID(BINDING_ID, AWTRIX_APP), connectionBridgeUid, appName))
.withBridge(connectionBridgeUid).withProperty(PROP_APPID, appId).withProperty(PROP_APPNAME, appName)
.withRepresentationProperty(PROP_APPID).withLabel("Awtrix App " + appName).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@
package org.openhab.binding.mqtt.awtrixlight.internal.discovery;

import static org.openhab.binding.mqtt.MqttBindingConstants.BINDING_ID;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.*;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.AWTRIX_CLOCK;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_FIRMWARE;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_TYPE;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_UID;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.PROP_APPLOCKTIMEOUT;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.PROP_BASETOPIC;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.PROP_DISCOVERDEFAULT;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.PROP_FIRMWARE;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.PROP_UNIQUEID;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.PROP_VENDOR;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.THING_TYPE_BRIDGE;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.TOPIC_BASE;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.TOPIC_STATS;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
Expand Down Expand Up @@ -62,26 +74,24 @@ public void receivedMessage(ThingUID connectionBridge, MqttBrokerConnection conn
String message = new String(payload, StandardCharsets.UTF_8);
if (topic.endsWith(TOPIC_STATS)) {
String baseTopic = topic.replace(TOPIC_STATS, "");
if (baseTopic != null) {
HashMap<String, Object> messageParams = Helper.decodeJson(message);
String vendorString = "Unknown";
Object vendor = messageParams.get(FIELD_BRIDGE_TYPE);
if (vendor != null && vendor instanceof Integer) {
vendorString = vendor.equals(0) ? "Ulanzi" : "Generic";
}
String firmwareString = "Unknown";
Object firmware = messageParams.get(FIELD_BRIDGE_FIRMWARE);
if (firmware != null && firmware instanceof String) {
firmwareString = (String) firmware;
}
String hardwareUidString = "Unknown";
Object hardwareUid = messageParams.get(FIELD_BRIDGE_UID);
if (hardwareUid != null && hardwareUid instanceof String) {
hardwareUidString = (String) hardwareUid;
}
logger.trace("Publishing an Awtrix Clock with ID :{}", hardwareUidString);
publishClock(connectionBridge, baseTopic, vendorString, firmwareString, hardwareUidString);
HashMap<String, Object> messageParams = Helper.decodeJson(message);
String vendorString = "Unknown";
Object vendor = messageParams.get(FIELD_BRIDGE_TYPE);
if (vendor != null && vendor instanceof Integer) {
vendorString = vendor.equals(0) ? "Ulanzi" : "Generic";
}
String firmwareString = "Unknown";
Object firmware = messageParams.get(FIELD_BRIDGE_FIRMWARE);
if (firmware != null && firmware instanceof String) {
firmwareString = (String) firmware;
}
String hardwareUidString = "Unknown";
Object hardwareUid = messageParams.get(FIELD_BRIDGE_UID);
if (hardwareUid != null && hardwareUid instanceof String) {
hardwareUidString = (String) hardwareUid;
}
logger.trace("Publishing an Awtrix Clock with ID :{}", hardwareUidString);
publishClock(connectionBridge, baseTopic, vendorString, firmwareString, hardwareUidString);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
QuantityType<?> blinkInS = ((QuantityType<?>) command).toUnit(Units.SECOND);
if (blinkInS != null) {
BigDecimal blinkInMs = blinkInS.toBigDecimal().multiply(THOUSAND);
if (blinkInMs != null) {
this.app.setBlinkText(blinkInMs);
}
this.app.setBlinkText(blinkInMs);
}
}
break;
Expand All @@ -230,9 +228,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
QuantityType<?> fadeInS = ((QuantityType<?>) command).toUnit(Units.SECOND);
if (fadeInS != null) {
BigDecimal fadeInMs = fadeInS.toBigDecimal().multiply(THOUSAND);
if (fadeInMs != null) {
this.app.setFadeText(fadeInMs);
}
this.app.setFadeText(fadeInMs);
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,8 @@ private void sendMQTT(String commandTopic, String payload, boolean retain) {
logger.debug("Will send {} to topic {}", payload, commandTopic);
byte[] payloadBytes = payload.getBytes();
MqttBrokerConnection localConnection = connection;
if (payloadBytes != null) {
if (localConnection != null) {
localConnection.publish(commandTopic, payloadBytes, 1, retain);
}
if (localConnection != null) {
localConnection.publish(commandTopic, payloadBytes, 1, retain);
}
}

Expand Down Expand Up @@ -533,14 +531,10 @@ private void handleStatsMessage(String statsMessage) {
HashMap<String, Object> params = Helper.decodeJson(statsMessage);
for (HashMap.Entry<String, Object> entry : params.entrySet()) {

Map<String, String> properties = thing.getProperties();
Object value = entry.getValue();
switch (entry.getKey()) {
case FIELD_BRIDGE_UID:
if (!properties.containsValue(PROP_UNIQUEID) || properties.get(PROP_UNIQUEID) == null
|| !properties.get(PROP_UNIQUEID).equals((String) value)) {
thing.setProperty(PROP_UNIQUEID, (String) value);
}
thing.setProperty(PROP_UNIQUEID, (String) value);
break;
case FIELD_BRIDGE_BATTERY:
if (value instanceof BigDecimal) {
Expand All @@ -556,18 +550,12 @@ private void handleStatsMessage(String statsMessage) {
// Not mapped to channel atm
break;
case FIELD_BRIDGE_FIRMWARE:
if (!properties.containsValue(PROP_FIRMWARE) || properties.get(PROP_FIRMWARE) == null
|| !properties.get(PROP_FIRMWARE).equals(value.toString())) {
thing.setProperty(PROP_FIRMWARE, value.toString());
}
thing.setProperty(PROP_FIRMWARE, value.toString());
break;
case FIELD_BRIDGE_TYPE:
if (value instanceof BigDecimal) {
String vendor = ((BigDecimal) value).compareTo(BigDecimal.ZERO) == 0 ? "Ulanzi" : "Generic";
if (!properties.containsValue(PROP_VENDOR) || properties.get(PROP_VENDOR) == null
|| !properties.get(PROP_VENDOR).equals(vendor)) {
thing.setProperty(PROP_VENDOR, vendor);
}
thing.setProperty(PROP_VENDOR, vendor);
break;
}
case FIELD_BRIDGE_LUX:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,25 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.*;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_APP;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_BATTERY;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_BATTERY_RAW;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_BRIGHTNESS;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_FIRMWARE;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_HUMIDITY;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_INDICATOR1;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_INDICATOR2;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_INDICATOR3;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_LDR_RAW;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_LUX;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_MESSAGES;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_RAM;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_TEMPERATURE;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_TYPE;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_UPTIME;
import static org.openhab.binding.mqtt.awtrixlight.internal.AwtrixLightBindingConstants.FIELD_BRIDGE_WIFI_SIGNAL;

import java.math.BigDecimal;
import java.util.Arrays;
Expand Down Expand Up @@ -57,11 +74,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)) {
assertEquals(String.class, convertedJson.get(s).getClass());
Object prop = convertedJson.get(s);
assertNotNull(prop);
assertEquals(String.class, prop.getClass());
} else if (Arrays.stream(booleanProperties).anyMatch(s::equals)) {
assertEquals(Boolean.class, convertedJson.get(s).getClass());
Object prop = convertedJson.get(s);
assertNotNull(prop);
assertEquals(Boolean.class, prop.getClass());
} else {
assertEquals(BigDecimal.class, convertedJson.get(s).getClass());
Object prop = convertedJson.get(s);
assertNotNull(prop);
assertEquals(BigDecimal.class, prop.getClass());
}
}
}
Expand Down

0 comments on commit e26c371

Please sign in to comment.