29 #define MAX_BAD_REQUESTS 2 40 m_accessToken(QStringLiteral(
"")),
45 m_resetTimer.setSingleShot(
true);
49 if (Details.contains(QStringLiteral(
"accesstoken")))
51 m_accessToken = Details.value(QStringLiteral(
"accesstoken")).toString();
56 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"No Pushbullet access token specified - disabling"));
63 QMutexLocker locker(&
lock);
77 return QStringList() << tr(
"Pushbullet");
82 if (m_accessToken.isEmpty() || m_badRequestCount >=
MAX_BAD_REQUESTS || m_resetTimer.isActive())
84 LOG(VB_GENERAL, LOG_WARNING, QStringLiteral(
"Ignoring Pushbullet notify request"));
90 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Not sending Pushbullet notification. Network is not available"));
98 object.insert(QStringLiteral(
"title"), title);
99 object.insert(QStringLiteral(
"body"), body);
100 object.insert(QStringLiteral(
"type"),
"note");
101 QJsonDocument doc(
object);
102 QByteArray content = doc.toJson(QJsonDocument::Compact);
105 qrequest.setRawHeader(
"Access-token", m_accessToken.toUtf8());
106 qrequest.setHeader(QNetworkRequest::ContentTypeHeader,
"application/json");
107 qrequest.setHeader(QNetworkRequest::ContentLengthHeader, content.size());
113 QMutexLocker locker(&
lock);
114 m_requests.append(request);
123 QMutexLocker locker(&
lock);
124 if (!m_requests.contains(Request))
126 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Response to unknown Pushbullet request"));
133 quint64 resetin = 3600;
137 QByteArray resettime = Request->
GetHeader(
"X-Ratelimit-Reset");
140 if (!resettime.isEmpty())
142 quint64 reset = resettime.toLongLong(&ok);
145 quint64 now = (QDateTime::currentMSecsSinceEpoch() + 500) / 1000;
147 resetin = reset - now;
151 LOG(VB_GENERAL, LOG_WARNING, QStringLiteral(
"Pushbullet rate limit exceeded. Restarting in %1 seconds").arg(resetin));
155 QJsonDocument result = QJsonDocument::fromJson(Request->
GetBuffer());
157 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Pushbullet replied with:\r\n%1").arg(result.toJson().constData()));
160 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Disabling Pushbullet notifier '%1' - too many bad requests. Check your access token.").arg(
uniqueId));
161 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Will try again in %1 seconds").arg(resetin));
174 m_badRequestCount = 0;
178 m_requests.removeAll(Request);
183 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Re-enabling Pushbullet notifier '%1'").arg(
uniqueId));
184 m_badRequestCount = 0;
190 TorcNotifier* Create(
const QString &Type,
const QVariantMap &Details)
override 192 if (Type == QStringLiteral(
"pushbullet") && Details.contains(QStringLiteral(
"accesstoken")))
void ResetTimerTimeout(void)
virtual void SetValid(bool Valid)
TorcPushbulletNotifier(const QVariantMap &Details)
A wrapper around QNetworkRequest.
static bool GetAsynchronous(TorcNetworkRequest *Request, QObject *Parent)
Queue an asynchronous HTTP request.
static QString StatusToString(HTTPStatus Status)
~TorcPushbulletNotifier()
#define PUSHBULLET_PUSH_URL
static bool IsAvailable(void)
virtual bool DownRef(void)
QByteArray & GetBuffer(void)
void StartResetTimer(int MSeconds)
TorcPushbulletNotifierFactory TorcPushbulletNotifierFactory
QStringList GetDescription(void) override
#define NOTIFICATION_BODY
#define LOG(_MASK_, _LEVEL_, _STRING_)
void Notify(const QVariantMap &Notification) override
#define NOTIFICATION_TITLE
void RequestReady(TorcNetworkRequest *Request)
static void Cancel(TorcNetworkRequest *Request)
int GetStatus(void) const
QByteArray GetHeader(const QByteArray &Header) const
Return the value of the given header, if present.