Mercurial > hg > fxanalyse
comparison 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 |
comparison
equal
deleted
inserted
replaced
89:c9e4f63c2033 | 90:c9aec93005a4 |
---|---|
1 /* FXAnalise data provider reading data from KK data file on disc */ | 1 /* FXAnalise data provider reading data from KK data file on disc */ |
2 | 2 |
3 #include <userint.h> | |
3 #include <ansi_c.h> | 4 #include <ansi_c.h> |
4 #include <utility.h> | 5 #include <utility.h> |
5 #include <formatio.h> | 6 #include <formatio.h> |
6 | 7 |
7 //#define LOGFILEPATH "C:\\Femto\\Software\\FXQE80" | 8 //#define LOGFILEPATH "C:\\Femto\\Software\\FXQE80" |
9 | 10 |
10 /* data acquisition flag */ | 11 /* data acquisition flag */ |
11 extern int acquiring; | 12 extern int acquiring; |
12 /* data queue */ | 13 /* data queue */ |
13 extern CmtTSQHandle dataQueue; | 14 extern CmtTSQHandle dataQueue; |
15 /* callback receiving messages in the main thread */ | |
16 void CVICALLBACK MessageCB (void *callbackData); | |
17 /* message */ | |
18 static char message[1024]; | |
14 | 19 |
15 void CurrentFileName(char *fname) | 20 #define SendMessage(threadId, ...) \ |
16 { | 21 do { \ |
17 char day[3], month[3], year[3]; | 22 snprintf(message, sizeof(message) - 1, ##__VA_ARGS__); \ |
18 char *date = DateStr(); | 23 PostDeferredCallToThread(MessageCB, message, threadId); \ |
19 Scan(date, "%s>%s[w2]-%s[w2]-20%s[w2]", month, day, year); | 24 } while (0) |
20 Fmt(fname, "%s<%s\\%s%s%s_Frequ.txt", LOGFILEPATH, year, month, day); | |
21 } | |
22 | 25 |
23 #define FXLINELENGTH 123 | |
24 | 26 |
25 int CVICALLBACK FileDataProvider (void *functionData) | 27 int CVICALLBACK FileDataProvider (void *functionData) |
26 { | 28 { |
27 int mainThreadId; | 29 int mainThreadId; |
30 char dataFileName[MAX_PATHNAME_LEN]; | |
31 int fd; | |
32 int read; | |
33 int year, month, day, hour, min, sec, msec; | |
34 struct tm t; | |
35 char line[1024]; | |
36 double data[5]; | |
37 double now; | |
28 | 38 |
29 /* get main thread id to post messages to it */ | 39 /* get main thread id to post messages to it */ |
30 mainThreadId = CmtGetMainThreadID(); | 40 mainThreadId = CmtGetMainThreadID(); |
31 | 41 |
32 int LogFile; | 42 /* guess current data log file name */ |
33 long LogFileSize; | 43 GetCurrentDateTime(&now); |
34 char LogFileName[MAX_PATHNAME_LEN]; | 44 GetDateTimeElements(now, NULL, NULL, NULL, &month, &day, &year); |
35 long OldLogFilePtr = 0; | 45 snprintf(dataFileName, sizeof(dataFileName) - 1, |
36 char LineBuffer[FXLINELENGTH + 10]; | 46 "%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year % 100, month, day); |
37 | 47 |
38 char TimeTag[] = "100103 000000.000"; // K+K time tag meaning here 2010 january the 3rd at 00:00:00.000 | 48 /* open file */ |
39 char Year[] = "2010"; | 49 fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII); |
40 char Month[] = "01"; | 50 if (fd < 0) { |
41 char Day[] = "03"; | 51 /* notify error to the main thread */ |
42 char Hour[] = "00"; | 52 SendMessage(mainThreadId, "error opening data file '%s'", dataFileName); |
43 char Min[] = "00" ; | 53 } |
44 char Sec[] = "00.000"; | |
45 struct tm LocalTime ; | |
46 time_t utcTime; | |
47 | 54 |
48 double data[5]; | 55 /* seek to file end */ |
49 | 56 SetFilePtr(fd, 0, 2); |
50 CurrentFileName(LogFileName); | |
51 GetFileInfo(LogFileName, &OldLogFilePtr); | |
52 OldLogFilePtr -= OldLogFilePtr%FXLINELENGTH + FXLINELENGTH - 2; | |
53 | 57 |
54 while (acquiring) { | 58 while (acquiring) { |
59 | |
60 /* read one line from data file */ | |
61 read = ReadLine(fd, line, sizeof(line) - 1); | |
62 if (read < 1) { | |
63 Delay(0.05); | |
64 continue; | |
65 } | |
66 | |
67 /* parse data */ | |
68 read = Scan(line, "%d[w2]%d[w2]%d[w2] %d[w2]%d[w2]%d[w2].%d %f %f %f %f", | |
69 &year, &month, &day, &hour, &min, &sec, &msec, &data[1], &data[2], &data[3], &data[4]); | |
70 if (read != 11) { | |
71 /* notify error to the main thread */ | |
72 SendMessage(mainThreadId, "error parsing data log line"); | |
73 /* skip line */ | |
74 continue; | |
75 } | |
76 | |
77 t.tm_hour = hour; | |
78 t.tm_min = min; | |
79 t.tm_sec = sec; | |
80 t.tm_mday = day; | |
81 /* correct month count */ | |
82 t.tm_mon = month - 1; | |
83 /* years since 1900 */ | |
84 t.tm_year = year + 2000 - 1900; | |
85 /* daylight saving must be set to -1 */ | |
86 t.tm_isdst = -1; | |
55 | 87 |
56 GetFileInfo(LogFileName, &LogFileSize); | 88 /* convert into seconds since 1 January 1900 00:00:00 and add milliseconds */ |
57 if (LogFileSize > OldLogFilePtr+2*FXLINELENGTH-2) { // if a complete newline has been written | 89 data[0] = mktime(&t) + 1e-3 * msec; |
58 | |
59 // Open Log file and get to the beginning of newly completed line | |
60 LogFile = OpenFile(LogFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII); | |
61 OldLogFilePtr += FXLINELENGTH; | |
62 SetFilePtr(LogFile, OldLogFilePtr, 0); | |
63 | 90 |
64 // return the last complete string from the log file and scan it for date and time information | 91 /* convert from kHz to Hz */ |
92 data[1] = data[1] * 1000.0; | |
93 data[2] = data[2] * 1000.0; | |
94 data[3] = data[3] * 1000.0; | |
95 data[4] = data[4] * 1000.0; | |
96 | |
97 /* push data into the data queue */ | |
98 CmtWriteTSQData(dataQueue, data, 5, TSQ_INFINITE_TIMEOUT, 0); | |
99 | |
100 /* handle switch to next data file */ | |
101 if ((hour == 23) && (min == 59) && (sec == 59)) { | |
65 | 102 |
66 // first, the time tag, and store it in various formats | 103 /* next data file name */ |
67 ReadFile(LogFile, TimeTag, 17); | 104 snprintf(dataFileName, sizeof(dataFileName) - 1, |
105 "%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year, month, day + 1); | |
68 | 106 |
69 CopyBytes(Year,2,TimeTag,0,2); // first 2 bytes of year string remains "20" | 107 /* close data file */ |
70 CopyBytes(Month,0,TimeTag,2,2); | 108 CloseFile(fd); |
71 CopyBytes(Day,0,TimeTag,4,2); | |
72 CopyBytes(Hour,0,TimeTag,7,2); | |
73 CopyBytes(Min,0,TimeTag,9,2); | |
74 CopyBytes(Sec,0,TimeTag,11,6); | |
75 | |
76 Fmt(&LocalTime.tm_year, "%d<%s", Year); | |
77 Fmt(&LocalTime.tm_mon, "%d<%s", Month); | |
78 Fmt(&LocalTime.tm_mday, "%d<%s", Day); | |
79 Fmt(&LocalTime.tm_hour, "%d<%s", Hour); | |
80 Fmt(&LocalTime.tm_min, "%d<%s", Min); | |
81 Fmt(&LocalTime.tm_sec, "%d<%s", "00"); // special case to handle non integer number of UTC seconds | |
82 | |
83 LocalTime.tm_hour += 0; | |
84 LocalTime.tm_min -= 0; | |
85 LocalTime.tm_sec -= 0; | |
86 LocalTime.tm_mon -= 1; // january is month 0 for tm struct | |
87 LocalTime.tm_year -= 1900; // year is number of years since 1900 for tm struct | |
88 LocalTime.tm_isdst = -1; // daylight saving flag MUST be set to -1 (unallocated is bugging and +1 is making 1 hour error in summer) | |
89 | |
90 utcTime = mktime (&LocalTime); | |
91 data[0] = (double) utcTime + strtod(Sec,NULL); | |
92 | |
93 // scan the line for counters's channels information | |
94 | 109 |
95 ReadLine(LogFile, LineBuffer, FXLINELENGTH+9); | 110 /* wait for new data file to appear */ |
96 CloseFile(LogFile); | 111 int retry = 0; |
97 | 112 while (retry--) { |
98 Scan(LineBuffer, "%f%f%f%f", &data[1], &data[2], &data[3], &data[4]); | 113 fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII); |
99 | 114 if (fd != -1) |
100 /* convert from kHz to Hz */ | 115 break; |
101 data[1] = data[1] * 1000.0; | 116 Delay(0.01); |
102 data[2] = data[2] * 1000.0; | |
103 data[3] = data[3] * 1000.0; | |
104 data[4] = data[4] * 1000.0; | |
105 | |
106 /* push data into the data queue */ | |
107 CmtWriteTSQData(dataQueue, data, 5, TSQ_INFINITE_TIMEOUT, 0); | |
108 | |
109 // Special case to handle change of day at next second | |
110 if ( LocalTime.tm_hour==23 && LocalTime.tm_min==59 && strtod(Sec,NULL)>=58 ) { | |
111 do { | |
112 Delay(5.1); | |
113 CurrentFileName(LogFileName); | |
114 } while (!GetFileInfo(LogFileName, &OldLogFilePtr)); | |
115 OldLogFilePtr = 2; | |
116 } | 117 } |
117 } | 118 } |
118 Delay(0.05); | |
119 } | 119 } |
120 return 0; | 120 return 0; |
121 } | 121 } |