Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Verstraeten committed Mar 31, 2018
2 parents fd8e3ff + 5718f70 commit 3e5bffc
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/io.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@
<password type="text"></password>
<throttler type="number">0</throttler>
</MQTT>
<Pushbullet>
<url type="text">https://api.pushbullet.com</url>
<token type="text">o.mC5LPVCvPCphtSsEgWZQpFM86w9ciWQ3</token>
<throttler type="number">10</throttler>
</Pushbullet>

</ios>
56 changes: 56 additions & 0 deletions include/kerberos/machinery/io/IoPushbullet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Class: IoPushbullet
// Description: A pushbullet class .
// Created: 13/11/2017
// Author: Gianrico D'Angelis
// Mail: [email protected]
//
//
/////////////////////////////////////////////////////

#ifndef __IoPushbullet_H_INCLUDED__ // if IoPushbullet.h hasn't been included yet...
#define __IoPushbullet_H_INCLUDED__ // #define this so the compiler knows it has been included

#include "machinery/io/Io.h"
#include "document.h"
#include "writer.h"
#include "restclient-cpp/connection.h"
#include "restclient-cpp/restclient.h"
#include "Throttler.h"

namespace kerberos
{
char PushbulletName[] = "Pushbullet";
class IoPushbullet : public IoCreator<PushbulletName, IoPushbullet>
{
private:
std::string m_url;
std::string m_instanceName;
std::string m_pbToken;
RestClient::Connection * pushbulletConnection;
Throttler throttle;

public:
IoPushbullet(){};
virtual ~IoPushbullet()
{
delete pushbulletConnection;
};
void setup(const StringMap & settings);
void fire(JSON & data){};
void disableCapture(){};

void setUrl(std::string url){m_url=url;};
const char * getUrl(){return m_url.c_str();};
void setToken(std::string token){m_pbToken=token;};
const char * getToken(){return m_pbToken.c_str();};
void setInstanceName(std::string instanceName){m_instanceName=instanceName;};
std::string getInstanceName(){return m_instanceName;};

bool save(Image & image){ return true; };
bool save(Image & image, JSON & data);

bool pbUploadImage(std::string tmpFile, std::string upUrl);
};
}
#endif
1 change: 1 addition & 0 deletions src/kerberos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
machinery/io/IoWebhook.cpp
machinery/io/IoScript.cpp
machinery/io/IoMQTT.cpp
machinery/io/IoPushbullet.cpp
)

set(KERBEROS_DEPENDENCIES ${KERBEROS_DEPENDENCIES} opencv restclient mosquitto)
Expand Down
159 changes: 159 additions & 0 deletions src/kerberos/machinery/io/IoPushbullet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#include "machinery/io/IoPushbullet.h"

namespace kerberos
{
void IoPushbullet::setup(const StringMap & settings)
{
Io::setup(settings);

// --------------------------
// Get name from instance

std::string instanceName = settings.at("name");
setInstanceName(instanceName);

// -------
// Get url

setUrl(settings.at("ios.Pushbullet.url").c_str());

//------
//set pushbullet token

setToken((settings.at("ios.Pushbullet.token").c_str()));

// -------------
// Set throttler

throttle.setRate(std::stoi(settings.at("ios.Pushbullet.throttler")));

// ----------------------------
// Initialize connection object

pushbulletConnection = new RestClient::Connection(m_url);
pushbulletConnection->SetTimeout(5); // set connection timeout to 5s
RestClient::HeaderFields headers;
headers["Content-Type"] = "application/json";
pushbulletConnection->SetHeaders(headers);
pushbulletConnection->AppendHeader("Access-Token",m_pbToken);
}

bool IoPushbullet::save(Image & image, JSON & data)
{
if(throttle.canExecute())
{
// ---------------------------------------
// Attach additional fields to JSON object

JSON dataCopy,pbResp;
JSON::AllocatorType& allocator = dataCopy.GetAllocator();
dataCopy.CopyFrom(data, allocator);

JSONValue instanceName;
instanceName.SetString(getInstanceName().c_str(), allocator);
dataCopy.AddMember("instanceName", instanceName, allocator);

// -----------------------------
// Convert JSON object to string

rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
dataCopy.Accept(writer);


std::string upUrl ;
std::string fileUrl ;
RestClient::Response r;

/*
* Step 1 : save file to tmp and provide path in a string var
*/
std::string tmpFile = "/tmp/detection.jpg";
if(image.save(tmpFile))
{

/*
* Step 2 : create push for file
*/

r = pushbulletConnection->post("/v2/upload-request", "{\"file_name\":\"detection.jpg\",\"file_type\":\"image/jpeg\"}");

if(r.code == 200){
pbResp.Parse(r.body.c_str());
upUrl = pbResp["upload_url"].GetString();
fileUrl = pbResp["file_url"].GetString();
BINFO << "IoPushbullet: response to upload request " + r.body;

/*
* Step 3 : upload file to pushbullet
*/
if (pbUploadImage(tmpFile, upUrl)) {

/*
* Step 4 : create text push for detection
*/
r = pushbulletConnection->post("/v2/pushes", "{\"type\":\"file\",\"file_url\":\""+ fileUrl +"\"}");
if(r.code==200)
BINFO << "IoPushbullet: response to push file request " + r.body ;
}

}
}
// -------------------
// Send a message to pushbullet

r = pushbulletConnection->post("/v2/pushes", "{\"body\":\"Motion detected\",\"title\":\"Kios\",\"type\":\"note\"}");

if(r.code == 200)
{
BINFO << "IoPushbullet: response to post to pushbullet " + r.body;
return true;
}

return false;
}
return true;
}

bool IoPushbullet::pbUploadImage(std::string tmpFile, std::string upUrl) {

CURL *curl;
CURLcode res;

struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;

curl_global_init(CURL_GLOBAL_ALL);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "file",
CURLFORM_FILENAME, "detection.jpg",
CURLFORM_FILE, tmpFile.c_str(),
CURLFORM_CONTENTTYPE, "image/jpeg",
CURLFORM_END);

curl = curl_easy_init();

if (curl) {

curl_easy_setopt(curl, CURLOPT_URL, (upUrl).c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

res = curl_easy_perform(curl);

curl_easy_cleanup(curl);
curl_formfree(formpost);

if (res != CURLE_OK){
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
return false;
}
}
else
return false;

return true;
}

}

0 comments on commit 3e5bffc

Please sign in to comment.