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

ty

parent 43e71ab7
No related branches found
No related tags found
No related merge requests found
......@@ -22,6 +22,13 @@ public class SubscribeCallback implements MqttCallback {
// Ho introdotto questa variabile perche' sia il microservizio dell'antifurto
// sia il microservizio degli scenari
// inviano il comando allo stesso OUT per dire che l'antifurto e' acceso
private String newSuonoConf = null; // quando arriva il comando per modificare la luce (OUT) che indica che
// l'allarme sta suonando, questa variabile conterra' il nuovo nome
// dell'OUT; altrimenti newSuonoConf = null
private String newNomeOutputAntifurtoConf = null; // quando arriva il comando per modificare la luce (OUT) che indica che
// l'antifurto e' acceso, questa variabile conterra' il nuovo nome
// dell'OUT; altrimenti newNomeOutputAntifurto = null
public SubscribeCallback(Antifurto client, Publisher publisher, Esecutore esec, Automa automa) {
this.client = client;
......@@ -98,15 +105,38 @@ public class SubscribeCallback implements MqttCallback {
if (msgJson.has("status"))
newStatus = msgJson.getInt("status");
else {
if (msgJson.has("event"))
newStatus = msgJson.getInt("event");
if (msgJson.has("event")) {
if(newSuonoConf != null) {
// sto facendo la configurazione. Si vuole modificare il nome dell'OUT che indica se l'allarme sta suonando
try {
newConfigNomeOutputSuono(newSuonoConf);
sendResponseConf();
} catch (MqttException e) {
sendErrorConf(e);
}
}
else
newStatus = msgJson.getInt("event");
}
}
handleStateMsgFromOutputSuono(newStatus);
return;
}
if (topic.equals("from/" + Antifurto.getMqttTree() + "/gpio/" + client.getNomeOutputAntifurto())) {
if (msgJson.has("event"))
handleLuceAntifurto();
if (msgJson.has("event")) {
if(newNomeOutputAntifurtoConf != null) {
// sto facendo la configurazione. Si vuole modificare il nome dell'OUT che indica che l'antifurto e' acceso/spento
try {
newConfigNomeOutputAntifurto(newNomeOutputAntifurtoConf);
sendResponseConf();
} catch (MqttException e) {
sendErrorConf(e);
}
}
else
handleLuceAntifurto();
}
if (msgJson.has("status"))
setConsistentStatus(msgJson);
return;
......@@ -236,7 +266,7 @@ public class SubscribeCallback implements MqttCallback {
publisher.aggiungiComando(topic, json.toString());
}
private void setConsistentStatus(JSONObject msgJson) throws JSONException, IOException {
private void setConsistentStatus(JSONObject msgJson) throws JSONException, IOException { //DA MODIFICARE (invio webapp)
int statoOutGpio = msgJson.getInt("status");
if (statoOutGpio == 0 && automa.getStatoInterruttore().equals("on")) {
// devo andare nello stato 0 dell'automa
......@@ -269,47 +299,77 @@ public class SubscribeCallback implements MqttCallback {
}
private void handleNewConfig(JSONObject msgJson) throws JSONException {
JSONObject jsResponse = new JSONObject();
try {
if(msgJson.has("interruttore")) {
client.unsubscribeTopic("from/"+Antifurto.getMqttTree()+"/gpio/" + client.getNomeInterruttoreAntifurto());
String interruttore = msgJson.getString("interruttore");
client.setNomeInterruttoreAntifurto(interruttore);
client.addTopicToSubscribe("from/"+Antifurto.getMqttTree()+"/gpio/" + interruttore);
}
if(msgJson.has("outputSuono")) {
client.unsubscribeTopic("from/"+Antifurto.getMqttTree()+"/gpio/" + client.getNomeOutputSuono());
String outputSuono = msgJson.getString("outputSuono");
client.setNomeOutputSuono(outputSuono);
client.addTopicToSubscribe("from/"+Antifurto.getMqttTree()+"/gpio/" + outputSuono);
}
if(msgJson.has("nomeOutputAntifurto")) {
client.unsubscribeTopic("from/"+Antifurto.getMqttTree()+"/gpio/" + client.getNomeOutputAntifurto());
String nomeOutputAntifurto = msgJson.getString("nomeOutputAntifurto");
client.setNomeOutputAntifurto(nomeOutputAntifurto);
client.addTopicToSubscribe("from/"+Antifurto.getMqttTree()+"/gpio/" + nomeOutputAntifurto);
if(msgJson.has("interruttore")) {
client.unsubscribeTopic("from/"+Antifurto.getMqttTree()+"/gpio/" + client.getNomeInterruttoreAntifurto());
String interruttore = msgJson.getString("interruttore");
client.setNomeInterruttoreAntifurto(interruttore);
client.addTopicToSubscribe("from/"+Antifurto.getMqttTree()+"/gpio/" + interruttore);
}
if(msgJson.has("outputSuono")) {
if(automa.getStatoAutoma()==2) { // l'allarme sta suonando
publisher.aggiungiComando("to/"+Antifurto.getMqttTree()+"/gpio/"+client.getNomeOutputSuono(), "{cmd:0}");
newSuonoConf = msgJson.getString("outputSuono");
}
else {
newConfigNomeOutputSuono(msgJson.getString("outputSuono"));
}
}
if(msgJson.has("nomeOutputAntifurto")) {
if(automa.getStatoAutoma()==1 || automa.getStatoAutoma()==2) { // l'antifurto e' acceso
publisher.aggiungiComando("to/"+Antifurto.getMqttTree()+"/gpio/"+client.getNomeOutputAntifurto(), "{cmd:0}");
newNomeOutputAntifurtoConf = msgJson.getString("nomeOutputAntifurto");
}
else {
newConfigNomeOutputAntifurto(msgJson.getString("nomeOutputAntifurto"));
}
}
sendResponseConf();
} catch (MqttException | IOException e) {
sendErrorConf(e);
}
}
private void newConfigNomeOutputAntifurto(String nomeOutputAntifurto) throws MqttException, JSONException {
client.unsubscribeTopic("from/"+Antifurto.getMqttTree()+"/gpio/" + client.getNomeOutputAntifurto());
client.setNomeOutputAntifurto(nomeOutputAntifurto);
client.addTopicToSubscribe("from/"+Antifurto.getMqttTree()+"/gpio/" + nomeOutputAntifurto);
newNomeOutputAntifurtoConf = null;
}
private void newConfigNomeOutputSuono(String outputSuono) throws MqttException, JSONException {
client.unsubscribeTopic("from/"+Antifurto.getMqttTree()+"/gpio/" + client.getNomeOutputSuono());
client.setNomeOutputSuono(outputSuono);
client.addTopicToSubscribe("from/"+Antifurto.getMqttTree()+"/gpio/" + outputSuono);
newSuonoConf = null;
}
jsResponse.put("interruttore", client.getNomeInterruttoreAntifurto());
jsResponse.put("outputSuono", client.getNomeOutputSuono());
jsResponse.put("nomeOutputAntifurto",client.getNomeOutputAntifurto());
// pubblica la risposta (per la webApp) solo se newNomeOutputAntifurtoConf = null e newSuonoConf = null
private void sendResponseConf() throws JSONException, IOException {
if(newNomeOutputAntifurtoConf==null && newSuonoConf==null) {
JSONObject jsResponse = new JSONObject();
jsResponse.put("interruttore", client.getNomeInterruttoreAntifurto());
jsResponse.put("outputSuono", client.getNomeOutputSuono());
jsResponse.put("nomeOutputAntifurto",client.getNomeOutputAntifurto());
JSONObject jsFile = new JSONObject(Helper.leggiFile(Antifurto.CONF_ZONA));
jsResponse.put("sensoriMovimento", jsFile.get("sensoriMovimento"));
Helper.scriviFile(jsResponse, Antifurto.CONF_ZONA);
jsResponse.remove("sensoriMovimento");
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto" , jsResponse.toString());
} catch (MqttException | IOException e) {
JSONObject jsError = new JSONObject();
jsError.put("error", e.getMessage());
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto", jsError.toString());
JSONObject jsFile = new JSONObject(Helper.leggiFile(Antifurto.CONF_ZONA));
jsResponse.put("sensoriMovimento", jsFile.get("sensoriMovimento"));
Helper.scriviFile(jsResponse, Antifurto.CONF_ZONA);
jsResponse.remove("sensoriMovimento");
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto" , jsResponse.toString());
}
}
// pubblica l'errore (per la webApp)
private void sendErrorConf(Exception e) throws JSONException {
JSONObject jsError = new JSONObject();
jsError.put("error", e.getMessage());
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto", jsError.toString());
}
}
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