/*  open2300 - fetch2300.c
 *  
 *  Version 1.10
 *  
 *  Control WS2300 weather station
 *  
 *  Copyright 2003-2005, Kenneth Lavrsen
 *  This program is published under the GNU General Public license
 */

#include "rw2300.h"

 
/********** MAIN PROGRAM ************************************************
 *
 * This program reads all current and min/max data from a WS2300
 * weather station and write it to standard out. This is the program that
 * the program weatherstation.php uses to display a nice webpage with
 * current weather data.
 *
 * It takes one parameter which is 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;
	unsigned char logline[3000] = "";
	char datestring[50];     //used to hold the date stamp for the log file
	const char *directions[]= {"E","EEK","EK","KEK","K","KDK","DK","DDK",
	                           "D","DDN","DN","NDN","N","NEN","EN","EEN"};
	float wind_x,wind_y;int w_idx;

	double winddir[6];
	char tendency[15];
	char forecast[15];
	struct config_type config;
	double tempfloat_min, tempfloat_max;
	int tempint, tempint_min, tempint_max;
	struct timestamp time_min, time_max;
	time_t basictime;

	get_configuration(&config, argv[1]);

	ws2300 = open_weatherstation(config.serial_device_name);

	/* READ WIND SPEED AND DIRECTION */
	
	printf("Szelsebesseg(m/s)=%.1f\n",
	       wind_current(ws2300, config.wind_speed_conv_factor, winddir));
	printf("Szelirany=%.1f\n", winddir[0]);
	w_idx = (int)(winddir[0]/22.5);			
	if (w_idx==0) { wind_x = 0; wind_y=1; } //E	
	if (w_idx==1) { wind_x = 0.382; wind_y=0.923; } //EEK	
	if (w_idx==2) { wind_x = 0.707; wind_y=0.707; } //EK	
	if (w_idx==3) { wind_x = 0.923; wind_y=0.382; } //KEK	
	
	if (w_idx==4) { wind_x = 1; wind_y=0; } //K	
	if (w_idx==5) { wind_x = 0.923; wind_y=-0.382; } //KDK	
	if (w_idx==6) { wind_x = 0.707; wind_y=-0.707; } //DK	
	if (w_idx==7) { wind_x = 0.382; wind_y=-0.923; } //DDK	
	
	if (w_idx==8) { wind_x = 0; wind_y=-1; } //D	
	if (w_idx==9) { wind_x = -0.382; wind_y=-0.923; } //DDN	
	if (w_idx==10) { wind_x = -0.707; wind_y=-0.707; } //DN	
	if (w_idx==11) { wind_x = -0.923; wind_y=-0.382; } //NDN	

	if (w_idx==12) { wind_x = -1; wind_y=0; } //N	
	if (w_idx==13) { wind_x = -0.923; wind_y=0.382; } //NENY	
	if (w_idx==14) { wind_x = -0.707; wind_y=0.707; } //ENY	
	if (w_idx==15) { wind_x = -0.382; wind_y=0.923; } //EENY	
	printf("Szelirany_xy=%.1f %.1f\n", wind_x,wind_y);

	close_weatherstation(ws2300);

	return(0);
}

