BeeHoneyt  1.1
BTS SNIR LaSalle Avignon 2021
Référence de la classe com.example.bee_honeyt.Communication

Déclaration de la classe Communication. Plus de détails...

Graphe de collaboration de com.example.bee_honeyt.Communication:
Collaboration graph

Fonctions membres publiques

 Communication (Context context, final Handler handler)
 Constructeur de la classe Communication. Plus de détails...
 
void connecter ()
 Connexion au TTN. Plus de détails...
 
void deconnecter ()
 Deconnexion du TTN. Plus de détails...
 
boolean estConnecte ()
 Retourne l'état de connexion au serveur TTN. Plus de détails...
 
String getPassword ()
 
String getUsername ()
 
void setCallback (MqttCallbackExtended callback)
 Installe les fonctions de rappel. Plus de détails...
 
void setClientId (String username)
 
void setPassword (String password)
 
void setUsername (String username)
 
boolean souscrireTopic (String deviceID)
 S'abonne à un topic (deviceID = ESP32 d'une ruche dans TTN) Plus de détails...
 
boolean unsubscribe (String deviceID)
 S'desabonne à un topic (deviceID = ESP32 d'une ruche dans TTN) Plus de détails...
 

Attributs publics statiques

static final int TTN_CONNECTE = 1
 
static final int TTN_DECONNECTE = 2
 
static final int TTN_MESSAGE = 3
 

Fonctions membres privées

void creerClientMQTTT (Context context, Handler handler)
 

Attributs privés

Handler handler = null
 
MqttAndroidClient mqttAndroidClient
 
String password = ""
 mot de passe TTN Plus de détails...
 
String username = ""
 nom d'utilisateur Plus de détails...
 

Attributs privés statiques

static final String TAG = "_Communication"
 

Description détaillée

Déclaration de la classe Communication.

Classe pour gérer la communication MQTT.

Permet la communication MQTT avec le serveur The Things Network

Auteur
Thierry Vaira
LastChangedRevision
166
LastChangedDate
2021-06-14 09:28:36 +0200 (lun. 14 juin 2021)

Définition à la ligne 35 du fichier Communication.java.

Documentation des constructeurs et destructeur

◆ Communication()

com.example.bee_honeyt.Communication.Communication ( Context  context,
final Handler  handler 
)

Constructeur de la classe Communication.

Paramètres
context
handler

Définition à la ligne 67 du fichier Communication.java.

68  {
69  Log.v(TAG, "[Communication()] clientId = " + clientId);
70  this.handler = handler;
71 
72  creerClientMQTTT(context, handler);
73  }

Références com.example.bee_honeyt.Communication.creerClientMQTTT(), com.example.bee_honeyt.Communication.handler, et com.example.bee_honeyt.Communication.TAG.

Documentation des fonctions membres

◆ connecter()

com.example.bee_honeyt.Communication.connecter ( )

Connexion au TTN.

Définition à la ligne 169 du fichier Communication.java.

170  {
171  MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
172  mqttConnectOptions.setAutomaticReconnect(true);
173  mqttConnectOptions.setCleanSession(false);
174  mqttConnectOptions.setUserName(username);
175  mqttConnectOptions.setPassword(password.toCharArray());
176 
177  try
178  {
179  Log.d(TAG, "[connecter()] serverUri = " + serverUri + " clientId = " + clientId);
180  mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener()
181  {
182  @Override
183  public void onSuccess(IMqttToken asyncActionToken)
184  {
185  DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
186  disconnectedBufferOptions.setBufferEnabled(true);
187  disconnectedBufferOptions.setBufferSize(100);
188  disconnectedBufferOptions.setPersistBuffer(false);
189  disconnectedBufferOptions.setDeleteOldestMessages(false);
190  mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
191  Log.d(TAG, "[onSuccess()] serverUri = " + serverUri + " clientId = " + clientId);
192  }
193 
194  @Override
195  public void onFailure(IMqttToken asyncActionToken, Throwable exception)
196  {
197  Log.d(TAG, "[onFailure()] serverUri = " + serverUri + " clientId = " + clientId + " exception = " + exception.toString());
198  }
199  });
200  }
201  catch (MqttException e)
202  {
203  e.printStackTrace();
204  }
205  }

