Skip to content
Snippets Groups Projects
Commit fcbf0ced authored by Alfredo Chissotti's avatar Alfredo Chissotti
Browse files

lettura domain+subdomain da file

parent 5f62ba4e
No related branches found
No related tags found
No related merge requests found
File added
File added
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -26,6 +26,9 @@ public class Scenari {
private static ArrayList<String> topicsSub;
private String clientId = Long.toString(new Date().getTime()) + "-scenari"; // unique client id
private Automa automa;
private String mqttDomain;
private String mqttSubdomain;
private static String mqttTree;
public MqttClient mqttClient;
......@@ -34,22 +37,25 @@ public class Scenari {
JSONObject config = new JSONObject(Helper.leggiFile("./CONF/conf.json"));
brokerUrl = config.getString("protocol") + "://" + config.getString("broker") + ":" + config.getInt("port");
mqttDomain = config.getString("mqttDomain");
mqttSubdomain = config.getString("mqttSubdomain");
makeMqttTree();
topicsSub = new ArrayList<String>();
topicsSub.add("to/all"); //mandare la mia descrizione json
topicsSub.add("rpc/gruppo2/luci/scenari"); //mandare lo stato attuale
topicsSub.add("rpc/"+mqttTree+"scenari"); //mandare lo stato attuale
// inutile aggiungere i topic, perche subbo gia' tutto gpio/#
config = new JSONObject(Helper.leggiFile("./CONF/zona.json"));
learnTrigger = config.getString("learn-trigger");
// topicsSub.add("from/gruppo2/luci/gpio/" + learnTrigger); // Sottoscrivo i messaggi che notificano il cambiamento di stato dell'interruttore
// topicsSub.add("from/"+Scenari.getMqttTree()+"gpio/" + learnTrigger); // Sottoscrivo i messaggi che notificano il cambiamento di stato dell'interruttore
attivaScenari = config.getString("attiva-scenari");
// topicsSub.add("from/gruppo2/luci/gpio/"+attivaScenari);
// topicsSub.add("from/"+Scenari.getMqttTree()+"gpio/"+attivaScenari);
// interruttoreOutputSuono = config.getString("outputSuono");
// topicsSub.add("from/gruppo2/luci/gpio/" + interruttoreOutputSuono); // Sottoscrivo i messaggi che notificano il cambiamento di stato dell'interruttore
// topicsSub.add("to/gruppo2/luci/scenari/learn");
topicsSub.add("from/gruppo2/luci/gpio/#");
// topicsSub.add("from/"+Scenari.getMqttTree()+"gpio/" + interruttoreOutputSuono); // Sottoscrivo i messaggi che notificano il cambiamento di stato dell'interruttore
// topicsSub.add("to/"+Scenari.getMqttTree()+"scenari/learn");
topicsSub.add("from/"+mqttTree+"gpio/#");
this.mqttClient = new MqttClient(brokerUrl, clientId, new MemoryPersistence());
//memory persistence serve per non avere errori in console
......@@ -74,6 +80,15 @@ public class Scenari {
msgTopic.publish(new MqttMessage(msg.getBytes()));
}
private void makeMqttTree() {
if(mqttTree != null)
return;
mqttTree = mqttDomain + "/" + mqttSubdomain + "/";
}
public static String getMqttTree() {
return mqttTree;
}
......
......@@ -104,10 +104,10 @@ public class Esecutore extends Thread {
// System.out.println("\tpublishing");
String nomeLuce = jline.getString("luce");
String cmd = jline.getString("cmd");
// System.out.println("to/gruppo2/luci/gpio/"+nomeLuce);
// System.out.println("to/"+Scenari.getMqttTree()+"gpio/"+nomeLuce);
// System.out.println("{cmd:"+cmd+"}");
// publisher.aggiungiComando("to/gruppo2/luci/gpio/"+nomeLuce, "{cmd:"+cmd+"}");
scenari.sendMqttMessage("to/gruppo2/luci/gpio/"+nomeLuce, "{cmd:"+cmd+"}");
// publisher.aggiungiComando("to/"+Scenari.getMqttTree()+"gpio/"+nomeLuce, "{cmd:"+cmd+"}");
scenari.sendMqttMessage("to/"+Scenari.getMqttTree()+"gpio/"+nomeLuce, "{cmd:"+cmd+"}");
} //else System.out.println("useless");
} catch (InterruptedException | MqttException e) {
e.printStackTrace();
......
......@@ -15,44 +15,6 @@ import org.json.JSONObject;
public class Helper {
/*public static void sendResponse(String response, HttpExchange exchange) throws IOException {
System.out.println(response);
exchange.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
public static void badRequest(HttpExchange exchange) throws IOException {
System.out.println("Errors in the request!");
// exchange.getResponseHeaders().remove("content-type");
String response = "{\"message\":\"Errors in the request!\"}";
exchange.sendResponseHeaders(400, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
public static void pageNotFound(HttpExchange exchange) throws IOException {
System.out.println("Page not found!");
// exchange.getResponseHeaders().remove("content-type");
String response = "{\"message\":\"Page not found!\"}";
exchange.sendResponseHeaders(404, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
public static void methodNotAllowed(HttpExchange exchange) throws IOException {
System.out.println("Method not allowed!");
// exchange.getResponseHeaders().remove("content-type");
String response = "{\"message\":\"Method not allowed!\"}";
exchange.sendResponseHeaders(405, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}*/
public static boolean compareText(String a, String b){
return a.compareToIgnoreCase(b) == 0;
}
......
......@@ -81,12 +81,12 @@ public class SubscribeCallback implements MqttCallback{
System.out.println("new message: "+topic+" "+message.toString());
if(topic.equals("to/all")) {
System.out.println("invio lo stato dell'automa");
sendRequested("from/gruppo2/luci/scenari/description",false);
sendRequested("from/"+Scenari.getMqttTree()+"scenari/description",false);
return;
}
if(topic.equals("rpc/gruppo2/luci/scenari")) {
if(topic.equals("rpc/"+Scenari.getMqttTree()+"scenari")) {
System.out.println("invio la mia configurazione");
sendRequested("from/gruppo2/luci/scenari",true); // 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
sendRequested("from/"+Scenari.getMqttTree()+"scenari",true); // 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
return;
}
......@@ -95,7 +95,7 @@ public class SubscribeCallback implements MqttCallback{
// 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)
// gli event sono 0 o 1; i trigger quando sono attivati/premuti inviano uno 0, che e' l'unico elemento che mi interessa
if(topic.equals("from/gruppo2/luci/gpio/"+learnTrigger)) {
if(topic.equals("from/"+Scenari.getMqttTree()+"gpio/"+learnTrigger)) {
System.out.println("trigger per learn\t"+!automa.getAntifurto());
if(automa.getAntifurto())//se l'antifurto e' attivo, non posso attivare gli scenari
return;
......@@ -105,7 +105,7 @@ public class SubscribeCallback implements MqttCallback{
handleEvent(newStatus,learnTrigger);
return;
}
if(topic.equals("from/gruppo2/luci/gpio/"+attivaScenari)) {
if(topic.equals("from/"+Scenari.getMqttTree()+"gpio/"+attivaScenari)) {
System.out.println("trigger per antifurto\t"+!automa.getLearn());
if(automa.getLearn())//se l'apprendimento e' attivo, non posso attivare l'antifurto
return;
......@@ -115,17 +115,17 @@ public class SubscribeCallback implements MqttCallback{
handleEvent(newStatus,attivaScenari);
return;
}
if(topic.startsWith("from/gruppo2/luci/gpio/IN")) {
if(topic.startsWith("from/"+Scenari.getMqttTree()+"gpio/IN")) {
System.out.println("totalmente inutile per me!");
return;//non mi interessa altro da IN
}
// if(topic.equals("to/gruppo2/luci/scenari/learn")) {//mi aspetto un {learn:true/false}, scritto da handleEvent
// if(topic.equals("to/"+Scenari.getMqttTree()+"scenari/learn")) {//mi aspetto un {learn:true/false}, scritto da handleEvent
// System.out.println("messaggio per iniziare/terminare a imparare");
// boolean val = msgJson.getBoolean("learn");
// handleLearn(val);
// return;
// }
if(topic.startsWith("from/gruppo2/luci/gpio/OUT")) {
if(topic.startsWith("from/"+Scenari.getMqttTree()+"gpio/OUT")) {
if(message.toString().contains("event")) {
System.out.println("se learn==true, salvo questo event!");
System.out.println(automa.getLearn()+"\t"+msgJson.toString());
......@@ -156,8 +156,8 @@ public class SubscribeCallback implements MqttCallback{
System.out.println("stato\t"+automa.getStato());
if(Helper.compareText(cosa, "learn")) {
// JSONObject j = new JSONObject().put("learn", Helper.compareText(cmd, "on"));
// Helper.sendMqttMessage(scenari.mqttClient, "to/gruppo2/luci/scenari/learn", j.toString());
// publisher.aggiungiComando("to/gruppo2/luci/scenari/learn", j.toString());//NON posso inviare ad un topic che subbo
// Helper.sendMqttMessage(scenari.mqttClient, "to/"+Scenari.getMqttTree()+"scenari/learn", j.toString());
// publisher.aggiungiComando("to/"+Scenari.getMqttTree()+"scenari/learn", j.toString());//NON posso inviare ad un topic che subbo
boolean val = Helper.compareText(cmd, "on");
handleLearn(val);
}
......
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