/*       mysql2300.c
 *
 *       Version 1.4?
 *
 *       Get data from WS2300 weather station
 *       and add them to MySQL database
 *
 *       Copyright 2003,2004, Kenneth Lavrsen/Thomas Grieder
 *
 *       This program is published under the GNU General Public license
 *
 *  0.1  2004 Feb 21  Thomas Grieder
 *       Creates mysql2300. A Rewrite of log2300.
 *       Logline is now comma delimited for added support to write
 *       to MySQL
 *       (see also http://www.unixreview.com/documents/s=8989/ur0401a/)
 *
 *  1.2  2004 Mar 07  Kenneth Lavrsen
 *       Complete rewrite of the code to support the new rw2300 library.
 *       Logfile feature removed to make it a clean MySQL program.
 *       Added support for config file and program should be able to
 *       compile under Windows with the right MySQL client headers
 *       and libraries installed.
 *
 *  1.3  As 1.2
 */

#include <mysql.h>
#include "rw2300.h"

 
/********** MAIN PROGRAM ************************************************
 *
 * This program reads current weather data from a WS2300
 * and writes the data to a MySQL database.
 *
 * The open2300.conf config file must contain the following parameters
 * 
 * Table structure for table `weather`
 * 
DROP TABLE IF EXISTS `weather`;
CREATE TABLE `weather` (
  `datetime` datetime NOT NULL default '0000-00-00 00:00:00',
  `temp_in` decimal(3,1) NOT NULL default '0.0',
  `temp_out` decimal(3,1) NOT NULL default '0.0',
  `dewpoint` decimal(3,1) NOT NULL default '0.0',
  `rel_hum_in` tinyint(3) NOT NULL default '0',
  `rel_hum_out` tinyint(3) NOT NULL default '0',
  `wind_speed` decimal(3,1) NOT NULL default '0.0',
# SECTION MODIFIEE (4,1)
  `wind_angle` decimal(4,1) NOT NULL default '0.0',
# FIN SECTION MODIFIEE
  `wind_direction` char(3) NOT NULL default '',
# SECTION AJOUTEE 
  `wind_speed_min` decimal(3,1) NOT NULL default '0.0',
  `wind_speed_max` decimal(3,1) NOT NULL default '0.0',
# FIN SECTION AJOUTEE
  `wind_chill` decimal(3,1) NOT NULL default '0.0',
  `rain_1h` decimal(3,1) NOT NULL default '0.0',
  `rain_24h` decimal(3,1) NOT NULL default '0.0',
  `rain_total` decimal(4,1) NOT NULL default '0.0',
  `rel_pressure` decimal(4,1) NOT NULL default '0.0',
  `tendency` varchar(7) NOT NULL default '',
  `forecast` varchar(6) NOT NULL default '',
  UNIQUE KEY `timestamp` (`datetime`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;



 * It takes one parameters. The config file name with path
 * If this parameter is omitted the program will look at the default paths
 * See the open2300.conf-dist file for info
 *
 ***********************************************************************/
int main(int argc, char *argv[])
{
	WEATHERSTATION ws2300;
	MYSQL mysql;
	unsigned char logline[3000] = "";
	const char *directions[]= {"N","NNE","NE","ENE","E","ESE","SE","SSE",
	                           "S","SSW","SW","WSW","W","WNW","NW","NNW"};
	double winddir[6];
	int tempint;
/* SECTION AJOUTEE */
	double tempfloat_min;
	double tempfloat_max;
/* FIN SECTION AJOUTEE */
	char tendency[15];
	char forecast[15];
	struct config_type config;
	char query[4096];

	get_configuration(&config, argv[1]);
	ws2300 = open_weatherstation(config.serial_device_name);

	/* READ TEMPERATURE INDOOR */
	sprintf(logline,"%s\'%.1f\',", logline,
	        temperature_indoor(ws2300, config.temperature_conv) );
	
	/* READ TEMPERATURE OUTDOOR */
	sprintf(logline,"%s\'%.1f\',", logline,
	        temperature_outdoor(ws2300, config.temperature_conv) );
	
	/* READ DEWPOINT */
	sprintf(logline,"%s\'%.1f\',", logline,
	        dewpoint(ws2300, config.temperature_conv) );
	
	/* READ RELATIVE HUMIDITY INDOOR */
	sprintf(logline,"%s\'%d\',", logline, humidity_indoor(ws2300) );
	
	/* READ RELATIVE HUMIDITY OUTDOOR */
	sprintf(logline,"%s\'%d\',", logline, humidity_outdoor(ws2300) );

	/* READ WIND SPEED AND DIRECTION */
	sprintf(logline,"%s\'%.1f\',", logline,
	        wind_all(ws2300, config.wind_speed_conv_factor,
	                 &tempint, winddir) );
	sprintf(logline,"%s\'%.1f\',\'%s\',", logline,
	        winddir[0], directions[tempint]);

/* SECTION AJOUTEE */
	/* READ WINDSPEED MIN/MAX */
	//on ne veut pas les heures de prélèvement, seuls les mini et maxi suffiront
	wind_minmax(ws2300, config.wind_speed_conv_factor, &tempfloat_min,
	            &tempfloat_max,NULL,NULL);
 	sprintf(logline,"%s\'%.1f\',", logline, tempfloat_min);
	sprintf(logline,"%s\'%.1f\',", logline, tempfloat_max);
	/* RESET MIN WIND */
	wind_reset(ws2300, RESET_MIN);
	/* RESET MAX WIND */
	wind_reset(ws2300, RESET_MAX);
/* FIN SECTION AJOUTEE */

	/* READ WINDCHILL */
	sprintf(logline,"%s\'%.1f\',", logline,
	        windchill(ws2300, config.temperature_conv) );

	/* READ RAIN 1H */
	sprintf(logline,"%s\'%.1f\',", logline,
	        rain_1h(ws2300, config.rain_conv_factor) );
	
	/* READ RAIN 24H */
	sprintf(logline,"%s\'%.1f\',", logline,
	        rain_24h(ws2300, config.rain_conv_factor) );
	
	/* READ RAIN TOTAL */
	sprintf(logline,"%s\'%.1f\',", logline,
	        rain_total(ws2300, config.rain_conv_factor) );

	/* READ RELATIVE PRESSURE */
	sprintf(logline,"%s\'%.1f\',", logline,	
	        rel_pressure(ws2300, config.pressure_conv_factor) );

	/* READ TENDENCY AND FORECAST */
	tendency_forecast(ws2300, tendency, forecast);
	sprintf(logline,"%s\'%s\',\'%s\'", logline, tendency, forecast);

	/* CLOSE THE WEATHER STATION TO ENABLE OTHER PROGRAMS TO ACCESS */
	close_weatherstation(ws2300);

	/* INIT MYSQL AND CONNECT */
	if(!mysql_init(&mysql))
	{
	fprintf(stderr, "Cannot initialize MySQL");
	exit(1);
	}

	if(!mysql_real_connect(&mysql, config.mysql_host, config.mysql_user,
	                       config.mysql_passwd, config.mysql_database,
	                       config.mysql_port, NULL, 0))
	{
		fprintf(stderr, "%d: %s \n",
		mysql_errno(&mysql), mysql_error(&mysql));
		exit(1);
	}

	sprintf(query, "INSERT INTO weather VALUES ( NOW(), %s )", logline);

	if(mysql_query(&mysql, query))
	{
		fprintf(stderr, "Could not insert row. %s %d: \%s \n", query, mysql_errno(&mysql), mysql_error(&mysql));
		mysql_close(&mysql);
		exit(1);
	}

	mysql_close(&mysql);

	return(0);
}
