#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "CAENHVWrapper.h" 

int main(void) {
    
	// VARIABLES DECLARATION
	char		address[30], address2[30], address3[30], userName[20], passwd[30], name[64], name2[64], name3[64], ParName[8], channelname[30], stringaexec[100], directory_data[100], meteofile[100], meteofile2[100], header[200];
	int		link, is, n, slot, NrOfCh=1, pw, status, station;
	float		vmon, imon;
	time_t 		now;
    
    float           dew, pressure=0,rainlast,rainday,rainmonth,rainyear,heat,temp1=0,temp2=0,rh1=0,rh2=0,wind,gust,dir;
    int             day,month,year,hour,min;
    
	int channel_temp, slot_temp;
	float channel_offset;

	float *param = NULL;
	param = malloc(sizeof(float));
    
	uint *param2 = NULL;
	param2 = malloc(sizeof(uint));
    
	ushort *id_channel = NULL;
	id_channel = malloc(sizeof(ushort));
    
    
	// CAEN SY1527 settings
	sprintf(userName,"%s","admin");
	sprintf(passwd,"%s","admin");
    
    
	// SETTING DATA DIRECTORY (it should exist!!)
	strcpy (directory_data,"/home/user/re4data/");
	strcpy (meteofile,"/home/user/meteolog");
	strcpy (meteofile2,"/home/user/meteolog2");
    
    
	// Reading latest values from the weather station
	FILE *stream3;
	stream3=fopen(meteofile,"r");
	//fgets(header,sizeof(header),stream3);
	while (!feof(stream3)) {
		fscanf(stream3,"%d %d %d %d %d %f %f %f %f %f %f %f %f %f %f %f %f\n",&day,&month,&year,&hour,&min,&temp1,&rh1,&dew,&pressure,&wind,&gust,&dir,&rainlast,&rainday,&rainmonth,&rainyear,&heat);
	}
	fflush(stream3);
	fclose(stream3);
    
	// Reading latest values from the weather station (indoor values)
	FILE *stream5;
	stream5=fopen(meteofile2,"r");
	//fgets(header,sizeof(header),stream3);
	while (!feof(stream5)) {
		fscanf(stream5,"%d %d %d %d %d %f %f\n",&day,&month,&year,&hour,&min,&temp1,&rh1);
	}
	fflush(stream5);
	fclose(stream5);
    
    
    // Preparing connection to the mainframe
    CAENHVRESULT  ret_init, ret;
    link = LINKTYPE_TCPIP;
    
    
	// Reading the Configuration file CHAMBERS (it cointains the path file also)
	FILE *stream1;
	stream1=fopen("/srv/www/htdocs/conf.txt","r");
	fscanf(stream1,"%s\n",name);
	fscanf(stream1,"%s\n",address);
    
        
	// Reading values from the Mainframe
    ret_init =  CAENHVInitSystem(name, link, address, userName, passwd);
	//printf("\nCAENHVInitSystem: %s (num. %d)\n\n", CAENHVGetError(name), ret_init);
    if ( ret_init == CAENHV_OK ) {
        
		while (!feof(stream1)) {
            
			// READING THE CONFIGURATION FILE
			fscanf(stream1,"%s %d %d %d\n",channelname, &slot, &id_channel[0], &station);
			//printf("Slot: %d Channel in the slot = %d \n tdc_station = %d\n", slot,id_channel[0], station);
			time(&now);
            
            
            
			// READING THE HIGH VOLTAGE
			sprintf(ParName,"%s","VMon");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param);
            
			if (ret==CAENHV_OK) {
				vmon = param[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
			// READING THE CURRENTS
			sprintf(ParName,"%s","IMon");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param);
            
			if (ret==CAENHV_OK) {
				imon = param[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}

        		FILE *stream11;
        		stream11=fopen("/home/user/workspace/rpcupgrade/CAEN1527_monitoring-1.00/CAEN1527_monitoring/chamber_offsets.ini","r");
			while (!feof(stream11)) {
				fscanf(stream11,"%d %d %f\n", &slot_temp, &channel_temp, &channel_offset);
				
				if ( channel_temp == id_channel[0] && slot_temp == slot ) { 
					imon = imon + channel_offset;
				}
			}
    			fflush(stream11);
    			fclose(stream11);
            
            
            
			// READING THE STATUS
			sprintf(ParName,"%s","Status");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param2);
            
			if (ret==CAENHV_OK) {
				status = param2[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
            
			// READING THE POWER ON/OFF
			sprintf(ParName,"%s","Pw");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param2);
            
			if (ret==CAENHV_OK) {
				pw = param2[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
			// DEBUGGING PRINTOUT OF THE VARIABLES
			//printf("%s %d %s %d %d %.2f %.5f %d %d\n",name, now, channelname, slot, id_channel[0], vmon ,imon ,status, pw);
            
            
			// OPENING FILE TO WRITE DATA (chamber by chamber)
            strcpy (stringaexec,directory_data);
            strcat (stringaexec,channelname);
            strcat (stringaexec,".dat");
            FILE *stream2;
			stream2=fopen(stringaexec,"a");
			fprintf(stream2,"%s %d %s %d %d %.2f %.5f %d %d %f %f %f\n",name, now, channelname, slot, id_channel[0], vmon ,imon ,status, pw, temp1, rh1, pressure);
            fflush(stream2);
            fclose(stream2);
            
			// WRITING INFOS INTO THE ELOG
            //strcpy (stringaexec,"elog -h cms904daq -p 8080 -l cms-rpc -a Author=DAQ USER -a Type=""New configuration"" ");
            //strcat (stringaexec,"-a Subject=""Automatic new HV configuration"" /srv/www/html/confh.txt");
            //system(stringaexec);
		}
        
    }
    else {
        //printf("CH: Error connecting to the caen %s (num %d) \n",CAENHVGetError(name),ret_init);
    }
    // CLOSING FILES
    fflush(stream1);
    fclose(stream1);
    ret_init = CAENHVDeinitSystem(name);

    
    
    
    
	// Reading the Configuration file GAPS (it cointains the path file also)
	FILE *stream6;
	stream6=fopen("/srv/www/htdocs/conf_gap.txt","r");
	fscanf(stream6,"%s\n",name);
	fscanf(stream6,"%s\n",address);
    
    
	// Reading values from the Mainframe
    ret_init =  CAENHVInitSystem(name, link, address, userName, passwd);
	//printf("\nCAENHVInitSystem: %s (num. %d)\n\n", CAENHVGetError(name), ret_init);    
	if ( ret_init == CAENHV_OK ) {
        
		while (!feof(stream6)) {
            
			// READING THE CONFIGURATION FILE
			fscanf(stream6,"%s %d %d %d\n",channelname, &slot, &id_channel[0], &station);
			//printf("Slot: %d gap in the slot = %d \n tdc_station = %d", slot,id_channel[0], station);
			time(&now);
            


            
            
            
            
            
			// READING THE HIGH VOLTAGE
			sprintf(ParName,"%s","VMon");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param);
            
			if (ret==CAENHV_OK) {
				vmon = param[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
            
            
			// READING THE CURRENTS
			sprintf(ParName,"%s","IMon");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param);
            
			if (ret==CAENHV_OK) {
				imon = param[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}

        		FILE *stream61;
        		stream61=fopen("/home/user/workspace/rpcupgrade/CAEN1527_monitoring-1.00/CAEN1527_monitoring/gap_offsets.ini","r");
			while (!feof(stream61)) {
				fscanf(stream61,"%d %d %f\n", &slot_temp, &channel_temp, &channel_offset);
				
				if ( channel_temp == id_channel[0] && slot_temp == slot ) { 
					imon = imon + channel_offset;
				}
			}
    			fflush(stream61);
    			fclose(stream61);


            
            
            
			// READING THE STATUS
			sprintf(ParName,"%s","Status");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param2);
            
			if (ret==CAENHV_OK) {
				status = param2[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
            
            
			// READING THE POWER ON/OFF
			sprintf(ParName,"%s","Pw");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param2);
            
			if (ret==CAENHV_OK) {
				pw = param2[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
			// DEBUGGING PRINTOUT OF THE VARIABLES
			//printf("%s %d %s %d %d %.2f %.5f %d %d\n",name, now, channelname, slot, id_channel[0], vmon ,imon ,status, pw);
            
            
			// OPENING FILE TO WRITE DATA (gap by gap)
            strcpy (stringaexec,directory_data);
            strcat (stringaexec,channelname);
            strcat (stringaexec,"_gap.dat");
            FILE *stream2;
            stream2=fopen(stringaexec,"a");
			fprintf(stream2,"%s %d %s %d %d %.2f %.5f %d %d %f %f %f\n",name, now, channelname, slot, id_channel[0], vmon ,imon ,status, pw, temp1, rh1, pressure);
            fflush(stream2);
            fclose(stream2);
            
            
            
			// WRITING INFOS INTO THE ELOG
            //strcpy (stringaexec,"elog -h cms904daq -p 8080 -l cms-rpc -a Author=DAQ USER -a Type=""New configuration"" ");
            //strcat (stringaexec,"-a Subject=""Automatic new HV configuration"" /srv/www/html/confh.txt");
            //system(stringaexec);
		}
        
    }
    else {
        //printf("GAP: Error connecting to the caen %s (num %d) \n",CAENHVGetError(name),ret_init);
    }
    // CLOSING FILES
    fflush(stream6);
    fclose(stream6);
    ret_init = CAENHVDeinitSystem(name);
    
    
    // Reading the Configuration file SM (it cointains the path file also)
	FILE *stream7;
	stream7=fopen("/srv/www/htdocs/conf_sm.txt","r");
	fscanf(stream7,"%s\n",name);
	fscanf(stream7,"%s\n",address);
    
    
    // Preparing connection to the mainframe
    ret_init =  CAENHVInitSystem(name, link, address, userName, passwd);
	//printf("\nCAENHVInitSystem: %s (num. %d)\n\n", CAENHVGetError(name), ret_init);
    
	// Reading values from the Mainframe
	if ( ret_init == CAENHV_OK ) {
        
		while (!feof(stream7)) {
            
			// READING THE CONFIGURATION FILE
			fscanf(stream7,"%s %d %d %d\n",channelname, &slot, &id_channel[0], &station);
			//printf("Slot: %d Channel in the slot = %d \n tdc_station = %d", slot,id_channel[0], station);
			time(&now);
            
            
            
            
            
            
			// READING THE HIGH VOLTAGE
			sprintf(ParName,"%s","VMon");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param);
            
			if (ret==CAENHV_OK) {
				vmon = param[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
            
            
			// READING THE CURRENTS
			sprintf(ParName,"%s","IMon");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param);
            
			if (ret==CAENHV_OK) {
				imon = param[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
        		FILE *stream71;
        		stream71=fopen("/home/user/workspace/rpcupgrade/CAEN1527_monitoring-1.00/CAEN1527_monitoring/qc42_offsets.ini","r");
			while (!feof(stream71)) {
				fscanf(stream71,"%d %d %f\n", &slot_temp, &channel_temp, &channel_offset);
				
				if ( channel_temp == id_channel[0] && slot_temp == slot ) { 
					imon = imon + channel_offset;
				}
			}
    			fflush(stream71);
    			fclose(stream71);            
            
			// READING THE STATUS
			sprintf(ParName,"%s","Status");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param2);
            
			if (ret==CAENHV_OK) {
				status = param2[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
            
            
			// READING THE POWER ON/OFF
			sprintf(ParName,"%s","Pw");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param2);
            
			if (ret==CAENHV_OK) {
				pw = param2[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
			// DEBUGGING PRINTOUT OF THE VARIABLES
			//printf("%s %d %s %d %d %.2f %.5f %d %d\n",name, now, channelname, slot, id_channel[0], vmon ,imon ,status, pw);
            
            
			// OPENING FILE TO WRITE DATA (sm by sm)
            strcpy (stringaexec,directory_data);
            strcat (stringaexec,channelname);
            strcat (stringaexec,"_sm.dat");
			FILE *stream2;
			stream2=fopen(stringaexec,"a");
			fprintf(stream2,"%s %d %s %d %d %.2f %.5f %d %d %f %f %f\n",name, now, channelname, slot, id_channel[0], vmon ,imon ,status, pw, temp1, rh1, pressure);
            fflush(stream2);
            fclose(stream2);
            
            
			// WRITING INFOS INTO THE ELOG
            //strcpy (stringaexec,"elog -h cms904daq -p 8080 -l cms-rpc -a Author=DAQ USER -a Type=""New configuration"" ");
            //strcat (stringaexec,"-a Subject=""Automatic new HV configuration"" /srv/www/html/confh.txt");
            //system(stringaexec);
		}
        
	}
	else {
		//printf("SM: Error connecting to the caen %s (num %d) \n",CAENHVGetError(name),ret_init);
	}
    // CLOSING FILES
    fflush(stream7);
    fclose(stream7);
    ret_init = CAENHVDeinitSystem(name);








	// Reading the Configuration file CHAMBER QC41 (it cointains the path file also)
	FILE *stream8;
	stream8=fopen("/srv/www/htdocs/conf_sm_qc41.txt","r");
	fscanf(stream8,"%s\n",name);
	fscanf(stream8,"%s\n",address);
    
    
	// Reading values from the Mainframe
    ret_init =  CAENHVInitSystem(name, link, address, userName, passwd);
	//printf("\nCAENHVInitSystem: %s (num. %d)\n\n", CAENHVGetError(name), ret_init);    
	if ( ret_init == CAENHV_OK ) {
        
		while (!feof(stream8)) {
            
			// READING THE CONFIGURATION FILE
			fscanf(stream8,"%s %d %d %d\n",channelname, &slot, &id_channel[0], &station);
			//printf("Slot: %d ch in the slot = %d \n tdc_station = %d", slot,id_channel[0], station);
			time(&now);
            
            
            
            
            
            
			// READING THE HIGH VOLTAGE
			sprintf(ParName,"%s","VMon");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param);
            
			if (ret==CAENHV_OK) {
				vmon = param[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
            
            
			// READING THE CURRENTS
			sprintf(ParName,"%s","IMon");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param);
            
			if (ret==CAENHV_OK) {
				imon = param[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}

        		FILE *stream81;
        		stream81=fopen("/home/user/workspace/rpcupgrade/CAEN1527_monitoring-1.00/CAEN1527_monitoring/qc41_offsets.ini","r");
			while (!feof(stream81)) {
				fscanf(stream81,"%d %d %f\n", &slot_temp, &channel_temp, &channel_offset);
				
				if ( channel_temp == id_channel[0] && slot_temp == slot ) { 
					imon = imon + channel_offset;
				}
			}
    			fflush(stream81);
    			fclose(stream81);
            
            
            
			// READING THE STATUS
			sprintf(ParName,"%s","Status");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param2);
            
			if (ret==CAENHV_OK) {
				status = param2[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
            
            
			// READING THE POWER ON/OFF
			sprintf(ParName,"%s","Pw");
			ret = CAENHVGetChParam(name, slot, ParName, NrOfCh, id_channel, param2);
            
			if (ret==CAENHV_OK) {
				pw = param2[0];
			}
			else {
				//printf("\nCAEN1527: %s (num. %d)\n Paramenter Name %s \n", CAENHVGetError(name), ret, ParName);
			}
            
            
			// DEBUGGING PRINTOUT OF THE VARIABLES
			//printf("%s %d %s %d %d %.2f %.5f %d %d\n",name, now, channelname, slot, id_channel[0], vmon ,imon ,status, pw);
            
            
			// OPENING FILE TO WRITE DATA (ch by ch)
            strcpy (stringaexec,directory_data);
            strcat (stringaexec,channelname);
            strcat (stringaexec,"_sm_qc41.dat");
            FILE *stream2;
            stream2=fopen(stringaexec,"a");
			fprintf(stream2,"%s %d %s %d %d %.2f %.5f %d %d %f %f %f\n",name, now, channelname, slot, id_channel[0], vmon ,imon ,status, pw, temp1, rh1, pressure);
            fflush(stream2);
            fclose(stream2);
            
            
            
			// WRITING INFOS INTO THE ELOG
            //strcpy (stringaexec,"elog -h cms904daq -p 8080 -l cms-rpc -a Author=DAQ USER -a Type=""New configuration"" ");
            //strcat (stringaexec,"-a Subject=""Automatic new HV configuration"" /srv/www/html/confh.txt");
            //system(stringaexec);
		}
        
    }
    else {
        //printf("QC41: Error connecting to the caen %s (num %d) \n",CAENHVGetError(name),ret_init);
    }
    // CLOSING FILES
    fflush(stream8);
    fclose(stream8);
    ret_init = CAENHVDeinitSystem(name);











    //printf("\nDisconnecting from caen on %s \n",name);
}
/* strcpy (stringaexec,directory_data); */
/*             strcat (stringaexec,channelname); */
/*             strcat (stringaexec,"_sm_qc41.dat"); */
/*             FILE *stream2; */
/*             stream2=fopen(stringaexec,"a"); */
/* 			fprintf(stream2,"%s %d %s %d %d %.2f %.5f %d %d %f %f %f\n",name, now, channelname, slot, id_channel[0], vmon ,imon ,status, pw, temp1qc4, rh1qc4, pressureqc4); */
/*             fflush(stream2); */
/*             fclose(stream2); */
            
            
            
/* 			// WRITING INFOS INTO THE ELOG */
/*             //strcpy (stringaexec,"elog -h cms904daq -p 8080 -l cms-rpc -a Author=DAQ USER -a Type=""New configuration"" "); */
/*             //strcat (stringaexec,"-a Subject=""Automatic new HV configuration"" /srv/www/html/confh.txt"); */
/*             //system(stringaexec); */
/* 		} */
        
/*     } */
/*     else { */
/*         //printf("QC41: Error connecting to the caen %s (num %d) \n",CAENHVGetError(name),ret_init); */
/*     } */
/*     // CLOSING FILES */
/*     fflush(stream8); */
/*     fclose(stream8); */
/*     ret_init = CAENHVDeinitSystem(name); */











/*     //printf("\nDisconnecting from caen on %s \n",name); */
/* } */
