Commit 91165ef3 authored by Riccardo Presotto's avatar Riccardo Presotto

update MQTT exercises

parent 5309619d
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
REST/beans/User.java
REST.beans.User
MQTT/PubSubExample.java
MQTT.PubSubExample
MQTT.PubSubExample$1
REST/beans/Users.java
REST.beans.Users
REST/StartServer.java
REST.StartServer
MQTT/PubExampleCallback.java
MQTT.PubExampleCallback
MQTT.PubExampleCallback$1
REST/services/HelloWorld.java
REST.services.HelloWorld
REST/services/UsersService.java
REST.services.UsersService
REST/StartServer.java
REST.StartServer
REST/beans/Users.java
REST.beans.Users
MQTT/PubExample.java
MQTT.PubExample
MQTT/SubExample.java
......
package MQTT;
import org.eclipse.paho.client.mqttv3.*;
public class PubExample {
public static void main(String[] args) {
MqttClient client;
String broker = "tcp://localhost:1883";
String clientId = MqttClient.generateClientId();
String topic = "home/sensors/light";
int qos = 2;
try {
client = new MqttClient(broker, clientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
//connOpts.setUserName(username); // optional
//connOpts.setPassword(password.toCharArray()); // optional
//connOpts.setWill("this/is/a/topic","will message".getBytes(),1,false); // optional
//connOpts.setKeepAliveInterval(60); // optional
// Connect the client
System.out.println(clientId + " Connecting Broker " + broker);
client.connect(connOpts);
System.out.println(clientId + " Connected");
String payload = String.valueOf(0 + (Math.random() * 10)); // create a random number between 0 and 10
MqttMessage message = new MqttMessage(payload.getBytes());
// Set the QoS on the Message
message.setQos(qos);
System.out.println(clientId + " Publishing message: " + payload + " ...");
client.publish(topic, message);
System.out.println(clientId + " Message published");
if (client.isConnected())
client.disconnect();
System.out.println("Publisher " + clientId + " disconnected");
} catch (MqttException me ) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}
}
}
package MQTT;
import org.eclipse.paho.client.mqttv3.*;
public class PubExampleCallback {
public static void main(String[] args) {
MqttClient client;
String broker = "tcp://localhost:1883";
String clientId = MqttClient.generateClientId();
String topic = "home/sensors/temp";
int qos = 2;
try {
client = new MqttClient(broker, clientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
// Connect the client
System.out.println(clientId + " Connecting Broker " + broker);
client.connect(connOpts);
System.out.println(clientId + " Connected - Thread PID: " + Thread.currentThread().getId());
// Callback
client.setCallback(new MqttCallback() {
public void messageArrived(String topic, MqttMessage message) {
// Not used Here
}
public void connectionLost(Throwable cause) {
System.out.println(clientId + " Connectionlost! cause:" + cause.getMessage());
}
public void deliveryComplete(IMqttDeliveryToken token) {
// Until the delivery is completed, messages with QoS 1 or 2 are retained from the client
// Delivery for a message is completed when all acknowledgments have been received
// When the callback returns from deliveryComplete to the main thread, the client removes the retained messages with QoS 1 or 2.
if (token.isComplete()) {
System.out.println(clientId + " Message delivered - Thread PID: " + Thread.currentThread().getId());
}
}
});
String payload = String.valueOf(0 + (Math.random() * 10)); // create a random number between 0 and 10
MqttMessage message = new MqttMessage(payload.getBytes());
// Set the QoS on the Message
message.setQos(qos);
System.out.println(clientId + " Publishing message: " + payload + " ...");
client.publish(topic, message);
System.out.println(clientId + " Message published - Thread PID: " + Thread.currentThread().getId());
if (client.isConnected())
client.disconnect();
System.out.println("Publisher " + clientId + " disconnected - Thread PID: " + Thread.currentThread().getId());
} catch (MqttException me ) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}
}
}
package MQTT;
import org.eclipse.paho.client.mqttv3.*;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Scanner;
public class PubSubExample {
public static void main(String[] args) {
MqttClient client;
String broker = "tcp://localhost:1883";
String clientId = MqttClient.generateClientId();
String pubTopic = "home/sensors/master";
String subTopicArray [] = new String[] {"home/sensors/light", "home/sensors/temp"};
int subQosArray [] = new int[] {1,2};
int pubQos = 2;
try {
client = new MqttClient(broker, clientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true); // false = the broker stores all subscriptions for the client and all missed messages for the client that subscribed with a Qos level 1 or 2
// Connect the client
System.out.println(clientId + " Connecting Broker " + broker);
client.connect(connOpts);
System.out.println(clientId + " Connected " + Thread.currentThread().getId());
// Callback
client.setCallback(new MqttCallback() {
public void messageArrived(String topic, MqttMessage message) {
// Called when a message arrives from the server that matches any subscription made by the client
String time = new Timestamp(System.currentTimeMillis()).toString();
String receivedMessage = new String(message.getPayload());
System.out.println(clientId +" Received a Message! - Callback - Thread PID: " + Thread.currentThread().getId() +
"\n\tTime: " + time +
"\n\tTopic: " + topic +
"\n\tMessage: " + receivedMessage +
"\n\tQoS: " + message.getQos() + "\n");
System.out.println("\n *** Press a random key to exit *** \n");
}
public void connectionLost(Throwable cause) {
System.out.println(clientId + " Connectionlost! cause:" + cause.getMessage()+ "- Thread PID: " + Thread.currentThread().getId());
}
public void deliveryComplete(IMqttDeliveryToken token) {
if (token.isComplete()) {
System.out.println(clientId + " Message delivered - Thread PID: " + Thread.currentThread().getId());
}
}
});
System.out.println(clientId + " Subscribing ... - Thread PID: " + Thread.currentThread().getId());
client.subscribe(subTopicArray,subQosArray);
System.out.println(clientId + " Subscribed to topics : " + Arrays.toString(subTopicArray));
String payload = "Client connected at time: " + new Timestamp(System.currentTimeMillis()).toString();
MqttMessage message = new MqttMessage(payload.getBytes());
// Set the QoS on the Message
message.setQos(pubQos);
System.out.println(clientId + " Publishing message: " + payload + " ...");
client.publish(pubTopic, message);
System.out.println(clientId + " Message published - Thread PID: " + Thread.currentThread().getId());
System.out.println("\n *** Press a random key to exit *** \n");
Scanner command = new Scanner(System.in);
command.nextLine();
client.disconnect();
} catch (MqttException me ) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}
}
}
package MQTT;
import org.eclipse.paho.client.mqttv3.*;
import java.sql.Timestamp;
import java.util.Scanner;
public class SubExample {
public static void main(String[] args) {
MqttClient client;
String broker = "tcp://localhost:1883";
String clientId = MqttClient.generateClientId();
String topic = "home/sensors/#";
int qos = 2;
try {
client = new MqttClient(broker, clientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
// Connect the client
System.out.println(clientId + " Connecting Broker " + broker);
client.connect(connOpts);
System.out.println(clientId + " Connected - Thread PID: " + Thread.currentThread().getId());
// Callback
client.setCallback(new MqttCallback() {
public void messageArrived(String topic, MqttMessage message) {
// Called when a message arrives from the server that matches any subscription made by the client
String time = new Timestamp(System.currentTimeMillis()).toString();
String receivedMessage = new String(message.getPayload());
System.out.println(clientId +" Received a Message! - Callback - Thread PID: " + Thread.currentThread().getId() +
"\n\tTime: " + time +
"\n\tTopic: " + topic +
"\n\tMessage: " + receivedMessage +
"\n\tQoS: " + message.getQos() + "\n");
System.out.println("\n *** Press a random key to exit *** \n");
}
public void connectionLost(Throwable cause) {
System.out.println(clientId + " Connectionlost! cause:" + cause.getMessage()+ "- Thread PID: " + Thread.currentThread().getId());
}
public void deliveryComplete(IMqttDeliveryToken token) {
// Not used here
}
});
System.out.println(clientId + " Subscribing ... - Thread PID: " + Thread.currentThread().getId());
client.subscribe(topic,qos);
System.out.println(clientId + " Subscribed to topics : " + topic);
System.out.println("\n *** Press a random key to exit *** \n");
Scanner command = new Scanner(System.in);
command.nextLine();
client.disconnect();
} catch (MqttException me ) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}
}
}
......@@ -11,14 +11,12 @@ public class HelloWorld {
@GET
@Produces("text/plain")
public String helloWorld(){
return "Hello world!";
}
@GET
@Path("{name}")
@Produces("text/plain")
@Produces({"text/plain"})
public String helloWorldName(@PathParam("name") String name){
return "Hello, "+name+"!";
......@@ -28,7 +26,6 @@ public class HelloWorld {
@GET
@Produces("application/json")
public String helloWorld2(){
return "{\"message\": \"helloWorld\"}";
}
......
......@@ -19,7 +19,6 @@ public class UsersService {
@GET
@Produces({"application/json", "application/xml"})
public Response getUsersList(){
return Response.ok(Users.getInstance()).build();
}
......@@ -33,7 +32,7 @@ public class UsersService {
return Response.ok().build();
}
//permette di prelevare con un determinato nome
//permette di prelevare un utente con un determinato nome
@Path("get/{name}")
@GET
@Produces({"application/json", "application/xml"})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment