Mercurial > ~darius > hgwebdir.cgi > wh1080
comparison db.c @ 0:9dab44dcb331
Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 09 Feb 2010 13:44:25 +1030 |
parents | |
children | 9da35e705144 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9dab44dcb331 |
---|---|
1 /* | |
2 * Report weather conditions to remote web sites. | |
3 * Database access and startup functions. | |
4 * | |
5 * Greg Lehey, 14 December 2009 | |
6 * | |
7 * $Id: db.c,v 1.5 2010/02/04 03:50:35 grog Exp $ | |
8 */ | |
9 | |
10 #include "wh1080.h" | |
11 | |
12 #define STAMPSIZE 32 /* size of time stamp to print out */ | |
13 | |
14 /* Command line options */ | |
15 int debug; /* -d */ | |
16 int update = 1; /* -n turns updating off */ | |
17 int recover = 0; /* -r: recover missed archive data (wh1080 only) */ | |
18 int verbose; /* -v */ | |
19 int once; /* -1 */ | |
20 | |
21 struct passwd *mypasswd; /* password information for default user */ | |
22 | |
23 /* MySQL stuff */ | |
24 MYSQL *mysql; | |
25 MYSQL_RES *mysql_result; | |
26 MYSQL_ROW mysql_row; | |
27 char mysql_querytext [1024]; /* build up queries here */ | |
28 struct config config; | |
29 | |
30 void db_login () | |
31 { | |
32 if (! mysql_init (mysql)) | |
33 { | |
34 fprintf (stderr, "Can't initialize MySQL\n"); | |
35 exit (1); | |
36 } | |
37 mysql = mysql_init (mysql); | |
38 if (! mysql) | |
39 { | |
40 fprintf (stderr, "Can't initialize MySQL: insufficient memory\n"); | |
41 exit (1); | |
42 } | |
43 if (! mysql_real_connect (mysql, /* DB state */ | |
44 config.db_host, /* database host */ | |
45 config.db_user, | |
46 config.db_passwd, | |
47 config.db, | |
48 0, | |
49 NULL, | |
50 0 )) | |
51 { | |
52 fprintf (stderr, | |
53 "Can't connect to databaase: %s (%d)\n", | |
54 mysql_error (mysql), | |
55 mysql_errno (mysql) ); | |
56 exit (1); | |
57 } | |
58 } | |
59 | |
60 /* | |
61 * Perform a database query, return result to myresult. | |
62 */ | |
63 int domyquery (char *query, MYSQL_RES **myresult) | |
64 { | |
65 if (mysql_query (mysql, query)) | |
66 { | |
67 fprintf (stderr, | |
68 "Can't query database: %s (%d)\n" | |
69 " query:%s\n", | |
70 mysql_error (mysql), | |
71 mysql_errno (mysql), | |
72 query ); | |
73 return 1; | |
74 } | |
75 *myresult = mysql_store_result (mysql); | |
76 return 0; | |
77 } | |
78 | |
79 /* | |
80 * Perform a database query, return result to implicit mysql_result. | |
81 */ | |
82 int doquery (char *query) | |
83 { | |
84 return domyquery (query, &mysql_result); | |
85 } | |
86 | |
87 /* | |
88 * Make a copy of a string in malloced memory and return it. | |
89 */ | |
90 char *mycopy (char *string) | |
91 { | |
92 char *result = malloc (strlen (string) + 1); | |
93 strcpy (result, string); | |
94 return result; | |
95 } | |
96 | |
97 /* | |
98 * Read in config from database. | |
99 * | |
100 * XXX We have a chicken and egg problem here: the config information includes | |
101 * the database name, but we need to know that before we start. For the time | |
102 * being we supply necessary information on the command line, with defaults. | |
103 * FIXME. | |
104 */ | |
105 int read_config (int argc, char *argv []) | |
106 { | |
107 int col; | |
108 int arg; | |
109 | |
110 for (arg = 1; arg < argc; arg++) | |
111 { | |
112 if (! strcmp (argv [arg], "-d")) | |
113 debug = 1; | |
114 else if (! strcmp (argv [arg], "-n")) /* turn off DB updates */ | |
115 update = 0; | |
116 else if (! strcmp (argv [arg], "-v")) /* verbose */ | |
117 verbose = 1; | |
118 else if (! strcmp (argv [arg], "-1")) /* run once and exit */ | |
119 once = 1; | |
120 else | |
121 { | |
122 if (! config.station_id) | |
123 config.station_id = argv [arg]; | |
124 else if (! config.db_user) | |
125 config.db_user = argv [arg]; | |
126 else if (! config.db_passwd) | |
127 config.db_passwd = argv [arg]; | |
128 else if (! config.db_host) | |
129 config.db_host = argv [arg]; | |
130 else if (! config.db) | |
131 config.db = argv [arg]; | |
132 else | |
133 { | |
134 fprintf (stderr, "Too many arguments: %s\n", argv [arg]); | |
135 return -1; | |
136 } | |
137 } | |
138 } | |
139 | |
140 /* Set default values for config stuff above. */ | |
141 if (! config.station_id) | |
142 config.station_id = "mystation"; | |
143 if (! config.db_host) | |
144 config.db_host = "localhost"; | |
145 if (! config.db_user) | |
146 { | |
147 /* Find out user name and use it */ | |
148 mypasswd = getpwuid (getuid ()); | |
149 if (mypasswd) | |
150 config.db_user = mycopy (mypasswd->pw_name); | |
151 else | |
152 { | |
153 fprintf (stderr, "Can't get default user id\n"); | |
154 exit (1); | |
155 } | |
156 } | |
157 if (! config.db_passwd) | |
158 config.db_passwd = ""; | |
159 if (! config.db) | |
160 config.db = "weather"; | |
161 | |
162 if (! mysql_init (mysql)) | |
163 { | |
164 fprintf (stderr, "Can't initialize MySQL\n"); | |
165 exit (1); | |
166 } | |
167 mysql = mysql_init (mysql); | |
168 if (! mysql) | |
169 { | |
170 fprintf (stderr, "Can't initialize MySQL: insufficient memory\n"); | |
171 exit (1); | |
172 } | |
173 if (! mysql_real_connect (mysql, /* DB state */ | |
174 config.db_host, /* database host */ | |
175 config.db_user, | |
176 config.db_passwd, | |
177 config.db, | |
178 0, | |
179 NULL, | |
180 0 )) | |
181 { | |
182 fprintf (stderr, | |
183 "Can't connect to databaase: %s (%d)\n", | |
184 mysql_error (mysql), | |
185 mysql_errno (mysql) ); | |
186 exit (1); | |
187 } | |
188 | |
189 /* | |
190 * Read the rest of the config from specified database. | |
191 */ | |
192 | |
193 sprintf (mysql_querytext, | |
194 "SELECT version,\n" | |
195 " latitude,\n" | |
196 " longitude,\n" | |
197 " elevation,\n" | |
198 " pressure_error,\n" | |
199 " poll_interval,\n" | |
200 " address,\n" | |
201 " wunderground_station_id,\n" | |
202 " wunderground_passwd,\n" | |
203 " wunderground_report_interval,\n" | |
204 " weatheforyou_station_id,\n" | |
205 " weatheforyou_passwd,\n" | |
206 " weatherforyou_report_interval,\n" | |
207 " website_generation_interval,\n" | |
208 " db_table,\n" | |
209 " php_header,\n" | |
210 " comparefile\n" | |
211 "FROM config\n" | |
212 "WHERE station_id = \"%s\";\n", | |
213 config.station_id ); | |
214 if (doquery (mysql_querytext)) /* failure */ | |
215 exit (1); | |
216 | |
217 if (! (mysql_row = mysql_fetch_row (mysql_result))) /* got something */ | |
218 { | |
219 fprintf (stderr, "No configuration file found\n"); | |
220 exit (1); | |
221 } | |
222 | |
223 /* | |
224 * XXX here we should perform a version check. Do it when we introduce | |
225 * version 2. | |
226 */ | |
227 /* And this is filthy ugly. Fix it */ | |
228 col = 1; | |
229 config.latitude = atof (mysql_row [col++]); | |
230 config.longitude = atof (mysql_row [col++]); | |
231 config.elevation = atof (mysql_row [col++]); /* metres */ | |
232 config.pressure_error = atof (mysql_row [col++]); /* difference between actual and real pressure, hPa */ | |
233 config.poll_interval = atoi (mysql_row [col++]); /* time, in seconds, between reading the device */ | |
234 config.address = mycopy (mysql_row [col++]); /* Address to display on web pages */ | |
235 config.wunderground_station_id = mycopy (mysql_row [col++]); | |
236 config.wunderground_passwd = mycopy (mysql_row [col++]); | |
237 config.wunderground_report_interval = atoi (mysql_row [col++]); /* how many seconds between reports to Wunderground */ | |
238 config.weatheforyou_station_id = mycopy (mysql_row [col++]); | |
239 config.weatheforyou_passwd = mycopy (mysql_row [col++]); | |
240 config.weatherforyou_report_interval = atoi (mysql_row [col++]); /* how many seconds between reports to Weather for you */ | |
241 config.website_generation_interval = atoi (mysql_row [col++]); /* how many seconds between generating PHP header file */ | |
242 config.db_table = mycopy (mysql_row [col++]); /* table */ | |
243 config.php_header = mycopy (mysql_row [col++]); /* php header file with weather data */ | |
244 config.comparefile = mycopy (mysql_row [col++]); /* file to compare local weather stations */ | |
245 return 0; /* success */ | |
246 } | |
247 |