/
NotificationPushsafer.cpp
74 lines (63 loc) · 2.27 KB
/
NotificationPushsafer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "stdafx.h"
#include "NotificationPushsafer.h"
#include "../httpclient/HTTPClient.h"
#include "../main/Logger.h"
#include "../httpclient/UrlEncode.h"
#include "../httpclient/HTTPClient.h"
#include "../webserver/Base64.h"
CNotificationPushsafer::CNotificationPushsafer() : CNotificationBase(std::string("pushsafer"), OPTIONS_URL_SUBJECT | OPTIONS_URL_BODY | OPTIONS_URL_PARAMS)
{
SetupConfig(std::string("PushsaferEnabled"), &m_IsEnabled);
SetupConfig(std::string("PushsaferAPI"), _apikey);
SetupConfig(std::string("PushsaferImage"), _apiuser);
}
CNotificationPushsafer::~CNotificationPushsafer()
{
}
bool CNotificationPushsafer::SendMessageImplementation(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const int Priority,
const std::string &Sound,
const bool bFromNotification)
{
//send message to Pushsafer
std::string cSubject = (Subject == Text) ? "Domoticz" : Subject;
bool bRet;
std::string sResult;
std::stringstream sPostData;
std::vector<std::string> ExtraHeaders;
if (!_apiuser.empty()) {
std::vector<unsigned char> camimage;
std::vector<std::string> ExtraHeadersBinary;
if (HTTPClient::GETBinary(CURLEncode::URLDecode(_apiuser.c_str()), ExtraHeadersBinary, camimage, 10))
{
std::string base64ImageString(camimage.begin(), camimage.end());
base64ImageString = base64_encode((const unsigned char*)base64ImageString.c_str(), base64ImageString.size());
sPostData << "i=12&k=" << _apikey << "&t=" << cSubject << "&m=" << Text << "&p=data:image/jpeg;base64," << base64ImageString;
}
else
{
_log.Log(LOG_ERROR, "Pushsafer: can't download image > wrong url");
sPostData << "i=12&k=" << _apikey << "&t=" << cSubject << "&m=" << Text;
}
}
else
{
sPostData << "i=12&k=" << _apikey << "&t=" << cSubject << "&m=" << Text;
}
//Add the required Content Type
ExtraHeaders.push_back("Content-Type: application/x-www-form-urlencoded");
bRet = HTTPClient::POST("https://www.pushsafer.com/api",sPostData.str(),ExtraHeaders,sResult);
bool bSuccess = (sResult.find("\"success\":") != std::string::npos);
if (!bSuccess)
_log.Log(LOG_ERROR, "Pushsafer: %s", sResult.c_str());
return (bRet && bSuccess);
}
bool CNotificationPushsafer::IsConfigured()
{
return _apikey != "";
}