Newer
Older
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import org.json.JSONException;
import org.json.JSONObject;
public class Esecutore extends Thread {
private MyQueue<Integer> codaVal; // coda in cui vengono mantenuti i valori da sommare alla variabile valore
private int valore; // variabile numerica cumulativa inizialmente impostata a 0
private Publisher publisher;
private Automa automa;
private String outputSuono; // nome logico dell'interruttore che fa scattare il suono dell'allarme
private LocalDateTime tempoAllarme; //valorizzato solo quando l'allarme sta suonando. Contiene l'ora in cui l'allarme ha iniziato a suonare
private static long allarmeTimeout;
public Esecutore(Publisher publisher, MyQueue<Integer> codaVal, Automa automa, String outputSuono) throws JSONException, IOException {
this.codaVal = codaVal;
this.automa = automa;
this.publisher = publisher;
this.outputSuono = outputSuono;
publisher.aggiungiComando("to/gruppo2/luci/gpio", "{request:status}");
JSONObject statoJson = new JSONObject(Helper.leggiFile(Automa.FILE_STATO));
this.valore = statoJson.getInt("valore");
setSoglia(statoJson.getInt("soglia"));
if(statoJson.getInt("stato")==2)
this.tempoAllarme = LocalDateTime.parse(statoJson.getString("tempoAllarme"));
private int getSogliaMax() {
return Math.round(soglia+soglia*0.2f); // il valore non puo' andare oltre il 20 % sopra la soglia
}
final long DURATA_SUONO = 1; // l'allarme dura 1 minuti e poi smette di suonare.
int valoreToSend = valore; // ogni volta che aumento / diminuisco il valore di 5, notifico la web app pubblicando un messaggio
while(true) {
while(automa.antifurtoAttivo()) { System.out.println("VALORE = "+ valore);
delta = codaVal.receive();
valore = valore + delta;
valore = 0;
if(valore > getSogliaMax())
valore = getSogliaMax();
if(Math.abs(valore-valoreToSend) >= 5 || (valore==0 && valoreToSend!=0) || valore==getSogliaMax()) {
JSONObject msgJson = new JSONObject();
msgJson.put("event", valore);
publisher.aggiungiComando("from/"+Antifurto.getMqttTree()+"/antifurto/valore", msgJson.toString());
valoreToSend = valore;
}
JSONObject statoJson = new JSONObject(Helper.leggiFile(Automa.FILE_STATO));
statoJson.put("valore", valore);
if(valore >= soglia && (!automa.allarme()) && delta>0) {
publisher.aggiungiComando("to/"+Antifurto.getMqttTree()+"/gpio/"+outputSuono, "{cmd:1}");
tempoAllarme = LocalDateTime.now();
statoJson.put("tempoAllarme",tempoAllarme.toString());
}
else {
if(automa.allarme()) {
LocalDateTime tempoAttuale = LocalDateTime.now();
long durata = Math.abs(Duration.between(tempoAllarme, tempoAttuale).toMinutes());
if(valore < soglia || durata >= DURATA_SUONO || !automa.antifurtoAttivo() ) {
publisher.aggiungiComando("to/"+Antifurto.getMqttTree()+"/gpio/"+outputSuono, "{cmd:0}"); // l'allarme viene disattivato
}
statoJson.put("soglia", soglia);
Helper.scriviFile(statoJson, Automa.FILE_STATO);
catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if(!automa.antifurtoAttivo()) {
reset();
}
}
}
public void aggiungiVal(int n) {
codaVal.send(n);
}
public void reset() {
valore = 0;
codaVal.removeAll();
}
public static int getSoglia() {
return soglia;
}
public int getValore() {
return valore;
}
public static long getAllarmeTimeout() {
return allarmeTimeout;
}
public static void setSoglia(int newValue) throws JSONException, IOException {
JSONObject js = new JSONObject(Helper.leggiFile(Automa.FILE_STATO));
js.put("soglia", newValue);
Helper.scriviFile(js, Automa.FILE_STATO);
soglia = newValue;
}