From cf3c22e6dd9d528a3418a2e2951a657d75ab4f4f Mon Sep 17 00:00:00 2001
From: Elisa <20030784@studenti.uniupo.it>
Date: Wed, 21 Sep 2022 10:46:45 +0200
Subject: [PATCH] modifiche necessarie per la corretta configurazione del
 microservizio antifurto

---
 WebServer/.gitignore                          |  1 +
 WebServer/.project                            | 28 ++++++++++++++
 antifurtoMicroservizio/bin/.gitignore         |  1 +
 .../res/CONF/deltaSensoriMovimento.json       |  2 +-
 antifurtoMicroservizio/res/CONF/zona.json     |  2 +-
 antifurtoMicroservizio/res/stato.json         |  2 +-
 .../src/code/Antifurto.java                   |  2 +-
 .../src/code/SubscribeCallback.java           | 37 +++++++++++++++----
 8 files changed, 63 insertions(+), 12 deletions(-)
 create mode 100644 WebServer/.gitignore
 create mode 100644 WebServer/.project
 create mode 100644 antifurtoMicroservizio/bin/.gitignore

diff --git a/WebServer/.gitignore b/WebServer/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/WebServer/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/WebServer/.project b/WebServer/.project
new file mode 100644
index 0000000..193e0c6
--- /dev/null
+++ b/WebServer/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>pissirsmarthome_WebServer</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+	<filteredResources>
+		<filter>
+			<id>1658153304390</id>
+			<name></name>
+			<type>30</type>
+			<matcher>
+				<id>org.eclipse.core.resources.regexFilterMatcher</id>
+				<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
+			</matcher>
+		</filter>
+	</filteredResources>
+</projectDescription>
diff --git a/antifurtoMicroservizio/bin/.gitignore b/antifurtoMicroservizio/bin/.gitignore
new file mode 100644
index 0000000..3725a21
--- /dev/null
+++ b/antifurtoMicroservizio/bin/.gitignore
@@ -0,0 +1 @@
+/code/
diff --git a/antifurtoMicroservizio/res/CONF/deltaSensoriMovimento.json b/antifurtoMicroservizio/res/CONF/deltaSensoriMovimento.json
index ff038e5..36f1ea9 100644
--- a/antifurtoMicroservizio/res/CONF/deltaSensoriMovimento.json
+++ b/antifurtoMicroservizio/res/CONF/deltaSensoriMovimento.json
@@ -1 +1 @@
-{"IN5":32,"IN2":4,"IN1":33}
\ No newline at end of file
+{"IN2":4,"IN1":33,"IN3":10}
\ No newline at end of file
diff --git a/antifurtoMicroservizio/res/CONF/zona.json b/antifurtoMicroservizio/res/CONF/zona.json
index e80fbcd..b3472c2 100644
--- a/antifurtoMicroservizio/res/CONF/zona.json
+++ b/antifurtoMicroservizio/res/CONF/zona.json
@@ -1 +1 @@
-{"nomeOutputAntifurto":"OUT0","interruttore":"IN0","sensoriMovimento":["IN1","IN2","IN5"],"outputSuono":"OUT1"}
\ No newline at end of file
+{"nomeOutputAntifurto":"OUT0","interruttore":"IN0","sensoriMovimento":["IN1","IN2","IN3"],"outputSuono":"OUT1"}
\ No newline at end of file
diff --git a/antifurtoMicroservizio/res/stato.json b/antifurtoMicroservizio/res/stato.json
index e152b4e..d2a6c3e 100644
--- a/antifurtoMicroservizio/res/stato.json
+++ b/antifurtoMicroservizio/res/stato.json
@@ -1 +1 @@
-{"soglia":32,"stato":1,"valore":0,"tempoAllarme":"2022-09-19T17:23:27.756890"}
\ No newline at end of file
+{"soglia":10,"stato":0,"valore":0,"tempoAllarme":"2022-09-20T22:01:57.853743"}
\ No newline at end of file
diff --git a/antifurtoMicroservizio/src/code/Antifurto.java b/antifurtoMicroservizio/src/code/Antifurto.java
index ce25cce..c6ab2a3 100644
--- a/antifurtoMicroservizio/src/code/Antifurto.java
+++ b/antifurtoMicroservizio/src/code/Antifurto.java
@@ -88,7 +88,7 @@ public class Antifurto {
 		topicsSub.add("from/"+getMqttTree()+"/gpio/" + interruttoreOutputSuono); // Sottoscrivo i messaggi che notificano il cambiamento di stato dell'interruttore
 
 
-		topicsSub.add("to/"+getMqttTree()+"/antifurto/soglia"); // Su questo topic mi arrivera' un messaggio {"soglia": 30} e dovrò impostare la soglia di conseguenza
+		topicsSub.add("conf/"+getMqttTree()+"/antifurto/soglia"); // Su questo topic mi arrivera' un messaggio {"soglia": 30} e dovrò impostare la soglia di conseguenza
 
 		topicsSub.add("conf/"+getMqttTree()+"/antifurto");
 		topicsSub.add("conf/"+getMqttTree()+"/antifurto/sensore"); // Su questo topic mi arrivera' un messaggio per l'aggiunta di un sensore di movimento.
diff --git a/antifurtoMicroservizio/src/code/SubscribeCallback.java b/antifurtoMicroservizio/src/code/SubscribeCallback.java
index 4391572..71c0931 100644
--- a/antifurtoMicroservizio/src/code/SubscribeCallback.java
+++ b/antifurtoMicroservizio/src/code/SubscribeCallback.java
@@ -90,7 +90,7 @@ public class SubscribeCallback implements MqttCallback {
 				sendOutAntifurto();
 			return;
 		}
-		if (topic.equals("to/" + Antifurto.getMqttTree() + "/antifurto/soglia")) {
+		if (topic.equals("conf/" + Antifurto.getMqttTree() + "/antifurto/soglia")) {
 			if (msgJson.has("soglia"))
 				handleSetSoglia(msgJson);
 			return;
@@ -166,7 +166,7 @@ public class SubscribeCallback implements MqttCallback {
 		else
 			// voglio accendere l'antifurto, quindi invio un messaggio per accendere la luce che indica che l'antifurto e' acceso
 			publisher.aggiungiComando("to/" + Antifurto.getMqttTree() + "/gpio/" + client.getNomeOutputAntifurto(), "{cmd:1}");
-																													//
+																													
 
 		contatore++;
 	}
@@ -266,24 +266,33 @@ public class SubscribeCallback implements MqttCallback {
 		publisher.aggiungiComando(topic, json.toString());
 	}
 
-	private void setConsistentStatus(JSONObject msgJson) throws JSONException, IOException { //DA MODIFICARE (invio webapp)
+	private void setConsistentStatus(JSONObject msgJson) throws JSONException, IOException { 
+		
 		int statoOutGpio = msgJson.getInt("status");
+		JSONObject js = new JSONObject();		
+		
 		if (statoOutGpio == 0 && automa.getStatoInterruttore().equals("on")) {
 			// devo andare nello stato 0 dell'automa
 			automa.setStatoAutoma(0);
 			automa.aggiornaInterruttoreESuono();
 			esec.reset();
+			js.put("event", automa.getStatoInterrutoreTrueFalse());
 		}
 		if (statoOutGpio == 1 && automa.getStatoInterruttore().equals("off")) {
 			// devo andare nello stato 1 dell'automa
 			automa.setStatoAutoma(1);
 			automa.aggiornaInterruttoreESuono();
+			js.put("event", automa.getStatoInterrutoreTrueFalse());
+		}
+		if(js.has("event")) {			
+			// aggiorno il file di persistenza
+			JSONObject statoJson = new JSONObject(Helper.leggiFile(Automa.FILE_STATO));
+			statoJson.put("stato", automa.getStatoAutoma());
+			statoJson.put("valore", esec.getValore());
+			Helper.scriviFile(statoJson, Automa.FILE_STATO);
+			
+			publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto/antifurto", js.toString());
 		}
-		// aggiorno il file di persistenza
-		JSONObject statoJson = new JSONObject(Helper.leggiFile(Automa.FILE_STATO));
-		statoJson.put("stato", automa.getStatoAutoma());
-		statoJson.put("valore", esec.getValore());
-		Helper.scriviFile(statoJson, Automa.FILE_STATO);
 	}
 
 	private void sendOutAntifurto() throws JSONException {
@@ -340,6 +349,12 @@ public class SubscribeCallback implements MqttCallback {
 		client.setNomeOutputAntifurto(nomeOutputAntifurto);
 		client.addTopicToSubscribe("from/"+Antifurto.getMqttTree()+"/gpio/" + nomeOutputAntifurto);
 		newNomeOutputAntifurtoConf = null;
+		String message = null;
+		if(automa.getStatoAutoma() == 1 || automa.getStatoAutoma()==2) 
+			message = "{cmd:1}";
+		else
+			message = "{cmd:0}";
+		publisher.aggiungiComando("to/"+Antifurto.getMqttTree()+"/gpio/"+client.getNomeOutputAntifurto(), message);
 	}
 	
 	private void newConfigNomeOutputSuono(String outputSuono) throws MqttException, JSONException {
@@ -347,6 +362,12 @@ public class SubscribeCallback implements MqttCallback {
 		client.setNomeOutputSuono(outputSuono);
 		client.addTopicToSubscribe("from/"+Antifurto.getMqttTree()+"/gpio/" + outputSuono);	
 		newSuonoConf = null;
+		String message = null;
+		if(automa.getStatoAutoma()==2) 
+			message = "{cmd:1}";
+		else
+			message = "{cmd:0}";
+		publisher.aggiungiComando("to/"+Antifurto.getMqttTree()+"/gpio/"+client.getNomeOutputSuono(), message);
 	}
 
 	// pubblica la risposta (per la webApp) solo se newNomeOutputAntifurtoConf = null e newSuonoConf = null
-- 
GitLab