Projet Bee-Honey't (Mobile)  0.2
BTS SNIR LaSalle Avignon 2020
MainActivity.java
Aller à la documentation de ce fichier.
1 package com.lasalle.beehoneyt;
2 
3 import androidx.appcompat.app.AppCompatActivity;
4 import androidx.recyclerview.widget.LinearLayoutManager;
5 import androidx.recyclerview.widget.RecyclerView;
6 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
7 
8 import android.os.Bundle;
9 import android.os.Handler;
10 import android.os.Message;
11 import android.util.Log;
12 
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;
17 
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21 
36 public class MainActivity extends AppCompatActivity
37 {
41  private static final String TAG = "MainActivity";
42  private static final int NB_RUCHES = 5;
43 
47  private List<Ruche> ruches = new ArrayList<>();
48  private boolean nouvelleRuche = false;
50 
54  private RecyclerView recyclerView; // la vue
55  private RecyclerView.Adapter adapter; // l'adaptateur
56  private RecyclerView.LayoutManager layoutManager; // le gestionnaire de mise en page
57  private SwipeRefreshLayout swipeRefreshLayout;
58 
65  @Override
66  protected void onCreate(Bundle savedInstanceState)
67  {
68  super.onCreate(savedInstanceState);
69  setContentView(R.layout.activity_main);
70  Log.d(TAG, "onCreate()");
71 
72  communicationMQTT = new CommunicationMQTT(getApplicationContext(), handler);
73 
75 
76  nouvelleRuche = true;
78  }
79 
83  @Override
84  protected void onStart()
85  {
86  super.onStart();
87  Log.i(TAG,"onStart()");
88  }
89 
93  @Override
94  protected void onResume()
95  {
96  super.onResume();
97  Log.i(TAG,"onResume()");
102  }
103 
107  @Override
108  protected void onPause()
109  {
110  super.onPause();
111  Log.i(TAG,"onPause()");
112 
113  }
114 
118  @Override
119  protected void onStop()
120  {
121  super.onStop();
122  Log.i(TAG,"onStop()");
123 
124  }
125 
129  @Override
130  protected void onDestroy()
131  {
132  super.onDestroy();
133  Log.i(TAG,"onDestroy()");
134 
135  }
136 
137  private void initialiserVue()
138  {
139  Log.d(TAG, "initialiserVue()");
140  swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeRefreshLayout);
141  swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
142  {
143  @Override
144  public void onRefresh()
145  {
146  recupererRuches();
147  }
148  });
149 
150  recyclerView = (RecyclerView) findViewById(R.id.listeRuche);
151  recyclerView.setHasFixedSize(true);
152 
153  layoutManager = new LinearLayoutManager(this);
154  recyclerView.setLayoutManager(layoutManager);
155 
156  adapter = new RucheAdapter(this, ruches);
157  recyclerView.setAdapter(adapter);
158  }
159 
165  private void recupererRuches()
166  {
167  Log.d(TAG, "recupererRuches()");
171  if(nouvelleRuche)
172  {
173  nouvelleRuche = false;
174 
175  // Pour les tests
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")
181  );
182 
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");
190 
191  ruches.clear();
192  for (int i = 0; i < listeRuches.size(); i++)
193  {
194  ruches.add(listeRuches.get(i));
195  }
196 
197  rafraichir();
198  }
199  }
200 
206  private void rafraichir()
207  {
208  swipeRefreshLayout.setRefreshing(false);
209  adapter.notifyDataSetChanged();
210  }
211 
212  private void communiquerTTN(final String deviceID)
213  {
214  Log.d(TAG, "communiquerTTN() deviceID = " + deviceID);
215  CommunicationMQTT.setCallback(new MqttCallbackExtended()
216  {
217  @Override
218  public void connectComplete(boolean b, String s)
219  {
220  }
221  @Override
222  public void connectionLost(Throwable throwable)
223  {
224  }
225  @Override
226  public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception
227  {
228  Log.d(TAG, "communiquerTTN() topic = " + topic);
229  Log.d(TAG, "communiquerTTN() mqttMessage = " + mqttMessage.toString());
233  // Juste pour tester (à retirer rapidement) !
234  JSONObject json = new JSONObject(mqttMessage.toString());
235  String deviceID = json.getString("dev_id");
236  for (int i = 0; i < ruches.size(); i++)
237  {
238  if(ruches.get(i).getDeviceID().equals(deviceID))
239  {
240  ruches.get(i).setInfos("Message reçu !");
241  }
242  else
243  {
244  ruches.get(i).setInfos("Simulateur");
245  }
246  }
247  // fin du test
248 
249  rafraichir();
250  }
251 
252  @Override
253  public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)
254  {
255  }
256  });
257  }
258 
259  private void abonnerRuches()
260  {
261  for (int i = 0; i < ruches.size(); i++)
262  {
263  ruches.get(i).souscrireTopic();
264  communiquerTTN(ruches.get(i).getDeviceID());
265  }
266  }
267 
271  final private Handler handler = new Handler()
272  {
273  @Override
274  public void handleMessage(Message msg)
275  {
276  super.handleMessage(msg);
277 
278  Bundle bundle = msg.getData();
279 
280  switch(bundle.getInt("etat"))
281  {
283  Log.d(TAG, "handleMessage() TTN connecté");
284  abonnerRuches();
285  break;
287  Log.d(TAG, "handleMessage() TTN déconnecté");
288  break;
290  Log.d(TAG, "handleMessage() TTN topic = " + bundle.getString("topic") + " message = " + bundle.getString("message"));
291  break;
292  }
293  }
294  };
295 }
void onStart()
Méthode appelée au démarrage après le onCreate() ou un restart après un onStop()
Déclaration de la classe MainActivity.
SwipeRefreshLayout swipeRefreshLayout
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&#39;activité MainActivity.
static final int NB_RUCHES
le nombre max. de ruches
Déclaration de la classe CommunicationMQTT.
final Handler handler
Handler de communication entre l&#39;activité et la communication MQTT.
Déclaration de la classe Ruche.
Definition: Ruche.java:16
Déclaration de la classe RucheAdapter.
void onPause()
Méthode appelée après qu&#39;une boîte de dialogue s&#39;est affichée (on reprend sur un onResume()) ou avant...
void onDestroy()
Méthode appelée à la destruction de l&#39;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&#39;activité n&#39;est plus visible.
RecyclerView.LayoutManager layoutManager
List< Ruche > ruches
la liste des ruches connues