weather: removed constraints of 3 locations per uri from docs; corrected possible...
[monky] / src / weather.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * Please see COPYING for details
5  *
6  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
7  *      (see AUTHORS)
8  * All rights reserved.
9  *
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.
14  *
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/>.
21  *
22  */
23
24 #include "conky.h"
25 #include "logging.h"
26 #include "weather.h"
27 #include "temphelper.h"
28 #include <time.h>
29 #include <ctype.h>
30 #include <curl/curl.h>
31 #include <curl/types.h>
32 #include <curl/easy.h>
33
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"
38 };
39
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",
44         "SH", "DR", "FZ"
45 };
46
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",
53 };
54
55 typedef struct location_ {
56         char *uri;
57         int last_update;
58         PWEATHER data;
59         timed_thread *p_timed_thread;
60         struct location_ *next;
61 } location;
62
63 static location *locations_head = 0;
64
65 location *find_location(char *uri)
66 {
67         location *tail = locations_head;
68         location *new = 0;
69         while (tail) {
70                 if (tail->uri &&
71                                 strcmp(tail->uri, uri) == EQUAL) {
72                         return tail;
73                 }
74                 tail = tail->next;
75         }
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) {
82                         tail = tail->next;
83                 }
84                 if (!tail) {
85                         // omg the first one!!!!!!!
86                         locations_head = new;
87                 } else {
88                         tail->next = new;
89                 }
90         }
91         return new;
92 }
93
94 void free_weather_info(void)
95 {
96         location *tail = locations_head;
97         location *last = 0;
98
99         while (tail) {
100                 if (tail->uri) free(tail->uri);
101                 last = tail;
102                 tail = tail->next;
103                 free(last);
104         }
105         locations_head = 0;
106 }
107
108 int rel_humidity(int dew_point, int air) {
109         const float a = 17.27f;
110         const float b = 237.7f;
111
112         float g = a*dew_point/(b+dew_point);
113         return (int)(100.f*expf(g-a*air/(b+air)));
114 }
115
116 /*
117  * Horrible hack to avoid using regexes
118  *
119  */
120
121 static inline void parse_token(PWEATHER *res, char *token) {
122
123         int i;
124         char s_tmp[64];
125
126         switch (strlen(token)) {
127
128                 //Check all tokens 2 chars long
129                 case 2:
130
131                         //Check if token is a weather condition
132                         for (i=0; i<2; i++) {
133                                 if (!isalpha(token[i])) break;
134                         }
135                         if (i==2) {
136                                 for(i=0; i<NUM_WC_CODES; i++) {
137                                         if (!strncmp(token, WC_CODES[i], 2)) {
138                                                 res->wc=i+1;
139                                                 break;
140                                         }
141                                 }
142                                 return;
143                         }
144
145                         //Check for CB
146                         if (!strcmp(token, "CB")) {
147                                 res->cc = 8;
148                                 return;
149                         }
150
151                         break;
152
153                         //Check all tokens 3 chars long
154                 case 3:
155
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;
160                                 }
161                                 if (i==3) {
162                                         for(i=0; i<NUM_WC_CODES; i++) {
163                                                 if (!strncmp(&token[1], WC_CODES[i], 2)) {
164                                                         res->wc=i+1;
165                                                         break;
166                                                 }
167                                         }
168                                         return;
169                                 }
170                         }
171
172                         //Check for NCD or NSC
173                         if ((!strcmp(token, "NCD")) || (!strcmp(token, "NSC"))) {
174                                 res->cc = 1;
175                                 return;
176                         }
177
178                         //Check for TCU
179                         if (!strcmp(token, "TCU")) {
180                                 res->cc = 7;
181                                 return;
182                         }
183
184                         break;
185
186                         //Check all tokens 4 chars long
187                 case 4:
188
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)) {
194                                                         res->wc=i+1;
195                                                         return;
196                                                 }
197                                         }
198                                         break;
199                                 }
200                         }
201
202                         break;
203
204                         //Check all tokens 5 chars long
205                 case 5:
206
207                         //Check for CAVOK
208                         if (!strcmp(token, "CAVOK")) {
209                                 res->cc = 1;
210                                 return;
211                         }
212
213                         //Check if token is the temperature
214                         for (i=0; i<2; i++) {
215                                 if (!isdigit(token[i])) break;
216                         }
217                         if ((i==2) && (token[2] == '/')) {
218                                 for (i=3; i<5; i++) {
219                                         if (!isdigit(token[i])) break;
220                                 }
221                                 if (i==5) {
222                                         //First 2 digits gives the air temperature
223                                         res->temp=atoi(token);
224
225                                         //4th and 5th digits gives the dew point temperature
226                                         res->dew=atoi(&token[3]);
227
228                                         //Compute humidity
229                                         res->hmid = rel_humidity(res->dew, res->temp);
230
231                                         return;
232                                 }
233                         }
234
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;
239                                 }
240                                 if (i==5) {
241                                         if (token[0] == 'A') {
242                                                 //Convert inches of mercury to mbar
243                                                 res->bar = (int)(atoi(&token[1])*0.338637526f);
244                                                 return;
245                                         }
246
247                                         //Last 4 digits is pressure im mbar
248                                         res->bar = atoi(&token[1]);
249                                         return;
250                                 }
251                         }
252
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)) {
259                                                                 res->wc=i+1;
260                                                                 return;
261                                                         }
262                                                 }
263                                                 break;
264                                         }
265                                 }
266                         }
267                         break;
268
269                         //Check all tokens 6 chars long
270                 case 6:
271
272                         //Check if token is the cloud cover
273                         for (i=0; i<3; i++) {
274                                 if (!isalpha(token[i])) break;
275                         }
276                         if (i==3) {
277                                 for (i=3; i<6; i++) {
278                                         if (!isdigit(token[i])) break;
279                                 }
280                                 if (i==6) {
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)) {
284                                                         res->cc=i+1;
285                                                         break;
286                                                 }
287                                         }
288                                         return;
289                                 }
290                         }
291
292                         //Check if token is positive temp and negative dew
293                         for (i=0; i<2; i++) {
294                                 if (!isdigit(token[i])) break;
295                         }
296                         if ((i==2) && (token[2] == '/')  && (token[3] == 'M')) {
297                                 for (i=4; i<6; i++) {
298                                         if (!isdigit(token[i])) break;
299                                 }
300                                 if (i==6) {
301                                         //1st and 2nd digits gives the temperature
302                                         res->temp = atoi(token);
303
304                                         //5th and 6th digits gives the dew point temperature
305                                         res->dew = -atoi(&token[4]);
306
307                                         //Compute humidity
308                                         res->hmid = rel_humidity(res->dew, res->temp);
309
310                                         return;
311                                 }
312                         }
313
314                         break;
315
316                         //Check all tokens 7 chars long
317                 case 7:
318
319                         //Check if token is the observation time
320                         for (i=0; i<6; i++) {
321                                 if (!isdigit(token[i])) break;
322                         }
323                         if ((i==6) && (token[6] == 'Z')) return;
324
325                         //Check if token is the wind speed/direction in knots
326                         for (i=0; i<5; i++) {
327                                 if (!isdigit(token[i])) break;
328                         }
329                         if ((i==5) && (token[5] == 'K') &&  (token[6] == 'T')) {
330
331                                 //First 3 digits are wind direction
332                                 strncpy(s_tmp, token, 3);
333                                 res->wind_d=atoi(s_tmp);
334
335                                 //4th and 5th digit are wind speed in knots (convert to km/hr)
336                                 res->wind_s = (int)(atoi(&token[3])*1.852);
337
338                                 return;
339                         }
340
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;
345                                 }
346                                 if ((i==3) && (token[3] == '/')) {
347                                         for (i=5; i<7; i++) {
348                                                 if (!isdigit(token[i])) break;
349                                         }
350                                         if (i==7) {
351                                                 //2nd and 3rd digits gives the temperature
352                                                 res->temp = -atoi(&token[1]);
353
354                                                 //6th and 7th digits gives the dew point temperature
355                                                 res->dew = -atoi(&token[5]);
356
357                                                 //Compute humidity
358                                                 res->hmid = rel_humidity(res->dew, res->temp);
359
360                                                 return;
361                                         }
362                                 }
363                         }
364
365                         //Check if token is wind variability
366                         for (i=0; i<3; i++) {
367                                 if (!isdigit(token[i])) break;
368                         }
369                         if ((i==3) && (token[3] == 'V')) {
370                                 for (i=4; i<7; i++) {
371                                         if (!isdigit(token[i])) break;
372                                 }
373                                 if (i==7) return;
374                         }
375
376                         break;
377
378                         //Check all tokens 8 chars long
379                 case 8:
380
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;
384                         }
385                         if ((i==5)&&(token[5] == 'M')&&(token[6] == 'P')&&(token[7] == 'S')) {
386
387                                 //First 3 digits are wind direction
388                                 strncpy(s_tmp, token, 3);
389                                 res->wind_d=atoi(s_tmp);
390
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);
393
394                                 return;
395                         }
396
397                 default:
398
399                         //printf("token : %s\n", token);
400                         break;
401         }
402 }
403
404 static void parse_weather(PWEATHER *res, const char *data)
405 {
406         char s_tmp[256];
407         const char delim[] = " ";
408
409         memset(res, 0, sizeof(PWEATHER));
410
411         //Divide time stamp and metar data
412         if (sscanf(data, "%[^'\n']\n%[^'\n']", res->lastupd, s_tmp) == 2) {
413
414                 //Process all tokens
415                 char *p_tok = NULL;
416                 char *p_save = NULL;
417
418                 if ((strtok_r(s_tmp, delim, &p_save)) != NULL) {
419
420                         //Jump first token, must be icao
421                         p_tok = strtok_r(NULL, delim, &p_save);
422
423                         do {
424
425                                 parse_token(res, p_tok);
426                                 p_tok = strtok_r(NULL, delim, &p_save);
427
428                         } while (p_tok != NULL);
429                 }
430                 return;
431         }
432         else {
433                 return;
434         }
435 }
436
437
438 void fetch_weather_info(location *curloc)
439 {
440         CURL *curl = NULL;
441         CURLcode res;
442
443         // curl temps
444         struct MemoryStruct chunk;
445
446         chunk.memory = NULL;
447         chunk.size = 0;
448
449         curl = curl_easy_init();
450         if (curl) {
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");
456
457                 res = curl_easy_perform(curl);
458                 if (chunk.size) {
459                         timed_thread_lock(curloc->p_timed_thread);
460                         parse_weather(&curloc->data, chunk.memory);
461                         timed_thread_unlock(curloc->p_timed_thread);
462                         free(chunk.memory);
463                 } else {
464                         ERR("No data from server");
465                 }
466
467                 curl_easy_cleanup(curl);
468         }
469
470         return;
471 }
472
473 void *weather_thread(void *) __attribute__((noreturn));
474
475 void init_thread(location *curloc, int interval)
476 {
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");
482         }
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");
487         }
488 }
489
490
491 void process_weather_info(char *p, int p_max_size, char *uri, char *data_type, int interval)
492 {
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"
498         };
499
500         location *curloc = find_location(uri);
501         if (!curloc->p_timed_thread) init_thread(curloc, interval);
502
503
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);
522                 } else  {
523                         strncpy(p, "cumulonimbus", p_max_size);
524                 }
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);
562                 };
563         } else if (strcmp(data_type, "wind_dir_DEG") == EQUAL) {
564                 snprintf(p, p_max_size, "%d", curloc->data.wind_d);
565
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);
570         }
571         timed_thread_unlock(curloc->p_timed_thread);
572 }
573
574 void *weather_thread(void *arg)
575 {
576         location *curloc = (location*)arg;
577
578         while (1) {
579                 fetch_weather_info(curloc);
580                 if (timed_thread_test(curloc->p_timed_thread, 0)) {
581                         timed_thread_exit(curloc->p_timed_thread);
582                 }
583         }
584         /* never reached */
585 }
586