Torc  0.1
torciotplotternotifier.cpp
Go to the documentation of this file.
1 /* Class TorcIoTPlotterNotifier
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2018
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 "torciotplotternotifier.h"
28 
29 #define IOTPLOTTER_UPDATE_URL QStringLiteral("http://iotplotter.com/api/v2/feed/") // NB insecure
30 #define IOTPLOTTER_MAX_ERRORS 5
31 #define IOTPLOTTER_MAX_FIELDS 8 // I can't actually see a max
32 
34  : TorcIOTLogger(Details),
35  m_feedId()
36 {
37  m_description = tr("IoTPlotter");
40 
41  // as well as an api key, IoTPlotter needs a feed id
42  if (Details.contains(QStringLiteral("feedid")))
43  m_feedId = Details.value(QStringLiteral("feedid")).toString().trimmed();
44 
45  if (m_feedId.isEmpty())
46  {
47  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("%1 logger has no feed id - disabling").arg(m_description));
48  return;
49  }
50 
51  SetValid(Initialise(Details));
52 }
53 
55 {
56  QUrl url(IOTPLOTTER_UPDATE_URL + m_feedId);
57  QNetworkRequest qrequest(url);
58  qrequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
59  qrequest.setRawHeader("api-key", m_apiKey.toLocal8Bit());
60 
61  QJsonObject data;
62  QJsonObject raw;
63  // NB from Qt 5.8 we can use currentSecsSinceEpoch
64  QString epoch = QStringLiteral("%1").arg(((quint64)QDateTime::currentMSecsSinceEpoch() / 1000));
65 
66  for (int i = 0; i < m_maxFields; i++)
67  {
68  if (!m_fieldValues[i].isEmpty())
69  {
70  if (m_reverseFields.contains(i))
71  {
72  QJsonObject pair;
73  pair.insert(QStringLiteral("value"), m_fieldValues[i].toLongLong());
74  pair.insert(QStringLiteral("epoch"), epoch.toLongLong());
75  QJsonArray values;
76  values.append(pair);
77  raw.insert(m_reverseFields.value(i).toLocal8Bit(), values);
78  }
79  }
80 
81  // reset the fields
82  m_fieldValues[i] = QStringLiteral("");
83  }
84 
85  data.insert(QStringLiteral("data"), raw);
86  QJsonDocument doc(data);
87  QByteArray serialiseddata = doc.toJson(QJsonDocument::Indented);
88  qrequest.setHeader(QNetworkRequest::ContentLengthHeader, serialiseddata.size());
89  return new TorcNetworkRequest(qrequest, serialiseddata, &m_networkAbort);
90 }
91 
93 {
94  // IoTPlotter returns a simple HTTP code
95  (void)Request;
96 }
97 
99 {
100  TorcNotifier* Create(const QString &Type, const QVariantMap &Details) override
101  {
102  if (Type == QStringLiteral("iotplotter") && Details.contains(QStringLiteral("apikey")) && Details.contains(QStringLiteral("fields")) &&
103  Details.contains(QStringLiteral("feedid")))
104  {
105  return new TorcIoTPlotterNotifier(Details);
106  }
107  return nullptr;
108  }
virtual bool Initialise(const QVariantMap &Details)
#define IOTPLOTTER_MAX_ERRORS
virtual void SetValid(bool Valid)
Definition: torcdevice.cpp:101
A wrapper around QNetworkRequest.
QMap< int, QString > m_reverseFields
Definition: torciotlogger.h:50
void ProcessRequest(TorcNetworkRequest *Request) override
#define IOTPLOTTER_MAX_FIELDS
QString m_fieldValues[32]
Definition: torciotlogger.h:51
TorcIoTPlotterNotifier(const QVariantMap &Details)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
QString m_apiKey
Definition: torciotlogger.h:42
TorcIoTPlotterNotifierFactory TorcIoTPlotterNotifierFactory
#define IOTPLOTTER_UPDATE_URL
TorcNetworkRequest * CreateRequest(void) override
QString m_description
Definition: torciotlogger.h:39