lowercased units
[monky] / metarinfo.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * This program is licensed under BSD license, read COPYING
5  *
6  *  $Id$
7  */
8
9 #include "conky.h"
10 #include <string.h>
11 #include "ftp.h"
12 #include <math.h>
13 #include <time.h>
14 #include <pthread.h>
15 #include "metarinfo.h"
16
17 /* for threads */
18 static int status = 0;
19
20
21 int calculateRelativeHumidity(int temp, int dew)
22 {
23         float rh = 0.0;
24         rh = 100.0 * powf((112 - (0.1 * temp) + dew) /
25                           (112 + (0.9 * temp)), 8);
26         return (int) rh;
27 }
28
29 int calculateWindChill(int temperatureC, int windKn)
30 {
31         double windKmh = knTokph(windKn);
32         return (int) (13.112 + 0.6215 * temperatureC -
33                       11.37 * powf(windKmh,
34                                    .16) +
35                       0.3965 * temperatureC * powf(windKmh, .16));
36 }
37
38 int knTokph(int knSpeed)
39 {
40         return (knSpeed * 1.852);
41 }
42
43 const char *calculateWindDirectionString(int degree)
44 {
45         if (degree < 22.5) {
46                 return "North";
47         } else if (degree < 67.5) {
48                 return "Northeast";
49         } else if (degree < 112.5) {
50                 return "East";
51         } else if (degree < 157.5) {
52                 return "Southeast";
53         } else if (degree < 202.5) {
54                 return "South";
55         } else if (degree < 247.5) {
56                 return "Southwest";
57         } else if (degree < 292.5) {
58                 return "West";
59         } else if (degree < 337.5) {
60                 return "Northwest";
61         } else {
62                 return "North";
63         }
64 }
65 const char *calculateShortWindDirectionString(int degree)
66 {
67         if (degree < 22.5) {
68                 return "N";
69         } else if (degree < 67.5) {
70                 return "NE";
71         } else if (degree < 112.5) {
72                 return "E";
73         } else if (degree < 157.5) {
74                 return "SE";
75         } else if (degree < 202.5) {
76                 return "S";
77         } else if (degree < 247.5) {
78                 return "SW";
79         } else if (degree < 292.5) {
80                 return "W";
81         } else if (degree < 337.5) {
82                 return "NW";
83         } else {
84                 return "N";
85         }
86 }
87
88 void ftpData(void *userData, const char *data, int len)
89 {
90         if (userData) {
91         }                       // do nothing to make stupid warning go away
92         if (len <= 0)
93                 return;
94
95         if (data != NULL) {
96                 line = strdup(data);
97         }
98 }
99
100 extern unsigned int sleep(unsigned int);
101
102 void *fetch_ftp()
103 {
104         // try three times if it fails
105         int tries = 0;
106         do {
107                 if (tries > 0) {
108                         /* this happens too often, so i'll leave it commented fprintf(stderr, "METAR update failed for some reason, retry %i\n", tries); */
109                         sleep(15);      /* give it some time in case the server is busy or down */
110                 }
111
112                 tries++;
113                 if (strlen(metar_station) != 8) {
114                         ERR("You didn't supply a valid METAR station code\n");
115                         return NULL;
116                 }
117                 if (metar_server == NULL)
118                         metar_server = strdup("weather.noaa.gov");
119                 if (metar_path == NULL)
120                         metar_path =
121                             strdup("/data/observations/metar/stations");
122
123                 int res;
124                 initFtp();
125                 res = connectFtp(metar_server, 0);
126                 if (res < 0) {
127                         ERR("Couldn't connect to %s, retrying\n",
128                             metar_server);
129                         continue;
130                 }
131                 res = changeFtpDirectory(metar_path);
132                 if (res < 0) {
133                         ERR("Metar update failed (couldn't CWD to %s)\n",
134                             metar_path);
135                         disconnectFtp();
136                         continue;
137                 }
138                 if (res == 0) {
139                         ERR("Metar update failed\n");
140                         continue;
141                 }
142                 if (getFtp(ftpData, NULL, metar_station) < 0) {
143                         ERR("Failed to get file %s\n", metar_station);
144                 }
145
146                 disconnectFtp();
147                 if (line != NULL) {
148                         dcdNetMETAR(line, &data);
149                         free(line);
150                         line = NULL;
151                         ftp_ok = 1;
152                         metar_worked = 1;
153                 } else {
154                         ftp_ok = 0;
155                 }
156
157         } while (ftp_ok == 0 && tries < 3);
158         status = 1;
159         return NULL;
160 }
161
162 static pthread_t thread1;
163
164 void update_metar()
165 {
166         int iret1;
167         if (!status) {
168                 status = 2;
169                 iret1 = pthread_create(&thread1, NULL, fetch_ftp, NULL);
170         } else if (status == 2) {       /* thread is still running.  let's kill it and start again */
171                 pthread_cancel(thread1);
172                 status = 2;
173                 iret1 = pthread_create(&thread1, NULL, fetch_ftp, NULL);
174         } else {                /* status must be 1 */
175                 pthread_join(thread1, NULL);
176                 status = 2;
177                 iret1 = pthread_create(&thread1, NULL, fetch_ftp, NULL);
178         }
179
180 }