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