Mercurial > hg > fxanalyse
changeset 90:c9aec93005a4
Cleanup log file data provider
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Fri, 22 Mar 2013 18:49:58 +0100 |
parents | c9e4f63c2033 |
children | 4102fe614df2 |
files | file-data-provider.c |
diffstat | 1 files changed, 86 insertions(+), 86 deletions(-) [+] |
line wrap: on
line diff
--- a/file-data-provider.c Thu Mar 21 19:00:22 2013 +0100 +++ b/file-data-provider.c Fri Mar 22 18:49:58 2013 +0100 @@ -1,5 +1,6 @@ /* FXAnalise data provider reading data from KK data file on disc */ +#include <userint.h> #include <ansi_c.h> #include <utility.h> #include <formatio.h> @@ -11,111 +12,110 @@ 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]; -void CurrentFileName(char *fname) -{ - char day[3], month[3], year[3]; - char *date = DateStr(); - Scan(date, "%s>%s[w2]-%s[w2]-20%s[w2]", month, day, year); - Fmt(fname, "%s<%s\\%s%s%s_Frequ.txt", LOGFILEPATH, year, month, day); -} +#define SendMessage(threadId, ...) \ + do { \ + snprintf(message, sizeof(message) - 1, ##__VA_ARGS__); \ + PostDeferredCallToThread(MessageCB, message, threadId); \ + } while (0) -#define FXLINELENGTH 123 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(); - int LogFile; - long LogFileSize; - char LogFileName[MAX_PATHNAME_LEN]; - long OldLogFilePtr = 0; - char LineBuffer[FXLINELENGTH + 10]; + /* 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); - char TimeTag[] = "100103 000000.000"; // K+K time tag meaning here 2010 january the 3rd at 00:00:00.000 - char Year[] = "2010"; - char Month[] = "01"; - char Day[] = "03"; - char Hour[] = "00"; - char Min[] = "00" ; - char Sec[] = "00.000"; - struct tm LocalTime ; - time_t utcTime; + /* 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); + } - double data[5]; - - CurrentFileName(LogFileName); - GetFileInfo(LogFileName, &OldLogFilePtr); - OldLogFilePtr -= OldLogFilePtr%FXLINELENGTH + FXLINELENGTH - 2; + /* seek to file end */ + SetFilePtr(fd, 0, 2); while (acquiring) { - - GetFileInfo(LogFileName, &LogFileSize); - if (LogFileSize > OldLogFilePtr+2*FXLINELENGTH-2) { // if a complete newline has been written - - // Open Log file and get to the beginning of newly completed line - LogFile = OpenFile(LogFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII); - OldLogFilePtr += FXLINELENGTH; - SetFilePtr(LogFile, OldLogFilePtr, 0); - - // return the last complete string from the log file and scan it for date and time information - - // first, the time tag, and store it in various formats - ReadFile(LogFile, TimeTag, 17); - - CopyBytes(Year,2,TimeTag,0,2); // first 2 bytes of year string remains "20" - CopyBytes(Month,0,TimeTag,2,2); - CopyBytes(Day,0,TimeTag,4,2); - CopyBytes(Hour,0,TimeTag,7,2); - CopyBytes(Min,0,TimeTag,9,2); - CopyBytes(Sec,0,TimeTag,11,6); - - Fmt(&LocalTime.tm_year, "%d<%s", Year); - Fmt(&LocalTime.tm_mon, "%d<%s", Month); - Fmt(&LocalTime.tm_mday, "%d<%s", Day); - Fmt(&LocalTime.tm_hour, "%d<%s", Hour); - Fmt(&LocalTime.tm_min, "%d<%s", Min); - Fmt(&LocalTime.tm_sec, "%d<%s", "00"); // special case to handle non integer number of UTC seconds + + /* 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; - LocalTime.tm_hour += 0; - LocalTime.tm_min -= 0; - LocalTime.tm_sec -= 0; - LocalTime.tm_mon -= 1; // january is month 0 for tm struct - LocalTime.tm_year -= 1900; // year is number of years since 1900 for tm struct - LocalTime.tm_isdst = -1; // daylight saving flag MUST be set to -1 (unallocated is bugging and +1 is making 1 hour error in summer) + /* 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; - utcTime = mktime (&LocalTime); - data[0] = (double) utcTime + strtod(Sec,NULL); - - // scan the line for counters's channels information - - ReadLine(LogFile, LineBuffer, FXLINELENGTH+9); - CloseFile(LogFile); - - Scan(LineBuffer, "%f%f%f%f", &data[1], &data[2], &data[3], &data[4]); + /* push data into the data queue */ + CmtWriteTSQData(dataQueue, data, 5, TSQ_INFINITE_TIMEOUT, 0); - /* 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); - - // Special case to handle change of day at next second - if ( LocalTime.tm_hour==23 && LocalTime.tm_min==59 && strtod(Sec,NULL)>=58 ) { - do { - Delay(5.1); - CurrentFileName(LogFileName); - } while (!GetFileInfo(LogFileName, &OldLogFilePtr)); - OldLogFilePtr = 2; + /* 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); } } - Delay(0.05); } return 0; }