Projet Bee-Honey't  0.2
BTS SNIR LaSalle Avignon 2020
communication.cpp
Aller à la documentation de ce fichier.
1 #include "communication.h"
2 #include "ihm.h"
3 
16 Communication::Communication(QObject *parent) : QObject(parent), client(new QMqttClient(this))
17 {
18  qDebug() << Q_FUNC_INFO;
19  connect(client, SIGNAL(stateChanged(ClientState)), this, SLOT(changerEtatConnexion()));
20  connect(client, SIGNAL(messageReceived(const QByteArray &, const QMqttTopicName &)), this, SLOT(decoderJson(const QByteArray &)));
21 }
22 
27 {
28  if(client->state() == QMqttClient::Connected)
29  {
30  client->disconnectFromHost();
31  }
32  qDebug() << Q_FUNC_INFO;
33 }
34 
43 void Communication::connecterTTN(QString hostname, int port, QString username, QString password)
44 {
45  qDebug() << Q_FUNC_INFO << hostname << port << username << password;
46  if(client->state() == QMqttClient::Disconnected)
47  {
48  client->setHostname(hostname);
49  client->setPort(port);
50  client->setUsername(username);
51  client->setPassword(password);
52  client->connectToHost();
53  }
54  else if(client->state() == QMqttClient::Connected)
55  {
56  client->disconnectFromHost();
57  }
58 }
59 
65 void Communication::souscrireTopic(QString topic)
66 {
67  if(client->state() == QMqttClient::Connected)
68  {
69  subscription = client->subscribe(QMqttTopicFilter(topic));
70  qDebug() << Q_FUNC_INFO << topic;
71  }
72 }
73 
79 void Communication::desabonnerTopic(QString topic)
80 {
81  if(client->state() == QMqttClient::Connected)
82  {
83  client->unsubscribe(topic);
84  qDebug() << Q_FUNC_INFO << topic;
85  }
86 }
87 
93 void Communication::decoderJson(const QByteArray &json)
94 {
95  QJsonDocument documentJSON = QJsonDocument::fromJson(json);
96  QString nomDeLaRuche;
97  QString horodatage;
98 
99  qDebug() << Q_FUNC_INFO << json;
100  if(!documentJSON.isNull())
101  {
102  QJsonObject objetJSON = documentJSON.object();
103  QStringList listeCles = objetJSON.keys();
104  // les clés sont triés alphabétiquement
105  for(int i = 0; i < listeCles.count()-1; i++)
106  {
107  if(listeCles.at(i) == "dev_id")
108  {
109  nomDeLaRuche = extraireDeviceID(objetJSON, listeCles, i);
110  }
111  if(listeCles.at(i) == "metadata")
112  {
113  horodatage = formaterHorodatage(extraireHorodatage(objetJSON[listeCles.at(i)].toObject()));
114  }
115  if(listeCles.at(i) == "payload_fields")
116  {
117  QJsonObject objet = objetJSON[listeCles.at(i)].toObject();
118 
119  if(objet.contains("temperatureInt"))
120  {
121  emit nouvelleValeurTemperatureInterieure(nomDeLaRuche, extraireTemperatureInterieure(objet), horodatage);
122  }
123  if(objet.contains("temperatureExt"))
124  {
125  emit nouvelleValeurTemperatureExterieure(nomDeLaRuche, extraireTemperatureExterieure(objet), horodatage);
126  }
127  if(objet.contains("humiditeInt"))
128  {
129  emit nouvelleValeurHumiditeInterieure(nomDeLaRuche, extraireHumiditeInterieure(objet), horodatage);
130  }
131  if(objet.contains("humiditeExt"))
132  {
133  emit nouvelleValeurHumiditeExterieure(nomDeLaRuche, extraireHumiditeExterieure(objet), horodatage);
134  }
135  if(objet.contains("ensoleillement"))
136  {
137  emit nouvelleValeurEnsoleillement(nomDeLaRuche, extraireEnsoleillement(objet), horodatage);
138  }
139  if(objet.contains("pression"))
140  {
141  emit nouvelleValeurPression(nomDeLaRuche, extrairePression(objet), horodatage);
142  }
143  if(objet.contains("poids"))
144  {
145  emit nouvelleValeurPoids(nomDeLaRuche, extrairePoids(objet), horodatage);
146  }
147  if(objet.contains("charge"))
148  {
149  emit nouvelleValeurCharge(nomDeLaRuche, extraireCharge(objet), horodatage);
150  }
151  }
152  }
153  }
154 }
155 
162 QString Communication::extraireHorodatage(QJsonObject objetJSON)
163 {
164  return objetJSON.value(QString("time")).toString();
165 }
166 
175 QString Communication::extraireDeviceID(QJsonObject objetJSON, QStringList listeCles, int position)
176 {
177  return objetJSON[listeCles.at(position)].toString();
178 }
179 
186 double Communication::extraireTemperatureInterieure(QJsonObject objetJSON)
187 {
188  return objetJSON.value(QString("temperatureInt")).toDouble();
189 }
190 
197 double Communication::extraireTemperatureExterieure(QJsonObject objetJSON)
198 {
199  return objetJSON.value(QString("temperatureExt")).toDouble();
200 }
201 
208 double Communication::extraireHumiditeInterieure(QJsonObject objetJSON)
209 {
210  return objetJSON.value(QString("humiditeInt")).toDouble();
211 }
212 
219 double Communication::extraireHumiditeExterieure(QJsonObject objetJSON)
220 {
221  return objetJSON.value(QString("humiditeExt")).toDouble();
222 }
229 int Communication::extraireEnsoleillement(QJsonObject objetJSON)
230 {
231  return objetJSON.value(QString("ensoleillement")).toInt();
232 }
233 
240 int Communication::extrairePression(QJsonObject objetJSON)
241 {
242  return objetJSON.value(QString("pression")).toInt();
243 }
244 
251 double Communication::extrairePoids(QJsonObject objetJSON)
252 {
253  return objetJSON.value(QString("poids")).toDouble();
254 }
255 
262 int Communication::extraireCharge(QJsonObject objetJSON)
263 {
264  return objetJSON.value(QString("charge")).toInt();
265 }
266 
273 QString Communication::formaterHorodatage(QString horodatageBrut)
274 {
275  QDateTime horodatage = QDateTime::fromString(horodatageBrut, Qt::ISODate).toLocalTime();
276  return horodatage.toString("dd/MM/yyyy HH:mm:ss");
277 }
278 
283 {
284  QString message;
285  switch(client->state())
286  {
287  case 0:
288  message = "Déconnecté";
289  break;
290  case 1:
291  message = "En cours de connexion";
292  break;
293  case 2:
294  message = "Connecté";
295  break;
296  }
297  qDebug()<< Q_FUNC_INFO << client->state() << message;
298  emit nouvelEtatConnexion(client->state());
299 }
Déclaration de la classe Communication.
int extraireEnsoleillement(QJsonObject objetJSON)
Méthode pour extraire l&#39;ensoleillement de l&#39;objet JSON.
void nouvelleValeurPression(QString nomDeLaRuche, int pression, QString horodatage)
double extraireTemperatureInterieure(QJsonObject objetJSON)
Méthode pour extraire la température intérieure de l&#39;objet JSON.
void changerEtatConnexion()
Méthode pour notifier un changement d&#39;état de la connexion TTN.
double extraireHumiditeExterieure(QJsonObject objetJSON)
Méthode pour extraire l&#39;humidité extérieure de l&#39;objet JSON.
void nouvelleValeurTemperatureExterieure(QString nomDeLaRuche, double temperatureExterieure, QString horodatage)
~Communication()
Destructeur de la classe Communication.
void nouvelleValeurCharge(QString nomDeLaRuche, int charge, QString horodatage)
QString extraireDeviceID(QJsonObject objetJSON, QStringList listeCles, int position)
Méthode pour extraire le device ID de l&#39;objet JSON.
void nouvelleValeurPoids(QString nomDeLaRuche, double poids, QString horodatage)
void desabonnerTopic(QString topic)
Méthode pour se désabonner d&#39;un topic TTN.
int extraireCharge(QJsonObject objetJSON)
Méthode pour extraire la charge de la batterie le l&#39;objet JSON.
QMqttSubscription * subscription
Definition: communication.h:38
void souscrireTopic(QString topic)
Méthode pour s&#39;abonner à un topic TTN.
QString formaterHorodatage(QString horodatageBrut)
Méthode pour mettre dans le bon format l&#39;horodatage reçu.
void decoderJson(const QByteArray &json)
Méthode pour décoder le JSON reçu.
void nouvelleValeurHumiditeExterieure(QString nomDeLaRuche, double humiditeExterieure, QString horodatage)
int extrairePression(QJsonObject objetJSON)
Méthode pour extraire la pression de l&#39;objet JSON.
QMqttClient * client
Definition: communication.h:37
void connecterTTN(QString hostname, int port, QString username, QString password)
Méthode pour se connecter à TTN.
Déclaration de la classe Ihm.
void nouvelleValeurEnsoleillement(QString nomDeLaRuche, int ensoleillement, QString horodatage)
QString extraireHorodatage(QJsonObject objetJSON)
Méthode pour extraire le temps de l&#39;objet JSON.
double extrairePoids(QJsonObject objetJSON)
Méthode pour extraire le poids le l&#39;objet JSON.
void nouvelleValeurTemperatureInterieure(QString nomDeLaRuche, double temperatureInterieure, QString horodatage)
double extraireHumiditeInterieure(QJsonObject objetJSON)
Méthode pour extraire l&#39;humidité intérieure de l&#39;objet JSON.
double extraireTemperatureExterieure(QJsonObject objetJSON)
Méthode pour extraire la température extérieure de l&#39;objet JSON.
Communication(QObject *parent=nullptr)
Constructeur de la classe Communication.
void nouvelleValeurHumiditeInterieure(QString nomDeLaRuche, double humiditeInterieure, QString horodatage)
void nouvelEtatConnexion(int etat)