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/>.
25 * TODO: Add weather forecast info from weather.com
32 #include "temphelper.h"
38 #include <curl/curl.h>
39 #include <curl/types.h>
40 #include <curl/easy.h>
42 #include <libxml/parser.h>
45 /* Possible sky conditions */
46 #define NUM_CC_CODES 6
47 const char *CC_CODES[NUM_CC_CODES] = {
48 "SKC", "CLR", "FEW", "SCT", "BKN", "OVC"
51 /* Possible weather modifiers */
52 #define NUM_WM_CODES 9
53 const char *WM_CODES[NUM_WM_CODES] = {
54 "VC", "MI", "BC", "PR", "TS", "BL",
58 /* Possible weather conditions */
59 #define NUM_WC_CODES 17
60 const char *WC_CODES[NUM_WC_CODES] = {
61 "DZ", "RA", "GR", "GS", "SN", "SG",
62 "FG", "HZ", "FU", "BR", "DU", "SA",
63 "FC", "PO", "SQ", "SS", "DS"
66 typedef struct location_ {
70 timed_thread *p_timed_thread;
71 struct location_ *next;
74 static location *locations_head = 0;
76 location *find_location(char *uri)
78 location *tail = locations_head;
82 strcmp(tail->uri, uri) == EQUAL) {
87 if (!tail) { // new location!!!!!!!
88 new = malloc(sizeof(location));
89 memset(new, 0, sizeof(location));
90 new->uri = strndup(uri, text_buffer_size);
91 tail = locations_head;
92 while (tail && tail->next) {
96 // omg the first one!!!!!!!
105 void free_weather_info(void)
107 location *tail = locations_head;
111 if (tail->uri) free(tail->uri);
119 int rel_humidity(int dew_point, int air) {
120 const float a = 17.27f;
121 const float b = 237.7f;
123 float diff = a*(dew_point/(b+dew_point)-air/(b+air));
125 return (int)(100.f*expf(diff));
127 return (int)(16.666667163372f*(6.f+diff*(6.f+diff*(3.f+diff))));
132 //TODO: Lets get rid of the recursion
133 static void parse_cc(PWEATHER *res, xmlNodePtr cc)
136 xmlNodePtr cur = NULL;
138 for (cur = cc; cur; cur = cur->next) {
139 if (cur->type == XML_ELEMENT_NODE) {
140 if (!xmlStrcmp(cur->name, (const xmlChar *) "lsup")) {
141 strncpy(res->lastupd, (char *)cur->children->content, 31);
142 } else if (!xmlStrcmp(cur->name, (const xmlChar *) "tmp")) {
143 res->temp = atoi((char *)cur->children->content);
144 } else if (!xmlStrcmp(cur->name, (const xmlChar *) "t")) {
145 if(res->xoap_t[0] == '\0') {
146 strncpy(res->xoap_t, (char *)cur->children->content, 31);
148 } else if (!xmlStrcmp(cur->name, (const xmlChar *) "r")) {
149 res->bar = atoi((char *)cur->children->content);
150 } else if (!xmlStrcmp(cur->name, (const xmlChar *) "s")) {
151 res->wind_s = atoi((char *)cur->children->content);
152 } else if (!xmlStrcmp(cur->name, (const xmlChar *) "d")) {
153 if (isdigit((char)cur->children->content[0])) {
154 res->wind_d = atoi((char *)cur->children->content);
156 } else if (!xmlStrcmp(cur->name, (const xmlChar *) "hmid")) {
157 res->hmid = atoi((char *)cur->children->content);
160 parse_cc(res, cur->children);
165 static void parse_weather_xml(PWEATHER *res, const char *data)
170 if (!(doc = xmlReadMemory(data, strlen(data), "", NULL, 0))) {
171 ERR("weather: can't read xml data");
175 cur = xmlDocGetRootElement(doc);
178 if (cur->type == XML_ELEMENT_NODE) {
179 if (!xmlStrcmp(cur->name, (const xmlChar *) "weather")) {
181 while (cur != NULL) {
182 if (cur->type == XML_ELEMENT_NODE) {
183 if (!xmlStrcmp(cur->name, (const xmlChar *) "cc")) {
184 parse_cc(res, cur->children);
196 ERR("weather: incorrect xml data");
203 * Horrible hack to avoid using regexes
207 static inline void parse_token(PWEATHER *res, char *token) {
212 switch (strlen(token)) {
214 //Check all tokens 2 chars long
217 //Check if token is a weather condition
218 for (i=0; i<2; i++) {
219 if (!isalpha(token[i])) break;
222 for(i=0; i<NUM_WC_CODES; i++) {
223 if (!strncmp(token, WC_CODES[i], 2)) {
232 if (!strcmp(token, "CB")) {
239 //Check all tokens 3 chars long
242 //Check if token is a modified weather condition
243 if ((token[0] == '+') || (token[0] == '-')) {
244 for (i=1; i<3; i++) {
245 if (!isalpha(token[i])) break;
248 for(i=0; i<NUM_WC_CODES; i++) {
249 if (!strncmp(&token[1], WC_CODES[i], 2)) {
258 //Check for NCD or NSC
259 if ((!strcmp(token, "NCD")) || (!strcmp(token, "NSC"))) {
265 if (!strcmp(token, "TCU")) {
272 //Check all tokens 4 chars long
275 //Check if token is a modified weather condition
276 for(i=0; i<NUM_WM_CODES; i++) {
277 if (!strncmp(token, WM_CODES[i], 2)) {
278 for(i=0; i<NUM_WC_CODES; i++) {
279 if (!strncmp(&token[2], WC_CODES[i], 2)) {
290 //Check all tokens 5 chars long
294 if (!strcmp(token, "CAVOK")) {
299 //Check if token is the temperature
300 for (i=0; i<2; i++) {
301 if (!isdigit(token[i])) break;
303 if ((i==2) && (token[2] == '/')) {
304 for (i=3; i<5; i++) {
305 if (!isdigit(token[i])) break;
308 //First 2 digits gives the air temperature
309 res->temp=atoi(token);
311 //4th and 5th digits gives the dew point temperature
312 res->dew=atoi(&token[3]);
315 res->hmid = rel_humidity(res->dew, res->temp);
321 //Check if token is the pressure
322 if ((token[0] == 'Q') || (token[0] == 'A')) {
323 for (i=1; i<5; i++) {
324 if (!isdigit(token[i])) break;
327 if (token[0] == 'A') {
328 //Convert inches of mercury to mbar
329 res->bar = (int)(atoi(&token[1])*0.338637526f);
333 //Last 4 digits is pressure im mbar
334 res->bar = atoi(&token[1]);
339 //Check if token is a modified weather condition
340 if ((token[0] == '+') || (token[0] == '-')) {
341 for(i=0; i<NUM_WM_CODES; i++) {
342 if (!strncmp(&token[1], WM_CODES[i], 2)) {
343 for(i=0; i<NUM_WC_CODES; i++) {
344 if (!strncmp(&token[3], WC_CODES[i], 2)) {
355 //Check all tokens 6 chars long
358 //Check if token is the cloud cover
359 for (i=0; i<3; i++) {
360 if (!isalpha(token[i])) break;
363 for (i=3; i<6; i++) {
364 if (!isdigit(token[i])) break;
367 //Check if first 3 digits gives the cloud cover condition
368 for(i=0; i<NUM_CC_CODES; i++) {
369 if (!strncmp(token, CC_CODES[i], 3)) {
378 //Check if token is positive temp and negative dew
379 for (i=0; i<2; i++) {
380 if (!isdigit(token[i])) break;
382 if ((i==2) && (token[2] == '/') && (token[3] == 'M')) {
383 for (i=4; i<6; i++) {
384 if (!isdigit(token[i])) break;
387 //1st and 2nd digits gives the temperature
388 res->temp = atoi(token);
390 //5th and 6th digits gives the dew point temperature
391 res->dew = -atoi(&token[4]);
394 res->hmid = rel_humidity(res->dew, res->temp);
402 //Check all tokens 7 chars long
405 //Check if token is the observation time
406 for (i=0; i<6; i++) {
407 if (!isdigit(token[i])) break;
409 if ((i==6) && (token[6] == 'Z')) return;
411 //Check if token is the wind speed/direction in knots
412 for (i=0; i<5; i++) {
413 if (!isdigit(token[i])) break;
415 if ((i==5) && (token[5] == 'K') && (token[6] == 'T')) {
417 //First 3 digits are wind direction
418 strncpy(s_tmp, token, 3);
420 res->wind_d=atoi(s_tmp);
422 //4th and 5th digit are wind speed in knots (convert to km/hr)
423 res->wind_s = (int)(atoi(&token[3])*1.852);
428 //Check if token is negative temperature
429 if ((token[0] == 'M') && (token[4] == 'M')) {
430 for (i=1; i<3; i++) {
431 if (!isdigit(token[i])) break;
433 if ((i==3) && (token[3] == '/')) {
434 for (i=5; i<7; i++) {
435 if (!isdigit(token[i])) break;
438 //2nd and 3rd digits gives the temperature
439 res->temp = -atoi(&token[1]);
441 //6th and 7th digits gives the dew point temperature
442 res->dew = -atoi(&token[5]);
445 res->hmid = rel_humidity(res->dew, res->temp);
452 //Check if token is wind variability
453 for (i=0; i<3; i++) {
454 if (!isdigit(token[i])) break;
456 if ((i==3) && (token[3] == 'V')) {
457 for (i=4; i<7; i++) {
458 if (!isdigit(token[i])) break;
465 //Check all tokens 8 chars long
468 //Check if token is the wind speed/direction in m/s
469 for (i=0; i<5; i++) {
470 if (!isdigit(token[i])) break;
472 if ((i==5)&&(token[5] == 'M')&&(token[6] == 'P')&&(token[7] == 'S')) {
474 //First 3 digits are wind direction
475 strncpy(s_tmp, token, 3);
477 res->wind_d=atoi(s_tmp);
479 //4th and 5th digit are wind speed in m/s (convert to km/hr)
480 res->wind_s = (int)(atoi(&token[3])*3.6);
487 //printf("token : %s\n", token);
492 static void parse_weather(PWEATHER *res, const char *data)
495 memset(res, 0, sizeof(PWEATHER));
498 //Check if it is an xml file
499 if ( strncmp(data, "<?xml ", 6) == 0 ) {
500 parse_weather_xml(res, data);
504 //We assume its a text file
506 const char delim[] = " ";
508 //Divide time stamp and metar data
509 if (sscanf(data, "%[^'\n']\n%[^'\n']", res->lastupd, s_tmp) == 2) {
515 if ((strtok_r(s_tmp, delim, &p_save)) != NULL) {
517 //Jump first token, must be icao
518 p_tok = strtok_r(NULL, delim, &p_save);
522 parse_token(res, p_tok);
523 p_tok = strtok_r(NULL, delim, &p_save);
525 } while (p_tok != NULL);
535 void fetch_weather_info(location *curloc)
541 struct MemoryStruct chunk;
546 curl = curl_easy_init();
548 curl_easy_setopt(curl, CURLOPT_URL, curloc->uri);
549 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
550 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
551 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
552 curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-weather/1.0");
554 res = curl_easy_perform(curl);
555 if (res == CURLE_OK && chunk.size) {
556 timed_thread_lock(curloc->p_timed_thread);
557 parse_weather(&curloc->data, chunk.memory);
558 timed_thread_unlock(curloc->p_timed_thread);
561 ERR("weather: no data from server");
564 curl_easy_cleanup(curl);
570 void *weather_thread(void *) __attribute__((noreturn));
572 void init_thread(location *curloc, int interval)
574 curloc->p_timed_thread =
575 timed_thread_create(&weather_thread,
576 (void *)curloc, interval * 1000000);
577 if (!curloc->p_timed_thread) {
578 ERR("weather: error creating timed thread");
580 timed_thread_register(curloc->p_timed_thread,
581 &curloc->p_timed_thread);
582 if (timed_thread_run(curloc->p_timed_thread)) {
583 ERR("weather: error running timed thread");
588 void process_weather_info(char *p, int p_max_size, char *uri, char *data_type, int interval)
590 static const char *wc[] = {
591 "", "drizzle", "rain", "hail", "soft hail",
592 "snow", "snow grains", "fog", "haze", "smoke",
593 "mist", "dust", "sand", "funnel cloud tornado",
594 "dust/sand", "squall", "sand storm", "dust storm"
597 location *curloc = find_location(uri);
598 if (!curloc->p_timed_thread) init_thread(curloc, interval);
600 timed_thread_lock(curloc->p_timed_thread);
601 if (strcmp(data_type, "last_update") == EQUAL) {
602 strncpy(p, curloc->data.lastupd, p_max_size);
603 } else if (strcmp(data_type, "temperature") == EQUAL) {
604 temp_print(p, p_max_size, curloc->data.temp, TEMP_CELSIUS);
605 } else if (strcmp(data_type, "cloud_cover") == EQUAL) {
607 if (curloc->data.xoap_t[0] != '\0') {
608 strncpy(p, curloc->data.xoap_t, p_max_size);
611 if (curloc->data.cc == 0) {
612 strncpy(p, "", p_max_size);
613 } else if (curloc->data.cc < 3) {
614 strncpy(p, "clear", p_max_size);
615 } else if (curloc->data.cc < 5) {
616 strncpy(p, "partly cloudy", p_max_size);
617 } else if (curloc->data.cc == 5) {
618 strncpy(p, "cloudy", p_max_size);
619 } else if (curloc->data.cc == 6) {
620 strncpy(p, "overcast", p_max_size);
621 } else if (curloc->data.cc == 7) {
622 strncpy(p, "towering cumulus", p_max_size);
624 strncpy(p, "cumulonimbus", p_max_size);
626 } else if (strcmp(data_type, "pressure") == EQUAL) {
627 snprintf(p, p_max_size, "%d", curloc->data.bar);
628 } else if (strcmp(data_type, "wind_speed") == EQUAL) {
629 snprintf(p, p_max_size, "%d", curloc->data.wind_s);
630 } else if (strcmp(data_type, "wind_dir") == EQUAL) {
631 if ((curloc->data.wind_d >= 349) || (curloc->data.wind_d < 12)) {
632 strncpy(p, "N", p_max_size);
633 } else if (curloc->data.wind_d < 33) {
634 strncpy(p, "NNE", p_max_size);
635 } else if (curloc->data.wind_d < 57) {
636 strncpy(p, "NE", p_max_size);
637 } else if (curloc->data.wind_d < 79) {
638 strncpy(p, "ENE", p_max_size);
639 } else if (curloc->data.wind_d < 102) {
640 strncpy(p, "E", p_max_size);
641 } else if (curloc->data.wind_d < 124) {
642 strncpy(p, "ESE", p_max_size);
643 } else if (curloc->data.wind_d < 147) {
644 strncpy(p, "SE", p_max_size);
645 } else if (curloc->data.wind_d < 169) {
646 strncpy(p, "SSE", p_max_size);
647 } else if (curloc->data.wind_d < 192) {
648 strncpy(p, "S", p_max_size);
649 } else if (curloc->data.wind_d < 214) {
650 strncpy(p, "SSW", p_max_size);
651 } else if (curloc->data.wind_d < 237) {
652 strncpy(p, "SW", p_max_size);
653 } else if (curloc->data.wind_d < 259) {
654 strncpy(p, "WSW", p_max_size);
655 } else if (curloc->data.wind_d < 282) {
656 strncpy(p, "W", p_max_size);
657 } else if (curloc->data.wind_d < 304) {
658 strncpy(p, "WNW", p_max_size);
659 } else if (curloc->data.wind_d < 327) {
660 strncpy(p, "NW", p_max_size);
661 } else if (curloc->data.wind_d < 349) {
662 strncpy(p, "NNW", p_max_size);
664 } else if (strcmp(data_type, "wind_dir_DEG") == EQUAL) {
665 snprintf(p, p_max_size, "%d", curloc->data.wind_d);
667 } else if (strcmp(data_type, "humidity") == EQUAL) {
668 snprintf(p, p_max_size, "%d", curloc->data.hmid);
669 } else if (strcmp(data_type, "weather") == EQUAL) {
670 strncpy(p, wc[curloc->data.wc], p_max_size);
673 timed_thread_unlock(curloc->p_timed_thread);
676 void *weather_thread(void *arg)
678 location *curloc = (location*)arg;
681 fetch_weather_info(curloc);
682 if (timed_thread_test(curloc->p_timed_thread, 0)) {
683 timed_thread_exit(curloc->p_timed_thread);