-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwsweather.cpp
164 lines (142 loc) · 4.34 KB
/
pwsweather.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
162
163
164
#include "pwsweather.h"
#include "time.h"
#include <signal.h>
#include "utils.h"
#include <sys/ioctl.h>
size_t PWSWeather::curl_write( void *buffer, size_t size, size_t nmemb, void* userp )
{
return size*nmemb;
}
PWSWeather::PWSWeather( Config cfg, WeatherData *_wd ):
WeatherSink( cfg, _wd, "PWSWeather" ),
failureCount( 0 ),
updateFrequency( 60 )
{
pthread_mutex_init( ¬ify, NULL );
uploadTime.updateTime();
startMain();
}
PWSWeather::~PWSWeather()
{
endMain();
}
void PWSWeather::newData()
{
pthread_mutex_unlock( ¬ify );
}
void PWSWeather::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, PWSWEATHER_STRING_SIZE,
"%s?ID=%s&PASSWORD=%s&dateutc=%s",
PWSWEATHER_RT_URL,
cfg.getString("pwsweather_id").c_str(),
cfg.getString("pwsweather_password").c_str(),
st );
appendData( "&winddir=%.0f", WeatherData::windDirection );
appendData( "&windspeedmph=%.2f",WeatherData::averageWindSpeed );
appendData( "&windgustmph=%.2f", WeatherData::windGust );
appendData( "&tempf=%.2f", WeatherData::outsideTemp );
appendData( "&rainin=%.2f", WeatherData::rainRate, 100.0 );
appendData( "&dailyrainin=%.2f", WeatherData::dailyRain, 100.0 );
appendData( "&baromin=%.2f", WeatherData::SLP );
appendData( "&dewptf=%.2f", WeatherData::dewPoint );
appendData( "&humidity=%.0f", WeatherData::outsideHumidity );
appendData( "&solarradiation=%.0f", WeatherData::solarRadiation );
appendData( "&UV=%.1f", WeatherData::UV );
strnfcat( uploadString, PWSWEATHER_STRING_SIZE,
"&softwaretype=%s%s&action=updateraw",
"WxPro%20",
"DR1");
}
void PWSWeather::uploadPacket()
{
if ( cfg.getInteger("test_only") != 1 )
{
curl = curl_easy_init();
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, PWSWeather::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 PWSWeather. Error: %s (%d)\n",
curlErrorBuf, res );
}
else
{
dbg.printf(CRIT, "Could not write to PWSWeather 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 PWSWeather\n");
dbg.printf(DEBUG, "uploadString: %s\n", uploadString );
}
uploadTime.updateTime();
}
void PWSWeather::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 PWSWeather::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 ) >= PWSWEATHER_STRING_SIZE )
{
dbg.printf(CRIT, "Buffer size exceeded.\n");
return false;
}
strcat( uploadString, buf );
return true;
}
return false;
}