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