From 21faa3c2a0123caa5510212fee0f74001c49d6bf Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Tue, 3 Jun 2014 21:56:42 -0400 Subject: [PATCH] Added data upload thread --- src/CMakeLists.txt | 1 + src/daemon.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 65b0f0e..228cc38 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -77,6 +77,7 @@ TARGET_LINK_LIBRARIES(alprd openalpr support uuid + curl ${OpenCV_LIBS} ${Tesseract_LIBS} ) diff --git a/src/daemon.cpp b/src/daemon.cpp index 6d4f1b5..89381d0 100644 --- a/src/daemon.cpp +++ b/src/daemon.cpp @@ -10,14 +10,15 @@ #include "support/tinythread.h" #include "videobuffer.h" #include "uuid.h" - +#include // prototypes void streamRecognitionThread(void* arg); bool writeToQueue(std::string jsonResult); +bool uploadPost(std::string url, std::string data); -struct ThreadData +struct CaptureThreadData { std::string stream_url; int camera_id; @@ -27,6 +28,11 @@ struct ThreadData std::string output_image_folder; }; +struct UploadThreadData +{ + std::string upload_data; +}; + bool daemon_active; int main( int argc, const char** argv ) @@ -119,7 +125,7 @@ int main( int argc, const char** argv ) for (int i = 0; i < stream_urls.size(); i++) { - ThreadData* tdata = new ThreadData(); + CaptureThreadData* tdata = new CaptureThreadData(); tdata->stream_url = stream_urls[i]; tdata->camera_id = i + 1; tdata->config_file = configFile; @@ -140,7 +146,7 @@ int main( int argc, const char** argv ) void streamRecognitionThread(void* arg) { - ThreadData* tdata = (ThreadData*) arg; + CaptureThreadData* tdata = (CaptureThreadData*) arg; std::cout << "country: " << tdata->country_code << " -- config file: " << tdata->config_file << std::endl; std::cout << "Stream " << tdata->camera_id << ": " << tdata->stream_url << std::endl; @@ -225,7 +231,6 @@ bool writeToQueue(std::string jsonResult) Beanstalk::Client client("127.0.0.1", 11300); client.use("alpr"); - //client.watch("test"); int id = client.put(jsonResult); @@ -236,3 +241,76 @@ bool writeToQueue(std::string jsonResult) return true; } + + + +void dataUploadThread(void* arg) +{ + + UploadThreadData* tdata = (UploadThreadData*) arg; + + Beanstalk::Client client("127.0.0.1", 11300); + client.use("alpr"); + + client.watch("alpr"); + + while(daemon_active) + { + Beanstalk::Job job; + + client.reserve(job); + + if (job.id() > 0) + { + if (uploadPost("http://localhost/", tdata->upload_data)) + { + client.del(job.id()); + } + else + { + client.release(job); + } + } + + usleep(10000); + } + +} + + +bool uploadPost(std::string url, std::string data) +{ + bool success = true; + CURL *curl; + CURLcode res; + + /* In windows, this will init the winsock stuff */ + curl_global_init(CURL_GLOBAL_ALL); + + /* get a curl handle */ + curl = curl_easy_init(); + if(curl) { + /* First set the URL that is about to receive our POST. This URL can + just as well be a https:// URL if that is what should receive the + data. */ + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + /* Now specify the POST data */ + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + /* Check for errors */ + if(res != CURLE_OK) + { + success = false; + } + + /* always cleanup */ + curl_easy_cleanup(curl); + } + curl_global_cleanup(); + + return success; + + +} \ No newline at end of file