-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwu.cpp
144 lines (127 loc) · 3.37 KB
/
wu.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
#include "wu.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <netdb.h>
#define PORT_HTTP 80
#define WU_SERVER "weatherstation.wunderground.com"
#define WU_URL_BASE "/weatherstation/updateweatherstation.php"
#define USER_AGENT "Raspberry Pi"
/*****************************************************************************/
void Add(const unsigned int nIP, unsigned int *&pnIPs, unsigned short &nIPCount)
{
bool bFound = false;
unsigned short n;
for(n = 0; n < nIPCount; ++n)
{
if(pnIPs[n] == nIP)
{
bFound = true;
break;
}
}
if(!bFound)
{
unsigned int *pTmp = (unsigned int*)malloc((nIPCount + 1) * sizeof(unsigned int));
if(pTmp)
{
for(n = 0; n < nIPCount; ++n)
pTmp[n] = pnIPs[n];
pTmp[nIPCount] = nIP;
if(pnIPs)
free(pnIPs);
pnIPs = pTmp;
++nIPCount;
}
}
}
/*****************************************************************************/
void GetServerIPAddresses(const char *szServer, unsigned int *&pnIPs, unsigned short &nIPCount)
{
struct addrinfo aiHints = {0}, *paiResults = nullptr;
if(getaddrinfo(szServer, nullptr, &aiHints, &paiResults) == 0)
{
struct addrinfo *paiScan = paiResults;
while(paiScan)
{
Add(((struct sockaddr_in*)paiScan->ai_addr)->sin_addr.s_addr, pnIPs, nIPCount);
paiScan = paiScan->ai_next;
}
freeaddrinfo(paiResults);
}
}
/*****************************************************************************/
bool UpdateWU(const time_t nRaw, const CWeatherData &data)
{
bool bSuccess = false;
unsigned int *pnIPs = nullptr;
unsigned short nIPCount = 0;
GetServerIPAddresses(WU_SERVER, pnIPs, nIPCount);
if(nIPCount)
{
struct sockaddr_in saiServer;
saiServer.sin_family = AF_INET;
saiServer.sin_port = htons(PORT_HTTP);
// Build the HTTP string
struct tm *pTime = gmtime(&nRaw);
char szUrl[1024] = {0};
char szQuery[1024] = {0};
sprintf(szUrl, "%s?"\
"ID=XXXXXXXXXX&"\
"PASSWORD=XXXXXXXX&"\
"dateutc=%04d-%02d-%02d+%02d%%3A%02d%%3A%02d&"\
"tempf=%.2f&"\
"baromin=%.2f&"\
"humidity=%.2f&"\
"dewptf=%.2f&"\
"windspeedmph=%.2f&"\
"windgustmph=%.2f&"\
"rainin=%.2f&"\
"dailyrainin=%.2f&",
WU_URL_BASE,
pTime->tm_year + 1900,
pTime->tm_mon + 1,
pTime->tm_mday,
pTime->tm_hour,
pTime->tm_min,
pTime->tm_sec,
data.GetTemperature(),
data.GetPressure(),
data.GetHumidity(),
data.GetDewPoint(),
data.GetWindSpeed(),
data.GetWindGust(),
data.GetRainHour(),
data.GetRainDay());
if(data.GetWindSpeed() >= 3.0)
{
char szWindDir[64] = {0};
sprintf(szWindDir, "winddir=%.2f&", data.GetWindDirection());
strcat(szUrl, szWindDir);
}
strcat(szUrl, "action=updateraw");
sprintf(szQuery, "GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n", szUrl, WU_SERVER, USER_AGENT);
for(unsigned short n = 0; n < nIPCount; ++n)
{
saiServer.sin_addr.s_addr = pnIPs[n];
int nSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(nSocket != -1)
{
if(connect(nSocket, (struct sockaddr*)&saiServer, sizeof(saiServer)) != -1)
{
if(send(nSocket, szQuery, strlen(szQuery), 0))
{
bSuccess = true;
break;
}
}
shutdown(nSocket, SHUT_RDWR);
close(nSocket);
}
}
}
if(pnIPs)
free(pnIPs);
}