Torc  0.1
torcthingspeaknotifier.cpp
Go to the documentation of this file.
1 /* Class TorcThingSpeakNotifier
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2016-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Torc
24 #include "torclogging.h"
25 #include "torchttprequest.h"
26 #include "torcnetwork.h"
27 #include "torcthingspeaknotifier.h"
28 
29 #define THINGSPEAK_UPDATE_URL QStringLiteral("https://api.thingspeak.com/update")
30 #define THINGSPEAK_MAX_ERRORS 5
31 #define THINGSPEAK_MAX_FIELDS 8
32 
34  : TorcIOTLogger(Details)
35 {
36  m_description = tr("ThingSpeak");
39 
40  SetValid(Initialise(Details));
41 }
42 
44 {
45  if (!Request)
46  return;
47 
48  // a successful update returns a non-zero update id - and zero on error.
49  QByteArray result = Request->ReadAll(1000).trimmed();
50  bool ok = false;
51  quint64 id = result.toLongLong(&ok);
52  if (ok)
53  {
54  if (id == 0)
55  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("%1 update failed for '%2'").arg(m_description, uniqueId));
56  }
57  else
58  {
59  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Failed to parse update id from %1 response (%2)").arg(m_description, uniqueId));
60  }
61 }
62 
64 {
65  QUrl url(THINGSPEAK_UPDATE_URL);
66  QUrlQuery query;
67  query.addQueryItem(QStringLiteral("api_key"), m_apiKey);
68  for (int i = 0; i < m_maxFields; i++)
69  {
70  if (!m_fieldValues[i].isEmpty())
71  query.addQueryItem(QStringLiteral("field%1").arg(i+1), m_fieldValues[i]);
72  // reset the fields
73  m_fieldValues[i] = QStringLiteral("");
74  }
75  url.setQuery(query);
76  QNetworkRequest qrequest(url);
77 
78  return new TorcNetworkRequest(qrequest, QNetworkAccessManager::GetOperation, 0, &m_networkAbort);
79 }
80 
82 {
83  TorcNotifier* Create(const QString &Type, const QVariantMap &Details) override
84  {
85  if (Type == QStringLiteral("thingspeak") && Details.contains(QStringLiteral("apikey")) && Details.contains(QStringLiteral("fields")))
86  return new TorcThingSpeakNotifier(Details);
87  return nullptr;
88  }
90 
91 
92 
#define THINGSPEAK_MAX_ERRORS
virtual bool Initialise(const QVariantMap &Details)
QString uniqueId
Definition: torcdevice.h:63
virtual void SetValid(bool Valid)
Definition: torcdevice.cpp:101
A wrapper around QNetworkRequest.
void ProcessRequest(TorcNetworkRequest *Request) override
TorcThingSpeakNotifierFactory TorcThingSpeakNotifierFactory
#define THINGSPEAK_MAX_FIELDS
TorcNetworkRequest * CreateRequest(void) override
QString m_fieldValues[32]
Definition: torciotlogger.h:51
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
QByteArray ReadAll(int Timeout)
QString m_apiKey
Definition: torciotlogger.h:42
TorcThingSpeakNotifier(const QVariantMap &Details)
#define THINGSPEAK_UPDATE_URL
QString m_description
Definition: torciotlogger.h:39