2 * Conky, a system monitor, based on torsmo
4 * Please see COPYING for details
6 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "temphelper.h"
30 #include <curl/curl.h>
31 #include <curl/types.h>
32 #include <curl/easy.h>
34 /* Possible sky conditions */
35 #define NUM_CC_CODES 6
36 const char *CC_CODES[NUM_CC_CODES] = {
37 "SKC", "CLR", "FEW", "SCT", "BKN", "OVC"
40 /* Possible weather modifiers */
41 #define NUM_WM_CODES 9
42 const char *WM_CODES[NUM_WM_CODES] = {
43 "VC", "MI", "BC", "PR", "TS", "BL",
47 /* Possible weather conditions */
48 #define NUM_WC_CODES 17
49 const char *WC_CODES[NUM_WC_CODES] = {
50 "DZ", "RA", "GR", "GS", "SN", "SG",
51 "FG", "HZ", "FU", "BR", "DU", "SA",
52 "FC", "PO", "SQ", "SS", "DS",
55 typedef struct location_ {
59 timed_thread *p_timed_thread;
60 struct location_ *next;
63 static location *locations_head = 0;
65 location *find_location(char *uri)
67 location *tail = locations_head;
71 strcmp(tail->uri, uri) == EQUAL) {
76 if (!tail) { // new location!!!!!!!
77 new = malloc(sizeof(location));
78 memset(new, 0, sizeof(location));
79 new->uri = strndup(uri, text_buffer_size);
80 tail = locations_head;
81 while (tail && tail->next) {
85 // omg the first one!!!!!!!
94 void free_weather_info(void)
96 location *tail = locations_head;
100 if (tail->uri) free(tail->uri);
108 int rel_humidity(int dew_point, int air) {
109 const float a = 17.27f;
110 const float b = 237.7f;
112 float g = a*dew_point/(b+dew_point);
113 return (int)(100.f*expf(g-a*air/(b+air)));
117 * Horrible hack to avoid using regexes
121 static inline void parse_token(PWEATHER *res, char *token) {
126 switch (strlen(token)) {
128 //Check all tokens 2 chars long
131 //Check if token is a weather condition
132 for (i=0; i<2; i++) {
133 if (!isalpha(token[i])) break;
136 for(i=0; i<NUM_WC_CODES; i++) {
137 if (!strncmp(token, WC_CODES[i], 2)) {
146 if (!strcmp(token, "CB")) {
153 //Check all tokens 3 chars long
156 //Check if token is a modified weather condition
157 if ((token[0] == '+') || (token[0] == '-')) {
158 for (i=1; i<3; i++) {
159 if (!isalpha(token[i])) break;
162 for(i=0; i<NUM_WC_CODES; i++) {
163 if (!strncmp(&token[1], WC_CODES[i], 2)) {
172 //Check for NCD or NSC
173 if ((!strcmp(token, "NCD")) || (!strcmp(token, "NSC"))) {
179 if (!strcmp(token, "TCU")) {
186 //Check all tokens 4 chars long
189 //Check if token is a modified weather condition
190 for(i=0; i<NUM_WM_CODES; i++) {
191 if (!strncmp(token, WM_CODES[i], 2)) {
192 for(i=0; i<NUM_WC_CODES; i++) {
193 if (!strncmp(&token[2], WC_CODES[i], 2)) {
204 //Check all tokens 5 chars long
208 if (!strcmp(token, "CAVOK")) {
213 //Check if token is the temperature
214 for (i=0; i<2; i++) {
215 if (!isdigit(token[i])) break;
217 if ((i==2) && (token[2] == '/')) {
218 for (i=3; i<5; i++) {
219 if (!isdigit(token[i])) break;
222 //First 2 digits gives the air temperature
223 res->temp=atoi(token);
225 //4th and 5th digits gives the dew point temperature
226 res->dew=atoi(&token[3]);
229 res->hmid = rel_humidity(res->dew, res->temp);
235 //Check if token is the pressure
236 if ((token[0] == 'Q') || (token[0] == 'A')) {
237 for (i=1; i<5; i++) {
238 if (!isdigit(token[i])) break;
241 if (token[0] == 'A') {
242 //Convert inches of mercury to mbar
243 res->bar = (int)(atoi(&token[1])*0.338637526f);
247 //Last 4 digits is pressure im mbar
248 res->bar = atoi(&token[1]);
253 //Check if token is a modified weather condition
254 if ((token[0] == '+') || (token[0] == '-')) {
255 for(i=0; i<NUM_WM_CODES; i++) {
256 if (!strncmp(&token[1], WM_CODES[i], 2)) {
257 for(i=0; i<NUM_WC_CODES; i++) {
258 if (!strncmp(&token[3], WC_CODES[i], 2)) {
269 //Check all tokens 6 chars long
272 //Check if token is the cloud cover
273 for (i=0; i<3; i++) {
274 if (!isalpha(token[i])) break;
277 for (i=3; i<6; i++) {
278 if (!isdigit(token[i])) break;
281 //Check if first 3 digits gives the cloud cover condition
282 for(i=0; i<NUM_CC_CODES; i++) {
283 if (!strncmp(token, CC_CODES[i], 3)) {
292 //Check if token is positive temp and negative dew
293 for (i=0; i<2; i++) {
294 if (!isdigit(token[i])) break;
296 if ((i==2) && (token[2] == '/') && (token[3] == 'M')) {
297 for (i=4; i<6; i++) {
298 if (!isdigit(token[i])) break;
301 //1st and 2nd digits gives the temperature
302 res->temp = atoi(token);
304 //5th and 6th digits gives the dew point temperature
305 res->dew = -atoi(&token[4]);
308 res->hmid = rel_humidity(res->dew, res->temp);
316 //Check all tokens 7 chars long
319 //Check if token is the observation time
320 for (i=0; i<6; i++) {
321 if (!isdigit(token[i])) break;
323 if ((i==6) && (token[6] == 'Z')) return;
325 //Check if token is the wind speed/direction in knots
326 for (i=0; i<5; i++) {
327 if (!isdigit(token[i])) break;
329 if ((i==5) && (token[5] == 'K') && (token[6] == 'T')) {
331 //First 3 digits are wind direction
332 strncpy(s_tmp, token, 3);
333 res->wind_d=atoi(s_tmp);
335 //4th and 5th digit are wind speed in knots (convert to km/hr)
336 res->wind_s = (int)(atoi(&token[3])*1.852);
341 //Check if token is negative temperature
342 if ((token[0] == 'M') && (token[4] == 'M')) {
343 for (i=1; i<3; i++) {
344 if (!isdigit(token[i])) break;
346 if ((i==3) && (token[3] == '/')) {
347 for (i=5; i<7; i++) {
348 if (!isdigit(token[i])) break;
351 //2nd and 3rd digits gives the temperature
352 res->temp = -atoi(&token[1]);
354 //6th and 7th digits gives the dew point temperature
355 res->dew = -atoi(&token[5]);
358 res->hmid = rel_humidity(res->dew, res->temp);
365 //Check if token is wind variability
366 for (i=0; i<3; i++) {
367 if (!isdigit(token[i])) break;
369 if ((i==3) && (token[3] == 'V')) {
370 for (i=4; i<7; i++) {
371 if (!isdigit(token[i])) break;
378 //Check all tokens 8 chars long
381 //Check if token is the wind speed/direction in m/s
382 for (i=0; i<5; i++) {
383 if (!isdigit(token[i])) break;
385 if ((i==5)&&(token[5] == 'M')&&(token[6] == 'P')&&(token[7] == 'S')) {
387 //First 3 digits are wind direction
388 strncpy(s_tmp, token, 3);
389 res->wind_d=atoi(s_tmp);
391 //4th and 5th digit are wind speed in m/s (convert to km/hr)
392 res->wind_s = (int)(atoi(&token[3])*3.6);
399 //printf("token : %s\n", token);
404 static void parse_weather(PWEATHER *res, const char *data)
407 const char delim[] = " ";
409 memset(res, 0, sizeof(PWEATHER));
411 //Divide time stamp and metar data
412 if (sscanf(data, "%[^'\n']\n%[^'\n']", res->lastupd, s_tmp) == 2) {
418 if ((strtok_r(s_tmp, delim, &p_save)) != NULL) {
420 //Jump first token, must be icao
421 p_tok = strtok_r(NULL, delim, &p_save);
425 parse_token(res, p_tok);
426 p_tok = strtok_r(NULL, delim, &p_save);
428 } while (p_tok != NULL);
438 void fetch_weather_info(location *curloc)
444 struct MemoryStruct chunk;
449 curl = curl_easy_init();
451 curl_easy_setopt(curl, CURLOPT_URL, curloc->uri);
452 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
453 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
454 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
455 curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-weather/1.0");
457 res = curl_easy_perform(curl);
459 timed_thread_lock(curloc->p_timed_thread);
460 parse_weather(&curloc->data, chunk.memory);
461 timed_thread_unlock(curloc->p_timed_thread);
464 ERR("No data from server");
467 curl_easy_cleanup(curl);
473 void *weather_thread(void *) __attribute__((noreturn));
475 void init_thread(location *curloc, int interval)
477 curloc->p_timed_thread =
478 timed_thread_create(&weather_thread,
479 (void *)curloc, interval * 1000000);
480 if (!curloc->p_timed_thread) {
481 ERR("Error creating weather timed thread");
483 timed_thread_register(curloc->p_timed_thread,
484 &curloc->p_timed_thread);
485 if (timed_thread_run(curloc->p_timed_thread)) {
486 ERR("Error running weather timed thread");
491 void process_weather_info(char *p, int p_max_size, char *uri, char *data_type, int interval)
493 static const char *wc[] = {
494 "", "drizzle", "rain", "hail", "soft hail",
495 "snow", "snow grains", "fog", "haze", "smoke",
496 "mist", "dust", "sand", "funnel cloud tornado",
497 "dust/sand", "squall", "sand storm", "dust storm"
500 location *curloc = find_location(uri);
501 if (!curloc->p_timed_thread) init_thread(curloc, interval);
504 timed_thread_lock(curloc->p_timed_thread);
505 if (strcmp(data_type, "last_update") == EQUAL) {
506 strncpy(p, curloc->data.lastupd, p_max_size);
507 } else if (strcmp(data_type, "temperature") == EQUAL) {
508 temp_print(p, p_max_size, curloc->data.temp, TEMP_CELSIUS);
509 } else if (strcmp(data_type, "cloud_cover") == EQUAL) {
510 if (curloc->data.cc == 0) {
511 strncpy(p, "", p_max_size);
512 } else if (curloc->data.cc < 3) {
513 strncpy(p, "clear", p_max_size);
514 } else if (curloc->data.cc < 5) {
515 strncpy(p, "partly cloudy", p_max_size);
516 } else if (curloc->data.cc == 5) {
517 strncpy(p, "cloudy", p_max_size);
518 } else if (curloc->data.cc == 6) {
519 strncpy(p, "overcast", p_max_size);
520 } else if (curloc->data.cc == 7) {
521 strncpy(p, "towering cumulus", p_max_size);
523 strncpy(p, "cumulonimbus", p_max_size);
525 } else if (strcmp(data_type, "pressure") == EQUAL) {
526 snprintf(p, p_max_size, "%d", curloc->data.bar);
527 } else if (strcmp(data_type, "wind_speed") == EQUAL) {
528 snprintf(p, p_max_size, "%d", curloc->data.wind_s);
529 } else if (strcmp(data_type, "wind_dir") == EQUAL) {
530 if ((curloc->data.wind_d >= 349) || (curloc->data.wind_d < 12)) {
531 strncpy(p, "N", p_max_size);
532 } else if (curloc->data.wind_d < 33) {
533 strncpy(p, "NNE", p_max_size);
534 } else if (curloc->data.wind_d < 57) {
535 strncpy(p, "NE", p_max_size);
536 } else if (curloc->data.wind_d < 79) {
537 strncpy(p, "ENE", p_max_size);
538 } else if (curloc->data.wind_d < 102) {
539 strncpy(p, "E", p_max_size);
540 } else if (curloc->data.wind_d < 124) {
541 strncpy(p, "ESE", p_max_size);
542 } else if (curloc->data.wind_d < 147) {
543 strncpy(p, "SE", p_max_size);
544 } else if (curloc->data.wind_d < 169) {
545 strncpy(p, "SSE", p_max_size);
546 } else if (curloc->data.wind_d < 192) {
547 strncpy(p, "S", p_max_size);
548 } else if (curloc->data.wind_d < 214) {
549 strncpy(p, "SSW", p_max_size);
550 } else if (curloc->data.wind_d < 237) {
551 strncpy(p, "SW", p_max_size);
552 } else if (curloc->data.wind_d < 259) {
553 strncpy(p, "WSW", p_max_size);
554 } else if (curloc->data.wind_d < 282) {
555 strncpy(p, "W", p_max_size);
556 } else if (curloc->data.wind_d < 304) {
557 strncpy(p, "WNW", p_max_size);
558 } else if (curloc->data.wind_d < 327) {
559 strncpy(p, "NW", p_max_size);
560 } else if (curloc->data.wind_d < 349) {
561 strncpy(p, "NNW", p_max_size);
563 } else if (strcmp(data_type, "wind_dir_DEG") == EQUAL) {
564 snprintf(p, p_max_size, "%d", curloc->data.wind_d);
566 } else if (strcmp(data_type, "humidity") == EQUAL) {
567 snprintf(p, p_max_size, "%d", curloc->data.hmid);
568 } else if (strcmp(data_type, "weather") == EQUAL) {
569 strncpy(p, wc[curloc->data.wc], p_max_size);
571 timed_thread_unlock(curloc->p_timed_thread);
574 void *weather_thread(void *arg)
576 location *curloc = (location*)arg;
579 fetch_weather_info(curloc);
580 if (timed_thread_test(curloc->p_timed_thread, 0)) {
581 timed_thread_exit(curloc->p_timed_thread);