Newer
Older
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;
private Antifurto client;
private Publisher publisher;
private Esecutore esec;
private Automa automa;
private int contatore = 0; // quando pubblico un messaggio per accendere la luce che indica che l'antifurto
// e' on, incremento questa variabile.
// Quando ricevo la conferma dal gpio, decremento il contatore. Questo contatore
// 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
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
public SubscribeCallback(Antifurto client, Publisher publisher, Esecutore esec, Automa automa) {
this.client = client;
this.publisher = publisher;
this.esec = esec;
this.automa = automa;
}
@Override
public void connectionLost(Throwable arg0) {
boolean retry = true;
final Date d = new Date();
Date d2 = new Date();
} catch (MqttException | JSONException e) {
System.out.println("Tentativo di riconnessione fallito");
System.exit(1);
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
// Nessuna operazione
public void messageArrived(String topic, MqttMessage message) throws JSONException, IOException {
if (topic.equals("to/all")) {
System.out.println("MESSAGGIO MQTT = " + message.toString());
if (message.toString().equals("{request:description}"))
sendMyState("from/" + Antifurto.getMqttTree() + "/antifurto/description");
if (message.toString().equals("{request:status}"))
sendMyStateToAll("from/" + Antifurto.getMqttTree() + "/antifurto");
return;
Alfredo Chissotti
committed
if (topic.equals("conf/" + Antifurto.getMqttTree() + "/antifurto/sensore")) {
if (msgJson.has("in") && msgJson.has("delta"))
handleConfSensoreMovimento(msgJson);
return;
}
Alfredo Chissotti
committed
if (topic.equals("conf/" + Antifurto.getMqttTree() + "/antifurto")) {
return;
}
if (topic.equals("to/" + Antifurto.getMqttTree() + "/antifurto/luceAntifurto")) {
if (msgJson.has("request"))
sendOutAntifurto();
return;
}
Elisa Giglio
committed
if (topic.equals("conf/" + Antifurto.getMqttTree() + "/antifurto/soglia")) {
if (msgJson.has("soglia"))
handleSetSoglia(msgJson);
return;
}
if (topic.equals("rpc/" + Antifurto.getMqttTree() + "/antifurto")) {
if (message.toString().equals("{\"request\":\"description\"}"))
sendMyState("from/" + Antifurto.getMqttTree() + "/antifurto");
return;
}
if (topic.equals("from/" + Antifurto.getMqttTree() + "/gpio/" + client.getNomeOutputSuono())) {
int newStatus = -1;
if (msgJson.has("status"))
newStatus = msgJson.getInt("status");
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);
if (topic.equals("from/" + Antifurto.getMqttTree() + "/gpio/" + client.getNomeOutputAntifurto())) {
// sto facendo la configurazione. Si vuole modificare il nome dell'OUT che indica che l'antifurto e' acceso/spento
try {
newConfigNomeOutputAntifurto(newNomeOutputAntifurtoConf);
if (msgJson.has("status"))
setConsistentStatus(msgJson);
return;
}
int event = msgJson.getInt("event");
if (event == 1) {
// e' stato premuto l'interruttore / il sensore di movimento ha rilevato
// qualcuno
if (topic.equals("from/" + Antifurto.getMqttTree() + "/gpio/" + client.getNomeInterruttoreAntifurto())) {
handleStateMsgFromInterruttore();
return;
}
// il topic sara' from/gruppo2/luci/gpio/IN2 dove IN2 e' un sensore di movimento
if (topic.startsWith("from/" + Antifurto.getMqttTree() + "/gpio/IN")) {
String[] t = topic.split("/");
handleMovimento(t[4]);
}
}
private synchronized void handleStateMsgFromInterruttore() {
// voglio spegnere l'antifurto, quindi invio un messaggio per spegnere la luce che indica che l'antifurto e' acceso
publisher.aggiungiComando("to/" + Antifurto.getMqttTree() + "/gpio/" + client.getNomeOutputAntifurto(), "{cmd:0}");
//
// 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}");
private synchronized void handleLuceAntifurto() throws JSONException {
contatore--;
if (contatore < 0) {
contatore = 0;
return;
try {
JSONObject js = new JSONObject();
automa.changeStatoInterruttore();
if (automa.getStatoInterruttore().equals("off")) {
publisher.aggiungiComando("to/" + Antifurto.getMqttTree() + "/gpio/" + client.getNomeOutputSuono(), "{cmd:0}");
esec.reset();
js.put("event", esec.getValore());
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto/valore", js.toString());
}
js.put("event", automa.getStatoInterrutoreTrueFalse());
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto/antifurto", js.toString());
} catch (IOException e) {
JSONObject jsError = new JSONObject();
jsError.put("error", e.getMessage());
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto/antifurto", jsError.toString());
private synchronized void handleStateMsgFromOutputSuono(int newStatus) throws JSONException, IOException {
String statusOnOff = automa.converter(newStatus); // lo stato e' della forma "on" oppure "off"
JSONObject js = new JSONObject();
js.put("event", automa.getStatoSuonoTrueFalse());
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto/allarme", js.toString());
js.put("event", esec.getValore());
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto/valore", js.toString());
private void handleMovimento(String nomeSensore) {
if (automa.getStatoInterruttore().equals("on"))
private void handleConfSensoreMovimento(JSONObject toAdd) throws JSONException {
// aggiorno il file che contiene l'associazione sensore-movimento, delta
try {
JSONObject sensori = new JSONObject(Helper.leggiFile(Automa.FILE_DELTA_SENSORI));
String nomeSensore = toAdd.getString("in").toUpperCase();
int delta = toAdd.getInt("delta");
sensori.put(nomeSensore, delta);
Helper.scriviFile(sensori, Automa.FILE_DELTA_SENSORI);
// aggiorno il file zona: ho un sensore di movimento in piu'
JSONObject zona = new JSONObject(Helper.leggiFile(Antifurto.CONF_ZONA));
zona.getJSONArray("sensoriMovimento").put(nomeSensore);
Helper.scriviFile(zona, Antifurto.CONF_ZONA);
Antifurto.addSensore(nomeSensore);
automa.addDeltaSensori(nomeSensore, delta);
client.addTopicToSubscribe("from/" + Antifurto.getMqttTree() + "/gpio/" + nomeSensore);
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto/sensore", toAdd.toString());
// invio questo messaggio per confermare alla web app che il sensore di movimento e' stato aggiunto
JSONObject jsError = new JSONObject();
jsError.put("error", e.getMessage());
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto/sensore", jsError.toString());
private void handleSetSoglia(JSONObject jsSoglia) throws JSONException {
try {
JSONObject jsToSend = new JSONObject();
jsToSend.put("soglia", Esecutore.getSoglia());
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto/soglia", jsToSend.toString());
} catch (IOException e) {
JSONObject jsError = new JSONObject();
jsError.put("error", e.getMessage());
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto/soglia", jsError.toString());
private void sendMyState(String topic) throws JSONException {
JSONObject json = new JSONObject();
json.put("stato", automa.getStatoAutoma());
json.put("interruttore", client.getNomeInterruttoreAntifurto());
json.put("sensori-movimento", Antifurto.getSensori());
json.put("output-suono", client.getNomeOutputSuono());
json.put("output-antifurto", client.getNomeOutputAntifurto());
json.put("soglia", Esecutore.getSoglia());
json.put("allarme", automa.getStatoSuonoTrueFalse());
json.put("valore-attuale", esec.getValore());
json.put("antifurto", automa.getStatoInterrutoreTrueFalse());
private void setConsistentStatus(JSONObject msgJson) throws JSONException, IOException {
if (statoOutGpio == 0 && automa.getStatoInterruttore().equals("on")) {
// devo andare nello stato 0 dell'automa
automa.setStatoAutoma(0);
automa.aggiornaInterruttoreESuono();
esec.reset();
Elisa Giglio
committed
js.put("event", automa.getStatoInterrutoreTrueFalse());
}
if (statoOutGpio == 1 && automa.getStatoInterruttore().equals("off")) {
// devo andare nello stato 1 dell'automa
automa.setStatoAutoma(1);
automa.aggiornaInterruttoreESuono();
Elisa Giglio
committed
js.put("event", automa.getStatoInterrutoreTrueFalse());
}
Elisa Giglio
committed
// 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);
Elisa Giglio
committed
publisher.aggiungiComando("from/" + Antifurto.getMqttTree() + "/antifurto/antifurto", js.toString());
public void sendOutAntifurto() throws JSONException {
js.put("attiva-scenari", client.getNomeOutputAntifurto());
publisher.aggiungiComando("to/" + Antifurto.getMqttTree() + "/scenari/luceAntifurto", js.toString());
private void sendMyStateToAll(String topic) throws JSONException {
JSONObject js = new JSONObject();
js.put("status", automa.getStatoAutoma());
publisher.aggiungiComando(topic, js.toString());
Alfredo Chissotti
committed
private void handleNewConfig(JSONObject msgJson) throws JSONException {
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")) {
if(automa.getStatoAutoma()==2) { // l'allarme sta suonando
publisher.aggiungiComando("to/"+Antifurto.getMqttTree()+"/gpio/"+client.getNomeOutputSuono(), "{cmd:0}");
newSuonoConf = msgJson.getString("outputSuono");
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");
newConfigNomeOutputAntifurto(msgJson.getString("nomeOutputAntifurto"));
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;
Elisa Giglio
committed
String message = null;
if(automa.getStatoAutoma() == 1 || automa.getStatoAutoma()==2)
Elisa Giglio
committed
message = "{cmd:1}";
else
message = "{cmd:0}";
publisher.aggiungiComando("to/"+Antifurto.getMqttTree()+"/gpio/"+client.getNomeOutputAntifurto(), message);
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);
Elisa Giglio
committed
String message = null;
Elisa Giglio
committed
message = "{cmd:1}";
else
message = "{cmd:0}";
publisher.aggiungiComando("to/"+Antifurto.getMqttTree()+"/gpio/"+client.getNomeOutputSuono(), message);
Alfredo Chissotti
committed
// 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());
Alfredo Chissotti
committed
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());
}