Références com.example.bee_honeyt.Communication.mqttAndroidClient, com.example.bee_honeyt.Communication.password, com.example.bee_honeyt.Communication.TAG, et com.example.bee_honeyt.Communication.username.

Référencé par com.example.bee_honeyt.IHMMobile.initialiserMQTT().

◆ creerClientMQTTT()

void com.example.bee_honeyt.Communication.creerClientMQTTT ( Context  context,
Handler  handler 
)
private

Définition à la ligne 105 du fichier Communication.java.

106  {
107  mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId);
108  mqttAndroidClient.setCallback(new MqttCallbackExtended()
109  {
110  @Override
111  public void connectComplete(boolean b, String s)
112  {
113  Log.w(TAG, "[connectComplete()] serverUri = " + s + " connecte = " + mqttAndroidClient.isConnected());
114  Message msg = Message.obtain();
115  Bundle bundle = new Bundle();
116  bundle.putInt("etat", TTN_CONNECTE); // clé -> valeur ici etat -> TTN_CONNECTE
117  msg.setData(bundle);
118  handler.sendMessage(msg);
119  }
120 
121  @Override
122  public void connectionLost(Throwable throwable)
123  {
124  Log.w(TAG, "[connectionLost()]");
125  Message msg = Message.obtain();
126  Bundle b = new Bundle();
127  b.putInt("etat", TTN_DECONNECTE);
128  msg.setData(b);
129  handler.sendMessage(msg);
130  }
131 
132  @Override
133  public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception
134  {
135  Log.w(TAG, "[messageArrived()] topic = " + topic + " message = " + mqttMessage.toString());
136  Message msg = Message.obtain();
137  Bundle b = new Bundle();
138  b.putInt("etat", TTN_MESSAGE);
139  b.putString("topic", topic);
140  b.putString("message", mqttMessage.toString());
141  msg.setData(b);
142  handler.sendMessage(msg);
143  }
144 
145  @Override
146  public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)
147  {
148  Log.w(TAG, "[deliveryComplete()]");
149  }
150  });
151  }

Références com.example.bee_honeyt.Communication.handler, com.example.bee_honeyt.Communication.mqttAndroidClient, com.example.bee_honeyt.Communication.TAG, com.example.bee_honeyt.Communication.TTN_CONNECTE, com.example.bee_honeyt.Communication.TTN_DECONNECTE, et com.example.bee_honeyt.Communication.TTN_MESSAGE.

Référencé par com.example.bee_honeyt.Communication.Communication().

◆ deconnecter()

com.example.bee_honeyt.Communication.deconnecter ( )

Deconnexion du TTN.

Définition à la ligne 212 du fichier Communication.java.

213  {
214  if(!estConnecte())
215  return;
216  Log.d(TAG, "[deconnecter()] serverUri = " + serverUri + " clientId = " + clientId);
217  try
218  {
219  IMqttToken disconToken = mqttAndroidClient.disconnect();
220  disconToken.setActionCallback(new IMqttActionListener()
221  {
222  @Override
223  public void onSuccess(IMqttToken asyncActionToken)
224  {
225  Log.d(TAG, "[onSuccess()] serverUri = " + serverUri + " clientId = " + clientId);
226  }
227 
228  @Override
229  public void onFailure(IMqttToken asyncActionToken, Throwable exception)
230  {
231  Log.d(TAG, "[onFailure()] serverUri = " + serverUri + " clientId = " + clientId + " exception = " + exception.toString());
232  }
233  });
234  }
235  catch (MqttException e)
236  {
237  e.printStackTrace();
238  }
239  }

