diff --git a/antifurto/src/Antifurto.class b/antifurto/src/Antifurto.class
new file mode 100644
index 0000000000000000000000000000000000000000..658211a1a444a9301270913d3a5bb0cea62c7ab4
Binary files /dev/null and b/antifurto/src/Antifurto.class differ
diff --git a/antifurto/src/Antifurto.java b/antifurto/src/Antifurto.java
new file mode 100644
index 0000000000000000000000000000000000000000..1a54c583dcc56d6fed1a00c310c0382d044243f7
--- /dev/null
+++ b/antifurto/src/Antifurto.java
@@ -0,0 +1,187 @@
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Date;
+
+import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
+import org.eclipse.paho.client.mqttv3.MqttException;
+import org.eclipse.paho.client.mqttv3.MqttMessage;
+import org.eclipse.paho.client.mqttv3.MqttTopic;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class Antifurto {
+
+	private static String brokerUrl;
+	private String interruttore;
+	private String interruttoreOutputSuono;
+	private static ArrayList<String> topicsSub;
+	private Date date = new Date();
+	private String clientId = Long.toString(date.getTime()) + "-sub-pub"; // unique client id
+	private Automa automa;
+	
+	private MqttClient mqttClient;
+	
+	public Antifurto(Automa automa) throws JSONException, IOException, MqttException {
+		this.automa = automa;
+		
+		JSONObject jsonObject = new JSONObject(leggiFile("./CONF/conf.json"));
+		brokerUrl = jsonObject.getString("protocol") + "://" + jsonObject.getString("broker") + ":" + jsonObject.getInt("port");
+		topicsSub = new ArrayList<String>();
+		
+		// Su questo topic ricevero' un messaggio del tipo {request:description.json} 
+		// Dovro' quindi mandare la mia descrizione json
+		topicsSub.add("to/all"); 
+		
+		// Su questo topic ricevero' le richieste di inviare il mio stato attuale
+		topicsSub.add("rpc/gruppo2/luci/antifurto");
+		
+		
+		jsonObject = new JSONObject(leggiFile("./CONF/zona.json"));
+		interruttore = jsonObject.getString("interruttore"); 
+		topicsSub.add("from/gruppo2/luci/gpio/" + interruttore); // Sottoscrivo i messaggi che notificano il cambiamento di stato dell'interruttore
+		
+		// Per ogni sensore di movimento, sottoscrivo i messaggi che notificano il loro cambiamento di stato 
+		JSONArray sensori = jsonObject.getJSONArray("sensoriMovimento");
+		for(int i=0; i<sensori.length(); i++) {
+			topicsSub.add("from/gruppo2/luci/gpio/" + sensori.get(i)); 
+		}
+		
+		interruttoreOutputSuono = jsonObject.getString("outputSuono");
+		topicsSub.add("from/gruppo2/luci/gpio/" + interruttoreOutputSuono); // Sottoscrivo i messaggi che notificano il cambiamento di stato dell'interruttore
+		
+		
+		this.mqttClient = new MqttClient(brokerUrl, clientId);
+		
+	}
+	
+	public void startClient(Esecutore esec, Publisher publisher) throws MqttException {
+		MqttConnectOptions options = new MqttConnectOptions();
+		options.setCleanSession(false);
+		mqttClient.setCallback(new SubscribeCallback(this, publisher, interruttore, interruttoreOutputSuono, esec, automa));
+		
+		mqttClient.connect(options);
+		
+		
+		for(String t: topicsSub) 
+			mqttClient.subscribe(t);
+	}
+	
+	public void publishMethod(String topic, String msg) throws MqttException {
+		final MqttTopic msgTopic = mqttClient.getTopic(topic);
+		msgTopic.publish(new MqttMessage(msg.getBytes()));
+	}
+	
+	private String leggiFile(String path) throws IOException {
+		String line;
+		String answer = "";
+		FileReader fileReader = new FileReader(path);
+		BufferedReader bufferedReader = new BufferedReader(fileReader);
+		while((line = bufferedReader.readLine()) != null) {
+			answer += line;
+		}
+		bufferedReader.close();
+		return answer;
+	}
+	
+	
+	
+	
+	/*
+	 * Abbiamo 3 tipi di eventi:
+	 * 		to 		Command Events - mi viene richiesto di eseguire un comando --> sottoscrivo 
+	 * 		from 	Status Events - io annuncio un cambiamento di stato  	
+	 * 								(inviato a tutti i client che hanno sottoscritto il topic su cui faccio la publish)
+	 * 		rpc 	Query Events - richiesta di inviare uno stato a un client (o microservizio). --> sottoscrivo 
+	 * 			  				   Query implementati come coppia di eventi: <query event, status event>
+	 * RICORDIAMO COME ABBIAMO IMPLEMENTATO LA RPC SU MQTT
+	 * 
+	 * 
+	 * 
+	 * Il cuore della nostra business logic è un metodo in cui mi metto in attesa su mqtt e: 
+	 * 		se arriva un comando, chiamo il metodo responsabile per eseguire quel comando;
+	 * 		se arriva un evento di stato, chiamo il metodo responsabile per gestire quell'evento di stato;
+	 * 		se arriva una remote procedure call, attivo la risposta sulla remote procedure call 
+	 * 
+	 * 
+	 * 
+	 * Capire come gestire sistema a regole (vedi slide "Esempio" in "Applicazioni IoT "Cloud Based"")
+	 * 
+	 * 
+	 * 
+	 * Sicurezza gestita con TLS
+	 * 
+	 * 
+	 * 
+	 * FILE DA FARE per un servizio: (?)
+	 * 		file che contiene le informazioni per connettersi al mosquitto locale 
+	 * 		file che dice qual'è la porta che lui deve esporre
+	 * 		file che mi dice tutte le cose che lo configurano, ad esempio quanti device deve guardare
+	 * 		...
+	 * 
+	 * 	I file di configurazione sono in JSON o in XML
+	 * I dati persistiti si troveranno nella sottodirectory (chiamata con il nome del servizio) di Home
+	 * 
+	 * 
+	 * Nella fase di configurazione potremo ancora modificare i file di configurazione (ad esempio aggiungendo device, 
+	 * regole). Dopodiché quando si fa partire il servizio si inizierà a usare l'altra parte di file system (ovvero il Local 
+	 * Dynamic File System) dove si andranno a salvare gli stati per renderli persistenti.
+	 * 
+	 * 
+	 * 
+	 * Ho un processo principale che come unico compito ha quello di fare una fork() e creare un processo figlio. 
+	 * Sarà questo processo figlio a eseguire effettivamente il programma. Se il processo figlio termina (PER UN QUALCHE 
+	 * PROBLEMA), allora il processo padre se ne accorge e fa ripartire un nuovo processo figlio.
+	 * 
+	 *  
+	 * Devo avere una thread di configurazione che obbedisce ai miei comandi (dati sulla HAT interface andando su 
+	 * 	192.168.0.101:9001/configure.html (dove 192.168.0.101 è l'indirizzo della beaglebone)
+	 * 
+	 * 
+	 * Ho una classe che implementa l'automa a stati finiti
+	 * 
+	 * 
+	*/
+	
+	public static void main(String args[]) throws JSONException, IOException, MqttException {
+		//PER TESTARE PRIMA TOGLI IL TRY CATCH !!!!! POI QUANDO TUTTO FUNZIONA, METTI IL TRY CATCH, togli le throws E IMPLEMENTA LA RIPARTENZA!
+		
+//		while(true) {
+//			try {
+				startSystem();
+//			}
+//			catch(Exception e) {
+//				// DA FARE:
+//				// qui metto il codice per far ripartire il processo: rileggo lo stato dei sensori e dell'interruttore, 
+//				// capisco di conseguenza in quale stato dell'automa mi trovo e riparto.
+//				startSystem();
+//			}
+//		}
+	}
+	
+	
+	private static void startSystem() throws JSONException, IOException, MqttException {
+		
+		MyQueue<Integer> codaVal = new MyQueue<Integer>();
+		MyQueue<Pair> codaMsg = new MyQueue<Pair>();
+		Automa automa = new Automa();
+		
+		Antifurto antifurto = new Antifurto(automa);
+		Publisher publisher = new Publisher(codaMsg, antifurto);
+		Esecutore esec = new Esecutore(publisher, codaVal, automa, antifurto.interruttoreOutputSuono);
+		Timer timer = new Timer(30000,-5,esec,automa);
+		
+		antifurto.startClient(esec, publisher);
+		publisher.start();
+		esec.start();
+		timer.start();
+	}
+
+	
+	
+	
+
+}
diff --git a/antifurto/src/Automa.class b/antifurto/src/Automa.class
new file mode 100644
index 0000000000000000000000000000000000000000..227fd713c0f831f7de384b63c9bc1dd8ce5f2626
Binary files /dev/null and b/antifurto/src/Automa.class differ
diff --git a/antifurto/src/Automa.java b/antifurto/src/Automa.java
new file mode 100644
index 0000000000000000000000000000000000000000..0bf0d4549640f7864c14bc67ae4490eabb50dae9
--- /dev/null
+++ b/antifurto/src/Automa.java
@@ -0,0 +1,163 @@
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Hashtable;
+import java.util.Iterator;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class Automa {
+	private JSONObject jsonObject; //tabella che descrive l'automa
+	private Hashtable<String,Integer> deltaSensori; // ad ogni sensore di movimento e' associato il valore di delta
+	private int statoAttuale;
+	private String statoInterruttore;
+	private String statoSuono;
+	
+	
+	public Automa() throws JSONException, IOException {
+		this.jsonObject = new JSONObject(leggiFile("./automa.json"));
+		this.statoInterruttore = "off";
+		this.statoSuono = "off";
+		this.statoAttuale = 0;
+		riempiHashTable();
+	}
+	
+	
+	private void riempiHashTable() throws JSONException, IOException {
+		this.deltaSensori = new Hashtable<String, Integer>();
+		JSONObject jsObj = new JSONObject(leggiFile("./CONF/deltaSensoriMovimento.json"));
+		Iterator<String> keys = jsObj.keys();
+		while(keys.hasNext()) {
+			String key = keys.next();
+			int value = jsObj.getInt(key);
+			deltaSensori.put(key, value);
+		}
+	}
+	
+	
+	private String leggiFile(String path) throws IOException {
+		String line;
+		String answer = "";
+		FileReader fileReader = new FileReader(path);
+		BufferedReader bufferedReader = new BufferedReader(fileReader);
+		while((line = bufferedReader.readLine()) != null) {
+			answer += line;
+		}
+		bufferedReader.close();
+		return answer;
+	}
+	
+	public int getDelta(String nomeSensore) {
+		return deltaSensori.get(nomeSensore);
+	}
+	
+// SERVIRA FORSE PER LA PERSISTENZA	
+//	private synchronized void calcolaStato(String newStatoInterruttore, String newStatoSuono) throws JSONException {
+//		Iterator<String> keys = jsonObject.keys();
+//		while(keys.hasNext()) {
+//			String key = keys.next();
+//			JSONObject riga = jsonObject.getJSONObject(key);
+//			if(riga.getString("out0").equals(newStatoInterruttore) && riga.getString("out1").equals(newStatoSuono)) {
+//				this.statoAttuale = riga.getInt("stato");
+//				this.statoInterruttore = newStatoInterruttore;
+//				this.statoSuono = newStatoSuono;
+//			}
+//		}
+//	}
+	
+	
+	// e' cambiato lo stato dell'interruttore, per cui cerco nel file json in quale nuovo stato devo andare
+	private synchronized void calcolaNuovoStatoMsgFromInterruttore(String newStatoInterruttore) throws JSONException {
+		Iterator<String> keys = jsonObject.keys();
+		while(keys.hasNext()) {
+			String key = keys.next();
+			JSONObject riga = jsonObject.getJSONObject(key);
+			if(riga.getInt("stato")==statoAttuale) {
+				this.statoAttuale = riga.getInt("cmd"+newStatoInterruttore);
+				aggiornaInterruttoreESuono();
+			}
+		}
+	}
+	//in base allo stato attuale dell'antifurto, calcolo lo stato dell'interruttore e del suono
+	private synchronized void aggiornaInterruttoreESuono() throws JSONException {
+		Iterator<String> keys = jsonObject.keys();
+		while(keys.hasNext()) {
+			String key = keys.next();
+			JSONObject riga = jsonObject.getJSONObject(key);
+			if(riga.getInt("stato")==statoAttuale) {
+				this.statoInterruttore = riga.getString("out0");
+				this.statoSuono = riga.getString("out1");
+			}
+		}
+	}
+	
+	// e' cambiato lo stato del suono, per cui cerco nel file json in quale nuovo stato devo andare
+	private synchronized void calcolaNuovoStatoMsgFromSuono(String newStatoSuono) throws JSONException {
+		String jsonKey = "";
+		if(newStatoSuono.equals("on"))
+			jsonKey = "eventValMaggioreSoglia";
+		else
+			jsonKey = "eventValMinoreSoglia";
+		Iterator<String> keys = jsonObject.keys();
+		while(keys.hasNext()) {
+			String key = keys.next();
+			JSONObject riga = jsonObject.getJSONObject(key);
+			if(riga.getInt("stato")==statoAttuale) {
+				this.statoAttuale = riga.getInt(jsonKey);
+				aggiornaInterruttoreESuono();
+			}
+		}
+	}
+	
+	
+	public synchronized void changeStatoInterruttore() throws JSONException {
+		if(statoInterruttore.equals("on"))
+			calcolaNuovoStatoMsgFromInterruttore("off");
+		else 
+			calcolaNuovoStatoMsgFromInterruttore("on");
+	}
+	
+	public synchronized void aggiornaStatoSuono(String newStatoSuono) throws JSONException {
+		calcolaNuovoStatoMsgFromSuono(newStatoSuono);
+	}
+	
+	
+	public synchronized String getStatoAutoma() {
+		return "{\"stato\":"+statoAttuale+",\"interruttore\":\""+statoInterruttore+"\",\"allarme\":\""+statoSuono+"\"}";
+	}
+	
+	
+	public synchronized String getStatoInterruttore() {
+		return statoInterruttore;
+	}
+	
+	
+	public synchronized String getStatoSuono() {
+		return statoSuono;
+	}
+	
+	
+	// Ritorna true se l'antifurto e' attivo
+	public synchronized boolean antifurtoAttivo() {
+		if(statoInterruttore.equals("on"))
+			return true;
+		return false;
+	}
+	
+	// Ritorna true se l'antifurto sta suonando
+	public synchronized boolean allarme() {
+		if(statoSuono.equals("on"))
+			return true;
+		return false;
+	}
+	
+	
+	// con input la stringa "0" restituisce "off"
+	// con input la stringa "1" restituisce "on"
+	public String converter(int num) {
+		if(num==0)
+			return "off";
+		return "on";
+	}
+}
diff --git a/antifurto/src/CONF/conf.json b/antifurto/src/CONF/conf.json
new file mode 100644
index 0000000000000000000000000000000000000000..e0e44ba3a91cd48fa1ac27fa410d9bb8711e3f18
--- /dev/null
+++ b/antifurto/src/CONF/conf.json
@@ -0,0 +1,7 @@
+{
+    "confdir": "./CONF",
+    "homedir": "./home",
+    "broker": "localhost",
+    "protocol": "tcp",
+    "port": 1883
+}
diff --git a/antifurto/src/CONF/deltaSensoriMovimento.json b/antifurto/src/CONF/deltaSensoriMovimento.json
new file mode 100644
index 0000000000000000000000000000000000000000..798edf6d07714548dd4a4b86ea61df3ce2d5c5e1
--- /dev/null
+++ b/antifurto/src/CONF/deltaSensoriMovimento.json
@@ -0,0 +1,5 @@
+{
+    "IN1":33,
+    "IN2":4,
+    "IN3":8
+}
diff --git a/antifurto/src/CONF/zona.json b/antifurto/src/CONF/zona.json
new file mode 100644
index 0000000000000000000000000000000000000000..4d6845d2a7658d037e59631a4ed6f3a95cb68701
--- /dev/null
+++ b/antifurto/src/CONF/zona.json
@@ -0,0 +1,9 @@
+{
+    "interruttore": "IN0",
+    "sensoriMovimento": [
+      "IN1",
+      "IN2",
+      "IN3"
+    ],
+    "outputSuono":"OUT3"
+}
diff --git a/antifurto/src/Conf.class b/antifurto/src/Conf.class
new file mode 100644
index 0000000000000000000000000000000000000000..b3ab01929c6ec669e9904ecfb6993e9cb47c0257
Binary files /dev/null and b/antifurto/src/Conf.class differ
diff --git a/antifurto/src/Esecutore.class b/antifurto/src/Esecutore.class
new file mode 100644
index 0000000000000000000000000000000000000000..3a10616f2b3ad22f10290a135955c179110b79c0
Binary files /dev/null and b/antifurto/src/Esecutore.class differ
diff --git a/antifurto/src/Esecutore.java b/antifurto/src/Esecutore.java
new file mode 100644
index 0000000000000000000000000000000000000000..afc44b37e878c1a8e287c59da15d2458dca05db0
--- /dev/null
+++ b/antifurto/src/Esecutore.java
@@ -0,0 +1,63 @@
+import java.time.Duration;
+import java.time.LocalDateTime;
+
+
+public class Esecutore extends Thread {
+	
+	private MyQueue<Integer> codaVal; // coda in cui vengono mantenuti i valori da sommare alla variabile valore
+	private int valore; // variabile numerica cumulativa inizialmente impostata a 0
+	public final int SOGLIA = 100;
+	private Publisher publisher;
+	private Automa automa;
+	private String outputSuono; // nome logico dell'interruttore che fa scattare il suono dell'allarme 
+	
+	
+	public Esecutore(Publisher publisher, MyQueue<Integer> codaVal, Automa automa, String outputSuono) {
+		this.codaVal = codaVal;
+		this.valore = 0;
+		this.automa = automa;
+		this.publisher = publisher;
+		this.outputSuono = outputSuono;
+	}
+	
+	
+	public void run() {
+		final long DURATA_SUONO = 5; // l'allarme dura 5 minuti e poi smette di suonare
+		int delta;
+		LocalDateTime tempoAllarme = null;
+		while(true) {
+			while(automa.antifurtoAttivo()) { System.out.println("VALORE = "+ valore);
+				delta = codaVal.receive();
+				valore = valore + delta;
+				if(valore >= SOGLIA && (!automa.allarme()) ) {	
+					publisher.aggiungiComando("to/gruppo2/luci/gpio/"+outputSuono, "{cmd:1}");
+					tempoAllarme = LocalDateTime.now();
+				}
+				else {
+					if(automa.allarme()) {
+						LocalDateTime tempoAttuale = LocalDateTime.now();
+						long durata = Math.abs(Duration.between(tempoAllarme, tempoAttuale).toMinutes());
+						if(valore < SOGLIA || durata >= DURATA_SUONO || !automa.antifurtoAttivo() ) {
+							publisher.aggiungiComando("to/gruppo2/luci/gpio/"+outputSuono, "{cmd:0}"); // l'allarme viene disattivato
+						}
+					}
+				}
+				
+			}
+			if(!automa.antifurtoAttivo()) {
+				reset();
+			}
+		}
+	}
+
+	
+	public void aggiungiVal(int n) {
+		codaVal.send(n);
+	}
+	
+	public void reset() {
+		valore = 0;
+		codaVal.removeAll();
+	}
+	
+}
diff --git a/antifurto/src/MsgQueue.class b/antifurto/src/MsgQueue.class
new file mode 100644
index 0000000000000000000000000000000000000000..d6956128de101c25dd7df3fa5b3a936253bc75f9
Binary files /dev/null and b/antifurto/src/MsgQueue.class differ
diff --git a/antifurto/src/MyQueue.class b/antifurto/src/MyQueue.class
new file mode 100644
index 0000000000000000000000000000000000000000..ed150b738845949457f13c011b425006c71015aa
Binary files /dev/null and b/antifurto/src/MyQueue.class differ
diff --git a/antifurto/src/MyQueue.java b/antifurto/src/MyQueue.java
new file mode 100644
index 0000000000000000000000000000000000000000..1d4553821deb6a59ba1f1012bfbdef9f2504801e
--- /dev/null
+++ b/antifurto/src/MyQueue.java
@@ -0,0 +1,34 @@
+import java.util.ArrayList;
+
+public class MyQueue<T> {
+
+	private ArrayList<T> queue;
+	
+	public MyQueue() {
+		this.queue = new ArrayList<T>();
+	}
+	
+	public boolean isEmpty() {
+		return queue.isEmpty();
+	}
+	
+	public synchronized void send(T n) {
+		queue.add(n);
+		notifyAll();
+	}
+	
+	public synchronized T receive() {
+		while(isEmpty()) {
+			try {
+				wait();
+			} catch(InterruptedException ex) {
+				System.out.println("Interrupted exception");
+			}
+		}
+		return queue.remove(0);
+	}
+	
+	public synchronized void removeAll() {
+		queue.clear();
+	}
+}
diff --git a/antifurto/src/Pair.class b/antifurto/src/Pair.class
new file mode 100644
index 0000000000000000000000000000000000000000..2cac32bc7272b7d02a6aacad0738ef50b5930999
Binary files /dev/null and b/antifurto/src/Pair.class differ
diff --git a/antifurto/src/Pair.java b/antifurto/src/Pair.java
new file mode 100644
index 0000000000000000000000000000000000000000..2c47068961be4223ddbf52184e7c2bdd2d88fc1c
--- /dev/null
+++ b/antifurto/src/Pair.java
@@ -0,0 +1,20 @@
+
+public class Pair {
+
+	private String topic;
+	private String msg;
+	
+	
+	public Pair(String topic, String msg) {
+		this.topic = topic;
+		this.msg = msg;
+	}
+	
+	public String getTopic() {
+		return topic;
+	}
+	
+	public String getMsg() {
+		return msg;
+	}
+}
diff --git a/antifurto/src/Publisher.class b/antifurto/src/Publisher.class
new file mode 100644
index 0000000000000000000000000000000000000000..3cddd01bee097b77cc544d9436535a0ecc5474f5
Binary files /dev/null and b/antifurto/src/Publisher.class differ
diff --git a/antifurto/src/Publisher.java b/antifurto/src/Publisher.java
new file mode 100644
index 0000000000000000000000000000000000000000..2309b0634ec7c2b46702f22b4acf45c1879e90a2
--- /dev/null
+++ b/antifurto/src/Publisher.java
@@ -0,0 +1,28 @@
+import org.eclipse.paho.client.mqttv3.MqttException;
+
+public class Publisher extends Thread {
+
+	private MyQueue<Pair> coda;
+	private Antifurto client;
+	
+	public Publisher(MyQueue<Pair> coda, Antifurto client) {
+		this.coda = coda;
+		this.client = client;
+	}
+	
+	public void aggiungiComando(String topic, String msg) {
+		coda.send(new Pair(topic, msg));
+	}
+	
+	
+	public void run() {
+		while(true) {
+			Pair p = coda.receive();
+			try {
+				client.publishMethod(p.getTopic(), p.getMsg());
+			} catch (MqttException e) {
+				e.printStackTrace();
+			}
+		}
+	}
+}
diff --git a/antifurto/src/SubscribeCallback.class b/antifurto/src/SubscribeCallback.class
new file mode 100644
index 0000000000000000000000000000000000000000..a971029b402f4ad48d870288ad4ad7adb8b289c6
Binary files /dev/null and b/antifurto/src/SubscribeCallback.class differ
diff --git a/antifurto/src/SubscribeCallback.java b/antifurto/src/SubscribeCallback.java
new file mode 100644
index 0000000000000000000000000000000000000000..be24d4b6270672ebeb62febac497c734bc82d3c3
--- /dev/null
+++ b/antifurto/src/SubscribeCallback.java
@@ -0,0 +1,137 @@
+import java.util.Date;
+
+import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
+import org.eclipse.paho.client.mqttv3.MqttCallback;
+import org.eclipse.paho.client.mqttv3.MqttException;
+import org.eclipse.paho.client.mqttv3.MqttMessage;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class SubscribeCallback implements MqttCallback{
+	
+	private Antifurto client;
+	private Publisher publisher;
+	private String nomeInterruttore; 
+	private String nomeOutputSuono;
+	private Esecutore esec;
+	private Automa automa;
+	
+	public SubscribeCallback(Antifurto client, Publisher publisher, String nomeInterruttore, String nomeOutputSuono, Esecutore esec, Automa automa) {
+		this.client = client;
+		this.publisher = publisher;
+		this.nomeInterruttore = nomeInterruttore;
+		this.nomeOutputSuono = nomeOutputSuono;
+		this.esec = esec;
+		this.automa = automa;
+	}
+
+	@Override
+	public void connectionLost(Throwable arg0) {
+		boolean retry = true;
+		final Date d = new Date();
+		Date d2 = new Date();
+		long time = Math.abs(d2.getTime()-d.getTime());
+		while(retry & (time<600000)) {
+			try {
+				client.startClient(esec, publisher);
+				retry = false;
+			} catch (MqttException e) {
+				d2 = new Date();
+				time = Math.abs(d2.getTime()-d.getTime());
+			}
+		}
+		if(time>=600000) {
+			System.out.println("Tentativo di riconnessione fallito");
+			System.exit(1);
+		}
+	}
+
+	
+	@Override
+	public void deliveryComplete(IMqttDeliveryToken arg0) {
+		// Nessuna operazione
+		
+	}
+	
+
+	@Override
+	public void messageArrived(String topic, MqttMessage message) throws Exception {
+		if(topic.equals("to/all"))
+			sendDescription("from/gruppo2/luci/antifurto/description");
+		else {
+			if(topic.equals("rpc/gruppo2/luci/antifurto"))
+				sendMyState("from/gruppo2/luci/antifurto"); // CONTROLLA SE VA BENE PUBBLICARE LO STATO SU QUESTO TOPIC O SE IL TOPIC DEVE ESSERE UN'ALTRO. NOTA: NON POSSO PUBBLICARE SULLO STESSO TOPIC CHE HO SOTTOSCRITTO
+			else {
+				JSONObject msgJson = new JSONObject(message.toString());
+				
+				
+				// CONTROLLA SULLA BEAGLEBONE CHE IL MESSAGGIO MANDATO SIA UN INT. CONTROLLA ANCHE IL TOPIC SU CUI MANDA I MESSAGGI (da aver sottoscritto i topic correttamente)
+				
+				if(topic.equals("from/gruppo2/luci/gpio/"+nomeOutputSuono)) {
+					int newStatus = -1;
+					if(msgJson.has("status"))
+						newStatus = msgJson.getInt("status");
+					else {
+						if(msgJson.has("event"))
+							newStatus = msgJson.getInt("event");
+					}
+					handleStateMsgFromOutputSuono(newStatus);
+				}
+				else {
+					int event = msgJson.getInt("event");
+					if(event==0) {
+						// e' stato premuto l'interruttore / il sensore di movimento ha rilevato qualcuno
+						if(topic.equals("from/gruppo2/luci/gpio/"+nomeInterruttore))
+							handleStateMsgFromInterruttore();
+						else {
+							// il topic sara' from/gruppo2/luci/gpio/IN0  dove IN0 e' un sensore di movimento
+							String[] t = topic.split("/");
+							handleMovimento(t[4]);
+						}
+					}
+				}				
+			}
+		}	
+		
+	}
+	
+
+	
+	private synchronized void handleStateMsgFromInterruttore() throws JSONException {
+		automa.changeStatoInterruttore();
+		if(automa.getStatoInterruttore().equals("off")) {
+			publisher.aggiungiComando("to/gruppo2/luci/gpio/"+nomeOutputSuono, "{cmd:0}"); 
+			esec.reset();
+		}
+	}
+	
+	
+	private synchronized void handleStateMsgFromOutputSuono(int newStatus) throws JSONException {
+		if(newStatus!=-1) {
+			String statusOnOff = automa.converter(newStatus); // lo stato e' della forma "on" oppure "off"
+			if(! statusOnOff.equals(automa.getStatoSuono()) ) {
+				automa.aggiornaStatoSuono(statusOnOff);
+			}
+		}
+	}
+	
+
+	
+	
+	private void handleMovimento(String nomeSensore) {
+		if(automa.getStatoInterruttore().equals("on"))
+			esec.aggiungiVal(automa.getDelta(nomeSensore));
+	}
+	
+	private void sendMyState(String topic) throws MqttException {
+		publisher.aggiungiComando(topic, "{\"status\":"+automa.getStatoAutoma()+"}"); 
+	}
+	
+	private void sendDescription(String topic) {
+		// DA FARE: guardare quale descrizione manda netmon, archiver, meter,... e implementare di conseguenza questo metodo
+//		String msg = 
+//		publisher.aggiungiComando(topic, msg); 
+	}
+	
+
+}
diff --git a/antifurto/src/Timer.class b/antifurto/src/Timer.class
new file mode 100644
index 0000000000000000000000000000000000000000..a922d3a359f247a50f10d27f2ab8cc235c553dd6
Binary files /dev/null and b/antifurto/src/Timer.class differ
diff --git a/antifurto/src/Timer.java b/antifurto/src/Timer.java
new file mode 100644
index 0000000000000000000000000000000000000000..808651da6e93561eaec39a01bb8839642cc1bc53
--- /dev/null
+++ b/antifurto/src/Timer.java
@@ -0,0 +1,30 @@
+
+public class Timer extends Thread {
+	
+	private int delay;
+	private int deltaK; // e' un valore negativo
+	private Esecutore esec;
+	private Automa automa;
+	
+	public Timer(int delay, int deltaK, Esecutore esec, Automa automa) {
+		this.delay = delay;
+		this.deltaK = deltaK;
+		this.esec = esec;
+		this.automa = automa;
+	}
+
+	public void run() {
+		while(true) {
+			while(automa.antifurtoAttivo()) { // finché l'antifurto è acceso
+				esec.aggiungiVal(deltaK);
+				
+				try {
+					Thread.sleep(delay);
+				} catch(InterruptedException e) {
+					System.out.println(e);
+				}
+			}
+		}
+	}
+	
+}
diff --git a/antifurto/src/ValQueue.class b/antifurto/src/ValQueue.class
new file mode 100644
index 0000000000000000000000000000000000000000..e05e8939d7f67878f2f2d04707cf78247d069d4b
Binary files /dev/null and b/antifurto/src/ValQueue.class differ
diff --git a/antifurto/src/automa.json b/antifurto/src/automa.json
new file mode 100644
index 0000000000000000000000000000000000000000..aea84d7420e840fab0a6f79e04595af3fa5c4c9b
--- /dev/null
+++ b/antifurto/src/automa.json
@@ -0,0 +1,26 @@
+{
+  "riga0": {"stato": 0,
+    "cmdoff": 0,
+    "cmdon": 1,
+    "eventValMaggioreSoglia": 0,
+    "eventValMinoreSoglia": 0,
+    "out0":"off",
+    "out1":"off"
+  },
+  "riga1": {"stato": 1,
+    "cmdoff": 0,
+    "cmdon": 1,
+    "eventValMaggioreSoglia": 2,
+    "eventValMinoreSoglia": 1,
+    "out0":"on",
+    "out1":"off"
+  },
+  "riga2": {"stato": 2,
+    "cmdoff": 0,
+    "cmdon": 2,
+    "eventValMaggioreSoglia": 2,
+    "eventValMinoreSoglia": 1,
+    "out0":"on",
+    "out1":"on"
+  }
+}
diff --git a/antifurto/src/bin/install.bash b/antifurto/src/bin/install.bash
new file mode 100755
index 0000000000000000000000000000000000000000..5b474c2c4537e30ea0d6ebe4431f2d84420dd07d
--- /dev/null
+++ b/antifurto/src/bin/install.bash
@@ -0,0 +1,2 @@
+#!/bin/bash
+if [ ! -d "/home/debian/hat_home" ]; then mkdir /home/debian/hat_home; fi
diff --git a/antifurto/src/bin/start.bash b/antifurto/src/bin/start.bash
new file mode 100755
index 0000000000000000000000000000000000000000..139636330902298aa2bebbcdc6b2f6027bc2736c
--- /dev/null
+++ b/antifurto/src/bin/start.bash
@@ -0,0 +1,6 @@
+#!/bin/bash
+status="OFF"
+running=$(/usr/bin/pgrep antifurto)
+if [ -f "/home/debian/CONFIG/antifurto/STATUS" ]; then status=$(cat /home/debian/CONFIG/antifurto/STATUS); fi
+if [ ! -z "$running" ]; then status="OFF"; fi
+if [ $status == "ON" ]; then (/home/debian/CONFIG/antifurto/bin/antifurto /home/debian/CONFIG/antifurto &>/dev/null&); fi 
diff --git a/antifurto/src/bin/stop.bash b/antifurto/src/bin/stop.bash
new file mode 100755
index 0000000000000000000000000000000000000000..c7fc4610f10ea3118206c3f0cbbc0bcadde78d90
--- /dev/null
+++ b/antifurto/src/bin/stop.bash
@@ -0,0 +1,5 @@
+#!/bin/bash
+status="NONE"
+if [ -f "/home/debian/CONFIG/antifurto/STATUS" ]; then status=$(cat /home/debian/CONFIG/antifurto/STATUS); fi
+if [ $status == "OFF" ]; then /home/debian/bin/stopag antifurto; fi
+
diff --git a/antifurto/src/bridge.conf b/antifurto/src/bridge.conf
new file mode 100644
index 0000000000000000000000000000000000000000..cf74ca90b0293f5305a45727cccd33a392211d2e
--- /dev/null
+++ b/antifurto/src/bridge.conf
@@ -0,0 +1,15 @@
+connection bridge-pissir-20030784
+address luci.local:1883
+bridge_cafile /etc/mosquitto/ca_certificates/ca_certificate.pem
+
+tls_version tlsv1.2
+try_private false
+remote_username gruppo2
+remote_password funziona
+
+topic to/all in 2
+topic rpc/gruppo2/luci/antifurto in 2
+topic from/gruppo2/luci/gpio/+ both 2
+
+topic from/gruppo2/luci/antifurto/description out 1
+topic from/gruppo2/luci/antifurto out 1
diff --git a/antifurto/src/conf.xml b/antifurto/src/conf.xml
new file mode 100644
index 0000000000000000000000000000000000000000..b0ba5f71b929c3598c0adeb2e3d05df60b4932d6
--- /dev/null
+++ b/antifurto/src/conf.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" ?><set type="pathnames">
+<attribute category="generic" name="agenttype" value="antifurto"/>
+<attribute category="generic" name="agentname" value="antifurto_$HOST"/>
+<attribute category="generic" name="homedir" value="/home/debian/hat_home/antifurto"/>
+<attribute category="generic" name="emergency_homedir" value="/home/debian/hat_home/antifurto"/>
+<attribute category="generic" name="description" value="Local Antifurto"/>
+<attribute category="generic" name="port" value="8090"/>
+<attribute category="generic" name="threads" value="threads"/>
+<attribute category="generic" name="mqtt_domain" value=""/>
+<attribute category="generic" name="mqtt_subdomain" value=""/>
+<attribute category="generic" name="mqtt_service" value="antifurto"/>
+</set>
diff --git a/antifurto/src/logger.xml b/antifurto/src/logger.xml
new file mode 100644
index 0000000000000000000000000000000000000000..6bd6903e0e7bed8b59c5b6acbe8cc0a9b603b4b6
--- /dev/null
+++ b/antifurto/src/logger.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" ?><set type="pathnames">
+	<attribute category="generic" name="log_theme" value="from/$MQTT_DOMAIN/$MQTT_SUBDOMAIN/$MQTT_SERVICE/logger"/>
+	<attribute category="generic" name="status_theme" value="from/$MQTT_DOMAIN/$MQTT_SUBDOMAIN/$MQTT_SERVICE/$LOCALNAME"/>
+	<attribute category="generic" name="notify_theme" value="from/any"/>
+	<attribute category="generic" name="describe_theme" value="from/$MQTT_DOMAIN/$MQTT_SUBDOMAIN/$MQTT_SERVICE/description"/>
+	<attribute category="generic" name="log_msg" value="{$LOCALNAME:$MESSAGE}"/>
+	<attribute category="generic" name="status_msg" value="{status:$MESSAGE}"/>
+	<attribute category="generic" name="notify_msg" value="{$INFOTYPE:$MESSAGE}"/>
+	<attribute category="generic" name="describe_msg" value=" $MESSAGE "/>
+</set>
diff --git a/antifurto/src/org.eclipse.paho.client.mqttv3_1.2.5.jar b/antifurto/src/org.eclipse.paho.client.mqttv3_1.2.5.jar
new file mode 100644
index 0000000000000000000000000000000000000000..f75a141b10d88b1fa98a90519c826e690b62beab
Binary files /dev/null and b/antifurto/src/org.eclipse.paho.client.mqttv3_1.2.5.jar differ
diff --git a/antifurto/src/org.json-1.0.0.v201011060100.jar b/antifurto/src/org.json-1.0.0.v201011060100.jar
new file mode 100644
index 0000000000000000000000000000000000000000..2aa70fdf80f4cc14a2063a8f118c847506c6c579
Binary files /dev/null and b/antifurto/src/org.json-1.0.0.v201011060100.jar differ
diff --git a/antifurto/src/org.json-chargebee-1.0-javadoc.jar b/antifurto/src/org.json-chargebee-1.0-javadoc.jar
new file mode 100644
index 0000000000000000000000000000000000000000..256575384cbadfd8afd42c0639795d21d3fe8822
Binary files /dev/null and b/antifurto/src/org.json-chargebee-1.0-javadoc.jar differ
diff --git a/antifurto/src/templates/interruttore.xml b/antifurto/src/templates/interruttore.xml
new file mode 100644
index 0000000000000000000000000000000000000000..e0f6b5aeaa693f3bc6971498ac1b9ab9eee18a1d
--- /dev/null
+++ b/antifurto/src/templates/interruttore.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<set type="interruttore">
+<attribute name="type" value="booleanin"/>
+<attribute name="name" value=""/> <!-- DA RIEMPIRE CON IL NOME SPECIFICATO DALL'UTENTE PER QUELL'INTERRUTTORE. SE NON SPECIFICATO, IL NOME SARA' LO STESSO DEL NOME LOGICO DI QUELL'INPUT -->
+<attribute name="capename" value=""/> <!-- DA RIEMPIRE CON IL NOME LOGICO DELL'INPUT -->
+</set>
diff --git a/antifurto/src/templates/sensoremovimento1.xml b/antifurto/src/templates/sensoremovimento1.xml
new file mode 100644
index 0000000000000000000000000000000000000000..706315adb672836504a033a5ec883942c4e12677
--- /dev/null
+++ b/antifurto/src/templates/sensoremovimento1.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<set type="sensoremovimento">
+<attribute name="type" value="booleanin"/>
+<attribute name="name" value=""/> <!-- DA RIEMPIRE CON IL NOME SPECIFICATO DALL'UTENTE PER QUEL SENSORE. SE NON SPECIFICATO, IL NOME SARA' LO STESSO DEL NOME LOGICO DI QUELL'INPUT -->
+<attribute name="capename" value=""/> <!-- DA RIEMPIRE CON IL NOME LOGICO DELL'INPUT -->
+</set>
diff --git a/antifurto/src/threads/MqttClientThread.xml b/antifurto/src/threads/MqttClientThread.xml
new file mode 100644
index 0000000000000000000000000000000000000000..f17afa14bd5deeda8db6237107bc9515cc44e554
--- /dev/null
+++ b/antifurto/src/threads/MqttClientThread.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0"?>
+<set type="thread">
+<attribute category="generic" name="status" value="ON"/>
+<attribute category="generic" name="host" value="localhost"/>
+<attribute category="generic" name="port" value="1883"/>
+<attribute category="topic" name="to/$MQTT_DOMAIN/$MQTT_SUBDOMAIN/$MQTT_SERVICE/#" value="mqtt_sub"/> <!-- Sottoscrivo i messaggi che provengono da qualsiasi mia componente, ovvero dall'interruttore on/off e dai sensori di movimento -->
+<attribute category="topic" name="from/$MQTT_DOMAIN/+/gpio/#" value="mqtt_sub"/> <!-- Sottoscrivo i messaggi che notificano il cambiamento di stato dei sensori -->
+<attribute category="topic" name="rpc/$MQTT_DOMAIN/$MQTT_SUBDOMAIN/$MQTT_SERVICE" value="mqtt_sub"/> <!-- Su questo topic ricevero' le richieste di inviare il mio stato attuale -->
+<attribute category="topic" name="rpc/$MQTT_DOMAIN/$MQTT_SUBDOMAIN/$MQTT_SERVICE/#" value="mqtt_sub"/> <!-- Su questo topic ricevero' le richieste di inviare lo stato delle mie componenti, che prendero' dallo stato corrispondente del gpio -->
+<attribute category="topic" name="to/all" value="mqtt_sub"/> <!-- Su questo topic ricevero' un messaggio del tipo {request:descripion.json} Dovro' quindi mandare la mia descrizione json -->
+</set>