-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherbug.cpp
161 lines (139 loc) · 4.19 KB
/
weatherbug.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "weatherbug.h"
#include "time.h"
#include <signal.h>
#include "utils.h"
#include <sys/ioctl.h>
size_t WeatherBug::curl_write( void *buffer, size_t size, size_t nmemb, void* userp )
{
return size*nmemb;
}
WeatherBug::WeatherBug( Config cfg, WeatherData *_wd ):
WeatherSink( cfg, _wd, "WeatherBug" ),
failureCount( 0 ),
updateFrequency( 60 )
{
pthread_mutex_init( ¬ify, NULL );
uploadTime.updateTime();
startMain();
}
WeatherBug::~WeatherBug()
{
endMain();
}
void WeatherBug::newData()
{
pthread_mutex_unlock( ¬ify );
}
void WeatherBug::generatePacket()
{
struct tm gmt;
time_t t;
t = (time_t) wd->now->unixtime() ;
char st[32];
gmtime_r( &t, &gmt );
strftime( st, 31, "%Y-%m-%d+%H%%3A%M%%3A%S", &gmt );
snprintf(uploadString, WEATHERBUG_STRING_SIZE,
"%s?ID=%s&Key=%s&num=%s&dateutc=%s",
WEATHERBUG_RT_URL,
cfg.getString("weatherbug_id").c_str(),
cfg.getString("weatherbug_key").c_str(),
cfg.getString("weatherbug_num").c_str(),
st );
appendData( "&winddir=%.0f", WeatherData::windDirection );
appendData( "&windspeedmph=%.2f",WeatherData::averageWindSpeed );
appendData( "&windgustmph=%.2f", WeatherData::windGust );
appendData( "&humidity=%.0f", WeatherData::outsideHumidity );
appendData( "&tempf=%.2f", WeatherData::outsideTemp );
appendData( "&rainin=%.2f", WeatherData::rainRate, 100.0 );
appendData( "&dailyrainin=%.2f", WeatherData::dailyRain, 100.0 );
appendData( "&baromin=%.2f", WeatherData::SLP );
strnfcat( uploadString, WEATHERBUG_STRING_SIZE, "&softwaretype=%s%s",
"WxPro%20",
"DR1");
}
void WeatherBug::uploadPacket()
{
if ( cfg.getInteger("test_only") != 1 )
{
curl = curl_easy_init();
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, WeatherBug::curl_write);
curl_easy_setopt( curl, CURLOPT_TIMEOUT, 5 );
curl_easy_setopt( curl, CURLOPT_ERRORBUFFER, curlErrorBuf );
curl_easy_setopt( curl, CURLOPT_NOSIGNAL, 1 );
curl_easy_setopt( curl, CURLOPT_URL, uploadString );
dbg.printf( DEBUG, "CURL upload string: '%s'\n", uploadString );
res=curl_easy_perform(curl);
if ( (int)res != 0 )
{
failureCount++;
if ( failureCount < 10 )
{
dbg.printf(DEBUG, "Could not write to WeatherBug. Error: %s (%d)\n",
curlErrorBuf, res );
}
else
{
dbg.printf(CRIT, "Could not write to WeatherBug after 10 attempts. Error: %s (%d)\n",
curlErrorBuf, res );
failureCount = 0;
}
}
else
{
failureCount = 0;
}
if ( curl )
curl_easy_cleanup( curl );
}
else
{
dbg.printf(NOTICE,"Pretending to Uploading to WeatherBug\n");
dbg.printf(DEBUG, "uploadString: %s\n", uploadString );
}
uploadTime.updateTime();
}
void WeatherBug::main()
{
while( threadRunning )
{
pthread_mutex_lock( ¬ify );
if ( !threadRunning )
return;
float timeSinceLast = uploadTime.timeSinceLast();
dbg.printf(DEBUG, "time since last: %f\n", timeSinceLast);
if ( timeSinceLast <= 2 )
{
dbg.printf(DEBUG, "too soon since last update.. not doing anything\n");
continue;
}
if ( timeSinceLast > 2 && timeSinceLast < updateFrequency )
{
int sleepTime = (int)((updateFrequency - timeSinceLast ) );
dbg.printf(DEBUG, "too soon since last update.. sleeping for %d usec\n", sleepTime);
for ( int i = 0; i < sleepTime; i++)
{
sleep(1);
if ( !threadRunning )
return;
}
}
generatePacket();
uploadPacket();
}
}
bool WeatherBug::appendData( const char *format, WeatherData::PROPERTY property, float divider )
{
char buf[255];
if ( wd->hasData( property ) )
{
snprintf(buf, 255, format, wd->getValue( property, 0 ) / divider );
if ( strlen(buf) + strlen( uploadString ) >= WEATHERBUG_STRING_SIZE )
{
dbg.printf(CRIT, "Buffer size exceeded.\n");
return false;
}
strcat( uploadString, buf );
return true;
}
return false;
}