view file-data-provider.c @ 90:c9aec93005a4

Cleanup log file data provider
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 22 Mar 2013 18:49:58 +0100
parents 9b7588cd4013
children 4102fe614df2
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>

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

/* data acquisition flag */
extern int acquiring;
/* data queue */
extern CmtTSQHandle dataQueue;
/* callback receiving messages in the main thread */
void CVICALLBACK MessageCB (void *callbackData);
/* message */
static char message[1024];

#define SendMessage(threadId, ...)                                \
	do { 												          \
		snprintf(message, sizeof(message) - 1, ##__VA_ARGS__);    \
		PostDeferredCallToThread(MessageCB, message, threadId);   \
	} while (0)


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];
	double data[5];
	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, &data[1], &data[2], &data[3], &data[4]);
		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;
		/* daylight saving must be set to -1 */
		t.tm_isdst = -1;
			
		/* convert into seconds since 1 January 1900 00:00:00 and add milliseconds */
		data[0] = mktime(&t) + 1e-3 * msec;
				
		/* convert from kHz to Hz */
		data[1] = data[1] * 1000.0;
		data[2] = data[2] * 1000.0;
		data[3] = data[3] * 1000.0;
		data[4] = data[4] * 1000.0;
			
		/* push data into the data queue */
		CmtWriteTSQData(dataQueue, data, 5, 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 = 0;
			while (retry--) {
				fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
				if (fd != -1)
					break;
				Delay(0.01);
			}
		}
	}
	return 0;
}