Skip to content
Snippets Groups Projects
Commit 85f0e9d6 authored by A C's avatar A C
Browse files

LuciMicro da testare

parent 3cb44b5a
No related branches found
No related tags found
No related merge requests found
{"sensM":"IN5", {"sensM":"IN5",
"lamp3":{"output":"OUT3",
"stato":false,
"input":"IN4",
"nome":"Garage"},
"lamp2":{"output":"OUT1",
"stato":false,
"input":"IN3",
"nome":"Ingresso"},
"lamp1":{"output":"OUT2", "lamp1":{"output":"OUT2",
"stato":false, "stato":false,
"input":"IN1", "input":"IN1",
......
...@@ -15,99 +15,79 @@ import org.eclipse.paho.client.mqttv3.MqttException; ...@@ -15,99 +15,79 @@ import org.eclipse.paho.client.mqttv3.MqttException;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
public class Esecutore /* extends Thread */ {
public class Esecutore extends Thread {
private final Luci luci; private final Luci luci;
static private Timer timer; static Timer timer;
private boolean firstrun; public Boolean running;
//private int durata; private static Esecutore istanza = null;//
static ArrayList<Luce> j = new ArrayList<Luce>();
public Esecutore(Luci luci) throws IOException, JSONException { // arraylist mi serve per sapere se una luce era accesa manualmente. Se la era
// non la spengo quando scade il timer
private Esecutore(Luci luci) throws IOException, JSONException {
this.luci = luci; this.luci = luci;
} }
public static Esecutore istanziatore(Luci l) throws IOException, JSONException {
if (istanza == null) {
istanza = new Esecutore(l);
}
return istanza;
}
class AccendiTutto extends TimerTask { class AccendiTutto extends TimerTask {
// boolean running=false;
int durata=5; int durata = 5;
ArrayList<Luce> j=new ArrayList<Luce>(); ArrayList<Luce> j;
//arraylist mi serve per sapere se una luce era accesa manualmente. Se la era non la spengo quando scade il timer
public AccendiTutto(ArrayList j) {
this.j = j;
}
public void run() { public void run() {
//int durata=12;
if (durata > 0) { System.out.println("-----" + durata);
String req="{cmd:1}"; if (durata > 0) {
//System.out.println("Restano " + durata + " secondi"); String req = "{cmd:1}";
for(Luce k:luci.luciList) { for (Luce k : Luci.luciList) {
if(firstrun && k.getStato()) j.add(k); try {
k.setStato(true); luci.sendMqttMessage(Luci.getMqttTree("to", "gpio/" + k.getOUT()), req);
try {
luci.sendMqttMessage(luci.getMqttTree("to", "gpio/" +k.getOUT()), req);
} catch (MqttException e) { } catch (MqttException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
durata--; durata--;
} else {
}else{ System.out.println("FINE!" + running);
//System.out.println("FINE!"); String req = "{cmd:0}";
String req="{cmd:0}"; for (Luce k : Luci.luciList) {
for(Luce k:luci.luciList) { if (!j.contains(k)) {
if(!j.contains(k)) { k.setStato(false);
k.setStato(false); try {
try { luci.sendMqttMessage(Luci.getMqttTree("to", "gpio/" + k.getOUT()), req);
luci.sendMqttMessage(luci.getMqttTree("to", "gpio/" +k.getOUT()), req); } catch (MqttException e) {
} catch (MqttException e) { // TODO Auto-generated catch block
// TODO Auto-generated catch block e.printStackTrace();
e.printStackTrace(); }
} }
} }
} j.clear();
timer.cancel();
//System.exit(0); timer.purge();
Subscriber.runningEsec = false;
timer.cancel(); }
}
firstrun=false;
}
public void resetDurata() {
this.durata=5;
} }
} }
public void run() { public void run(Boolean running) {
// qui ci va la business logic
if(timer != null)
timer.cancel();
timer = new Timer(); timer = new Timer();
AccendiTutto a=new AccendiTutto(); AccendiTutto a = new AccendiTutto(j);
a.resetDurata(); this.running = running;
firstrun=true; timer.schedule(a, 0, 1000);
//try {
timer.schedule(a, 0, 1000);
// }catch(IllegalStateException e) {
// Helper.print("Errore illegal state");
// //run();
// }
//
//
} }
} }
\ No newline at end of file
package code; package code;
import java.util.Date; import java.util.Date;
import java.util.Iterator; //import java.util.Iterator;
import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
...@@ -9,133 +9,113 @@ import org.eclipse.paho.client.mqttv3.MqttException; ...@@ -9,133 +9,113 @@ import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic; import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.json.JSONArray; //import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; //import java.util.Collections;
public class Luci { public class Luci {
public static ArrayList<Luce> luciList;//per ogni luce ho uno stato public static ArrayList<Luce> luciList;
private static String brokerUrl; private static String brokerUrl;
private JSONObject jsonObject; //private JSONObject jsonObject;
private final static String DATABASE_PATH = "./Luci.json"; //private final static String DATABASE_PATH = "./Luci.json";
private static ArrayList<String> topicsSub; private static ArrayList<String> topicsSub;
private String clientId = Long.toString(new Date().getTime()) + "-luci"; // unique client id private String clientId = Long.toString(new Date().getTime()) + "-luci"; // unique client id
private String mqttDomain; private String mqttDomain;
private String mqttSubdomain; private String mqttSubdomain;
private static String mqttTree; private static String mqttTree;
//public static final String RES_FOLDER = "../res/";
public static final String CONF_FOLDER = "../CONF/"; public static final String CONF_FOLDER = "../CONF/";
private final static String FILE_CONF = CONF_FOLDER+"conf.json";//"./CONF/conf.json"; private final static String FILE_CONF = CONF_FOLDER + "conf.json";// "./CONF/conf.json";
//private final static String FILE_ZONA = CONF_FOLDER+"zona.json";//"./CONF/zona.json" // private final static String FILE_ZONA =
final static String FILE_ZONA = "../CONF/zona.json";//"./CONF/zona.json" // CONF_FOLDER+"zona.json";//"./CONF/zona.json"
final static String FILE_ZONA = "../CONF/zona.json";// "./CONF/zona.json"
public MqttClient mqttClient; public MqttClient mqttClient;
public Luci() throws JSONException, IOException, MqttException { public Luci() throws JSONException, IOException, MqttException {
JSONObject config = new JSONObject(Helper.leggiFile(FILE_CONF));// dove mi salvo le strutture dal file? cioè
// leggo e poi?
JSONObject config = new JSONObject(Helper.leggiFile(FILE_CONF));//dove mi salvo le strutture dal file? cioè leggo e poi?
brokerUrl = config.getString("protocol") + "://" + config.getString("broker") + ":" + config.getInt("port"); brokerUrl = config.getString("protocol") + "://" + config.getString("broker") + ":" + config.getInt("port");
mqttDomain = config.getString("mqttDomain"); mqttDomain = config.getString("mqttDomain");
mqttSubdomain = config.getString("mqttSubdomain"); mqttSubdomain = config.getString("mqttSubdomain");
makeMqttTree(); makeMqttTree();
topicsSub = new ArrayList<String>(); topicsSub = new ArrayList<String>();
topicsSub.add("to/all"); //mandare la mia descrizione json topicsSub.add("to/all"); // mandare la mia descrizione json
topicsSub.add(getMqttTree("rpc/","luci")); //mandare lo stato attuale topicsSub.add(getMqttTree("rpc/", "luci")); // mandare lo stato attuale
// inutile aggiungere i topic, perche subbo gia' tutto gpio/# // inutile aggiungere i topic, perche subbo gia' tutto gpio/#
config = new JSONObject(Helper.leggiFile(FILE_ZONA)); config = new JSONObject(Helper.leggiFile(FILE_ZONA));
//learnTrigger = config.getString("learn-trigger"); topicsSub.add(getMqttTree("from/", "gpio/#"));
// topicsSub.add("from/"+Scenari.getMqttTree()+"gpio/" + learnTrigger); // Sottoscrivo i messaggi che notificano il cambiamento di stato dell'interruttore topicsSub.add(getMqttTree("to/", "luci/#"));// {"evento":0,"nome":nome_scenario} per attivare/disattivare uno
//attivaScenari = config.getString("attiva-scenari"); topicsSub.add(getMqttTree("from/", "arduino/AN0")); // scenario; risposta su from/tree/scenari/attiva con
// topicsSub.add("from/"+Scenari.getMqttTree()+"gpio/"+attivaScenari); // {"nome":nome_scenario}
// topicsSub.add(getMqttTree("to/","luci/salva"));//per rinominare lo scenario
// piu recente
// interruttoreOutputSuono = config.getString("outputSuono");
// 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(getMqttTree("from/","gpio/#"));
topicsSub.add(getMqttTree("to/","luci/#"));//{"evento":0,"nome":nome_scenario} per attivare/disattivare uno scenario; risposta su from/tree/scenari/attiva con {"nome":nome_scenario}
//topicsSub.add(getMqttTree("to/","luci/salva"));//per rinominare lo scenario piu recente
this.mqttClient = new MqttClient(brokerUrl, clientId, new MemoryPersistence()); this.mqttClient = new MqttClient(brokerUrl, clientId, new MemoryPersistence());
//memory persistence serve per non avere errori in console
// https://github.com/eclipse/paho.mqtt.java/issues/794
} }
private void makeMqttTree() { private void makeMqttTree() {
if(mqttTree != null) if (mqttTree != null)
return; return;
mqttTree = mqttDomain + "/" + mqttSubdomain + "/"; mqttTree = mqttDomain + "/" + mqttSubdomain + "/";
} }
public static String getMqttTree(String prefix,String suffix) { public static String getMqttTree(String prefix, String suffix) {
return (prefix.endsWith("/") ? prefix : prefix+"/") + mqttTree + (suffix.startsWith("/") ? suffix.substring(1) : suffix); return (prefix.endsWith("/") ? prefix : prefix + "/") + mqttTree
+ (suffix.startsWith("/") ? suffix.substring(1) : suffix);
} }
public static void main(String args[]) throws JSONException, IOException, MqttException { public static void main(String args[]) throws JSONException, IOException, MqttException {
System.out.println("started"); System.out.println("started");
luciList= new ArrayList<Luce>(); luciList = new ArrayList<Luce>();
startSystemo(); startSystemo();
} }
private static void startSystemo() throws JSONException, IOException, MqttException { private static void startSystemo() throws JSONException, IOException, MqttException {
System.out.println(Helper.leggiFile(FILE_ZONA)); System.out.println(Helper.leggiFile(FILE_ZONA));
JSONObject config = new JSONObject(Helper.leggiFile(FILE_ZONA)); JSONObject config = new JSONObject(Helper.leggiFile(FILE_ZONA));
System.out.println(config); System.out.println(config);
String sensM=config.getString("sensM"); String sensM = config.getString("sensM");
int index=0; int index = 0;
while (config.has("lamp"+index)) { //(keys.hasNext()) { while (config.has("lamp" + index)) {
System.out.println(index+"\n"); System.out.println(index + "\n");
//String key = keys.next(); JSONObject k = config.getJSONObject("lamp" + index);
//if (config.get(key) instanceof String) { System.out.println(k);
JSONObject k= config.getJSONObject("lamp"+index); String a = k.getString("output");
System.out.println(k); String b = k.getString("input");
String a=k.getString("output"); boolean c = k.getBoolean("stato");
String b=k.getString("input"); String d = k.getString("nome");
boolean c = k.getBoolean("stato");
String d = k.getString("nome"); Luce luce = new Luce(a, b, c, d);
Luce luce=new Luce(a,b,c,d); luciList.add(luce);
System.out.println(config.get("lamp" + index));
luciList.add(luce); index++;
System.out.println(config.get("lamp"+index));
index++;
} }
Luci luci = new Luci();
Luci luci = new Luci(); Esecutore esec = Esecutore.istanziatore(luci);
Esecutore esec = new Esecutore(luci); luci.startClient(esec, sensM);
luci.startClient(esec, sensM/*esec,,publisher*/);//, publisher);
//esec.start();
} }
public void sendMqttMessage(String topic, String msg) throws MqttException { public void sendMqttMessage(String topic, String msg) throws MqttException {
final MqttTopic msgTopic = mqttClient.getTopic(topic); final MqttTopic msgTopic = mqttClient.getTopic(topic);
msgTopic.publish(new MqttMessage(msg.getBytes())); msgTopic.publish(new MqttMessage(msg.getBytes()));
} }
public void startClient(Esecutore esec, String sensM) throws MqttException { public void startClient(Esecutore esec, String sensM) throws MqttException {
MqttConnectOptions options = new MqttConnectOptions(); MqttConnectOptions options = new MqttConnectOptions();
options.setUserName("gruppo2"); options.setUserName("gruppo2");
...@@ -145,12 +125,8 @@ public class Luci { ...@@ -145,12 +125,8 @@ public class Luci {
mqttClient.connect(options); mqttClient.connect(options);
for(String t: topicsSub) for (String t : topicsSub)
mqttClient.subscribe(t); mqttClient.subscribe(t);
// query the antifurto microservice to set the SAME luceAntifurto
} }
} }
package code; package code;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
...@@ -18,26 +19,29 @@ import org.json.JSONObject; ...@@ -18,26 +19,29 @@ import org.json.JSONObject;
//import scenari.Automa; //import scenari.Automa;
//import scenari.Scenari; //import scenari.Scenari;
public class Subscriber implements MqttCallback{ public class Subscriber implements MqttCallback {
private Luci luci; private Luci luci;
private Esecutore esec; private Esecutore esec;
static Boolean runningEsec;
public String sensM; public String sensM;
Boolean giorno=false;
public Subscriber(Luci luci, Esecutore esec, String sensm) { public Subscriber(Luci luci, Esecutore esec, String sensm) {
this.luci=luci; this.luci = luci;
this.esec = esec; this.esec = esec;
this.sensM = sensm; this.sensM = sensm;
Subscriber.runningEsec = false;
} }
@Override @Override
public void connectionLost(Throwable arg0) { public void connectionLost(Throwable arg0) {
Helper.print("connessione persa\t"+arg0); Helper.print("connessione persa\t" + arg0);
final Date d = new Date(); final Date d = new Date();
Date d2 = new Date(); Date d2 = new Date();
long time = Math.abs(d2.getTime()-d.getTime()); long time = Math.abs(d2.getTime() - d.getTime());
while (time<600000) { while (time < 600000) {
try { try {
Helper.print("retrying..."); Helper.print("retrying...");
...@@ -46,17 +50,16 @@ public class Subscriber implements MqttCallback{ ...@@ -46,17 +50,16 @@ public class Subscriber implements MqttCallback{
} catch (MqttException | JSONException | IOException e) { } catch (MqttException | JSONException | IOException e) {
Helper.print("non sono riusciuto"); Helper.print("non sono riusciuto");
d2 = new Date(); d2 = new Date();
time = Math.abs(d2.getTime()-d.getTime()); time = Math.abs(d2.getTime() - d.getTime());
} }
} }
Helper.print("fuori dal while"); Helper.print("fuori dal while");
if(time>=600000) { if (time >= 600000) {
Helper.print("Tentativo di riconnessione fallito"); Helper.print("Tentativo di riconnessione fallito");
System.exit(1); System.exit(1);
} }
} }
@Override @Override
public void deliveryComplete(IMqttDeliveryToken arg0) { public void deliveryComplete(IMqttDeliveryToken arg0) {
Helper.print("inviato"); Helper.print("inviato");
...@@ -64,179 +67,163 @@ public class Subscriber implements MqttCallback{ ...@@ -64,179 +67,163 @@ public class Subscriber implements MqttCallback{
@Override @Override
public void messageArrived(String topic, MqttMessage message) throws MqttException, JSONException, IOException { public void messageArrived(String topic, MqttMessage message) throws MqttException, JSONException, IOException {
Helper.print("new message: "+topic+" "+message.toString()); Helper.print("new message: " + topic + " " + message.toString());
if(topic.equals(Luci.getMqttTree("rpc/","luci"))) { if (topic.equals(Luci.getMqttTree("rpc/", "luci"))) {
Helper.print("invio la mia configurazione"); Helper.print("invio la mia configurazione");
sendFile(Luci.getMqttTree("from/","luci")); sendFile(Luci.getMqttTree("from/", "luci"));
return; return;
} }
if(topic.equals("to/all")) { if (topic.equals("to/all")) {
Helper.print("invio il mio stato"); Helper.print("invio il mio stato");
sendStatus(Luci.getMqttTree("from/","luci")); sendStatus(Luci.getMqttTree("from/", "luci"));
return; return;
} }
JSONObject msgJson = new JSONObject(message.toString()); JSONObject msgJson = new JSONObject(message.toString());
//la beaglebone risponde sutopic {"event":1} se è acceso, 0 altrimenti // la beaglebone risponde sutopic {"event":1} se è acceso, 0 altrimenti
if (topic.equals(Luci.getMqttTree("to", "luci/new"))) {
if(topic.equals(Luci.getMqttTree("to", "luci/new"))){
JSONObject config = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA)); JSONObject config = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA));
String nome= msgJson.getString("nome"); String nome = msgJson.getString("nome");
for(Luce k: Luci.luciList) { for (Luce k : Luci.luciList) {
if(k.getNome().equalsIgnoreCase(nome)) { if (k.getNome().equalsIgnoreCase(nome)) {
//luci.sendMqttMessage(topic, topic)//che ci scrivo se fallisco?
//rispondi impossibile su topic
System.out.println("Nome gia' presente, inserimento annullato\n"); System.out.println("Nome gia' presente, inserimento annullato\n");
return; return;
} }
} }
int k=Luci.luciList.size(); int k = Luci.luciList.size();
config.put("lamp"+k, msgJson); config.put("lamp" + k, msgJson);
//System.out.println("---"+config); // System.out.println("---"+config);
Luci.luciList.add(new Luce(msgJson));//,"lamp"+k Luci.luciList.add(new Luce(msgJson));// ,"lamp"+k
Helper.scriviFile(config, Luci.FILE_ZONA); Helper.scriviFile(config, Luci.FILE_ZONA);
luci.sendMqttMessage(Luci.getMqttTree("from", "luci/new"), msgJson.toString()); luci.sendMqttMessage(Luci.getMqttTree("from", "luci/new"), msgJson.toString());
return; return;
} }
if (topic.equals(Luci.getMqttTree("to", "luci/sensore"))) {
// salvati il sensore di movimento
if(topic.equals(Luci.getMqttTree("to", "luci/sensore"))){ sensM = msgJson.getString("sensM");
//salvati il sensore di movimento // devo aggiornare in file
sensM=msgJson.getString("sensM");
//devo aggiornare in file
JSONObject config = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA)); JSONObject config = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA));
config.put("sensM", sensM); config.put("sensM", sensM);
Helper.scriviFile(config, Luci.FILE_ZONA); Helper.scriviFile(config, Luci.FILE_ZONA);
luci.sendMqttMessage(Luci.getMqttTree("from", "luci/sensore"), msgJson.toString()); luci.sendMqttMessage(Luci.getMqttTree("from", "luci/sensore"), msgJson.toString());
return; return;
} // gruppo2/luci/luci/sensore
// sensM tempo
if (topic.equals(Luci.getMqttTree("from", "gpio/" + sensM))) {
System.out.println(giorno);
if(giorno)return;
if (msgJson.has("event") && msgJson.getInt("event") == 1) {
}//gruppo2/luci/luci/sensore if (runningEsec) {
Esecutore.timer.cancel();
//sensM tempo Esecutore.timer.purge();
if(topic.equals(Luci.getMqttTree("from", "gpio/"+sensM))){ } else {
//prendo l'ultimo pezzo del topic, diventa il mio nome for (Luce k : Luci.luciList) {
//String nomeSensM =topic.substring(23);//seleziono solo il nome della lamp if (k.getStato())
//System.out.println("nome lamp\t"+pezzo); Esecutore.j.add(k);
//if(nomeSensM==sensM) { }
//Esecutore es=new Esecutore(luci); }
//Esecutore esec= new runningEsec = true;
if(msgJson.has("event")&& msgJson.getInt("event")==1)esec.run(); esec.run(runningEsec);
//
}
return; return;
} }
//} // controllo i topic su cui mi arrivano gli on/off
if (topic.startsWith(Luci.getMqttTree("from", "gpio/IN"))) {
// prendo l'ultimo pezzo del topic, diventa il mio nome
String pezzo = topic.substring(23);// seleziono solo il nome della lamp
//controllo i topic su cui mi arrivano gli on/off System.out.println("nome lamp\t" + pezzo);
if(topic.startsWith(Luci.getMqttTree("from", "gpio/IN"))){
//prendo l'ultimo pezzo del topic, diventa il mio nome for (int i = 0; i < Luci.luciList.size(); i++) {
if (Luci.luciList.get(i).getIN().equalsIgnoreCase(pezzo) && msgJson.has("event")
String pezzo =topic.substring(23);//seleziono solo il nome della lamp && msgJson.getInt("event") == 1) {// non è k.getIN()?
System.out.println("nome lamp\t"+pezzo);
String req = Luci.luciList.get(i).getStato() ? "{cmd:0}" : "{cmd:1}";
for(int i=0;i<Luci.luciList.size();i++) { luci.sendMqttMessage(Luci.getMqttTree("to", "gpio/" + Luci.luciList.get(i).getOUT()), req);
if(Luci.luciList.get(i).getIN().equalsIgnoreCase(pezzo) && msgJson.has("event") && msgJson.getInt("event")==1) {//non è k.getIN()?
String req=Luci.luciList.get(i).getStato() ? "{cmd:0}":"{cmd:1}";
// if(Luci.luciList.get(i).getStato()) {
// req = "{cmd:0}";
// //topic corretto? poi risponde, se risposta è ok allora setto stato e scrivo
// }
// else {req="{cmd:1}";}
luci.sendMqttMessage(luci.getMqttTree("to", "gpio/" + Luci.luciList.get(i).getOUT()), req);
//to/gruppo2/luci/gpio/OUTn
//JSON {cmd:1} o 0 accendi/spegni
} }
} }
return; return;
}//gruppo2/luci/luci/sensore } // gruppo2/luci/luci/sensore
if(topic.startsWith(Luci.getMqttTree("from", "gpio/OUT"))){ if (topic.startsWith(Luci.getMqttTree("from", "gpio/OUT"))) {
if(msgJson.has("status"))return; if (msgJson.has("status"))
String pezzo =topic.substring(23);//seleziono solo il nome della lamp return;
System.out.println("nome lamp\t"+pezzo); String pezzo = topic.substring(23);// seleziono solo il nome della lamp
System.out.println("nome lamp\t" + pezzo);
for(int i=0;i<Luci.luciList.size();i++) {
if(Luci.luciList.get(i).getOUT().equalsIgnoreCase(pezzo) && msgJson.has("event")) { for (int i = 0; i < Luci.luciList.size(); i++) {
//&& msgJson.getInt("event")==1) if (Luci.luciList.get(i).getOUT().equalsIgnoreCase(pezzo) && msgJson.has("event")) {
// && msgJson.getInt("event")==1)
boolean stato= msgJson.getInt("event")==1;
boolean stato = msgJson.getInt("event") == 1;
Luci.luciList.get(i).setStato(stato);//(!Luci.luciList.get(i).getStato());
Luci.luciList.get(i).setStato(stato);// (!Luci.luciList.get(i).getStato());
JSONObject config = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA)); JSONObject config = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA));
JSONObject lux=new JSONObject(); JSONObject lux = new JSONObject();
lux.put("nome", Luci.luciList.get(i).getNome()); lux.put("nome", Luci.luciList.get(i).getNome());
lux.put("stato", Luci.luciList.get(i).getStato()); lux.put("stato", Luci.luciList.get(i).getStato());
lux.put("input", Luci.luciList.get(i).getIN()); lux.put("input", Luci.luciList.get(i).getIN());
lux.put("output", Luci.luciList.get(i).getOUT()); lux.put("output", Luci.luciList.get(i).getOUT());
config.put("lamp"+i, lux); config.put("lamp" + i, lux);
Helper.scriviFile(config, Luci.FILE_ZONA); Helper.scriviFile(config, Luci.FILE_ZONA);
//mando conferma // mando conferma
lux.remove("input"); lux.remove("input");
lux.remove("nome"); lux.remove("nome");
luci.sendMqttMessage(Luci.getMqttTree("from", "luci/luce"), lux.toString() ); luci.sendMqttMessage(Luci.getMqttTree("from", "luci/luce"), lux.toString());
} }
} }
return; return;
} }
if (topic.startsWith(Luci.getMqttTree("from", "arduino/AN0"))) {
if (msgJson.has("event")){
//
// if(msgJson.getInt("event")<200)giorno=false;
// else giorno=true;
giorno=(msgJson.getInt("event")>=200);
}
return;
}
Helper.print("Impossibile"); Helper.print("Impossibile");
} }
private void sendFile(String topic) throws MqttException, JSONException, IOException { private void sendFile(String topic) throws MqttException, JSONException, IOException {
JSONObject j = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA)); JSONObject j = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA));
//System.out.println(luci.FILE_ZONA);
//j.put("stato", luci.luciList.toString());
luci.sendMqttMessage(topic, j.toString()); luci.sendMqttMessage(topic, j.toString());
} }
private void sendStatus(String topic) throws MqttException, JSONException, IOException { private void sendStatus(String topic) throws MqttException, JSONException, IOException {
JSONObject j = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA)); JSONObject j = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA));
j.remove("sensM"); j.remove("sensM");
//System.out.println(luci.FILE_ZONA);
//j.put("stato", luci.luciList.toString());
luci.sendMqttMessage(topic, j.toString()); luci.sendMqttMessage(topic, j.toString());
} }
} }
\ No newline at end of file
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