1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Please see COPYING for details
7 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * vim: ts=4 sw=4 noet ai cindent syntax=c
28 * TODO: Add weather forecast info from weather.com
35 #include "temphelper.h"
36 #include "ccurl_thread.h"
43 #include <libxml/parser.h>
44 #include <libxml/xpath.h>
46 /* Xpath expressions for XOAP xml parsing */
47 #define NUM_XPATH_EXPRESSIONS 8
48 const char *xpath_expression[NUM_XPATH_EXPRESSIONS] = {
49 "/weather/cc/lsup", "/weather/cc/tmp", "/weather/cc/t",
50 "/weather/cc/bar/r", "/weather/cc/wind/s", "/weather/cc/wind/d",
51 "/weather/cc/hmid", "/weather/cc/icon"
55 /* Possible sky conditions */
56 #define NUM_CC_CODES 6
57 const char *CC_CODES[NUM_CC_CODES] = {
58 "SKC", "CLR", "FEW", "SCT", "BKN", "OVC"
61 /* Possible weather modifiers */
62 #define NUM_WM_CODES 9
63 const char *WM_CODES[NUM_WM_CODES] = {
64 "VC", "MI", "BC", "PR", "TS", "BL",
68 /* Possible weather conditions */
69 #define NUM_WC_CODES 17
70 const char *WC_CODES[NUM_WC_CODES] = {
71 "DZ", "RA", "GR", "GS", "SN", "SG",
72 "FG", "HZ", "FU", "BR", "DU", "SA",
73 "FC", "PO", "SQ", "SS", "DS"
76 static ccurl_location_t *locations_head = 0;
78 void weather_free_info(void)
80 ccurl_free_locations(&locations_head);
83 int rel_humidity(int dew_point, int air) {
84 const float a = 17.27f;
85 const float b = 237.7f;
87 float diff = a*(dew_point/(b+dew_point)-air/(b+air));
89 return (int)(100.f*expf(diff));
91 return (int)(16.666667163372f*(6.f+diff*(6.f+diff*(3.f+diff))));
96 static void parse_cc(PWEATHER *res, xmlXPathContextPtr xpathCtx)
100 xmlXPathObjectPtr xpathObj;
102 for (i = 0; i < NUM_XPATH_EXPRESSIONS; i++) {
103 xpathObj = xmlXPathEvalExpression((xmlChar *)xpath_expression[i], xpathCtx);
104 if ((xpathObj != NULL) && (xpathObj->nodesetval->nodeTab[0]->type == XML_ELEMENT_NODE)) {
105 content = (char *)xmlNodeGetContent(xpathObj->nodesetval->nodeTab[0]);
108 strncpy(res->lastupd, content, 31);
111 res->temp = atoi(content);
114 if(res->xoap_t[0] == '\0') {
115 strncpy(res->xoap_t, content, 31);
119 res->bar = atoi(content);
122 res->wind_s = atoi(content);
125 if (isdigit((char)content[0])) {
126 res->wind_d = atoi(content);
130 res->hmid = atoi(content);
133 strncpy(res->icon, content, 2);
137 xmlXPathFreeObject(xpathObj);
142 static void parse_weather_xml(PWEATHER *res, const char *data)
145 xmlXPathContextPtr xpathCtx;
147 if (!(doc = xmlReadMemory(data, strlen(data), "", NULL, 0))) {
148 ERR("weather: can't read xml data");
152 xpathCtx = xmlXPathNewContext(doc);
153 if(xpathCtx == NULL) {
154 ERR("weather: unable to create new XPath context");
159 parse_cc(res, xpathCtx);
160 xmlXPathFreeContext(xpathCtx);
167 * Horrible hack to avoid using regexes
171 static inline void parse_token(PWEATHER *res, char *token) {
176 switch (strlen(token)) {
178 //Check all tokens 2 chars long
181 //Check if token is a weather condition
182 for (i=0; i<2; i++) {
183 if (!isalpha(token[i])) break;
186 for(i=0; i<NUM_WC_CODES; i++) {
187 if (!strncmp(token, WC_CODES[i], 2)) {
196 if (!strcmp(token, "CB")) {
203 //Check all tokens 3 chars long
206 //Check if token is a modified weather condition
207 if ((token[0] == '+') || (token[0] == '-')) {
208 for (i=1; i<3; i++) {
209 if (!isalpha(token[i])) break;
212 for(i=0; i<NUM_WC_CODES; i++) {
213 if (!strncmp(&token[1], WC_CODES[i], 2)) {
222 //Check for NCD or NSC
223 if ((!strcmp(token, "NCD")) || (!strcmp(token, "NSC"))) {
229 if (!strcmp(token, "TCU")) {
236 //Check all tokens 4 chars long
239 //Check if token is a modified weather condition
240 for(i=0; i<NUM_WM_CODES; i++) {
241 if (!strncmp(token, WM_CODES[i], 2)) {
242 for(i=0; i<NUM_WC_CODES; i++) {
243 if (!strncmp(&token[2], WC_CODES[i], 2)) {
254 //Check all tokens 5 chars long
258 if (!strcmp(token, "CAVOK")) {
263 //Check if token is the temperature
264 for (i=0; i<2; i++) {
265 if (!isdigit(token[i])) break;
267 if ((i==2) && (token[2] == '/')) {
268 for (i=3; i<5; i++) {
269 if (!isdigit(token[i])) break;
272 //First 2 digits gives the air temperature
273 res->temp=atoi(token);
275 //4th and 5th digits gives the dew point temperature
276 res->dew=atoi(&token[3]);
279 res->hmid = rel_humidity(res->dew, res->temp);
285 //Check if token is the pressure
286 if ((token[0] == 'Q') || (token[0] == 'A')) {
287 for (i=1; i<5; i++) {
288 if (!isdigit(token[i])) break;
291 if (token[0] == 'A') {
292 //Convert inches of mercury to mbar
293 res->bar = (int)(atoi(&token[1])*0.338637526f);
297 //Last 4 digits is pressure im mbar
298 res->bar = atoi(&token[1]);
303 //Check if token is a modified weather condition
304 if ((token[0] == '+') || (token[0] == '-')) {
305 for(i=0; i<NUM_WM_CODES; i++) {
306 if (!strncmp(&token[1], WM_CODES[i], 2)) {
307 for(i=0; i<NUM_WC_CODES; i++) {
308 if (!strncmp(&token[3], WC_CODES[i], 2)) {
319 //Check all tokens 6 chars long
322 //Check if token is the cloud cover
323 for (i=0; i<3; i++) {
324 if (!isalpha(token[i])) break;
327 for (i=3; i<6; i++) {
328 if (!isdigit(token[i])) break;
331 //Check if first 3 digits gives the cloud cover condition
332 for(i=0; i<NUM_CC_CODES; i++) {
333 if (!strncmp(token, CC_CODES[i], 3)) {
342 //Check if token is positive temp and negative dew
343 for (i=0; i<2; i++) {
344 if (!isdigit(token[i])) break;
346 if ((i==2) && (token[2] == '/') && (token[3] == 'M')) {
347 for (i=4; i<6; i++) {
348 if (!isdigit(token[i])) break;
351 //1st and 2nd digits gives the temperature
352 res->temp = atoi(token);
354 //5th and 6th digits gives the dew point temperature
355 res->dew = -atoi(&token[4]);
358 res->hmid = rel_humidity(res->dew, res->temp);
366 //Check all tokens 7 chars long
369 //Check if token is the observation time
370 for (i=0; i<6; i++) {
371 if (!isdigit(token[i])) break;
373 if ((i==6) && (token[6] == 'Z')) return;
375 //Check if token is the wind speed/direction in knots
376 for (i=0; i<5; i++) {
377 if (!isdigit(token[i])) break;
379 if ((i==5) && (token[5] == 'K') && (token[6] == 'T')) {
381 //First 3 digits are wind direction
382 strncpy(s_tmp, token, 3);
384 res->wind_d=atoi(s_tmp);
386 //4th and 5th digit are wind speed in knots (convert to km/hr)
387 res->wind_s = (int)(atoi(&token[3])*1.852);
392 //Check if token is negative temperature
393 if ((token[0] == 'M') && (token[4] == 'M')) {
394 for (i=1; i<3; i++) {
395 if (!isdigit(token[i])) break;
397 if ((i==3) && (token[3] == '/')) {
398 for (i=5; i<7; i++) {
399 if (!isdigit(token[i])) break;
402 //2nd and 3rd digits gives the temperature
403 res->temp = -atoi(&token[1]);
405 //6th and 7th digits gives the dew point temperature
406 res->dew = -atoi(&token[5]);
409 res->hmid = rel_humidity(res->dew, res->temp);
416 //Check if token is wind variability
417 for (i=0; i<3; i++) {
418 if (!isdigit(token[i])) break;
420 if ((i==3) && (token[3] == 'V')) {
421 for (i=4; i<7; i++) {
422 if (!isdigit(token[i])) break;
429 //Check all tokens 8 chars long
432 //Check if token is the wind speed/direction in m/s
433 for (i=0; i<5; i++) {
434 if (!isdigit(token[i])) break;
436 if ((i==5)&&(token[5] == 'M')&&(token[6] == 'P')&&(token[7] == 'S')) {
438 //First 3 digits are wind direction
439 strncpy(s_tmp, token, 3);
441 res->wind_d=atoi(s_tmp);
443 //4th and 5th digit are wind speed in m/s (convert to km/hr)
444 res->wind_s = (int)(atoi(&token[3])*3.6);
451 //printf("token : %s\n", token);
456 void parse_weather(void *result, const char *data)
458 PWEATHER *res = (PWEATHER*)result;
460 memset(res, 0, sizeof(PWEATHER));
463 //Check if it is an xml file
464 if ( strncmp(data, "<?xml ", 6) == 0 ) {
465 parse_weather_xml(res, data);
469 //We assume its a text file
471 const char delim[] = " ";
473 //Divide time stamp and metar data
474 if (sscanf(data, "%[^'\n']\n%[^'\n']", res->lastupd, s_tmp) == 2) {
480 if ((strtok_r(s_tmp, delim, &p_save)) != NULL) {
482 //Jump first token, must be icao
483 p_tok = strtok_r(NULL, delim, &p_save);
487 parse_token(res, p_tok);
488 p_tok = strtok_r(NULL, delim, &p_save);
490 } while (p_tok != NULL);
500 void weather_process_info(char *p, int p_max_size, char *uri, char *data_type, int interval)
502 static const char *wc[] = {
503 "", "drizzle", "rain", "hail", "soft hail",
504 "snow", "snow grains", "fog", "haze", "smoke",
505 "mist", "dust", "sand", "funnel cloud tornado",
506 "dust/sand", "squall", "sand storm", "dust storm"
510 ccurl_location_t *curloc = ccurl_find_location(&locations_head, uri);
511 if (!curloc->p_timed_thread) {
512 curloc->result = malloc(sizeof(PWEATHER));
513 memset(curloc->result, 0, sizeof(PWEATHER));
514 curloc->process_function = &parse_weather;
515 ccurl_init_thread(curloc, interval);
516 if (!curloc->p_timed_thread) {
517 ERR("error setting up weather thread");
521 timed_thread_lock(curloc->p_timed_thread);
522 data = (PWEATHER*)curloc->result;
523 if (strcmp(data_type, "last_update") == EQUAL) {
524 strncpy(p, data->lastupd, p_max_size);
525 } else if (strcmp(data_type, "temperature") == EQUAL) {
526 temp_print(p, p_max_size, data->temp, TEMP_CELSIUS);
527 } else if (strcmp(data_type, "cloud_cover") == EQUAL) {
529 if (data->xoap_t[0] != '\0') {
530 strncpy(p, data->xoap_t, p_max_size);
534 strncpy(p, "", p_max_size);
535 } else if (data->cc < 3) {
536 strncpy(p, "clear", p_max_size);
537 } else if (data->cc < 5) {
538 strncpy(p, "partly cloudy", p_max_size);
539 } else if (data->cc == 5) {
540 strncpy(p, "cloudy", p_max_size);
541 } else if (data->cc == 6) {
542 strncpy(p, "overcast", p_max_size);
543 } else if (data->cc == 7) {
544 strncpy(p, "towering cumulus", p_max_size);
546 strncpy(p, "cumulonimbus", p_max_size);
549 } else if (strcmp(data_type, "icon") == EQUAL) {
550 strncpy(p, data->icon, p_max_size);
552 } else if (strcmp(data_type, "pressure") == EQUAL) {
553 snprintf(p, p_max_size, "%d", data->bar);
554 } else if (strcmp(data_type, "wind_speed") == EQUAL) {
555 snprintf(p, p_max_size, "%d", data->wind_s);
556 } else if (strcmp(data_type, "wind_dir") == EQUAL) {
557 if ((data->wind_d >= 349) || (data->wind_d < 12)) {
558 strncpy(p, "N", p_max_size);
559 } else if (data->wind_d < 33) {
560 strncpy(p, "NNE", p_max_size);
561 } else if (data->wind_d < 57) {
562 strncpy(p, "NE", p_max_size);
563 } else if (data->wind_d < 79) {
564 strncpy(p, "ENE", p_max_size);
565 } else if (data->wind_d < 102) {
566 strncpy(p, "E", p_max_size);
567 } else if (data->wind_d < 124) {
568 strncpy(p, "ESE", p_max_size);
569 } else if (data->wind_d < 147) {
570 strncpy(p, "SE", p_max_size);
571 } else if (data->wind_d < 169) {
572 strncpy(p, "SSE", p_max_size);
573 } else if (data->wind_d < 192) {
574 strncpy(p, "S", p_max_size);
575 } else if (data->wind_d < 214) {
576 strncpy(p, "SSW", p_max_size);
577 } else if (data->wind_d < 237) {
578 strncpy(p, "SW", p_max_size);
579 } else if (data->wind_d < 259) {
580 strncpy(p, "WSW", p_max_size);
581 } else if (data->wind_d < 282) {
582 strncpy(p, "W", p_max_size);
583 } else if (data->wind_d < 304) {
584 strncpy(p, "WNW", p_max_size);
585 } else if (data->wind_d < 327) {
586 strncpy(p, "NW", p_max_size);
587 } else if (data->wind_d < 349) {
588 strncpy(p, "NNW", p_max_size);
590 } else if (strcmp(data_type, "wind_dir_DEG") == EQUAL) {
591 snprintf(p, p_max_size, "%d", data->wind_d);
593 } else if (strcmp(data_type, "humidity") == EQUAL) {
594 snprintf(p, p_max_size, "%d", data->hmid);
595 } else if (strcmp(data_type, "weather") == EQUAL) {
596 strncpy(p, wc[data->wc], p_max_size);
599 timed_thread_unlock(curloc->p_timed_thread);
604 /* xoap suffix for weather from weather.com */
605 static char *xoap = NULL;
608 * TODO: make the xoap keys file readable from the config file
609 * make the keys directly readable from the config file
610 * make the xoap keys file giveable as a command line option
612 void load_xoap_keys(void)
615 char *par = (char *) malloc(11 * sizeof(char));
616 char *key = (char *) malloc(17 * sizeof(char));
618 xoap = (char *) malloc(64 * sizeof(char));
619 to_real_path(xoap, XOAP_FILE);
620 fp = fopen(xoap, "r");
622 if (fscanf(fp, "%10s %16s", par, key) == 2) {
623 strcpy(xoap, "?cc=*&link=xoap&prod=xoap&par=");
625 strcat(xoap, "&key=");
627 strcat(xoap, "&unit=m");
642 int process_weather_uri(char *uri, char *locID)
644 /* locID MUST BE upper-case */
647 *tmp_p = toupper(*tmp_p);
651 /* Construct complete uri */
653 if (strstr(uri, "xoap.weather.com")) {
663 if (strstr(uri, "weather.noaa.gov")) {
666 } else if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {