-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExternalProgramTrigger.cpp
More file actions
85 lines (67 loc) · 2.51 KB
/
Copy pathExternalProgramTrigger.cpp
File metadata and controls
85 lines (67 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#include "ExternalProgramTrigger.h"
THREAD_PROC_RETURN_VALUE ExternalProgramTriggerThread(void* pParam)
{
int id = (int)(intptr_t)pParam;
FILE* logexternalprogramtriggerfile = NULL;
char logexternalprogramtriggerfilename[MAX_BUF_LEN];
FILE* file = NULL;
int i = 0;
CHRONO chrono;
EnterCriticalSection(&strtimeCS);
sprintf(logexternalprogramtriggerfilename, LOG_FOLDER"logexternalprogramtrigger%d_%.64s.csv", id, strtimeex_fns());
LeaveCriticalSection(&strtimeCS);
logexternalprogramtriggerfile = fopen(logexternalprogramtriggerfilename, "w");
if (logexternalprogramtriggerfile == NULL)
{
printf("Unable to create log file.\n");
if (!bExit) bExit = TRUE; // Unexpected program exit...
return 0;
}
fprintf(logexternalprogramtriggerfile, "%% Time (in s); Trigger (1 : on, 0 : off);\n");
fflush(logexternalprogramtriggerfile);
StartChrono(&chrono);
for (;;)
{
uSleep(1000*(period_externalprogramtrigger[id] > 0? period_externalprogramtrigger[id]: 100));
if (bExit) break;
if (!bExternalProgramTrigger[id]) continue;
EnterCriticalSection(&ExternalProgramTriggerCS[id]);
file = fopen(ExternalProgramTriggerFileName[id], "r");
if (file != NULL)
{
fclose(file);
bExternalProgramTriggerDetected[id] = TRUE;
#pragma region Actions
fprintf(logexternalprogramtriggerfile, "%f;%d;\n", GetTimeElapsedChronoQuick(&chrono), bExternalProgramTriggerDetected[id]);
fflush(logexternalprogramtriggerfile);
if (procid_externalprogramtrigger[id] != -1)
{
// disableexternalprogramtrigger to avoid multiple execute...
bExternalProgramTrigger[id] = FALSE;
for (i = 0; i < nbretries_externalprogramtrigger[id]; i++)
{
if (remove(ExternalProgramTriggerFileName[id]) == 0) break;
uSleep(1000*retrydelay_externalprogramtrigger[id]);
}
if (bEcho) printf("execute %d\n", procid_externalprogramtrigger[id]);
ExecuteProcedure(procid_externalprogramtrigger[id]);
bWaiting = FALSE; // To interrupt and force execution of the next commands...
}
#pragma endregion
}
LeaveCriticalSection(&ExternalProgramTriggerCS[id]);
if (bExit) break;
}
StopChronoQuick(&chrono);
fclose(logexternalprogramtriggerfile);
if (!bExit) bExit = TRUE; // Unexpected program exit...
return 0;
}