Skip to content
Snippets Groups Projects
Commit b9817252 authored by Elisa Giglio's avatar Elisa Giglio
Browse files

aggiunto messaggio iniziale da mandare agli scenari

parent 7e5eec48
No related branches found
No related tags found
No related merge requests found
......@@ -100,7 +100,7 @@ public class Antifurto {
}
public void startClient(Esecutore esec, Publisher publisher) throws MqttException {
public void startClient(Esecutore esec, Publisher publisher) throws MqttException, JSONException {
String caFilePath = "";
String clientCrtFilePath = "";
String clientKeyFilePath = "";
......@@ -130,12 +130,15 @@ public class Antifurto {
e.printStackTrace();
}
mqttClient.setCallback(new SubscribeCallback(this, publisher, esec, automa));
SubscribeCallback subCall = new SubscribeCallback(this, publisher, esec, automa);
mqttClient.setCallback(subCall);
mqttClient.connect(options);
for(String t: topicsSub)
addTopicToSubscribe(t);
subCall.sendOutAntifurto();
}
// sottoscrive il topic passato come parametro
......
......@@ -16,6 +16,7 @@ public class Esecutore extends Thread {
private Automa automa;
private Antifurto antifurto;
private LocalDateTime tempoAllarme; //valorizzato solo quando l'allarme sta suonando. Contiene l'ora in cui l'allarme ha iniziato a suonare
private static boolean changedSoglia = false;
public Esecutore(Publisher publisher, MyQueue<Integer> codaVal, Automa automa, Antifurto antifurto) throws JSONException, IOException {
this.codaVal = codaVal;
......@@ -58,14 +59,17 @@ public class Esecutore extends Thread {
JSONObject statoJson = new JSONObject(Helper.leggiFile(Automa.FILE_STATO));
statoJson.put("valore", valore);
if(valore >= soglia && (!automa.allarme()) && delta>0) {
if(valore >= soglia && (!automa.allarme()) && (delta>0 || changedSoglia)) { // DA FARE: controlla se e' corretto: ho aggiunto un booleano che dice se la soglia e' appena stata settata.
// Questo per mandare il messaggio di accensione della luce dell'out del suono se si cambia la soglia abbassandola
// e facendo in modo che dopo la modifica della soglia si abbia subito valore>soglia
publisher.aggiungiComando("to/"+Antifurto.getMqttTree()+"/gpio/"+antifurto.getNomeOutputSuono(), "{cmd:1}");
tempoAllarme = LocalDateTime.now();
statoJson.put("tempoAllarme",tempoAllarme.toString());
changedSoglia = false;
}
else {
if(automa.allarme()) {
LocalDateTime tempoAttuale = LocalDateTime.now();
LocalDateTime tempoAttuale = LocalDateTime.now();
long durata = Math.abs(Duration.between(tempoAllarme, tempoAttuale).toMinutes());
if(valore < soglia || durata >= DURATA_SUONO || !automa.antifurtoAttivo() ) {
publisher.aggiungiComando("to/"+Antifurto.getMqttTree()+"/gpio/"+antifurto.getNomeOutputSuono(), "{cmd:0}"); // l'allarme viene disattivato
......@@ -113,6 +117,7 @@ public class Esecutore extends Thread {
js.put("soglia", newValue);
Helper.scriviFile(js, Automa.FILE_STATO);
soglia = newValue;
changedSoglia = true;
}
}
......@@ -47,7 +47,7 @@ public class SubscribeCallback implements MqttCallback {
try {
client.startClient(esec, publisher);
retry = false;
} catch (MqttException e) {
} catch (MqttException | JSONException e) {
d2 = new Date();
time = Math.abs(d2.getTime() - d.getTime());
}
......@@ -295,7 +295,7 @@ public class SubscribeCallback implements MqttCallback {
}
}
private void sendOutAntifurto() throws JSONException {
public void sendOutAntifurto() throws JSONException {
JSONObject js = new JSONObject();
js.put("attiva-scenari", client.getNomeOutputAntifurto());
publisher.aggiungiComando("to/" + Antifurto.getMqttTree() + "/scenari/luceAntifurto", js.toString());
......@@ -393,4 +393,7 @@ public class SubscribeCallback implements MqttCallback {
jsError.put("error", e.getMessage());
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto", jsError.toString());
}
// to/gruppo2/luci/antifurto/luceAntifurto
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment