Add SIN.04028 tutorial material, tutor prompt and conversation log
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bac80295b7
commit
f615bef426
30 files changed
+8123
No files matched your search
+138
@@ -0,0 +1,138 @@
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.eclipse.paho.mqttv5.client.IMqttAsyncClient;
|
||||
import org.eclipse.paho.mqttv5.client.IMqttToken;
|
||||
import org.eclipse.paho.mqttv5.client.MqttAsyncClient;
|
||||
import org.eclipse.paho.mqttv5.client.MqttCallback;
|
||||
import org.eclipse.paho.mqttv5.client.MqttConnectionOptions;
|
||||
import org.eclipse.paho.mqttv5.client.MqttDisconnectResponse;
|
||||
import org.eclipse.paho.mqttv5.client.persist.MemoryPersistence;
|
||||
import org.eclipse.paho.mqttv5.common.MqttException;
|
||||
import org.eclipse.paho.mqttv5.common.MqttMessage;
|
||||
import org.eclipse.paho.mqttv5.common.packet.MqttProperties;
|
||||
|
||||
record ConsoleOutMessage(String value) {}
|
||||
|
||||
record ConsoleOutSubscription(String topic) {}
|
||||
|
||||
record ConsoleOutIntent(ConsoleOutSubscription subscribe) {}
|
||||
|
||||
MqttAsyncClient client;
|
||||
String intentTopic;
|
||||
String subscriptionStatusTopic;
|
||||
String dynamicTextTopic;
|
||||
|
||||
void publish(
|
||||
IMqttAsyncClient client,
|
||||
String topic,
|
||||
String content,
|
||||
int qos,
|
||||
boolean retained)
|
||||
throws Exception {
|
||||
var message = new MqttMessage(
|
||||
content.getBytes(StandardCharsets.UTF_8)
|
||||
);
|
||||
message.setQos(qos);
|
||||
message.setRetained(retained);
|
||||
client.publish(topic, message).waitForCompletion();
|
||||
}
|
||||
|
||||
MqttConnectionOptions connectionOptions(String lastWillTopic) {
|
||||
var options = new MqttConnectionOptions();
|
||||
options.setAutomaticReconnect(true);
|
||||
|
||||
var will = new MqttMessage(
|
||||
"{\"value\":false}".getBytes(StandardCharsets.UTF_8),
|
||||
1,
|
||||
true,
|
||||
new MqttProperties()
|
||||
);
|
||||
options.setWill(lastWillTopic, will);
|
||||
return options;
|
||||
}
|
||||
|
||||
void handleIntent(MqttMessage message) throws Exception {
|
||||
var intent = JsonTools.deserializationObjectMapper.readValue(
|
||||
message.getPayload(),
|
||||
ConsoleOutIntent.class
|
||||
);
|
||||
|
||||
if (intent.subscribe() == null)
|
||||
return;
|
||||
|
||||
var requestedTopic = intent.subscribe().topic();
|
||||
|
||||
if (!MqttTools.isValidFilter(requestedTopic)) {
|
||||
IO.println("Rejected invalid topic filter: " + requestedTopic);
|
||||
return;
|
||||
}
|
||||
|
||||
if (dynamicTextTopic != null)
|
||||
client.unsubscribe(dynamicTextTopic).waitForCompletion();
|
||||
|
||||
dynamicTextTopic = requestedTopic;
|
||||
client.subscribe(dynamicTextTopic, 1).waitForCompletion();
|
||||
|
||||
var status = JsonTools.serializationObjectMapper
|
||||
.writeValueAsString(new ConsoleOutMessage(dynamicTextTopic));
|
||||
publish(client, subscriptionStatusTopic, status, 1, true);
|
||||
|
||||
IO.println("ConsoleOut now listens to " + dynamicTextTopic);
|
||||
}
|
||||
|
||||
void handlePublication(String topic, MqttMessage message) throws Exception {
|
||||
if (topic.equals(intentTopic)) {
|
||||
handleIntent(message);
|
||||
} else if (dynamicTextTopic != null
|
||||
&& MqttTools.isTopicMatchingFilter(topic, dynamicTextTopic)) {
|
||||
var text = JsonTools.deserializationObjectMapper.readValue(
|
||||
message.getPayload(),
|
||||
ConsoleOutMessage.class
|
||||
);
|
||||
IO.println(text.value());
|
||||
}
|
||||
}
|
||||
|
||||
void main(String[] arguments) throws Exception {
|
||||
var unit = arguments.length == 0 ? "alice" : arguments[0];
|
||||
var broker = System.getProperty("mqtt.broker", "tcp://localhost:1883");
|
||||
var root = "Tutorial/ConsoleOut/U/" + unit;
|
||||
intentTopic = root + "/I";
|
||||
subscriptionStatusTopic = root + "/S/subscriptions/text";
|
||||
var onlineTopic = root + "/S/online";
|
||||
|
||||
client = new MqttAsyncClient(
|
||||
broker,
|
||||
"console-out-" + unit + "-" + UUID.randomUUID(),
|
||||
new MemoryPersistence()
|
||||
);
|
||||
|
||||
client.setCallback(new MqttCallback() {
|
||||
public void messageArrived(String topic, MqttMessage message)
|
||||
throws Exception {
|
||||
handlePublication(topic, message);
|
||||
}
|
||||
|
||||
public void disconnected(MqttDisconnectResponse response) {}
|
||||
|
||||
public void mqttErrorOccurred(MqttException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
public void deliveryComplete(IMqttToken token) {}
|
||||
|
||||
public void connectComplete(boolean reconnect, String serverURI) {}
|
||||
|
||||
public void authPacketArrived(
|
||||
int reasonCode,
|
||||
MqttProperties properties) {}
|
||||
});
|
||||
|
||||
client.connect(connectionOptions(onlineTopic)).waitForCompletion();
|
||||
client.subscribe(intentTopic, 1).waitForCompletion();
|
||||
publish(client, onlineTopic, "{\"value\":true}", 1, true);
|
||||
|
||||
IO.println("ConsoleOut waits for Intent on " + intentTopic);
|
||||
Thread.currentThread().join();
|
||||
}
|
||||
Reference in new issue
Block a user