Skip to content

Commit

Permalink
sessionDuration and sessionGap are added
Browse files Browse the repository at this point in the history
  • Loading branch information
r-shf committed Nov 7, 2015
1 parent 79d613f commit 256ca8b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 7 deletions.
33 changes: 28 additions & 5 deletions filters/ehealth/EHealthSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 33,12 @@ Filter(name) {
}

void EHealthSender::init() {


mode = std::stoi(getProp("mode"));
httpHandler.setHost(getProp("host"));

jsonHandler.setMacID();
jsonHandler.setUserID(getProp("userid"));

jsonHandler.setType(1);

string message = jsonHandler.toJSON();
Expand All @@ -50,22 50,45 @@ void EHealthSender::init() {
sessionDuration = std::stoi(config.getValue("sessionduration"));
sessionGap = std::stoi(config.getValue("sessiongap"));

gapPeriod = false;

jsonHandler.setType(0);
chronometer.start();
}

void EHealthSender::run() {

long long timediff;
input->lock();
SensorData * inputData = input->get();
timediff = jsonHandler.insertData(inputData);
input->unlock();

if (mode==0 && gapPeriod) {
if (chronometer.now() < sessionGap) {
cout << "Sleeping..." << endl;
input->unlock();
return;
} else {
cout << "Waking up..." << endl;
chronometer.start();
gapPeriod = false;
}
}

timediff = jsonHandler.insertData(inputData);
input->unlock();

if (timediff >= sendingPeriod) {
cout << "Sending..." << endl;
string message = jsonHandler.toJSON();
std::string response = httpHandler.sendHTTP(message);
cout << response << endl;
}

if (mode==0 && chronometer.now() >= sessionDuration) {
gapPeriod = true;
chronometer.start();
cout << "Session duration is up..." << endl;
}

}

Expand Down
6 changes: 5 additions & 1 deletion filters/ehealth/EHealthSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 28,7 @@
#include <ehealthsensor/Sensor.h>
#include <filters/ehealth/tools/JSONHandler.h>
#include <filters/ehealth/tools/HTTPHandler.h>
#include <filters/ehealth/tools/Chronometer.h>

struct EHealthSender: public tmf::Filter {
private:
Expand All @@ -36,13 37,16 @@ struct EHealthSender: public tmf::Filter {

static tmf::FilterRegister<EHealthSender> reg;

std::string mode;
int mode;
int sendingPeriod;
int sessionDuration;
int sessionGap;
boolean gapPeriod;

JSONHandler jsonHandler;
HTTPHandler httpHandler;
Chronometer chronometer;

public:

EHealthSender(const std::string& name);
Expand Down
41 changes: 41 additions & 0 deletions filters/ehealth/tools/Chronometer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 1,41 @@
/*
*
* Tiny Multimedia Framework
* Copyright (C) 2014 Arash Shafiei
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "filters/ehealth/tools/Chronometer.h"


Chronometer::Chronometer() {
}

void Chronometer::start() {
startTime = std::chrono::high_resolution_clock::now();
}

long long Chronometer::now() {
endTime = std::chrono::high_resolution_clock::now();

auto duration = std::chrono::duration_cast<std::chrono::milliseconds>( endTime - startTime ).count();

return duration;

}

Chronometer::~Chronometer() {
}
40 changes: 40 additions & 0 deletions filters/ehealth/tools/Chronometer.h
Original file line number Diff line number Diff line change
@@ -0,0 1,40 @@
/*
*
* Tiny Multimedia Framework
* Copyright (C) 2014 Arash Shafiei
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


#ifndef CHRONOMETER_H
#define CHRONOMETER_H

#include <thread>
#include <chrono>

class Chronometer
{
private:
std::chrono::high_resolution_clock::time_point startTime, endTime;

public:
Chronometer();
void start();
long long now();
~Chronometer();
};

#endif // SCHEDULER_H
7 changes: 7 additions & 0 deletions filters/ehealth/tools/HTTPHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 32,12 @@ void HTTPHandler::setHost(std::string url) {
this->url = url;
}

size_t HTTPHandler::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}

std::string HTTPHandler::sendHTTP(std::string message)
{
std::string response;
Expand All @@ -46,6 52,7 @@ std::string HTTPHandler::sendHTTP(std::string message)
curl_easy_setopt(curl, CURLOPT_URL, this->url.c_str());
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

/* Perform the request, res will get the return code */
Expand Down
3 changes: 2 additions & 1 deletion filters/ehealth/tools/HTTPHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 32,8 @@ class HTTPHandler
std::string url;
CURL *curl;
CURLcode res;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp);

public:
HTTPHandler();
void setHost(std::string url);
Expand Down

0 comments on commit 256ca8b

Please sign in to comment.