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
72
73
74
75
76
77
78
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
public class Automa {
private JSONObject jsonObject; //tabella che descrive l'automa
private Hashtable<String,Integer> deltaSensori; // ad ogni sensore di movimento e' associato il valore di delta
private int statoAttuale;
private String statoInterruttore;
private String statoSuono;
public Automa() throws JSONException, IOException {
this.jsonObject = new JSONObject(leggiFile("./automa.json"));
this.statoInterruttore = "off";
this.statoSuono = "off";
this.statoAttuale = 0;
riempiHashTable();
}
private void riempiHashTable() throws JSONException, IOException {
this.deltaSensori = new Hashtable<String, Integer>();
JSONObject jsObj = new JSONObject(leggiFile("./CONF/deltaSensoriMovimento.json"));
Iterator<String> keys = jsObj.keys();
while(keys.hasNext()) {
String key = keys.next();
int value = jsObj.getInt(key);
deltaSensori.put(key, value);
}
}
private String leggiFile(String path) throws IOException {
String line;
String answer = "";
FileReader fileReader = new FileReader(path);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
answer += line;
}
bufferedReader.close();
return answer;
}
public int getDelta(String nomeSensore) {
return deltaSensori.get(nomeSensore);
}
// SERVIRA FORSE PER LA PERSISTENZA
// private synchronized void calcolaStato(String newStatoInterruttore, String newStatoSuono) throws JSONException {
// Iterator<String> keys = jsonObject.keys();
// while(keys.hasNext()) {
// String key = keys.next();
// JSONObject riga = jsonObject.getJSONObject(key);
// if(riga.getString("out0").equals(newStatoInterruttore) && riga.getString("out1").equals(newStatoSuono)) {
// this.statoAttuale = riga.getInt("stato");
// this.statoInterruttore = newStatoInterruttore;
// this.statoSuono = newStatoSuono;
// }
// }
// }
// e' cambiato lo stato dell'interruttore, per cui cerco nel file json in quale nuovo stato devo andare
private synchronized void calcolaNuovoStatoMsgFromInterruttore(String newStatoInterruttore) throws JSONException {
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
JSONObject riga = jsonObject.getJSONObject(key);
if(riga.getInt("stato")==statoAttuale) {
this.statoAttuale = riga.getInt("cmd"+newStatoInterruttore);
aggiornaInterruttoreESuono();
}
}
}
//in base allo stato attuale dell'antifurto, calcolo lo stato dell'interruttore e del suono
private synchronized void aggiornaInterruttoreESuono() throws JSONException {
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
JSONObject riga = jsonObject.getJSONObject(key);
if(riga.getInt("stato")==statoAttuale) {
this.statoInterruttore = riga.getString("out0");
this.statoSuono = riga.getString("out1");
}
}
}
// e' cambiato lo stato del suono, per cui cerco nel file json in quale nuovo stato devo andare
private synchronized void calcolaNuovoStatoMsgFromSuono(String newStatoSuono) throws JSONException {
String jsonKey = "";
if(newStatoSuono.equals("on"))
jsonKey = "eventValMaggioreSoglia";
else
jsonKey = "eventValMinoreSoglia";
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
JSONObject riga = jsonObject.getJSONObject(key);
if(riga.getInt("stato")==statoAttuale) {
this.statoAttuale = riga.getInt(jsonKey);
aggiornaInterruttoreESuono();
}
}
}
public synchronized void changeStatoInterruttore() throws JSONException {
if(statoInterruttore.equals("on"))
calcolaNuovoStatoMsgFromInterruttore("off");
else
calcolaNuovoStatoMsgFromInterruttore("on");
}
public synchronized void aggiornaStatoSuono(String newStatoSuono) throws JSONException {
calcolaNuovoStatoMsgFromSuono(newStatoSuono);
}
public synchronized String getStatoAutoma() {
return "{\"stato\":"+statoAttuale+",\"interruttore\":\""+statoInterruttore+"\",\"allarme\":\""+statoSuono+"\"}";
}
public synchronized String getStatoInterruttore() {
return statoInterruttore;
}
public synchronized String getStatoSuono() {
return statoSuono;
}
// Ritorna true se l'antifurto e' attivo
public synchronized boolean antifurtoAttivo() {
if(statoInterruttore.equals("on"))
return true;
return false;
}
// Ritorna true se l'antifurto sta suonando
public synchronized boolean allarme() {
if(statoSuono.equals("on"))
return true;
return false;
}
// con input la stringa "0" restituisce "off"
// con input la stringa "1" restituisce "on"
public String converter(int num) {
if(num==0)
return "off";
return "on";
}
}