/*  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
 *
 ***********************************************************************/
	WEATHERSTATION ws2300;
	unsigned char logline[3000] = "";
	char datestring[50];     //used to hold the date stamp for the log file
	const char *directions[]= {"N","NNE","NE","ENE","E","ESE","SE","SSE",
	                           "S","SSW","SW","WSW","W","WNW","NW","NNW"};
	FILE *w_file;
	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;
	int w_idx; float wind_x,wind_y;char f_t;
	time_t raw_time0,raw_time;
	double t_in,t_out,r_p,a_p,r_1h,r_24h,wind,all_wind,rh_in,rh_out,w_x,w_y,w_dir,last_w,last_wx,last_wy;
	double max_wind,avg_wind;

	double dir_x[16],dir_y[16];

void get_wind()
{
        wind=wind_current(ws2300, config.wind_speed_conv_factor, winddir);
	w_idx = (int)(winddir[0]/22.5);			
	wind_x=dir_x[w_idx];
	wind_y=dir_y[w_idx];
	
	}

void init_wind()
{
	dir_x[0] = 0; 		dir_y[0]=1;  //E	
	dir_x[1] = 0.382; 	dir_y[1]=0.923;  //EEK	
	dir_x[2] = 0.707; 	dir_y[2]=0.707;  //EK	
	dir_x[3] = 0.923; 	dir_y[3]=0.382;  //KEK	
	
	dir_x[4] = 1; 		dir_y[4]=0;  //K	
	dir_x[5] = 0.923; 	dir_y[5]=-0.382;  //KDK	
	dir_x[6] = 0.707; 	dir_y[6]=-0.707;  //DK	
	dir_x[7] = 0.382; 	dir_y[7]=-0.923;  //DDK	
	
	dir_x[8] = 0; 		dir_y[8]=-1;  //D	
	dir_x[9] = -0.382; 	dir_y[9]=-0.923;  //DDN	
	dir_x[10] = -0.707; 	dir_y[10]=-0.707;  //DN	
	dir_x[11] = -0.923; 	dir_y[11]=-0.382;  //NDN	

	dir_x[12] = -1; 	dir_y[12]=0;  //N	
	dir_x[13] = -0.923; 	dir_y[13]=0.382; //NENY	
	dir_x[14] = -0.707;	dir_y[14]=0.707;  //ENY	
	dir_x[15] = -0.382;	dir_y[15]=0.923;  //EENY	
}


float find_direction(double xv,double yv)
{  float dx,dy,min_d,min_dx;int i;
  min_d=1000; min_dx=0;
  for (i=0; i<16; i++)
  {
    dx=xv-dir_x[i];dy=yv-dir_y[i];
    if (dx<0) dx=0-dx; if (dy<0) dy=0-dy;
    if ((dx+dy)<min_d)
    {
      min_d=dx+dy;
      min_dx=i;
    }
  }
  return(min_dx*22.5);
}


char filename[255];

