/*	Lector de Barometro
	file B4.C	P.Miranda
	LaPaz 28-Ag-2002
*/

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <io.h>
#include <string.h>
#include <dir.h>
#include <time.h>
#include<c:\barom\b2\comx.h>

char file_o1[60],file_o2[60], atime[60];
char drive[4],dir[30],name[10],ext1[6];
char ext[6]={".asc"};

/* Bytes de control de file  ................*/

enum keys {	ESC=27,
		F1=0x3B,F2,F3,F4,F5,F6,F7,F8,F9,F10,NUMLOCK,SCROLLOCK,
		HOME=0x47,UP,PGUP,MINUS,LEFT,FIVE,RIGHT,PLUS,END,DOWN,PGDN,INS,DEL
	   };
char *p2;
int i,j,k;
char datos[2400],dat_str[2400];
int tmin1,tmin2;

struct  time t;
struct date d;


/* ======================================================================*/

int main(void)
{
char ch;
	clrscr();

	i=comx_open("COM1 2400 N 1 8");
	if(i==0)	/* Verifiva */
	{	printf("\nNo Existe el Port Serial de Comunicacion, Revisar");
		exit(0);
	}
	fecha_y_tiempo();
	fecha_y_tiempo_actual();
	printf("\n%s",atime);
	printf("\n......PROGRAMA LECTURA DEL BAROMETRO .....ESC=termina....\n");
	k=0;
	do{  	if(comx_buffer())	  //pregunta si existe datos que leer
		{	i=comx_rd(datos); //lectura
			for(j=0;j<i;j++)
			{	//cprintf("%c",datos[j]);
				dat_str[k++]=datos[j];
				if(datos[j]==0x0A)
				{       dat_str[k]=0;
					printf("%s  %s",atime,dat_str);
					grabar();
					k=0;
			}       }
		}
		otro_minuto();
		if(tmin1!=tmin2)
		{       fecha_y_tiempo_actual();
			promediar_y_actualizar_mes();
			ch='P';		// OJO cambiar el comando
			comx_tx(ch);	// solicita datos
			tmin1=tmin2;	// switch
		}
		if(kbhit())		/* Esperar teclado Key */
		{	ch = getch();
			if(ch==ESC)break;	//termina
			comx_tx(ch);
		}
	}while(1);
	comx_close();
}

fecha_y_tiempo()
{

	gettime(&t);
	printf("\nThe current time is: %2d:%02d:%02d.%02d",
	       t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
	tmin1=t.ti_min;

	getdate(&d);
	printf("\nThe current year:month:day is: %d:%02d:%02d",
		d.da_year,d.da_mon,d.da_day);
	sprintf(file_o1,"B%02d%02d%02d.asc",d.da_year%100,d.da_mon,d.da_day);
	strcpy(file_o2,file_o1);
	printf("\nThe current file name is:%s",file_o1);
}

fecha_y_tiempo_actual()
{
struct time t;
struct date d;
	gettime(&t);
	getdate(&d);
	sprintf(atime,"LP%02d%02d%02d%02d%02d",
		d.da_year%100,d.da_mon,d.da_day,t.ti_hour, t.ti_min);
	sprintf(file_o1,"B%02d%02d%02d.asc",d.da_year%100,d.da_mon,d.da_day);
}

otro_minuto()
{
struct time t;
	gettime(&t);
	tmin2=t.ti_min;
}
grabar()
{
FILE *hdl;
	hdl=fopen(file_o1,"a");
	fprintf(hdl,"%s %s",atime,dat_str);
	fclose(hdl);
}

promediar_y_actualizar_mes()
{
char btime[20],param[20],btimep[20];
char n[20];
float d1,d2;
int d3,k,kp,ko;
FILE *hd1,*hd2;
	if(strcmp(file_o1,file_o2)==0)return 0;
	hd1=fopen(file_o1,"r");
	if(hd1==NULL)return 1;
	sprintf(file_o2,"B%02d%02d.asc",d.da_year%100,d.da_mon);
	hd2=fopen(file_o2,"a");
	if(hd2==NULL)return 2;
	i=0;
	d1=0;
	j=1;
	kp=1;
	ko=0;	//Estado inicial
	do
	{	fscanf(hd1,"%s %f %s %s %s",btime,&d2,param,n,n);
		d3=atoi(&btime[10]);

		if(d3<20)k=1;
		else	if(d3<40)k=2;
			else	if(d3<60)k=3;

		if(ko==0){	kp=k;	ko=1;
				strcpy(btimep,btime);	//solo al principio
			 }
		if(kp==k){ d1=d1+d2; 	i++;}
		else
		{
			d1=d1/i;
			fprintf(hd2,"\n%s  %8.3f  %s",btimep,d1,param);
			d1=d2;
			i=1;
			kp=k;
			strcpy(btimep,btime);
		}
	}while(!feof(hd1));
		d1=d1/i;
		fprintf(hd2,"\n%s  %8.3f  %s",btimep,d1,param);

	fclose(hd1);
	fclose(hd2);
	strcpy(file_o2,file_o1);
}

//....................... Prueba ..................


promediar_y_actualizar_mesx()
{
char btime[20],param[20];
float d1,d2;
char n[10];
FILE *hd1,*hd2;
	if(strcmp(file_o1,file_o2)==0)return 0;
	hd1=fopen(file_o2,"r");
	if(hd1==NULL)return 1;
	sprintf(file_o2,"B%02d%02d.dat",d.da_year%100,d.da_mon);
	hd2=fopen(file_o2,"a");
	if(hd2==NULL)return 2;
	i=1;
	do
	{	fscanf(hd1,"%s %f %s %s %s",btime,&d2,param,n,n);
		d1=d1+d2;
		if(i++==20)
		{       d1=d1/i;
			fprintf(hd2,"\n%s  %8.3f  %s",btime,d1,param);
			i=1;
			d1=0;
		}
	}while(!feof(hd1));
	fclose(hd1);
	fclose(hd2);
	strcpy(file_o2,file_o1);
}

