listener: Add separate thread for listener telemetry upload loop.

pull/43/head
Phil Crump 2018-02-08 22:47:48 +00:00
rodzic 4ee4530705
commit 6aafb8ac84
3 zmienionych plików z 132 dodań i 94 usunięć

101
gateway.c
Wyświetl plik

@ -34,6 +34,7 @@
#include "gateway.h"
#include "config.h"
#include "gui.h"
#include "listener.h"
#define VERSION "V1.8.12"
bool run = TRUE;
@ -724,97 +725,6 @@ void ProcessCallingMessage(int Channel, char *Message)
}
}
size_t write_data( void *buffer, size_t size, size_t nmemb, void *userp )
{
return size * nmemb;
}
void UploadListenerTelemetry( char *callsign, float gps_lat, float gps_lon, char *antenna )
{
int time_epoch = ( int ) time( NULL );
if ( Config.EnableHabitat )
{
CURL *curl;
CURLcode res;
char PostFields[300];
char JsonData[200];
/* In windows, this will init the winsock stuff */
/* get a curl handle */
curl = curl_easy_init( );
if ( curl )
{
// So that the response to the curl POST doesn;'t mess up my finely crafted display!
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, write_data );
// Set the URL that is about to receive our POST
curl_easy_setopt( curl, CURLOPT_URL,
"http://habitat.habhub.org/transition/listener_telemetry" );
// Now specify the POST data
sprintf( JsonData, "{\"latitude\": %f, \"longitude\": %f}",
gps_lat, gps_lon );
sprintf( PostFields, "callsign=%s&time=%d&data=%s", callsign,
time_epoch, JsonData );
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, PostFields );
// Perform the request, res will get the return code
res = curl_easy_perform( curl );
// Check for errors
if ( res == CURLE_OK )
{
LogMessage( "Uploaded listener %s position %f,%f\n",
Config.Tracker, Config.latitude,
Config.longitude );
}
else
{
LogMessage( "curl_easy_perform() failed: %s\n",
curl_easy_strerror( res ) );
}
// always cleanup
curl_easy_cleanup( curl );
}
/* In windows, this will init the winsock stuff */
/* get a curl handle */
curl = curl_easy_init( );
if ( curl )
{
// So that the response to the curl POST doesn;'t mess up my finely crafted display!
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, write_data );
// Set the URL that is about to receive our POST
curl_easy_setopt( curl, CURLOPT_URL,
"http://habitat.habhub.org/transition/listener_information" );
// Now specify the POST data
sprintf( JsonData, "{\"radio\": \"%s\", \"antenna\": \"%s\"}",
"LoRa RFM98W", antenna );
sprintf( PostFields, "callsign=%s&time=%d&data=%s", Config.Tracker, time_epoch, JsonData );
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, PostFields );
// Perform the request, res will get the return code
res = curl_easy_perform( curl );
// Check for errors
if ( res != CURLE_OK )
{
LogMessage( "curl_easy_perform() failed: %s\n",
curl_easy_strerror( res ) );
}
// always cleanup
curl_easy_cleanup( curl );
}
}
}
void RemoveOldPayloads(void)
{
int i;
@ -2243,7 +2153,7 @@ int main( int argc, char **argv )
int ch;
int LoopPeriod, MSPerLoop;
int Channel;
pthread_t SSDVThread, FTPThread, NetworkThread, HabitatThread, ServerThread, TelnetThread;
pthread_t SSDVThread, FTPThread, NetworkThread, HabitatThread, ServerThread, TelnetThread, ListenerThread;
struct TServerInfo JSONInfo, TelnetInfo;
atexit(bye);
@ -2383,8 +2293,11 @@ int main( int argc, char **argv )
if ( ( Config.latitude > -90 ) && ( Config.longitude > -90 ) )
{
UploadListenerTelemetry( Config.Tracker, Config.latitude,
Config.longitude, Config.antenna );
if ( pthread_create( &ListenerThread, NULL, ListenerLoop, NULL ) )
{
fprintf( stderr, "Error creating Listener thread\n" );
return 1;
}
}
char buffer[300];

124
listener.c 100644
Wyświetl plik

@ -0,0 +1,124 @@
#include <stdio.h>
#include <wiringPi.h>
#include <curl/curl.h>
#include "global.h"
#include "gateway.h"
#define LISTENER_UPDATE_INTERVAL 30 // Minutes
#define LISTENER_LOOP_SLEEP 1000 // Milliseconds
size_t write_data( void *buffer, size_t size, size_t nmemb, void *userp )
{
return size * nmemb;
}
void UploadListenerTelemetry( char *callsign, time_t gps_time, float gps_lat, float gps_lon, char *antenna )
{
char time_string[20];
struct tm * time_info;
time_info = localtime (&gps_time);
strftime(time_string, sizeof(time_string), "%H:%M:%S", time_info);
if ( Config.EnableHabitat )
{
CURL *curl;
CURLcode res;
char PostFields[300];
char JsonData[200];
/* In windows, this will init the winsock stuff */
/* get a curl handle */
curl = curl_easy_init( );
if ( curl )
{
// So that the response to the curl POST doesn;'t mess up my finely crafted display!
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, write_data );
// Set the URL that is about to receive our POST
curl_easy_setopt( curl, CURLOPT_URL,
"http://habitat.habhub.org/transition/listener_telemetry" );
// Now specify the POST data
sprintf( JsonData, "{\"latitude\": %f, \"longitude\": %f}",
gps_lat, gps_lon );
sprintf( PostFields, "callsign=%s&time=%d&data=%s", callsign,
(int)gps_time, JsonData );
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, PostFields );
// Perform the request, res will get the return code
res = curl_easy_perform( curl );
// Check for errors
if ( res == CURLE_OK )
{
LogMessage( "Uploaded listener %s %s,%f,%f\n",
callsign, time_string, gps_lat, gps_lon );
}
else
{
LogMessage( "curl_easy_perform() failed: %s\n",
curl_easy_strerror( res ) );
}
// always cleanup
curl_easy_cleanup( curl );
}
/* In windows, this will init the winsock stuff */
/* get a curl handle */
curl = curl_easy_init( );
if ( curl )
{
// So that the response to the curl POST doesn;'t mess up my finely crafted display!
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, write_data );
// Set the URL that is about to receive our POST
curl_easy_setopt( curl, CURLOPT_URL,
"http://habitat.habhub.org/transition/listener_information" );
// Now specify the POST data
sprintf( JsonData, "{\"radio\": \"%s\", \"antenna\": \"%s\"}",
"LoRa RFM98W", antenna );
sprintf( PostFields, "callsign=%s&time=%d&data=%s", callsign, (int)gps_time, JsonData );
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, PostFields );
// Perform the request, res will get the return code
res = curl_easy_perform( curl );
// Check for errors
if ( res != CURLE_OK )
{
LogMessage( "curl_easy_perform() failed: %s\n",
curl_easy_strerror( res ) );
}
// always cleanup
curl_easy_cleanup( curl );
}
}
}
void *ListenerLoop(void *ptr)
{
(void) ptr;
uint32_t LoopPeriod = 0;
UploadListenerTelemetry( Config.Tracker, time(NULL), Config.latitude, Config.longitude, Config.antenna );
while (1)
{
if (LoopPeriod > LISTENER_UPDATE_INTERVAL*60*1000)
{
UploadListenerTelemetry( Config.Tracker, time(NULL), Config.latitude, Config.longitude, Config.antenna );
LoopPeriod = 0;
}
delay(LISTENER_LOOP_SLEEP);
LoopPeriod += LISTENER_LOOP_SLEEP;
}
}

1
listener.h 100644
Wyświetl plik

@ -0,0 +1 @@
void *ListenerLoop(void *ptr);