-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuoyData.cpp
368 lines (296 loc) · 9.2 KB
/
BuoyData.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (c) 2013 Hyperceptive, LLC
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
#include "BuoyData.h"
#include "HttpRequest.h"
#include <cstdio>
#include <ctime>
#include <math.h>
#include <iostream>
#include <sstream>
#include <stdlib.h>
template <typename T>
T **newArray2D(int rows, int cols)
{
T **dynamicArray;
dynamicArray = new T*[rows];
for (int i=0; i < rows; i++)
{
dynamicArray[i] = new T[cols];
}
return dynamicArray;
}
template <typename T>
void deleteArray2D(T **array)
{
delete[] *array;
delete[] array;
}
int payloadSize;
std::string payload;
std::string **buoyInfoData;
//-----------------------------------------------------------------------------
void headersReady(const HttpResponse *response, void *additionalParams)
{
printf("HTTP Status: %d - %s\n", response->getStatus(), response->getReason());
payloadSize = 0;
payload.clear();
}
//-----------------------------------------------------------------------------
void receiveData(const HttpResponse *response, void *additionalParams, const unsigned char *data, int sizeOfData)
{
payloadSize += sizeOfData;
for (int i=0; i < sizeOfData; i++)
{
char c = data[i];
payload += c;
}
}
//-----------------------------------------------------------------------------
void responseComplete(const HttpResponse *response, void *additionalParams)
{
std::string onlyData;
//For CDIP service: unsigned posStart, posEnd, length;
//Param is an instance of the BuoyData class.
BuoyData *buoyData = static_cast<BuoyData*>(additionalParams);
// First, get the RAW data.
printf("Data Size: %d bytes\n\n", payloadSize);
//For CDIP service: posStart = payload.find("YYYYMMDDHHMM");
//For CDIP service: posEnd = payload.find("</pre>");
//For CDIP service: length = posEnd - posStart;
//For CDIP service: onlyData = payload.substr(posStart, length);
onlyData = payload;
// Next, replace spaces with TAB and count the number of rows & cols.
std::string tableData;
int rows = 0;
int cols = 1;
for (std::string::iterator itr = onlyData.begin(); itr != onlyData.end(); itr++)
{
if (*itr == ' ')
{
if (rows == 0) cols++;
tableData += '\t';
while (*itr && *itr == ' ')
{
itr++; // Skip additional spaces
}
if (*itr && *itr != ' ') itr--;
}
else if (*itr == '\n')
{
rows++;
tableData += *itr;
}
else
{
tableData += *itr;
}
}
// Then, put the data into a Table (2D Array).
buoyInfoData = newArray2D<std::string>(rows, cols);
std::string currentValue;
int currRow = 0;
int currCol = 0;
for (std::string::iterator itr = tableData.begin(); itr != tableData.end(); itr++)
{
if (*itr == '\t')
{
buoyInfoData[currRow][currCol] = currentValue;
currentValue.clear();
currCol++;
}
else if (*itr == '\n')
{
buoyInfoData[currRow][currCol] = currentValue;
currentValue.clear();
currRow++;
currCol = 0;
}
else
{
currentValue += *itr;
}
}
/*
std::cout << "Table Data: " << std::endl << tableData << std::endl
<< "rows: " << rows<< ", cols: " << cols << std::endl << std::endl;
std::cout << "buoyInfoData[1][0]: " << buoyInfoData[1][0] << std::endl
<< "buoyInfoData[4][4]: " << buoyInfoData[4][4] << std::endl;
*/
buoyData->setDate(buoyInfoData[2][0] + buoyInfoData[2][1] + buoyInfoData[2][2]); //YYYYMMDD
buoyData->setTime(buoyInfoData[2][3] + buoyInfoData[2][4]); //HHMM
buoyData->setGroundSwellHeight(buoyData->round(buoyData->convertMetersToFeet(buoyInfoData[2][6])));
buoyData->setGroundSwellPeriod(buoyData->round(buoyInfoData[2][7]));
buoyData->setGroundSwellDirection(buoyInfoData[2][10]);
buoyData->setWindSwellHeight(buoyData->round(buoyData->convertMetersToFeet(buoyInfoData[2][8])));
buoyData->setWindSwellPeriod(buoyData->round(buoyInfoData[2][9]));
buoyData->setWindSwellDirection(buoyInfoData[2][11]);
// No longer need buoyInfoData 2D Array
if (buoyInfoData != 0)
{
deleteArray2D(buoyInfoData);
}
time_t now = time(0);
std::cout << ctime(&now) << " Buoy Data Last Updated at: " <<
buoyData->getDate() << " " <<
buoyData->getTime() << std::endl
<< " Ground Swell: " <<
buoyData->getGroundSwellDirection() << " " <<
buoyData->getGroundSwellHeight() << "' @ " <<
buoyData->getGroundSwellPeriod() << "s" << std::endl
<< " Wind Swell: " <<
buoyData->getWindSwellDirection() << " " <<
buoyData->getWindSwellHeight() << "' @ " <<
buoyData->getWindSwellPeriod() << "s" << std::endl << std::endl;
}
int curTime()
{
time_t theTime = time(NULL);
return static_cast<int>(theTime);
}
//-----------------------------------------------------------------------------
BuoyData::BuoyData()
{
}
BuoyData::~BuoyData()
{
}
//-----------------------------------------------------------------------------
void BuoyData::run()
{
// Number of seconds between auto-refresh
int timeBetweenAutoRefresh = 5*60; //5 minutes
int timeOfLastRefresh = curTime(); //assume we just refreshed the data
while(true)
{
//has enough time elapsed that we should call AutoFunction?
if (curTime() - timeOfLastRefresh >= timeBetweenAutoRefresh)
{
timeOfLastRefresh = curTime();
getBuoyData();
}
sleep(30);
}
}
//-----------------------------------------------------------------------------
void BuoyData::getBuoyData()
{
const char *headers[] =
{
"Connection", "close",
"Content-type", "application/x-www-form-urlencoded",
"Accept", "text/plain",
0 //null terminator
};
/*
// Swell Energy
HttpRequest request("cdip.ucsd.edu", 80);
int buoyEnergy = 0;
request.initCallbacks(headersReady, receiveData, responseComplete, &buoyEnergy);
request.sendRequest("POST", "/data_access/justdar.cdip?142+de", headers);
while(request.responsesPending())
{
request.processRequest();
}
// Swell Direction
HttpRequest request2("cdip.ucsd.edu", 80);
int buoyDirection = 1;
request2.initCallbacks(headersReady, receiveData, responseComplete, &buoyDirection);
request2.sendRequest("POST", "/data_access/justdar.cdip?142+dd", headers);
while(request2.responsesPending())
{
request2.processRequest();
}
*/
//Try NOAA web-service - still accesses CDIP buoy info, just via NOAA.
//http://www.ndbc.noaa.gov/data/realtime2/46237.spec
//#YY MM DD hh mm WVHT SwH SwP WWH WWP SwD WWD STEEPNESS APD MWD
//#yr mo dy hr mn m m sec m sec - degT - sec degT
//2013 09 08 17 01 1.0 0.5 11.1 0.9 9.9 W WNW AVERAGE 6.5 285
// YY MM DD
// hh mm
// WVHT (m) - Significant Wave Height
// SwH (m) - Ground Swell Height
// SwP (s) - Ground Swell Period
// WWH (m) - Wind Swell Height
// WWP (s) - Wind Swell Period
// SwD - Ground Swell Direction
// WWD - Wind Swell Direction
// STEEPNESS
// APD (s) - Average Wave Period
// MWD (deg) - Dominant Wave Direction
//
HttpRequest request("www.ndbc.noaa.gov", 80);
request.initCallbacks(headersReady, receiveData, responseComplete, this);
request.sendRequest("POST", "/data/realtime2/46237.spec", headers);
while(request.responsesPending())
{
request.processRequest();
}
//http://www.ndbc.noaa.gov/data/realtime2/46237.txt
//#YY MM DD hh mm WDIR WSPD GST WVHT DPD APD MWD PRES ATMP WTMP DEWP VIS PTDY TIDE
//#yr mo dy hr mn degT m/s m/s m sec sec degT hPa degC degC degC nmi hPa ft
//2013 09 08 17 01 MM MM MM 1.0 10 6.5 285 MM MM 15.0 MM MM MM MM
// YY MM DD
// hh mm
// WVHT (m) -- see above
// DPD (s) -- Dominant Wave Period
// APD (s) -- also above
// MWD (deg) -- also above
// WTMP (C)
}
//-----------------------------------------------------------------------------
std::string BuoyData::convertMetersToFeet(std::string meters)
{
float feet = atof(meters.c_str()) * 3.2808;
feet = floorf(feet * 5 + 0.5) / 5;
std::ostringstream ss;
ss << feet;
return ss.str();
}
//-----------------------------------------------------------------------------
float BuoyData::convertCompassPointToDegrees(std::string cp)
{
if (cp == "N")
return 270;
else if (cp == "NNE")
return 292.5;
else if (cp == "NE")
return 315;
else if (cp == "ENE")
return 337.5;
else if (cp == "E")
return 0;
else if (cp == "ESE")
return 22.5;
else if (cp == "SE")
return 45;
else if (cp == "SSE")
return 67.5;
else if (cp == "S")
return 90;
else if (cp == "SSW")
return 112.5;
else if (cp == "SW")
return 135;
else if (cp == "WSW")
return 157.5;
else if (cp == "W")
return 180;
else if (cp == "WNW")
return 202.5;
else if (cp == "NW")
return 225;
else if (cp == "NNW")
return 247.5;
return 0;
}
//-----------------------------------------------------------------------------
std::string BuoyData::round(std::string realNumStr)
{
float realNum = atof(realNumStr.c_str());
realNum = floorf(realNum + 0.5);
std::ostringstream ss;
ss << realNum;
return ss.str();
}