Projet Bee-Honey't (Mobile)  0.2
BTS SNIR LaSalle Avignon 2020
RucheActivity.java
Aller à la documentation de ce fichier.
1 package com.lasalle.beehoneyt;
2 
9 import androidx.appcompat.app.AppCompatActivity;
10 
11 import android.content.Intent;
12 import android.os.Bundle;
13 import android.util.Log;
14 import android.widget.TextView;
15 
16 import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
17 import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
18 import org.eclipse.paho.client.mqttv3.MqttMessage;
19 import org.json.JSONException;
20 import org.json.JSONObject;
21 
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25 import java.util.Iterator;
26 import java.util.Locale;
27 import java.util.TimeZone;
28 
34 public class RucheActivity extends AppCompatActivity
35 {
39  private static final String TAG = "RucheActivity";
40 
44  private Ruche ruche = null;
45  private TextView nom_ruche;
46  private TextView topic_ruche;
47  private TextView message_ruche;
48 
49  static private TextView poids_ruche;
50  static private TextView temperature_interieure_ruche;
51  static private TextView temperature_exterieure_ruche;
52  static private TextView humidite_interieure_ruche;
53  static private TextView humidite_exterieure_ruche;
54  static private TextView pression_ruche;
55  static private TextView ensoleillement_ruche;
56 
57  @Override
58  protected void onCreate(Bundle savedInstanceState)
59  {
60  super.onCreate(savedInstanceState);
61  setContentView(R.layout.activity_ruche);
62 
63  nom_ruche = (TextView) this.findViewById(R.id.nom_ruche);
64  topic_ruche = (TextView) this.findViewById(R.id.topic_ruche);
65  message_ruche = (TextView) this.findViewById(R.id.message_ruche);
66  poids_ruche = (TextView) this.findViewById(R.id.poids_ruche);
67  temperature_interieure_ruche = (TextView) this.findViewById(R.id.temperature_interieure_ruche);
68  temperature_exterieure_ruche = (TextView) this.findViewById(R.id.temperature_exterieure_ruche);
69  humidite_interieure_ruche = (TextView) this.findViewById(R.id.humidite_interieure_ruche);
70  humidite_exterieure_ruche = (TextView) this.findViewById(R.id.humidite_exterieure_ruche);
71  pression_ruche = (TextView) this.findViewById(R.id.pression_ruche);
72  ensoleillement_ruche = (TextView) this.findViewById(R.id.ensoleillement_ruche);
73 
74 
75 
76  Intent intent = getIntent();
77  // Attention : un nouvel objet Ruche est créé
78  ruche = (Ruche)intent.getSerializableExtra("Ruche");
79  if(ruche != null)
80  {
81  Log.d(TAG, "Ruche : " + ruche.getNom() + " " + ruche);
82  afficherInfo();
83  communiquerTTN(ruche.getDeviceID());
84  }
85  }
86 
91  private void afficherInfo()
92  {
93  afficherNom(ruche.getNom());
94  afficherTopic(ruche.getDeviceID());
95  }
96 
101  public void afficherNom(String message)
102  {
103  nom_ruche.setText("Nom : " + message);
104  }
105 
113  public void afficherTopic(String message)
114  {
115  topic_ruche.setText("Topic : " + message);
116  }
117 
122  public void afficherJSON(String message)
123  {
124  message_ruche.setText("Message : " + message);
125  }
126 
132  private void communiquerTTN(final String deviceID)
133  {
134  Log.d(TAG, "deviceID : " + deviceID);
135  CommunicationMQTT.setCallback(new MqttCallbackExtended()
136  {
137  @Override
138  public void connectComplete(boolean b, String s) {
139 
140  }
141  @Override
142  public void connectionLost(Throwable throwable) {
143 
144  }
145  @Override
146  public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception
147  {
148  Log.d(TAG, "Topic : " + topic);
149  Log.d(TAG, "Message : " + mqttMessage.toString());
150  if(estBonneRuche(deviceID, mqttMessage.toString()))
151  {
152  afficherJSON(mqttMessage.toString());
153  CommunicationMQTT.decoderMessage(mqttMessage.toString(),ruche);
154  String horodatage = CommunicationMQTT.extraireHorodatage(mqttMessage.toString());
155  String horodatageFormate = formaterHorodatge(horodatage);
156  Log.d(TAG, "Horodatage formaté : " + horodatageFormate);
157  }
158 
159  }
160 
161  @Override
162  public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)
163  {
164  }
165  });
166  }
167 
174  private boolean estBonneRuche(String deviceID,String message)
175  {
176  try
177  {
178  JSONObject json = null;
179  Iterator<String> it = null;
180 
181  json = new JSONObject(message);
182  int port = json.getInt("port");
183 
184  it = json.keys();
185  while (it.hasNext())
186  {
187  String cle = it.next();
188  Log.i(TAG, "clé = " + cle);
189  Log.i(TAG, "valeur = " + json.getString(cle));
190  //Log.i(TAG, "type = " + json.get(cle).getClass());
191 
192  if (cle.equals("dev_id"))
193  {
194  if(json.getString(cle).equals(deviceID)) {
195  return true;
196  }
197  }
198  }
199  return false;
200  }
201  catch (JSONException e)
202  {
203  e.printStackTrace();
204  return false;
205  }
206  }
207 
213  private String formaterHorodatge(String horodatage)
214  {
218  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.FRENCH);
219  df.setTimeZone(TimeZone.getTimeZone("UTC"));
220  Date date = null;
221  String horodatageFormate = horodatage;
222  try
223  {
224  date = df.parse(horodatage);
225  df.setTimeZone(TimeZone.getDefault());
226  horodatageFormate = df.format(date);
227  }
228  catch (ParseException e)
229  {
230  e.printStackTrace();
231  }
232 
233  return horodatageFormate;
234  }
235 
240  static public void afficherPoids(int message)
241  {
242  poids_ruche.setText("Poids : " + message + "kg");
243  }
244  public void setPoids(int poids)
245  {
246  ruche.setPoids(poids);
247  }
252  static public void afficherTemperatureInterieure(String message)
253  {
254  temperature_interieure_ruche.setText("Temperature interieure : " + message + " °C");
255  }
260  static public void afficherTemperatureExterieure(String message)
261  {
262  temperature_exterieure_ruche.setText("Temperature exterieure : " + message + " °C");
263  }
268  static public void afficherHumiditeInterieure(String message)
269  {
270  humidite_interieure_ruche.setText("Humidité interieure : " + message + " %");
271  }
276  static public void afficherHumiditerExterieure(String message)
277  {
278  humidite_exterieure_ruche.setText("Humidité exterieure : " + message+ " %");
279  }
284  static public void afficherPression(String message)
285  {
286  pression_ruche.setText("Pression atmosphérique : " + message + " hPa");
287  }
292  static public void afficherEnsoleillement(String message)
293  {
294  ensoleillement_ruche.setText("Ensoleillement : " + message + " lux");
295  }
296 
297 }
298 
static void afficherHumiditerExterieure(String message)
affichie l&#39;humiditer exterieure dans le layout
void afficherTopic(String message)
afficher le topic
static void afficherTemperatureExterieure(String message)
affichie la Temperature exterieure dans le layout
static void afficherTemperatureInterieure(String message)
affichie la Temperature interieure dans le layout
static void afficherPoids(int message)
affichie le poid dans le layout
String getDeviceID()
Accesseur get du device id de la ruche.
Definition: Ruche.java:105
static void afficherEnsoleillement(String message)
affichie l&#39;Ensoleillement dans le layout
void afficherInfo()
afficher les information d&#39;un ruche
static void decoderMessage(String message, Ruche ruche)
decode le message recu
Déclaration de la classe CommunicationMQTT.
static void afficherHumiditeInterieure(String message)
affichie l&#39;humiditer interieure dans le layout
void onCreate(Bundle savedInstanceState)
Déclaration de la classe Ruche.
Definition: Ruche.java:16
String getNom()
Accesseur get du nom de la ruche.
Definition: Ruche.java:94
static void afficherPression(String message)
affichie la pression dans le layout
void communiquerTTN(final String deviceID)
paramete la communication avec le ttn
String formaterHorodatge(String horodatage)
formate l&#39;horodatage
static void setCallback(MqttCallbackExtended callback)
static String extraireHorodatage(String message)
extrait l&#39;horodatage
void afficherJSON(String message)
affiche le dernier message JSON recu
boolean estBonneRuche(String deviceID, String message)
verifie si le json reçu correspond a la ruche souhaité
void afficherNom(String message)
afficher le nom d&#39;un ruche
void setPoids(int Poids)
Mutateur set du poids de la ruche.
Definition: Ruche.java:82
Déclaration de la classe RucheActivity.