Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package code;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
//import scenari.Automa;
//import scenari.Scenari;
public class Subscriber implements MqttCallback{
private Luci luci;
private Esecutore esec;
public String sensM;
public Subscriber(Luci luci, Esecutore esec, String sensm) {
this.luci=luci;
this.esec = esec;
this.sensM = sensm;
}
@Override
public void connectionLost(Throwable arg0) {
Helper.print("connessione persa\t"+arg0);
final Date d = new Date();
Date d2 = new Date();
long time = Math.abs(d2.getTime()-d.getTime());
while (time<600000) {
try {
Helper.print("retrying...");
Luci.main(null);
return;
} catch (MqttException | JSONException | IOException e) {
Helper.print("non sono riusciuto");
d2 = new Date();
time = Math.abs(d2.getTime()-d.getTime());
}
}
Helper.print("fuori dal while");
if(time>=600000) {
Helper.print("Tentativo di riconnessione fallito");
System.exit(1);
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
Helper.print("inviato");
}
@Override
public void messageArrived(String topic, MqttMessage message) throws MqttException, JSONException, IOException {
Helper.print("new message: "+topic+" "+message.toString());
if(topic.equals(Luci.getMqttTree("rpc/","luci"))) {
Helper.print("invio la mia configurazione");
sendFile(Luci.getMqttTree("from/","luci"),true);
return;
}
if(topic.equals("to/all")) {
Helper.print("invio il mio stato");
sendStatus(Luci.getMqttTree("from/","luci"),true);
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
return;
}
JSONObject msgJson = new JSONObject(message.toString());
//la beaglebone risponde sutopic {"event":1} se è acceso, 0 altrimenti
if(topic.equals(Luci.getMqttTree("to", "luci/new"))){
JSONObject config = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA));
String nome= msgJson.getString("nome");
for(Luce k: Luci.luciList) {
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");
return;
}
}
int k=Luci.luciList.size();
config.put("lamp"+k, msgJson);
Luci.luciList.add(new Luce(msgJson));//,"lamp"+k
Helper.scriviFile(config, Luci.FILE_ZONA);
}
if(topic.equals(Luci.getMqttTree("to", "luci/sensore"))){
//salvati il sensore di movimento
sensM=msgJson.getString("sensM");
//devo aggiornare in file
JSONObject config = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA));
config.put("sensM", sensM);
Helper.scriviFile(config, Luci.FILE_ZONA);
}//gruppo2/luci/luci/sensore
//controllo i topic su cui mi arrivano gli on/off
if(topic.startsWith(Luci.getMqttTree("from", "luci/gpio/IN"))){
//prendo l'ultimo pezzo del topic, diventa il mio nome
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).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
Luci.luciList.get(i).setStato(!Luci.luciList.get(i).getStato());
JSONObject config = new JSONObject(Helper.leggiFile(Luci.FILE_ZONA));
JSONObject lux=new JSONObject();
lux.put("nome", Luci.luciList.get(i).getNome());
lux.put("stato", Luci.luciList.get(i).getStato());
lux.put("input", Luci.luciList.get(i).getIN());
lux.put("output", Luci.luciList.get(i).getOUT());
config.put("lamp"+i, lux);
Helper.scriviFile(config, Luci.FILE_ZONA);
}
}
//sensM tempo
if(topic.startsWith(Luci.getMqttTree("from", "luci/gpio/IN"))){
//prendo l'ultimo pezzo del topic, diventa il mio nome
String nomeSensM =topic.substring(23);//seleziono solo il nome della lamp
System.out.println("nome lamp\t"+pezzo);
if(nomeSensM==sensM) {
Esecutore es=new Esecutore(luci);
es.run();
}
}
}//gruppo2/luci/luci/sensore
Helper.print("Impossibile");
}
private void sendFile(String topic,boolean moreInfo) throws MqttException, JSONException, IOException {
JSONObject j = new JSONObject(luci.FILE_ZONA);
//System.out.println(luci.FILE_ZONA);
//j.put("stato", luci.luciList.toString());
luci.sendMqttMessage(topic, j.toString());
}
private void sendStatus(String topic,boolean moreInfo) throws MqttException, JSONException, IOException {