Références com.example.bee_honeyt.Communication.estConnecte(), com.example.bee_honeyt.Communication.mqttAndroidClient, et com.example.bee_honeyt.Communication.TAG.

◆ estConnecte()

com.example.bee_honeyt.Communication.estConnecte ( )

Retourne l'état de connexion au serveur TTN.

Renvoie
boolean

Définition à la ligne 247 du fichier Communication.java.

248  {
249  Log.w(TAG, "[estConnecte()] " + mqttAndroidClient.isConnected());
250 
251  return mqttAndroidClient.isConnected();
252  }

Références com.example.bee_honeyt.Communication.mqttAndroidClient, et com.example.bee_honeyt.Communication.TAG.

Référencé par com.example.bee_honeyt.Communication.deconnecter().

◆ getPassword()

String com.example.bee_honeyt.Communication.getPassword ( )

Définition à la ligne 80 du fichier Communication.java.

81  {
82  return this.password;
83  }

Références com.example.bee_honeyt.Communication.password.

◆ getUsername()

String com.example.bee_honeyt.Communication.getUsername ( )

Définition à la ligne 75 du fichier Communication.java.

76  {
77  return this.username;
78  }

Références com.example.bee_honeyt.Communication.username.

◆ setCallback()

com.example.bee_honeyt.Communication.setCallback ( MqttCallbackExtended  callback)

Installe les fonctions de rappel.

Paramètres
callbackle retour

Définition à la ligne 159 du fichier Communication.java.

160  {
161  mqttAndroidClient.setCallback(callback);
162  }

Références com.example.bee_honeyt.Communication.mqttAndroidClient.

◆ setClientId()

void com.example.bee_honeyt.Communication.setClientId ( String  username)

◆ setPassword()

void com.example.bee_honeyt.Communication.setPassword ( String  password)

Définition à la ligne 99 du fichier Communication.java.

100  {
101  if(password != null)
102  this.password = password;
103  }

Références com.example.bee_honeyt.Communication.password.

Référencé par com.example.bee_honeyt.IHMMobile.initialiserMQTT().

◆ setUsername()

void com.example.bee_honeyt.Communication.setUsername ( String  username)

Définition à la ligne 90 du fichier Communication.java.

91  {
92  if(username != null)
93  {
94  this.clientId = username;
95  this.username = username;
96  }
97  }

Références com.example.bee_honeyt.Communication.username.

Référencé par com.example.bee_honeyt.IHMMobile.initialiserMQTT(), et com.example.bee_honeyt.Communication.setClientId().

◆ souscrireTopic()

com.example.bee_honeyt.Communication.souscrireTopic ( String  topic)

S'abonne à un topic (deviceID = ESP32 d'une ruche dans TTN)

Paramètres
deviceIDle deviceID dans TTN

Définition à la ligne 260 du fichier Communication.java.

261  {
262  // Vérifications
263  if(mqttAndroidClient == null && !mqttAndroidClient.isConnected())
264  {
265  return false;
266  }
267 
268  final String topicTTN = clientId + "/devices/" + deviceID + "/up";
269  Log.w(TAG, "[souscrireTopic()] topic = " + topicTTN);
270  try
271  {
272  final boolean[] retour = {false};
273  mqttAndroidClient.subscribe(topicTTN, 0, null, new IMqttActionListener()
274  {
275  @Override
276  public void onSuccess(IMqttToken asyncActionToken)
277  {
278  Log.w(TAG, "[onSuccess()] topic = " + topicTTN);
279  retour[0] = true;
280  }
281 
282  @Override
283  public void onFailure(IMqttToken asyncActionToken, Throwable exception)
284  {
285  Log.w(TAG, "[onFailure()] topic = " + topicTTN);
286  retour[0] = false;
287  }
288  });
289  return retour[0];
290  }
291  catch (MqttException e)
292  {
293  Log.w(TAG, "Erreur topic = " + topicTTN);
294  e.printStackTrace();
295  return false;
296  }
297  }