int main(int argc, char *argv[])
{get_configuration(&config, "");//argv[1]
	

	ws2300 = open_weatherstation(config.serial_device_name);
        init_wind();
	while (1)
	{
	  time(&raw_time);
          last_w=-5;last_wx=-5; last_wy=-5;raw_time0=raw_time;max_wind=0; avg_wind=0; all_wind=0;w_x=0; w_y=0;
	  while (raw_time-raw_time0<240)
	  {
	    get_wind();
	    if (!((last_w==wind) && (last_wx==wind_x) && (last_wy==wind_y)))
	    {
	      avg_wind+= wind;
	      if (wind>max_wind)max_wind=wind;
	      w_x+=wind_x; w_y+=wind_y;
	      all_wind++;
	      last_w=wind; last_wx=wind_x; last_wy=wind_y;
	    }
	    time(&raw_time);
	  }
	  if (all_wind)
	  {
	    avg_wind/=all_wind;
	    w_x/=all_wind;
	    w_y/=all_wind;
	    w_dir=find_direction(w_x,w_y);
	  } else { avg_wind =-5; w_x=0; w_y=0; w_dir=0;}
	  
	  t_in=temperature_indoor(ws2300, config.temperature_conv);
	  rh_in=humidity_indoor_all(ws2300, &tempint_min, &tempint_max,  &time_min, &time_max);
	  r_p = rel_pressure(ws2300, config.pressure_conv_factor);
	  a_p= abs_pressure(ws2300, config.pressure_conv_factor);
	  t_out=temperature_outdoor(ws2300, config.temperature_conv);
	  rh_out=humidity_outdoor_all(ws2300, &tempint_min, &tempint_max, &time_min, &time_max);
	  r_1h=rain_1h_all(ws2300, config.rain_conv_factor, &tempfloat_max, &time_max);
	  r_24h=rain_24h_all(ws2300, config.rain_conv_factor,&tempfloat_max, &time_max);
	  w_file=fopen("/tmp/indoor.dat","wt");
	  if (w_file){
	    fprintf(w_file,"Belso_homerseklet=%.1f\n",t_in);
	    fprintf(w_file,"Belso_paratartalom=%.f\n",rh_in);
	    fprintf(w_file,"Abszolut_legnyomas=%.4f\n",a_p);
	    fprintf(w_file,"Relativ_legnyomas=%.4f\n",r_p);
	    fclose(w_file);
	  }
	  w_file=fopen("/tmp/outdoor.dat","wt");
	  if (w_file){
	    fprintf(w_file,"Kulso_homerseklet=%.1f\n",t_out);
	    fprintf(w_file,"Relativ_paratartalom=%.f\n",rh_out);
	    fclose(w_file);
	  }
	  w_file=fopen("/tmp/rain.dat","wt");
	  if (w_file){
	    fprintf(w_file,"Csapadek_1h=%.2f\n",r_1h);
	    fprintf(w_file,"Csapadek_24h=%.2f\n",r_24h);
	    fclose(w_file);
	  }
	  w_file=fopen("/tmp/wind.dat","wt");
	  if (w_file){
	    fprintf(w_file,"Atlagos_szelero=%.f\n",wind);
	    fprintf(w_file,"Max_szellokes=%.f\n",max_wind);
	    fprintf(w_file,"Atlagos_szelirany=%.f\n",w_dir);
	    fprintf(w_file,"Meresek_szama=%.f\n",all_wind);
	    fprintf(w_file,"Xv,Yv=%.1f,%.1f\n",wind_x,wind_y);
	    fclose(w_file);
	  }
	  w_file=fopen("/tmp/upload.dat","wt");
	  if (w_file){
	    fprintf(w_file,"&inhom=%.1f&inrh=%.f&p=%.1f&ap=%.1f&hom=%.1f&rh=%.f&szelero=%.1f&szelirany=%.0f&csap1h=%.1f&csap=%.1f&szellokes=%.1f",
			    (float)t_in,(float)rh_in,(float)r_p,(float)a_p,(float)t_out,(float)rh_out,(float)wind,(float)w_dir,(float)r_1h,(float)r_24h,(float)max_wind);
	    fclose(w_file);
	  }
	  if (raw_time>1238562000)//2009.04.01
	  {
	    sprintf(filename,"/tmp/upload.%d",raw_time);
	    w_file=fopen(filename,"wt");
	    if (w_file){
	      fprintf(w_file,"&unixtime=%d&inhom=%.1f&inrh=%.f&p=%.1f&ap=%.1f&hom=%.1f&rh=%.f&szelero=%.1f&szelirany=%.0f&csap1h=%.1f&csap=%.1f&szellokes=%.1f",
			      raw_time,(float)t_in,(float)rh_in,(float)r_p,(float)a_p,(float)t_out,(float)rh_out,(float)wind,(float)w_dir,(float)r_1h,(float)r_24h,(float)max_wind);
	      fclose(w_file);
	  }
	    
	  }
	}
	
	close_weatherstation(ws2300);
	return(0);
}

