8 |
#include "Exceptions.h" |
#include "Exceptions.h" |
9 |
#include "HttpClient.h" |
#include "HttpClient.h" |
10 |
#include <sstream> |
#include <sstream> |
11 |
|
#include <stdexcept> |
12 |
|
|
13 |
using namespace std; |
using namespace std; |
14 |
|
|
15 |
UrlTriggerPlugin::UrlTriggerPlugin(string cmd, string url) |
UrlTriggerPlugin::UrlTriggerPlugin(map<string,string> args) |
16 |
: Plugin(cmd, string("URL Trigger:")+url ), |
: Plugin("", "" ) |
|
command(cmd), URL(url) |
|
17 |
{ |
{ |
18 |
|
|
19 |
|
_command = args["trigger"]; |
20 |
|
_URL = args["url"];; |
21 |
|
_description = string("UrlTrigger(") + _command + ") > " + _URL; |
22 |
|
|
23 |
|
|
24 |
|
if (_command == "" || _URL == "") |
25 |
|
throw std::runtime_error("UrlTriggerPlugin usage: plugin = urltrigger trigger=<triggerword> url=<url>"); |
26 |
|
|
27 |
} |
} |
28 |
|
|
29 |
void UrlTriggerPlugin::Execute(ISmsTransceiver& modem, SMS& sms) |
void UrlTriggerPlugin::Execute(ISmsTransceiver& modem, SMS& sms) |
30 |
{ |
{ |
31 |
ostringstream request; |
ostringstream request; |
32 |
request << URL << "?"; |
request << _URL << "?"; |
33 |
request << "sender=" << sms.GetSender() << "&"; |
request << "sender=" << sms.GetSender() << "&"; |
34 |
request << "data=" << HttpClient::UrlEncode(sms.ExtractParameters()); |
request << "data=" << HttpClient::UrlEncode(sms.ExtractParameters()); |
35 |
|
|
36 |
string reply; |
string reply; |
37 |
try { |
try |
38 |
|
{ |
39 |
reply = HttpClient::GetString( request.str() ); |
reply = HttpClient::GetString( request.str() ); |
40 |
reply = Util::str_replace(reply, "\r"); //remove carriage return |
reply = Util::str_replace(reply, "\r"); //remove carriage return |
41 |
if (reply == "") |
if (reply == "") |
42 |
reply = "Empty reply from server !?!"; |
reply = "Empty reply from server !?!"; |
43 |
} catch (httpexception& e) { |
} |
44 |
|
catch (httpexception& e) |
45 |
|
{ |
46 |
reply = "Error: Could not execute command"; |
reply = "Error: Could not execute command"; |
47 |
Logger::logMessage(string("UrlTrigger failed: ") + command + ">" + e.what() ); |
Logger::logMessage(string("UrlTrigger failed: ") + _command + ">" + e.what() ); |
48 |
} |
} |
49 |
|
|
50 |
modem.SendSms(sms.GetSender(), reply, true); |
modem.SendSms(sms.GetSender(), reply, true); |