1 package com.lasalle.beehoneyt;
3 import androidx.appcompat.app.AppCompatActivity;
4 import androidx.recyclerview.widget.LinearLayoutManager;
5 import androidx.recyclerview.widget.RecyclerView;
6 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
8 import android.os.Bundle;
9 import android.os.Handler;
10 import android.os.Message;
11 import android.util.Log;
13 import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
14 import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
15 import org.eclipse.paho.client.mqttv3.MqttMessage;
16 import org.json.JSONObject;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
41 private static final String
TAG =
"MainActivity";
47 private List<Ruche>
ruches =
new ArrayList<>();
66 protected void onCreate(Bundle savedInstanceState)
68 super.onCreate(savedInstanceState);
69 setContentView(R.layout.activity_main);
70 Log.d(TAG,
"onCreate()");
87 Log.i(TAG,
"onStart()");
97 Log.i(TAG,
"onResume()");
111 Log.i(TAG,
"onPause()");
122 Log.i(TAG,
"onStop()");
133 Log.i(TAG,
"onDestroy()");
139 Log.d(TAG,
"initialiserVue()");
140 swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeRefreshLayout);
141 swipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener()
144 public void onRefresh()
150 recyclerView = (RecyclerView) findViewById(R.id.listeRuche);
151 recyclerView.setHasFixedSize(
true);
157 recyclerView.setAdapter(
adapter);
167 Log.d(TAG,
"recupererRuches()");
173 nouvelleRuche =
false;
176 List<Ruche> listeRuches = Arrays.asList(
177 new Ruche(
"Ruche 1",
"ruche_1"),
178 new Ruche(
"Ruche 2",
"ruche_2"),
179 new Ruche(
"Ruche 3",
"ruche_3"),
180 new Ruche(
"Ruche 4",
"ruche_3")
183 listeRuches.get(0).setInfos(
"Simulateur");
184 listeRuches.get(1).setPoids(35);
185 listeRuches.get(1).setInfos(
"Simulateur");
186 listeRuches.get(2).setPoids(32);
187 listeRuches.get(2).setInfos(
"Simulateur");
188 listeRuches.get(3).setPoids(40);
189 listeRuches.get(3).setInfos(
"Simulateur");
192 for (
int i = 0; i < listeRuches.size(); i++)
194 ruches.add(listeRuches.get(i));
208 swipeRefreshLayout.setRefreshing(
false);
209 adapter.notifyDataSetChanged();
214 Log.d(TAG,
"communiquerTTN() deviceID = " + deviceID);
218 public void connectComplete(
boolean b, String s)
222 public void connectionLost(Throwable throwable)
226 public void messageArrived(String topic, MqttMessage mqttMessage)
throws Exception
228 Log.d(TAG,
"communiquerTTN() topic = " + topic);
229 Log.d(TAG,
"communiquerTTN() mqttMessage = " + mqttMessage.toString());
234 JSONObject json =
new JSONObject(mqttMessage.toString());
235 String deviceID = json.getString(
"dev_id");
236 for (
int i = 0; i < ruches.size(); i++)
238 if(ruches.get(i).getDeviceID().equals(deviceID))
240 ruches.get(i).setInfos(
"Message reçu !");
244 ruches.get(i).setInfos(
"Simulateur");
253 public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)
261 for (
int i = 0; i < ruches.size(); i++)
263 ruches.get(i).souscrireTopic();
274 public void handleMessage(Message msg)
276 super.handleMessage(msg);
278 Bundle bundle = msg.getData();
280 switch(bundle.getInt(
"etat"))
283 Log.d(TAG,
"handleMessage() TTN connecté");
287 Log.d(TAG,
"handleMessage() TTN déconnecté");
290 Log.d(TAG,
"handleMessage() TTN topic = " + bundle.getString(
"topic") +
" message = " + bundle.getString(
"message"));
RecyclerView recyclerView
CommunicationMQTT communicationMQTT
void onStart()
Méthode appelée au démarrage après le onCreate() ou un restart après un onStop()
RecyclerView.Adapter adapter
Déclaration de la classe MainActivity.
static final int TTN_MESSAGE
SwipeRefreshLayout swipeRefreshLayout
static final int TTN_CONNECTE
boolean nouvelleRuche
indique si une nouvelle ruche a été récupérée
void onCreate(Bundle savedInstanceState)
Méthode appelée à la création de l'activité MainActivity.
static final int NB_RUCHES
le nombre max. de ruches
static final int TTN_DECONNECTE
Déclaration de la classe CommunicationMQTT.
final Handler handler
Handler de communication entre l'activité et la communication MQTT.
Déclaration de la classe Ruche.
Déclaration de la classe RucheAdapter.
void onPause()
Méthode appelée après qu'une boîte de dialogue s'est affichée (on reprend sur un onResume()) ou avant...
void onDestroy()
Méthode appelée à la destruction de l'application (après onStop() et détruite par le système Android)...
void onResume()
Méthode appelée après onStart() ou après onPause()
static void setCallback(MqttCallbackExtended callback)
void recupererRuches()
Méthode pour récupérer les ruches.
void communiquerTTN(final String deviceID)
void rafraichir()
Méthode pour rafraîchir la liste des ruches connues.
void onStop()
Méthode appelée lorsque l'activité n'est plus visible.
RecyclerView.LayoutManager layoutManager
List< Ruche > ruches
la liste des ruches connues