Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
pissirSmartHome
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alfredo Chissotti
pissirSmartHome
Commits
fb66e915
Commit
fb66e915
authored
2 years ago
by
Elisa Giglio
Browse files
Options
Downloads
Patches
Plain Diff
ty
parent
43e71ab7
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
antifurtoMicroservizio/src/code/SubscribeCallback.java
+101
-41
101 additions, 41 deletions
antifurtoMicroservizio/src/code/SubscribeCallback.java
with
101 additions
and
41 deletions
antifurtoMicroservizio/src/code/SubscribeCallback.java
+
101
−
41
View file @
fb66e915
...
...
@@ -22,6 +22,13 @@ public class SubscribeCallback implements MqttCallback {
// Ho introdotto questa variabile perche' sia il microservizio dell'antifurto
// sia il microservizio degli scenari
// inviano il comando allo stesso OUT per dire che l'antifurto e' acceso
private
String
newSuonoConf
=
null
;
// quando arriva il comando per modificare la luce (OUT) che indica che
// l'allarme sta suonando, questa variabile conterra' il nuovo nome
// dell'OUT; altrimenti newSuonoConf = null
private
String
newNomeOutputAntifurtoConf
=
null
;
// quando arriva il comando per modificare la luce (OUT) che indica che
// l'antifurto e' acceso, questa variabile conterra' il nuovo nome
// dell'OUT; altrimenti newNomeOutputAntifurto = null
public
SubscribeCallback
(
Antifurto
client
,
Publisher
publisher
,
Esecutore
esec
,
Automa
automa
)
{
this
.
client
=
client
;
...
...
@@ -98,15 +105,38 @@ public class SubscribeCallback implements MqttCallback {
if
(
msgJson
.
has
(
"status"
))
newStatus
=
msgJson
.
getInt
(
"status"
);
else
{
if
(
msgJson
.
has
(
"event"
))
newStatus
=
msgJson
.
getInt
(
"event"
);
if
(
msgJson
.
has
(
"event"
))
{
if
(
newSuonoConf
!=
null
)
{
// sto facendo la configurazione. Si vuole modificare il nome dell'OUT che indica se l'allarme sta suonando
try
{
newConfigNomeOutputSuono
(
newSuonoConf
);
sendResponseConf
();
}
catch
(
MqttException
e
)
{
sendErrorConf
(
e
);
}
}
else
newStatus
=
msgJson
.
getInt
(
"event"
);
}
}
handleStateMsgFromOutputSuono
(
newStatus
);
return
;
}
if
(
topic
.
equals
(
"from/"
+
Antifurto
.
getMqttTree
()
+
"/gpio/"
+
client
.
getNomeOutputAntifurto
()))
{
if
(
msgJson
.
has
(
"event"
))
handleLuceAntifurto
();
if
(
msgJson
.
has
(
"event"
))
{
if
(
newNomeOutputAntifurtoConf
!=
null
)
{
// sto facendo la configurazione. Si vuole modificare il nome dell'OUT che indica che l'antifurto e' acceso/spento
try
{
newConfigNomeOutputAntifurto
(
newNomeOutputAntifurtoConf
);
sendResponseConf
();
}
catch
(
MqttException
e
)
{
sendErrorConf
(
e
);
}
}
else
handleLuceAntifurto
();
}
if
(
msgJson
.
has
(
"status"
))
setConsistentStatus
(
msgJson
);
return
;
...
...
@@ -236,7 +266,7 @@ public class SubscribeCallback implements MqttCallback {
publisher
.
aggiungiComando
(
topic
,
json
.
toString
());
}
private
void
setConsistentStatus
(
JSONObject
msgJson
)
throws
JSONException
,
IOException
{
private
void
setConsistentStatus
(
JSONObject
msgJson
)
throws
JSONException
,
IOException
{
//DA MODIFICARE (invio webapp)
int
statoOutGpio
=
msgJson
.
getInt
(
"status"
);
if
(
statoOutGpio
==
0
&&
automa
.
getStatoInterruttore
().
equals
(
"on"
))
{
// devo andare nello stato 0 dell'automa
...
...
@@ -269,47 +299,77 @@ public class SubscribeCallback implements MqttCallback {
}
private
void
handleNewConfig
(
JSONObject
msgJson
)
throws
JSONException
{
JSONObject
jsResponse
=
new
JSONObject
();
try
{
if
(
msgJson
.
has
(
"interruttore"
))
{
client
.
unsubscribeTopic
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
client
.
getNomeInterruttoreAntifurto
());
String
interruttore
=
msgJson
.
getString
(
"interruttore"
);
client
.
setNomeInterruttoreAntifurto
(
interruttore
);
client
.
addTopicToSubscribe
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
interruttore
);
}
if
(
msgJson
.
has
(
"outputSuono"
))
{
client
.
unsubscribeTopic
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
client
.
getNomeOutputSuono
());
String
outputSuono
=
msgJson
.
getString
(
"outputSuono"
);
client
.
setNomeOutputSuono
(
outputSuono
);
client
.
addTopicToSubscribe
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
outputSuono
);
}
if
(
msgJson
.
has
(
"nomeOutputAntifurto"
))
{
client
.
unsubscribeTopic
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
client
.
getNomeOutputAntifurto
());
String
nomeOutputAntifurto
=
msgJson
.
getString
(
"nomeOutputAntifurto"
);
client
.
setNomeOutputAntifurto
(
nomeOutputAntifurto
);
client
.
addTopicToSubscribe
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
nomeOutputAntifurto
);
if
(
msgJson
.
has
(
"interruttore"
))
{
client
.
unsubscribeTopic
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
client
.
getNomeInterruttoreAntifurto
());
String
interruttore
=
msgJson
.
getString
(
"interruttore"
);
client
.
setNomeInterruttoreAntifurto
(
interruttore
);
client
.
addTopicToSubscribe
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
interruttore
);
}
if
(
msgJson
.
has
(
"outputSuono"
))
{
if
(
automa
.
getStatoAutoma
()==
2
)
{
// l'allarme sta suonando
publisher
.
aggiungiComando
(
"to/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
client
.
getNomeOutputSuono
(),
"{cmd:0}"
);
newSuonoConf
=
msgJson
.
getString
(
"outputSuono"
);
}
else
{
newConfigNomeOutputSuono
(
msgJson
.
getString
(
"outputSuono"
));
}
}
if
(
msgJson
.
has
(
"nomeOutputAntifurto"
))
{
if
(
automa
.
getStatoAutoma
()==
1
||
automa
.
getStatoAutoma
()==
2
)
{
// l'antifurto e' acceso
publisher
.
aggiungiComando
(
"to/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
client
.
getNomeOutputAntifurto
(),
"{cmd:0}"
);
newNomeOutputAntifurtoConf
=
msgJson
.
getString
(
"nomeOutputAntifurto"
);
}
else
{
newConfigNomeOutputAntifurto
(
msgJson
.
getString
(
"nomeOutputAntifurto"
));
}
}
sendResponseConf
();
}
catch
(
MqttException
|
IOException
e
)
{
sendErrorConf
(
e
);
}
}
private
void
newConfigNomeOutputAntifurto
(
String
nomeOutputAntifurto
)
throws
MqttException
,
JSONException
{
client
.
unsubscribeTopic
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
client
.
getNomeOutputAntifurto
());
client
.
setNomeOutputAntifurto
(
nomeOutputAntifurto
);
client
.
addTopicToSubscribe
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
nomeOutputAntifurto
);
newNomeOutputAntifurtoConf
=
null
;
}
private
void
newConfigNomeOutputSuono
(
String
outputSuono
)
throws
MqttException
,
JSONException
{
client
.
unsubscribeTopic
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
client
.
getNomeOutputSuono
());
client
.
setNomeOutputSuono
(
outputSuono
);
client
.
addTopicToSubscribe
(
"from/"
+
Antifurto
.
getMqttTree
()+
"/gpio/"
+
outputSuono
);
newSuonoConf
=
null
;
}
jsResponse
.
put
(
"interruttore"
,
client
.
getNomeInterruttoreAntifurto
());
jsResponse
.
put
(
"outputSuono"
,
client
.
getNomeOutputSuono
());
jsResponse
.
put
(
"nomeOutputAntifurto"
,
client
.
getNomeOutputAntifurto
());
// pubblica la risposta (per la webApp) solo se newNomeOutputAntifurtoConf = null e newSuonoConf = null
private
void
sendResponseConf
()
throws
JSONException
,
IOException
{
if
(
newNomeOutputAntifurtoConf
==
null
&&
newSuonoConf
==
null
)
{
JSONObject
jsResponse
=
new
JSONObject
();
jsResponse
.
put
(
"interruttore"
,
client
.
getNomeInterruttoreAntifurto
());
jsResponse
.
put
(
"outputSuono"
,
client
.
getNomeOutputSuono
());
jsResponse
.
put
(
"nomeOutputAntifurto"
,
client
.
getNomeOutputAntifurto
());
JSONObject
jsFile
=
new
JSONObject
(
Helper
.
leggiFile
(
Antifurto
.
CONF_ZONA
));
jsResponse
.
put
(
"sensoriMovimento"
,
jsFile
.
get
(
"sensoriMovimento"
));
Helper
.
scriviFile
(
jsResponse
,
Antifurto
.
CONF_ZONA
);
jsResponse
.
remove
(
"sensoriMovimento"
);
publisher
.
aggiungiComando
(
"from/"
+
Antifurto
.
getMqttTree
()
+
"/antifurto"
,
jsResponse
.
toString
());
}
catch
(
MqttException
|
IOException
e
)
{
JSONObject
jsError
=
new
JSONObject
();
jsError
.
put
(
"error"
,
e
.
getMessage
());
publisher
.
aggiungiComando
(
"from/"
+
Antifurto
.
getMqttTree
()
+
"/antifurto"
,
jsError
.
toString
());
JSONObject
jsFile
=
new
JSONObject
(
Helper
.
leggiFile
(
Antifurto
.
CONF_ZONA
));
jsResponse
.
put
(
"sensoriMovimento"
,
jsFile
.
get
(
"sensoriMovimento"
));
Helper
.
scriviFile
(
jsResponse
,
Antifurto
.
CONF_ZONA
);
jsResponse
.
remove
(
"sensoriMovimento"
);
publisher
.
aggiungiComando
(
"from/"
+
Antifurto
.
getMqttTree
()
+
"/antifurto"
,
jsResponse
.
toString
());
}
}
// pubblica l'errore (per la webApp)
private
void
sendErrorConf
(
Exception
e
)
throws
JSONException
{
JSONObject
jsError
=
new
JSONObject
();
jsError
.
put
(
"error"
,
e
.
getMessage
());
publisher
.
aggiungiComando
(
"from/"
+
Antifurto
.
getMqttTree
()
+
"/antifurto"
,
jsError
.
toString
());
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment