annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
1 /* FXAnalise data provider reading data from KK data file on disc */
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
2
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
3 #include <userint.h>
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
4 #include <ansi_c.h>
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
5 #include <utility.h>
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
6 #include <formatio.h>
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
7
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
8 //#define LOGFILEPATH "C:\\Femto\\Software\\FXQE80"
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
9 #define LOGFILEPATH "C:\\temp"
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
10
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
11 /* data acquisition flag */
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
12 extern int acquiring;
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
13 /* data queue */
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
14 extern CmtTSQHandle dataQueue;
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
15 /* callback receiving messages in the main thread */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
16 void CVICALLBACK MessageCB (void *callbackData);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
17 /* message */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
18 static char message[1024];
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
19
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
20 #define SendMessage(threadId, ...) \
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
21 do { \
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
22 snprintf(message, sizeof(message) - 1, ##__VA_ARGS__); \
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
23 PostDeferredCallToThread(MessageCB, message, threadId); \
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
24 } while (0)
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
25
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
26
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
27 int CVICALLBACK FileDataProvider (void *functionData)
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
28 {
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
29 int mainThreadId;
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
30 char dataFileName[MAX_PATHNAME_LEN];
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
31 int fd;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
32 int read;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
33 int year, month, day, hour, min, sec, msec;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
34 struct tm t;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
35 char line[1024];
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
36 double data[5];
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
37 double now;
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
38
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
39 /* get main thread id to post messages to it */
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
40 mainThreadId = CmtGetMainThreadID();
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
41
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
42 /* guess current data log file name */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
43 GetCurrentDateTime(&now);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
44 GetDateTimeElements(now, NULL, NULL, NULL, &month, &day, &year);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
45 snprintf(dataFileName, sizeof(dataFileName) - 1,
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
46 "%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year % 100, month, day);
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
47
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
48 /* open file */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
49 fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
50 if (fd < 0) {
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
51 /* notify error to the main thread */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
52 SendMessage(mainThreadId, "error opening data file '%s'", dataFileName);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
53 }
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
54
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
55 /* seek to file end */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
56 SetFilePtr(fd, 0, 2);
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
57
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58 while (acquiring) {
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
59
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
60 /* read one line from data file */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
61 read = ReadLine(fd, line, sizeof(line) - 1);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
62 if (read < 1) {
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
63 Delay(0.05);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
64 continue;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
65 }
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
66
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
67 /* parse data */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
68 read = Scan(line, "%d[w2]%d[w2]%d[w2] %d[w2]%d[w2]%d[w2].%d %f %f %f %f",
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
69 &year, &month, &day, &hour, &min, &sec, &msec, &data[1], &data[2], &data[3], &data[4]);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
70 if (read != 11) {
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
71 /* notify error to the main thread */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
72 SendMessage(mainThreadId, "error parsing data log line");
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
73 /* skip line */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
74 continue;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
75 }
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
76
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
77 t.tm_hour = hour;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
78 t.tm_min = min;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
79 t.tm_sec = sec;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
80 t.tm_mday = day;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
81 /* correct month count */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
82 t.tm_mon = month - 1;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
83 /* years since 1900 */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
84 t.tm_year = year + 2000 - 1900;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
85 /* daylight saving must be set to -1 */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
86 t.tm_isdst = -1;
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
87
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
88 /* convert into seconds since 1 January 1900 00:00:00 and add milliseconds */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
89 data[0] = mktime(&t) + 1e-3 * msec;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
91 /* convert from kHz to Hz */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
92 data[1] = data[1] * 1000.0;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
93 data[2] = data[2] * 1000.0;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
94 data[3] = data[3] * 1000.0;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
95 data[4] = data[4] * 1000.0;
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
96
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
97 /* push data into the data queue */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
98 CmtWriteTSQData(dataQueue, data, 5, TSQ_INFINITE_TIMEOUT, 0);
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
99
90
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
100 /* handle switch to next data file */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
101 if ((hour == 23) && (min == 59) && (sec == 59)) {
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
102
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
103 /* next data file name */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
104 snprintf(dataFileName, sizeof(dataFileName) - 1,
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
105 "%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year, month, day + 1);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
106
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
107 /* close data file */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
108 CloseFile(fd);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
109
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
110 /* wait for new data file to appear */
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
111 int retry = 0;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
112 while (retry--) {
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
113 fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
114 if (fd != -1)
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
115 break;
c9aec93005a4 Cleanup log file data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 88
diff changeset
116 Delay(0.01);
88
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
117 }
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
118 }
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
119 }
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
120 return 0;
9b7588cd4013 Move data acquisition into separate thread
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
121 }