diff --git a/.gradle/6.7/executionHistory/executionHistory.bin b/.gradle/6.7/executionHistory/executionHistory.bin index 08f5652dab4ac9d2fd8aaee03be0066740e0c11e..cf68230edc9d9ba5be3c1c19c012d35607759a02 100644 Binary files a/.gradle/6.7/executionHistory/executionHistory.bin and b/.gradle/6.7/executionHistory/executionHistory.bin differ diff --git a/.gradle/6.7/executionHistory/executionHistory.lock b/.gradle/6.7/executionHistory/executionHistory.lock index f5d327842988626ef4b0ffb4ea5e166f45cd1f29..764860d31f678bd696584538cc55e5d9d0cc14bd 100644 Binary files a/.gradle/6.7/executionHistory/executionHistory.lock and b/.gradle/6.7/executionHistory/executionHistory.lock differ diff --git a/.gradle/6.7/fileHashes/fileHashes.bin b/.gradle/6.7/fileHashes/fileHashes.bin index 7b5bfb4988a7b0e43abc483baac5d9eb672035ef..59d8ae85875a05cff86a7f1941d9eb44d3201092 100644 Binary files a/.gradle/6.7/fileHashes/fileHashes.bin and b/.gradle/6.7/fileHashes/fileHashes.bin differ diff --git a/.gradle/6.7/fileHashes/fileHashes.lock b/.gradle/6.7/fileHashes/fileHashes.lock index f235325ad01d016949d55ffe4167f0dab0f64a7f..5364a2b05eb42472f8ccd1c502f208f85ef02f15 100644 Binary files a/.gradle/6.7/fileHashes/fileHashes.lock and b/.gradle/6.7/fileHashes/fileHashes.lock differ diff --git a/.gradle/6.7/javaCompile/classAnalysis.bin b/.gradle/6.7/javaCompile/classAnalysis.bin index 95cfbd7d9e6754d8632eba78d9c6e6b63b8fb010..171f0c01e68dccad4143e6a46836bd4862ac1fb1 100644 Binary files a/.gradle/6.7/javaCompile/classAnalysis.bin and b/.gradle/6.7/javaCompile/classAnalysis.bin differ diff --git a/.gradle/6.7/javaCompile/javaCompile.lock b/.gradle/6.7/javaCompile/javaCompile.lock index 8c2e48cd28ad20826164cfc5876c303305e6df6a..0042925bd1410ce2bde3e31d8f2ae0a941cbd577 100644 Binary files a/.gradle/6.7/javaCompile/javaCompile.lock and b/.gradle/6.7/javaCompile/javaCompile.lock differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index e395a6aeb70677c15535d679bcaf6c63d9cc6321..5bed8506330eb7a9ea3cd5707c9ce1e82bf489e5 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000000000000000000000000000000000000..e96534fb27b68192f27f985d3879e173ec77adb8 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/classes/java/main/MQTT/PubExample.class b/build/classes/java/main/MQTT/PubExample.class index cbf5848d90467622e2c80eb309dccc545bf411a2..096af4d968d638287e8d55194f6dea477fa9c366 100644 Binary files a/build/classes/java/main/MQTT/PubExample.class and b/build/classes/java/main/MQTT/PubExample.class differ diff --git a/build/classes/java/main/MQTT/PubExampleCallback$1.class b/build/classes/java/main/MQTT/PubExampleCallback$1.class new file mode 100644 index 0000000000000000000000000000000000000000..cfcb1d13d55f5e5aa95a55b4f44039e74273c701 Binary files /dev/null and b/build/classes/java/main/MQTT/PubExampleCallback$1.class differ diff --git a/build/classes/java/main/MQTT/PubExampleCallback.class b/build/classes/java/main/MQTT/PubExampleCallback.class new file mode 100644 index 0000000000000000000000000000000000000000..385d647c7844796fa376ef87273f57ca0d02a62b Binary files /dev/null and b/build/classes/java/main/MQTT/PubExampleCallback.class differ diff --git a/build/classes/java/main/MQTT/PubSubExample$1.class b/build/classes/java/main/MQTT/PubSubExample$1.class new file mode 100644 index 0000000000000000000000000000000000000000..4e867400b577c341026242f07ad2e876c516727a Binary files /dev/null and b/build/classes/java/main/MQTT/PubSubExample$1.class differ diff --git a/build/classes/java/main/MQTT/PubSubExample.class b/build/classes/java/main/MQTT/PubSubExample.class new file mode 100644 index 0000000000000000000000000000000000000000..8658736e28e1f6ef4c6c3aaaff7b4894adc62222 Binary files /dev/null and b/build/classes/java/main/MQTT/PubSubExample.class differ diff --git a/build/classes/java/main/MQTT/SubExample$1.class b/build/classes/java/main/MQTT/SubExample$1.class index 422a3084ea496af76cf141b8d53de8b49cf03b90..b6ea4436ec69cb76ba063ed13c89f725ea28f5e5 100644 Binary files a/build/classes/java/main/MQTT/SubExample$1.class and b/build/classes/java/main/MQTT/SubExample$1.class differ diff --git a/build/classes/java/main/MQTT/SubExample.class b/build/classes/java/main/MQTT/SubExample.class index f6979cd01c6c2ab757c964a759badc497bb8c6b4..e9d6c1c60121338547706aa087b5f56dd7a1f6a7 100644 Binary files a/build/classes/java/main/MQTT/SubExample.class and b/build/classes/java/main/MQTT/SubExample.class differ diff --git a/build/classes/java/main/REST/services/HelloWorld.class b/build/classes/java/main/REST/services/HelloWorld.class index bd8e42a79689108dc9a22567812893d8651a6a35..28af175c95edacd5c4990d038abd8c2d32ba7bca 100644 Binary files a/build/classes/java/main/REST/services/HelloWorld.class and b/build/classes/java/main/REST/services/HelloWorld.class differ diff --git a/build/classes/java/main/REST/services/UsersService.class b/build/classes/java/main/REST/services/UsersService.class index 4103f648bc2e124aa6dd4cb4c8f1799cb3f9cbf0..57372452ad74121755537da309603cb23f4d3f5f 100644 Binary files a/build/classes/java/main/REST/services/UsersService.class and b/build/classes/java/main/REST/services/UsersService.class differ diff --git a/build/tmp/compileJava/source-classes-mapping.txt b/build/tmp/compileJava/source-classes-mapping.txt index 24efb495b5ebe9de94c8bb0d9d5cd365a94ef8d7..45d582092147ca00217b93fa0d93b5ec7eb87316 100644 --- a/build/tmp/compileJava/source-classes-mapping.txt +++ b/build/tmp/compileJava/source-classes-mapping.txt @@ -1,13 +1,19 @@ 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 diff --git a/src/main/java/MQTT/PubExample.java b/src/main/java/MQTT/PubExample.java new file mode 100644 index 0000000000000000000000000000000000000000..666b229878ebdc29bcbdccc7b4907731c4260980 --- /dev/null +++ b/src/main/java/MQTT/PubExample.java @@ -0,0 +1,51 @@ +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(); + } + } +} diff --git a/src/main/java/MQTT/PubExampleCallback.java b/src/main/java/MQTT/PubExampleCallback.java new file mode 100644 index 0000000000000000000000000000000000000000..403a22ed75439620b4daa3a5a17b4d0a6c5b9bd6 --- /dev/null +++ b/src/main/java/MQTT/PubExampleCallback.java @@ -0,0 +1,68 @@ +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(); + } + } +} diff --git a/src/main/java/MQTT/PubSubExample.java b/src/main/java/MQTT/PubSubExample.java new file mode 100644 index 0000000000000000000000000000000000000000..d3b61d94497b0252148e744514819e654887f6e9 --- /dev/null +++ b/src/main/java/MQTT/PubSubExample.java @@ -0,0 +1,91 @@ +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(); + } + + } + +} diff --git a/src/main/java/MQTT/SubExample.java b/src/main/java/MQTT/SubExample.java new file mode 100644 index 0000000000000000000000000000000000000000..73e23caefd7e04e4c8613eab16441954661a9996 --- /dev/null +++ b/src/main/java/MQTT/SubExample.java @@ -0,0 +1,75 @@ +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(); + } + + + } + + +} diff --git a/src/main/java/REST/services/HelloWorld.java b/src/main/java/REST/services/HelloWorld.java index 182441d044fa3d30e21f5d7efde0e76042ac4883..e7e31631bf77a7746037ba97b80b50e9469fe1a8 100644 --- a/src/main/java/REST/services/HelloWorld.java +++ b/src/main/java/REST/services/HelloWorld.java @@ -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\"}"; } diff --git a/src/main/java/REST/services/UsersService.java b/src/main/java/REST/services/UsersService.java index 51fffb738fdf43b03ccf03d396f171726d334880..89c24e29c82dc2e40805002272d26c02391c69fb 100644 --- a/src/main/java/REST/services/UsersService.java +++ b/src/main/java/REST/services/UsersService.java @@ -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"})