fileWeather.cpp
  0
  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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
*  Weather.cpp
*
*  Created by Daisuke Nogami on Sep, 05.
*  opyright 2005 Daisuke Nogami [null-null.net]. Some rights reserved.
*/
 
#include "stdafx.h"
#include "Weather.h"
 
// param
#define URL "http://weather.goo.ne.jp/area/4410/13201.html"
#define LOCATION "hachiouji, tokyo, jp"
// screen's direction
#define DIRECTION "south"
#define ADD_HOURS 8
 
#define M_PI 3.14159265358979323846
 
Weather::Weather( AirstreamFilter* asfilter, RainFilter* rfilter ){
  this->url = URL;
 
  this->asfilter = asfilter;
  this->rfilter = rfilter;
 
  hours = ADD_HOURS;
 
  // thread
  signaled = true;
}
 
void Weather::load( char* url ){
 
  HINTERNET hInet;
  HINTERNET hFile;
 
  hInet=InternetOpen( "TEST", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
  hFile=InternetOpenUrl( hInet, url, NULL, 0, INTERNET_FLAG_RELOAD, 0 );
 
  string buff = "";
  char* lpszBuf = (LPTSTR)GlobalAlloc(GPTR,1024);
  DWORD dwSize;
  bool bret = true;
  while( bret ){
        dwSize = 0;
        memset( lpszBuf, 0x00, sizeof( lpszBuf ) );
        bret = InternetReadFile( hFile, lpszBuf, sizeof( lpszBuf ) - 1, &dwSize);
    buff += lpszBuf;
        if( dwSize == 0){
            break;
        }
    }
  //cout << buff << endl;
 
  InternetCloseHandle(hFile);
  InternetCloseHandle(hInet);
 
  // today
  regex  r( "<!-- today -->[^\']+<!-- /today -->" );
  smatch m;
  string today = "";
  if( regex_search( buff, m, r) ){
    today = m.str();
  }
 
  // current hour
  next = 0;
  reg_expression<char> regex("(<td align=\"center\" bgcolor=\"#e7e7e7\" width=\"10%\"><b>)([^<]+)(</b></td>)" );
  regex_grep( cb_today_current_hour(), today.begin(), today.end(), regex );
 
  // today's weather
  next = current;
  regex = "(<img src=\"/weather/img/s/)([^/]+)(.gif\")";
  regex_grep( cb_today_weather(), today.begin(), today.end(), regex );
 
  // today's temperature
  next = current;
  regex = "(<font color=\"#0000ff\">)([^<]+)(</font>)";
  regex_grep( cb_today_temp(), today.begin(), today.end(), regex );
 
  // today's precipitation rate
  next = current;
  regex = "(<td align=\"center\" bgcolor=\"white\" width=\"10%\">\n)([^<]+)(<font size=\"-1\">mm</font>)";
  regex_grep( cb_today_prec_rate(), today.begin(), today.end(), regex );
 
  // today's humidity
  next = current;
  regex = "(<td align=\"center\" bgcolor=\"white\" width=\"10%\">\n)([^<]+)(<font size=\"-1\">%</font>)";
  regex_grep( cb_today_hum(), today.begin(), today.end(), regex );
 
  // today's wind direction
  next = current;
  regex = "(<img src=\"/weather/img/s/bear/bear)([^<]+)(.gif)";
  regex_grep( cb_today_win_dir(), today.begin(), today.end(), regex );
 
  // today's wind speed
  next = current;
  regex = "(<td align=\"center\" bgcolor=\"white\" width=\"10%\">\n)([^<]+)(<font size=\"-1\">m/s</font>)";
  regex_grep( cb_today_win_spd(), today.begin(), today.end(), regex );
 
  // tomorrow
  r = "<!-- tomorrow -->[^\']+<!-- /tomorrow -->";
  string tomorrow = "";
  if( regex_search( buff, m, r) ){
    tomorrow = m.str();
  }
 
  // tomorrow's weather
  next = 0;
  regex = "(<img src=\"/weather/img/s/)([^/]+)(.gif\")";
  regex_grep( cb_tomorrow_weather(), tomorrow.begin(), tomorrow.end(), regex );
 
  // tomorrow's temperature
  next = 0;
  regex = "(<font color=\"#0000ff\">)([^<]+)(</font>)";
  regex_grep( cb_tomorrow_temp(), tomorrow.begin(), tomorrow.end(), regex );
 
  // tomorrow's precipitation rate
  next = 0;
  regex = "(<td align=\"center\" bgcolor=\"white\" width=\"10%\">\n)([^<]+)(<font size=\"-1\">mm</font>)";
  regex_grep( cb_tomorrow_prec_rate(), tomorrow.begin(), tomorrow.end(), regex );
 
  // tomorrow's humidity
  next = 0;
  regex = "(<td align=\"center\" bgcolor=\"white\" width=\"10%\">\n)([^<]+)(<font size=\"-1\">%</font>)";
  regex_grep( cb_tomorrow_hum(), tomorrow.begin(), tomorrow.end(), regex );
 
  // tomorrow's wind direction
  next = 0;
  regex = "(<img src=\"/weather/img/s/bear/bear)([^<]+)(.gif)";
  regex_grep( cb_tomorrow_win_dir(), tomorrow.begin(), tomorrow.end(), regex );
 
  // tomorrow's wind speed
  next = 0;
  regex = "(<td align=\"center\" bgcolor=\"white\" width=\"10%\">\n)([^<]+)(<font size=\"-1\">m/s</font>)";
  regex_grep( cb_tomorrow_win_spd(), tomorrow.begin(), tomorrow.end(), regex );
 
  // week
  r = "<!-- /tomorrow -->[^\']+<!-- /main contents -->";
  string week = "";
  if( regex_search( buff, m, r) ){
    week = m.str();
  }
 
  // this week's weather
  next = 0;
  regex = "(<img src=\"/weather/img/s/big/)([^>]+)(.gif\")";
  regex_grep( cb_week_weather(), week.begin(), week.end(), regex );
 
  // this week's max temperature
  next = 0;
  regex = "(<font color=\"#ff0000\">\n)([^<]+)(\n</font>/)";
  regex_grep( cb_week_max_temp(), week.begin(), week.end(), regex );
 
  // this week's min temperature
  next = 0;
  regex = "(/<font color=\"#0000ff\">\n)([^<]+)(\n</font>)";
  regex_grep( cb_week_min_temp(), week.begin(), week.end(), regex );
 
  // this week's precipitation percentage
  next = 0;
  regex = "(<td bgcolor=\"#ffffff\">\n)([0-9]+)(\n</td>)";
  regex_grep( cb_week_prec_perc(), week.begin(), week.end(), regex );
 
  // print
  /*cout << endl << "> today's current hour" << endl;
	cout << current << endl;
 
	cout << endl << "> today's weather" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << today_weather[i] << endl;
	}
 
	cout << endl << "> today's temperature" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << today_temp[i] << endl;
	}
 
	cout << endl << "> today's precipitation rate" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << today_prec_rate[i] << endl;
	}
 
	cout << endl << "> today's humidity" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << today_hum[i] << endl;
	}
 
	cout << endl << "> today's wind direction" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << today_win_dir[i] << endl;
	}
 
	cout << endl << "> today's wind speed" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << today_win_spd[i] << endl;
	}
 
	cout << endl << "> tomorrow's weather" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << tomorrow_weather[i] << endl;
	}
 
	cout << endl << "> tomorrow's temperature" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << tomorrow_temp[i] << endl;
	}
 
	cout << endl << "> tomorrow's precipitation rate" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << tomorrow_prec_rate[i] << endl;
	}
 
	cout << endl << "> tomorrow's humidity" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << tomorrow_hum[i] << endl;
	}
 
	cout << endl << "> tomorrow's wind direction" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << tomorrow_win_dir[i] << endl;
	}
 
	cout << endl << "> tomorrow's wind speed" << endl;
	for( int i = 0; i < 8; i++ ){
		cout << tomorrow_win_spd[i] << endl;
	}
 
	cout << endl << "> this week's weather" << endl;
	for( int i = 0; i < 6; i++ ){
		cout << week_weather[i] << endl;
	}
 
	cout << endl << "> this week's max temperature" << endl;
	for( int i = 0; i < 6; i++ ){
		cout << week_max_temp[i] << endl;
	}
 
	cout << endl << "> this week's min temperature" << endl;
	for( int i = 0; i < 6; i++ ){
		cout << week_min_temp[i] << endl;
	}
 
	cout << endl << "> this week's precipitation percentage" << endl;
	for( int i = 0; i < 6; i++ ){
		cout << week_prec_perc[i] << endl;
	}*/
}
 
// get weather status
string Weather::getCurrentWeather(){
  if( current < 0 )  return "null";
 
  int add = abs( (crt_hour+hours-current*3)/3 );
  std::string crt_wet = current+add < 8 ? today_weather[current+add] : tomorrow_weather[current+add-8];
 
  if( crt_wet == "100" ){
    return "shine";
  }
  else if( crt_wet == "200" ){
    return "cloudiness";
  }
  else if( crt_wet == "300" ){
    return "rain";
  }
  else{
    return "snow";
  }
}
 
string Weather::getCurrentDate(){
  if( crt_hour+hours < 24 ){
    return "today";
  }
  else{
    return "tomorrow";
  }
}
 
string Weather::getLocation(){
  return LOCATION;
}
 
string Weather::getAddHours(){
  char buf[256];
  sprintf( buf, "%d", hours );
  return string( buf );
}
 
string Weather::getCurrentTemp(){
  if( current < 0 )  return "null";
 
  int add = abs( (crt_hour+hours-current*3)/3 );
  return current+add < 8 ? today_temp[current+add] : tomorrow_temp[current+add-8];
}
 
string Weather::getCurrentPrecRate(){
  if( current < 0 )  return "null";
 
  int add = abs( (crt_hour+hours-current*3)/3 );
  return current+add < 8 ? today_prec_rate[current+add] : tomorrow_prec_rate[current+add-8];
}
 
string Weather::getCurrentHum(){
  if( current < 0 )  return "null";
 
  int add = abs( (crt_hour+hours-current*3)/3 );
  return current+add < 8 ? today_hum[current+add] : tomorrow_hum[current+add-8];
}
 
string Weather::getCurrentWinDir(){
  if( current < 0 )  return "null";
 
  int add = abs( (crt_hour+hours-current*3)/3 );
  std::string crt_wet = current+add < 8 ? today_win_dir[current+add] : tomorrow_win_dir[current+add-8];
 
  if( crt_wet == "01" ){
    return "north-northeast";
  }
  else if( crt_wet == "02" ){
    return "northeast";
  }
  else if( crt_wet == "03" ){
    return "east-northeast";
  }
  else if( crt_wet == "04" ){
    return "east";
  }
  else if( crt_wet == "05" ){
    return "east-southeast";
  }
  else if( crt_wet == "06" ){
    return "southeast";
  }
  else if( crt_wet == "07" ){
    return "south-southeast";
  }
  else if( crt_wet == "08" ){
    return "south";
  }
  else if( crt_wet == "09" ){
    return "south-southwest";
  }
  else if( crt_wet == "10" ){
    return "southwest";
  }
  else if( crt_wet == "11" ){
    return "west-southwest";
  }
  else if( crt_wet == "12" ){
    return "west";
  }
  else if( crt_wet == "13" ){
    return "west-northwest";
  }
  else if( crt_wet == "14" ){
    return "northwest";
  }
  else if( crt_wet == "15" ){
    return "north-northwest";
  }
  else{
    return "north";
  }
}
 
string Weather::getCurrentWinSpd(){
  if( current < 0 )  return "null";
 
  int add = abs( (crt_hour+hours-current*3)/3 );
  return current+add < 8 ? today_win_spd[current+add] : tomorrow_win_spd[current+add-8];
}
 
float Weather::convertToDegreeFrom( string dir ){
  float degree;
 
  if( dir == "north-northeast" )    degree = 202.5;
  else if( dir == "northeast" )    degree = 225;
  else if( dir == "east-northeast" )  degree = 247.5;
  else if( dir == "east" )      degree = 270;
  else if( dir == "east-southeast" )  degree = 292.5;
  else if( dir == "southeast" )    degree = 315;
  else if( dir == "south-southeast" )  degree = 337.5;
  else if( dir == "south" )      degree = 0;
  else if( dir == "south-southwest" )  degree = 22.5;
  else if( dir == "southwest" )    degree = 45;
  else if( dir == "west-southwest" )  degree = 67.5;
  else if( dir == "west" )      degree = 90;
  else if( dir == "west-northwest" )  degree = 112.5;
  else if( dir == "northwest" )    degree = 135;
  else if( dir == "north-northwest" )  degree = 157.5;
  else                degree = 180;  // north
 
  return degree;
}
 
// refresh data
void Weather::refresh(){
  load( url );
 
  /*float wind_degree, me_degree, wvel, r;
	wind_degree = convertToDegreeFrom( getCurrentWinDir() );
	me_degree = convertToDegreeFrom( DIRECTION );
	wind_degree -= me_degree;
	r = 0.5;
	wvel = sin( wind_degree/360*M_PI*2 )*r;*/
 
  //asfilter->sync( 0, 1 );
  //rfilter->sync( 4 );
 
  //asfilter->sync( wvel, atoi( getCurrentWinSpd().c_str() ) );
  asfilter->sync( 0, atoi( getCurrentWinSpd().c_str() ) );
  rfilter->sync( atoi( getCurrentPrecRate().c_str() ) );
}
 
// run thread
void Weather::run(){
 
  long val = 0;
  long l_time;
  struct tm* now_time;
  int before = -1;
 
  time(&l_time);
  now_time = localtime( &l_time );
  crt_hour = now_time->tm_hour;
 
  while( signaled ){
    // per 1 second
    if( ++val > 200000000 ){
 
      time(&l_time);
      now_time = localtime( &l_time );
      crt_hour = now_time->tm_hour;
 
      // get start time
      if( before < 0 ){
        refresh();
        before = now_time->tm_min;
        printf( "> start time %d:%d:%d\n", now_time->tm_hour, now_time->tm_min, now_time->tm_sec );
      }
 
      // per 10 minute
      if( now_time->tm_min - before >= 10 || now_time->tm_min < before ){
        refresh();
        printf( "> per 10 minute %d:%d:%d\n", now_time->tm_hour, now_time->tm_min, now_time->tm_sec );
        before = now_time->tm_min;
      }
 
      val = 0;
    }
  }
 
  printf("run out\n");
}
 
// stop thread
void Weather::signal(){
  signaled = false;
}
[トップ] [一覧] [最終更新] [検索] [バックアップ]