-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
210 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<object class="300" name="MqttSubscription" version="1"> | ||
<features> | ||
<feature get="true" index="0" name="Topic" set="true" type="str" unit="string"> | ||
<hint lang="pl" value="Topic"/> | ||
<hint lang="en" value="Topic"/> | ||
<desc resKey="mqttsubscription_topic"/> | ||
</feature> | ||
<feature get="true" index="1" name="Message" set="false" type="str" unit="string"> | ||
<hint lang="pl" value="Zawartość wiadomości MQTT"/> | ||
<hint lang="en" value="The content of MQTT message"/> | ||
<desc resKey="mqttsubscription_message"/> | ||
</feature> | ||
</features> | ||
<methods> | ||
<method call="set" index="0" name="SetTopic" return="void"> | ||
<param name="Topic" type="str" unit="string"/> | ||
<hint lang="pl" value="Zmienia topic"/> | ||
<hint lang="en" value="Changes topic"/> | ||
<desc resKey="mqttsubscription_settopic"/> | ||
</method> | ||
<method call="execute" index="0" name="NextMessage" return="void"> | ||
<hint lang="pl" value="Oznacza wiadomość jako otrzymaną"/> | ||
<hint lang="en" value="Marks message as received"/> | ||
<desc resKey="mqttsubscription_nextmessage"/> | ||
</method> | ||
</methods> | ||
<events> | ||
<event address="0" name="OnMessage"> | ||
<hint lang="pl" value="Zdarzenie wywoływane w momencie otrzymania wiadomości"/> | ||
<hint lang="en" value="Event occurring when the message is received"/> | ||
<desc resKey="mqttsubscription_onmessage"/> | ||
</event> | ||
</events> | ||
</object> |
72 changes: 72 additions & 0 deletions
72
vclu/src/main/java/pl/psobiech/opengr8on/vclu/MqttSubscription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* OpenGr8on, open source extensions to systems based on Grenton devices | ||
* Copyright (C) 2023 Piotr Sobiech | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package pl.psobiech.opengr8on.vclu; | ||
|
||
import java.util.concurrent.LinkedBlockingDeque; | ||
|
||
import org.luaj.vm2.LuaValue; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class MqttSubscription extends VirtualObject { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(MqttSubscription.class); | ||
|
||
private final LinkedBlockingDeque<byte[]> messageQueue = new LinkedBlockingDeque<>(); | ||
|
||
public MqttSubscription(String name) { | ||
super(name); | ||
|
||
methodFunctions.put(0, this::onNextMessage); // mqttsubscription_nextmessage | ||
} | ||
|
||
private LuaValue onNextMessage(LuaValue arg1) { | ||
featureValues.remove(1); | ||
|
||
return LuaValue.NIL; | ||
} | ||
|
||
public String getTopic() { | ||
return String.valueOf(featureValues.get(0).checkstring()); | ||
} | ||
|
||
public void enqueueMessage(int id, byte[] payload) { | ||
while (!messageQueue.offer(payload)) { | ||
// TODO: retry/fail logic | ||
Thread.yield(); | ||
} | ||
} | ||
|
||
@Override | ||
public void loop() { | ||
final LuaValue currentPayload = featureValues.get(1); // mqttsubscription_message | ||
if (currentPayload == null) { | ||
final byte[] payload = messageQueue.poll(); | ||
if (payload != null) { | ||
featureValues.put(1, LuaValue.valueOf(new String(payload))); | ||
|
||
triggerEvent(0); // mqttsubscription_onmessage | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// NOP | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.