Références com.example.bee_honeyt.Communication.mqttAndroidClient, et com.example.bee_honeyt.Communication.TAG.

◆ unsubscribe()

com.example.bee_honeyt.Communication.unsubscribe ( String  deviceID)

S'desabonne à un topic (deviceID = ESP32 d'une ruche dans TTN)

Paramètres
deviceIDle deviceID dans TTN

Définition à la ligne 305 du fichier Communication.java.

306  {
307  if(mqttAndroidClient == null && !mqttAndroidClient.isConnected())
308  {
309  return false;
310  }
311  final String topicTTN = clientId + "/devices/" + deviceID + "/up";
312  Log.w(TAG, "[unsouscrireTopic()] topic = " + topicTTN);
313  try
314  {
315  final boolean[] retour = {false};
316  mqttAndroidClient.unsubscribe(topicTTN, null, null);
317 
318  return retour[0];
319  }
320  catch (MqttException e)
321  {
322  Log.w(TAG, "Erreur topic = " + topicTTN);
323  e.printStackTrace();
324  return false;
325  }
326  }

Références com.example.bee_honeyt.Communication.mqttAndroidClient, et com.example.bee_honeyt.Communication.TAG.

Documentation des données membres

◆ handler

Handler com.example.bee_honeyt.Communication.handler = null
private

◆ mqttAndroidClient

◆ password

String com.example.bee_honeyt.Communication.password = ""
private

◆ TAG

◆ TTN_CONNECTE

final int com.example.bee_honeyt.Communication.TTN_CONNECTE = 1
static

Constantes pour le Handler

Définition à la ligne 51 du fichier Communication.java.

Référencé par com.example.bee_honeyt.Communication.creerClientMQTTT().

◆ TTN_DECONNECTE

final int com.example.bee_honeyt.Communication.TTN_DECONNECTE = 2
static

Définition à la ligne 52 du fichier Communication.java.

Référencé par com.example.bee_honeyt.Communication.creerClientMQTTT().

◆ TTN_MESSAGE

final int com.example.bee_honeyt.Communication.TTN_MESSAGE = 3
static

Définition à la ligne 53 du fichier Communication.java.

Référencé par com.example.bee_honeyt.Communication.creerClientMQTTT().

◆ username

String com.example.bee_honeyt.Communication.username = ""
private

La documentation de cette classe a été générée à partir du fichier suivant :
com.example.bee_honeyt.Communication.TAG
static final String TAG
Definition: Communication.java:40
com.example.bee_honeyt.Communication.username
String username
nom d'utilisateur
Definition: Communication.java:57
com.example.bee_honeyt.Communication.TTN_CONNECTE
static final int TTN_CONNECTE
Definition: Communication.java:51
com.example.bee_honeyt.Communication.password
String password
mot de passe TTN
Definition: Communication.java:58
com.example.bee_honeyt.Communication.setUsername
void setUsername(String username)
Definition: Communication.java:90
com.example.bee_honeyt.Communication.handler
Handler handler
Definition: Communication.java:46
com.example.bee_honeyt.Communication.creerClientMQTTT
void creerClientMQTTT(Context context, Handler handler)
Definition: Communication.java:105
com.example.bee_honeyt.Communication.TTN_MESSAGE
static final int TTN_MESSAGE
Definition: Communication.java:53
com.example.bee_honeyt.Communication.TTN_DECONNECTE
static final int TTN_DECONNECTE
Definition: Communication.java:52
com.example.bee_honeyt.Communication.mqttAndroidClient
MqttAndroidClient mqttAndroidClient
Definition: Communication.java:45
com.example.bee_honeyt.Communication.estConnecte
boolean estConnecte()
Retourne l'état de connexion au serveur TTN.
Definition: Communication.java:247