Skip to content

Commit

Permalink
* Refactor MqttSubscription into MqttTopic and implement message publ…
Browse files Browse the repository at this point in the history
…ishing / multi subscription - code cleanup
  • Loading branch information
psobiech committed Dec 30, 2023
1 parent 85497cb commit 1a5520e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
22 changes: 11 additions & 11 deletions vclu/src/main/java/pl/psobiech/opengr8on/vclu/VirtualCLU.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public VirtualCLU(String name, Inet4Address address, Path aDriveDirectory) {
this.aDriveDirectory = aDriveDirectory;

featureFunctions.put(0, this::getUptime); // clu_uptime
VirtualCLU.this.featureValues.put(2, valueOf(0)); // clu_state
featureValues.put(2, valueOf(0)); // clu_state
featureFunctions.put(5, this::getCurrentDateAsString); // clu_date
featureFunctions.put(6, this::getCurrentTimeAsString); // clu_time
featureFunctions.put(7, this::getCurrentDayOfMonth); // clu_day
Expand All @@ -116,11 +116,11 @@ public VirtualCLU(String name, Inet4Address address, Path aDriveDirectory) {
featureFunctions.put(11, this::getCurrentHour); // clu_hour
featureFunctions.put(12, this::getCurrentMinute); // clu_minute
featureFunctions.put(13, this::getCurrentEpochSeconds); // clu_localtime
VirtualCLU.this.featureValues.put(14, valueOf(UTC_TIMEZONE_ID)); // clu_timezone
VirtualCLU.this.featureValues.put(20, valueOf("ssl://localhost:8883")); // clu_mqtturl
featureValues.put(14, valueOf(UTC_TIMEZONE_ID)); // clu_timezone
featureValues.put(20, valueOf("ssl://localhost:8883")); // clu_mqtturl
featureFunctions.put(21, arg1 -> { // clu_usemqtt
if (arg1.isnil()) {
return VirtualCLU.this.featureValues.get(21);
return featureValues.get(21);
}

// Sometimes OM uses true/false and sometimes 0/1
Expand Down Expand Up @@ -153,14 +153,14 @@ public VirtualCLU(String name, Inet4Address address, Path aDriveDirectory) {

@Override
public void setup() {
VirtualCLU.this.featureValues.put(2, valueOf(1)); // clu_state
featureValues.put(2, valueOf(1)); // clu_state

triggerEvent(0); // clu_oninit
}

@Override
public void loop() {
final boolean mqttEnable = VirtualCLU.this.featureValues.get(21).checkboolean();
final boolean mqttEnable = featureValues.get(21).checkboolean();
final boolean mqttAlreadyEnabled = mqttClient != null;
if (mqttEnable ^ mqttAlreadyEnabled) {
disableMqtt();
Expand Down Expand Up @@ -244,7 +244,7 @@ private ZonedDateTime getCurrentDateTime() {
}

private ZoneId getCurrentZoneId() {
final LuaValue zoneIdLuaValue = VirtualCLU.this.featureValues.get(14);
final LuaValue zoneIdLuaValue = featureValues.get(14);
if (!zoneIdLuaValue.isint()) {
return ZoneOffset.UTC;
}
Expand All @@ -260,18 +260,18 @@ private LuaNumber getCurrentEpochSeconds(LuaValue arg1) {
}

private LuaValue addToLog(LuaValue arg) {
VirtualCLU.this.featureValues.put(1, arg);
featureValues.put(1, arg);

if (!arg.isnil()) {
final String logValue = String.valueOf(arg.checkstring());
LOGGER.info(VirtualCLU.this.name + ": " + logValue);
LOGGER.info(name + ": " + logValue);
}

return LuaValue.NIL;
}

private LuaValue clearLog(LuaValue arg) {
VirtualCLU.this.featureValues.put(1, LuaValue.NIL);
featureValues.put(1, LuaValue.NIL);

return LuaValue.NIL;
}
Expand All @@ -293,7 +293,7 @@ private void disableMqtt() {
private void enableMqtt() {
// TODO: manage the client connection and topics
// TODO: expose some LUA API to publish/subscribe
final String mqttUrl = String.valueOf(VirtualCLU.this.featureValues.get(20).checkstring());
final String mqttUrl = String.valueOf(featureValues.get(20).checkstring());

try {
LOGGER.info("Connecting to MQTT on: {}", mqttUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private LuaValue publish(LuaValue arg1) {
return LuaValue.FALSE;
}

final LuaValue message = featureValues.remove(1); // mqttsubscription_message
final LuaValue message = removeMessage();

try {
mqttClient.publish(
Expand Down Expand Up @@ -158,14 +158,14 @@ public Set<String> getTopicFilters() {
}

private LuaValue onNextMessage(LuaValue arg1) {
featureValues.remove(1); // mqttsubscription_message
removeMessage();

return LuaValue.NIL;
}

@Override
public void loop() {
final LuaValue currentPayload = featureValues.get(1); // mqttsubscription_message
final LuaValue currentPayload = getMessage();
if (currentPayload == null || String.valueOf(currentPayload).isEmpty()) {
final Entry<String, byte[]> entry = messageQueue.poll();
if (entry != null) {
Expand All @@ -180,6 +180,14 @@ public void loop() {
}
}

private LuaValue getMessage() {
return featureValues.get(1); // mqttsubscription_message
}

private LuaValue removeMessage() {
return featureValues.remove(1); // mqttsubscription_message
}

@Override
public void close() {
// NOP
Expand Down

0 comments on commit 1a5520e

Please sign in to comment.