view file-data-provider.c @ 215:e925664b4b1b

Add streq() and strneq() definitions to utils.h
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 14 Apr 2014 12:46:14 +0200
parents 4102fe614df2
children ec81395bf08d
line wrap: on
line source

/* FXAnalise data provider reading data from KK data file on disc */

#include <userint.h>
#include <ansi_c.h>
#include <utility.h>
#include <formatio.h>

#include "data-provider.h"

//#define LOGFILEPATH "C:\\Femto\\Software\\FXQE80"
#define LOGFILEPATH "C:\\temp"


int CVICALLBACK FileDataProvider (void *functionData)
{
	int mainThreadId;
	char dataFileName[MAX_PATHNAME_LEN];
	int fd;
	int read;
	int year, month, day, hour, min, sec, msec;
	struct tm t;
	char line[1024];
	struct event event;
	double now;
	
	/* get main thread id to post messages to it */
	mainThreadId = CmtGetMainThreadID();
	
	/* guess current data log file name */
	GetCurrentDateTime(&now);
	GetDateTimeElements(now, NULL, NULL, NULL, &month, &day, &year);
	snprintf(dataFileName, sizeof(dataFileName) - 1, 
		"%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year % 100, month, day);
	
	/* open file */
	fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
	if (fd < 0) {
		/* notify error to the main thread */
		SendMessage(mainThreadId, "error opening data file '%s'", dataFileName);
	}
	
	/* seek to file end */
	SetFilePtr(fd, 0, 2);
	
	while (acquiring) {

		/* read one line from data file */
		read = ReadLine(fd, line, sizeof(line) - 1);
		if (read < 1) {
			Delay(0.05);
			continue;
		}
		
		/* parse data */
		read = Scan(line, "%d[w2]%d[w2]%d[w2] %d[w2]%d[w2]%d[w2].%d %f %f %f %f", 
			&year, &month, &day, &hour, &min, &sec, &msec,
			&event.data[0], &event.data[1], &event.data[2], &event.data[3]);
		if (read != 11) {
			/* notify error to the main thread */
			SendMessage(mainThreadId, "error parsing data log line");
			/* skip line */
			continue;
		}
		
		t.tm_hour = hour;
		t.tm_min = min;
		t.tm_sec = sec;
		t.tm_mday = day;
		/* correct month count */
		t.tm_mon = month - 1;
		/* years since 1900 */
		t.tm_year = year + 2000 - 1900;
		/* deduce if daylight saving is in place from the date and time itself */
		t.tm_isdst = -1;
		
		/* convert into seconds since 1 January 1900 00:00:00 and add milliseconds */
		event.time.tv_sec = mktime(&t);
		event.time.tv_usec = msec * 1000;
		
		/* convert from kHz to Hz */
		event.data[0] *= 1000.0;
		event.data[1] *= 1000.0;
		event.data[2] *= 1000.0;
		event.data[3] *= 1000.0;
		
		/* push data into the data queue */
		CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0);
			
		/* handle switch to next data file */
		if ((hour == 23) && (min == 59) && (sec == 59)) {
				
			/* next data file name */
			snprintf(dataFileName, sizeof(dataFileName) - 1, 
				"%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year, month, day + 1);
				
			/* close data file */
			CloseFile(fd);
				
			/* wait for new data file to appear */
			int retry = 20;
			while (retry--) {
				fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
				if (fd != -1)
					break;
				Delay(0.01);
			}
		}
	}
	return 0;
}