forked from PiInTheSky/lora-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.c
124 lines (99 loc) · 3.92 KB
/
listener.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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;
}
}