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 7
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",
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);
134 xmlXPathFreeObject(xpathObj);
139 static void parse_weather_xml(PWEATHER *res, const char *data)
142 xmlXPathContextPtr xpathCtx;
144 if (!(doc = xmlReadMemory(data, strlen(data), "", NULL, 0))) {
145 ERR("weather: can't read xml data");
149 xpathCtx = xmlXPathNewContext(doc);
150 if(xpathCtx == NULL) {
151 ERR("weather: unable to create new XPath context");
156 parse_cc(res, xpathCtx);
157 xmlXPathFreeContext(xpathCtx);
164 * Horrible hack to avoid using regexes
168 static inline void parse_token(PWEATHER *res, char *token) {
173 switch (strlen(token)) {
175 //Check all tokens 2 chars long
178 //Check if token is a weather condition
179 for (i=0; i<2; i++) {
180 if (!isalpha(token[i])) break;
183 for(i=0; i<NUM_WC_CODES; i++) {
184 if (!strncmp(token, WC_CODES[i], 2)) {
193 if (!strcmp(token, "CB")) {
200 //Check all tokens 3 chars long
203 //Check if token is a modified weather condition
204 if ((token[0] == '+') || (token[0] == '-')) {
205 for (i=1; i<3; i++) {
206 if (!isalpha(token[i])) break;
209 for(i=0; i<NUM_WC_CODES; i++) {
210 if (!strncmp(&token[1], WC_CODES[i], 2)) {
219 //Check for NCD or NSC
220 if ((!strcmp(token, "NCD")) || (!strcmp(token, "NSC"))) {
226 if (!strcmp(token, "TCU")) {
233 //Check all tokens 4 chars long
236 //Check if token is a modified weather condition
237 for(i=0; i<NUM_WM_CODES; i++) {
238 if (!strncmp(token, WM_CODES[i], 2)) {
239 for(i=0; i<NUM_WC_CODES; i++) {
240 if (!strncmp(&token[2], WC_CODES[i], 2)) {
251 //Check all tokens 5 chars long
255 if (!strcmp(token, "CAVOK")) {
260 //Check if token is the temperature
261 for (i=0; i<2; i++) {
262 if (!isdigit(token[i])) break;
264 if ((i==2) && (token[2] == '/')) {
265 for (i=3; i<5; i++) {
266 if (!isdigit(token[i])) break;
269 //First 2 digits gives the air temperature
270 res->temp=atoi(token);
272 //4th and 5th digits gives the dew point temperature
273 res->dew=atoi(&token[3]);
276 res->hmid = rel_humidity(res->dew, res->temp);
282 //Check if token is the pressure
283 if ((token[0] == 'Q') || (token[0] == 'A')) {
284 for (i=1; i<5; i++) {
285 if (!isdigit(token[i])) break;
288 if (token[0] == 'A') {
289 //Convert inches of mercury to mbar
290 res->bar = (int)(atoi(&token[1])*0.338637526f);
294 //Last 4 digits is pressure im mbar
295 res->bar = atoi(&token[1]);
300 //Check if token is a modified weather condition
301 if ((token[0] == '+') || (token[0] == '-')) {
302 for(i=0; i<NUM_WM_CODES; i++) {
303 if (!strncmp(&token[1], WM_CODES[i], 2)) {
304 for(i=0; i<NUM_WC_CODES; i++) {
305 if (!strncmp(&token[3], WC_CODES[i], 2)) {
316 //Check all tokens 6 chars long
319 //Check if token is the cloud cover
320 for (i=0; i<3; i++) {
321 if (!isalpha(token[i])) break;
324 for (i=3; i<6; i++) {
325 if (!isdigit(token[i])) break;
328 //Check if first 3 digits gives the cloud cover condition
329 for(i=0; i<NUM_CC_CODES; i++) {
330 if (!strncmp(token, CC_CODES[i], 3)) {
339 //Check if token is positive temp and negative dew
340 for (i=0; i<2; i++) {
341 if (!isdigit(token[i])) break;
343 if ((i==2) && (token[2] == '/') && (token[3] == 'M')) {
344 for (i=4; i<6; i++) {
345 if (!isdigit(token[i])) break;
348 //1st and 2nd digits gives the temperature
349 res->temp = atoi(token);
351 //5th and 6th digits gives the dew point temperature
352 res->dew = -atoi(&token[4]);
355 res->hmid = rel_humidity(res->dew, res->temp);
363 //Check all tokens 7 chars long
366 //Check if token is the observation time
367 for (i=0; i<6; i++) {
368 if (!isdigit(token[i])) break;
370 if ((i==6) && (token[6] == 'Z')) return;
372 //Check if token is the wind speed/direction in knots
373 for (i=0; i<5; i++) {
374 if (!isdigit(token[i])) break;
376 if ((i==5) && (token[5] == 'K') && (token[6] == 'T')) {
378 //First 3 digits are wind direction
379 strncpy(s_tmp, token, 3);
381 res->wind_d=atoi(s_tmp);
383 //4th and 5th digit are wind speed in knots (convert to km/hr)
384 res->wind_s = (int)(atoi(&token[3])*1.852);
389 //Check if token is negative temperature
390 if ((token[0] == 'M') && (token[4] == 'M')) {
391 for (i=1; i<3; i++) {
392 if (!isdigit(token[i])) break;
394 if ((i==3) && (token[3] == '/')) {
395 for (i=5; i<7; i++) {
396 if (!isdigit(token[i])) break;
399 //2nd and 3rd digits gives the temperature
400 res->temp = -atoi(&token[1]);
402 //6th and 7th digits gives the dew point temperature
403 res->dew = -atoi(&token[5]);
406 res->hmid = rel_humidity(res->dew, res->temp);
413 //Check if token is wind variability
414 for (i=0; i<3; i++) {
415 if (!isdigit(token[i])) break;
417 if ((i==3) && (token[3] == 'V')) {
418 for (i=4; i<7; i++) {
419 if (!isdigit(token[i])) break;
426 //Check all tokens 8 chars long
429 //Check if token is the wind speed/direction in m/s
430 for (i=0; i<5; i++) {
431 if (!isdigit(token[i])) break;
433 if ((i==5)&&(token[5] == 'M')&&(token[6] == 'P')&&(token[7] == 'S')) {
435 //First 3 digits are wind direction
436 strncpy(s_tmp, token, 3);
438 res->wind_d=atoi(s_tmp);
440 //4th and 5th digit are wind speed in m/s (convert to km/hr)
441 res->wind_s = (int)(atoi(&token[3])*3.6);
448 //printf("token : %s\n", token);
453 void parse_weather(void *result, const char *data)
455 PWEATHER *res = (PWEATHER*)result;
457 memset(res, 0, sizeof(PWEATHER));
460 //Check if it is an xml file
461 if ( strncmp(data, "<?xml ", 6) == 0 ) {
462 parse_weather_xml(res, data);
466 //We assume its a text file
468 const char delim[] = " ";
470 //Divide time stamp and metar data
471 if (sscanf(data, "%[^'\n']\n%[^'\n']", res->lastupd, s_tmp) == 2) {
477 if ((strtok_r(s_tmp, delim, &p_save)) != NULL) {
479 //Jump first token, must be icao
480 p_tok = strtok_r(NULL, delim, &p_save);
484 parse_token(res, p_tok);
485 p_tok = strtok_r(NULL, delim, &p_save);
487 } while (p_tok != NULL);
497 void weather_process_info(char *p, int p_max_size, char *uri, char *data_type, int interval)
499 static const char *wc[] = {
500 "", "drizzle", "rain", "hail", "soft hail",
501 "snow", "snow grains", "fog", "haze", "smoke",
502 "mist", "dust", "sand", "funnel cloud tornado",
503 "dust/sand", "squall", "sand storm", "dust storm"
507 ccurl_location_t *curloc = ccurl_find_location(&locations_head, uri);
508 if (!curloc->p_timed_thread) {
509 curloc->result = malloc(sizeof(PWEATHER));
510 memset(curloc->result, 0, sizeof(PWEATHER));
511 curloc->process_function = &parse_weather;
512 ccurl_init_thread(curloc, interval);
513 if (!curloc->p_timed_thread) {
514 ERR("error setting up weather thread");
518 timed_thread_lock(curloc->p_timed_thread);
519 data = (PWEATHER*)curloc->result;
520 if (strcmp(data_type, "last_update") == EQUAL) {
521 strncpy(p, data->lastupd, p_max_size);
522 } else if (strcmp(data_type, "temperature") == EQUAL) {
523 temp_print(p, p_max_size, data->temp, TEMP_CELSIUS);
524 } else if (strcmp(data_type, "cloud_cover") == EQUAL) {
526 if (data->xoap_t[0] != '\0') {
527 strncpy(p, data->xoap_t, p_max_size);
531 strncpy(p, "", p_max_size);
532 } else if (data->cc < 3) {
533 strncpy(p, "clear", p_max_size);
534 } else if (data->cc < 5) {
535 strncpy(p, "partly cloudy", p_max_size);
536 } else if (data->cc == 5) {
537 strncpy(p, "cloudy", p_max_size);
538 } else if (data->cc == 6) {
539 strncpy(p, "overcast", p_max_size);
540 } else if (data->cc == 7) {
541 strncpy(p, "towering cumulus", p_max_size);
543 strncpy(p, "cumulonimbus", p_max_size);
545 } else if (strcmp(data_type, "pressure") == EQUAL) {
546 snprintf(p, p_max_size, "%d", data->bar);
547 } else if (strcmp(data_type, "wind_speed") == EQUAL) {
548 snprintf(p, p_max_size, "%d", data->wind_s);
549 } else if (strcmp(data_type, "wind_dir") == EQUAL) {
550 if ((data->wind_d >= 349) || (data->wind_d < 12)) {
551 strncpy(p, "N", p_max_size);
552 } else if (data->wind_d < 33) {
553 strncpy(p, "NNE", p_max_size);
554 } else if (data->wind_d < 57) {
555 strncpy(p, "NE", p_max_size);
556 } else if (data->wind_d < 79) {
557 strncpy(p, "ENE", p_max_size);
558 } else if (data->wind_d < 102) {
559 strncpy(p, "E", p_max_size);
560 } else if (data->wind_d < 124) {
561 strncpy(p, "ESE", p_max_size);
562 } else if (data->wind_d < 147) {
563 strncpy(p, "SE", p_max_size);
564 } else if (data->wind_d < 169) {
565 strncpy(p, "SSE", p_max_size);
566 } else if (data->wind_d < 192) {
567 strncpy(p, "S", p_max_size);
568 } else if (data->wind_d < 214) {
569 strncpy(p, "SSW", p_max_size);
570 } else if (data->wind_d < 237) {
571 strncpy(p, "SW", p_max_size);
572 } else if (data->wind_d < 259) {
573 strncpy(p, "WSW", p_max_size);
574 } else if (data->wind_d < 282) {
575 strncpy(p, "W", p_max_size);
576 } else if (data->wind_d < 304) {
577 strncpy(p, "WNW", p_max_size);
578 } else if (data->wind_d < 327) {
579 strncpy(p, "NW", p_max_size);
580 } else if (data->wind_d < 349) {
581 strncpy(p, "NNW", p_max_size);
583 } else if (strcmp(data_type, "wind_dir_DEG") == EQUAL) {
584 snprintf(p, p_max_size, "%d", data->wind_d);
586 } else if (strcmp(data_type, "humidity") == EQUAL) {
587 snprintf(p, p_max_size, "%d", data->hmid);
588 } else if (strcmp(data_type, "weather") == EQUAL) {
589 strncpy(p, wc[data->wc], p_max_size);
592 timed_thread_unlock(curloc->p_timed_thread);
597 /* xoap suffix for weather from weather.com */
598 static char *xoap = NULL;
601 * TODO: make the xoap keys file readable from the config file
602 * make the keys directly readable from the config file
603 * make the xoap keys file giveable as a command line option
605 void load_xoap_keys(void)
608 char *par = (char *) malloc(11 * sizeof(char));
609 char *key = (char *) malloc(17 * sizeof(char));
611 xoap = (char *) malloc(64 * sizeof(char));
612 to_real_path(xoap, XOAP_FILE);
613 fp = fopen(xoap, "r");
615 if (fscanf(fp, "%10s %16s", par, key) == 2) {
616 strcpy(xoap, "?cc=*&link=xoap&prod=xoap&par=");
618 strcat(xoap, "&key=");
620 strcat(xoap, "&unit=m");
635 int process_weather_uri(char *uri, char *locID)
637 /* locID MUST BE upper-case */
640 *tmp_p = toupper(*tmp_p);
644 /* Construct complete uri */
646 if (strstr(uri, "xoap.weather.com")) {
656 if (strstr(uri, "weather.noaa.gov")) {
659 } else if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {