-
Notifications
You must be signed in to change notification settings - Fork 6
/
StatusFileReader.cpp
209 lines (198 loc) · 5.41 KB
/
StatusFileReader.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "StdAfx.h"
#include "StatusFileReader.h"
#include "Common/Common.h"
using namespace FileHandler;
CStatusFileReader::CStatusFileReader(void)
{
Common commonObj;
commonObj.GetExePath();
m_workingPath = commonObj.m_exePath;
memset(m_statusList, 0, sizeof(char) * 1024);
}
CStatusFileReader::~CStatusFileReader(void)
{
}
void CStatusFileReader::SetWorkingPath(CString path)
{
m_workingPath = path;
}
int CStatusFileReader::IsCommandReceived()
{
int status = CheckStatus(COMMAND);
return status;
}
int CStatusFileReader::IsStart()
{
int status = 0;
int startIndex, exitIndex;
exitIndex = 0;
startIndex = 0;
Reorder();
exitIndex = CheckStatus(STOPPROGRAM);
startIndex = CheckStatus(STARTPROGRAM);
if (startIndex > exitIndex)
status = 1;
return status;
}
int CStatusFileReader::IsExit()
{
int status = 0;
int startIndex, exitIndex;
exitIndex = 0;
startIndex = 0;
Reorder();
exitIndex = CheckStatus(STOPPROGRAM);
startIndex = CheckStatus(STARTPROGRAM);
if (startIndex < exitIndex)
status = 1;
return status;
}
int CStatusFileReader::IsPoweroff()
{
int status = 0;
int startIndex, exitIndex;
exitIndex = 0;
startIndex = 0;
Reorder();
exitIndex = CheckStatus(POWEROFF);
startIndex = CheckStatus(POWERON);
if (startIndex < exitIndex)
status = 1;
return status;
}
int CStatusFileReader::CheckStatus(unsigned char status)
{
int i;
for (i = 1023; i >= 4; i--)
{
if (m_statusList[i] == status)
return i;
}
return 0;
}
void CStatusFileReader::StatusPrint(unsigned char s)
{
switch (s)
{
case STARTPROGRAM:
RecordStatus("Started Program"); break;
case STOPPROGRAM:
RecordStatus("Exiting program"); break;
case WAIT4S:
RecordStatus("Waiting 4 seconds"); break;
case HOMEMOTOR:
RecordStatus("Homing motor"); break;
case MEMERR:
RecordStatus("Not enough memory"); break;
case USERINT:
RecordStatus("Interrupted by user"); break;
case EXECUTE:
RecordStatus("Executing"); break;
case DELETING:
RecordStatus("Deleting"); break;
case OPENCOMERR:
RecordStatus("Could not open COM port"); break;
case GPSTIMEOUT:
RecordStatus("GPS timeout"); break;
case SETCPUTIME:
RecordStatus("Setting CPU time&date from GPS time&date"); break;
case USEDCPUTIME:
RecordStatus("Used time from CPU"); break;
case POWERON:
RecordStatus("Power ON"); break;
case POWEROFF:
RecordStatus("Power OFF"); break;
case MOVEMOTOROK:
RecordStatus("MoveMotor Successful"); break;
case MOTORDONE:
RecordStatus("Motor done"); break;
case ALREADYHOME:
RecordStatus("Motor already at home. Will make one round to reset.\n"); break;
case MOVEMOTOR:
RecordStatus("MoveMotor"); break;
case DOSCANDONE:
RecordStatus("doscan DONE"); break;
case DOSCANTIMEOUT:
RecordStatus("Timeout in doscan"); break;
case SPECREFUSE:
RecordStatus("Spectrometer refuses to scan. Reinitializing");
case DONEINITSPEC:
RecordStatus("InitSpectrometer done"); break;
case INITSPEC:
RecordStatus("InitSpectrometer"); break;
case WRITEOK:
RecordStatus("Writing OK"); break;
case NOTOK:
RecordStatus("Not successful"); break;
case CONTACTSPEC:
RecordStatus("Got contact with spectrometer"); break;
case NOCONTACTSPEC:
RecordStatus("No contact with spectrometer"); break;
case SERIALOPEN:
RecordStatus("Opened serial port"); break;
case SERIALOPENERR:
RecordStatus("Could not open serial port"); break;
case COMMAND:
RecordStatus("Command"); break;
case PAUSE:
RecordStatus("Pause"); break;
case RESUME:
RecordStatus("Resume"); break;
}
}
void CStatusFileReader::RecordStatus(char* txt)
{
//puts(txt);
CString filePath;
filePath.Format("%sstatusDAT.txt", (LPCTSTR)m_workingPath);
FILE* f = fopen(filePath, "a+");
if (f != NULL) {
fprintf(f, "%s\n", txt);
fclose(f);
}
}
void CStatusFileReader::Reorder()
{
int i;
unsigned short last = 0;
unsigned short first = 0;
char txt[1024];
memset(txt, 0, sizeof(char) * 1024);
unsigned short* p = (unsigned short*)txt;
CString fileFullName;
fileFullName.Format("%sstatus.dat", (LPCTSTR)m_workingPath);
FILE* f = fopen(fileFullName, "rb");
if (f > (FILE*)0)
{
fread(p, 1024, 1, f);
fclose(f);
first = p[0];
last = p[1]; //last is the latest status
}
printf("first = %d,last = %d\n", first, last);
int count = 4;
if (first > last)
{
for (i = first; i < 1024; i++)
{
ASSERT(count < 1024);
m_statusList[count] = txt[i];
count++;
}
for (i = 4; i <= last; i++)
{
ASSERT(count < 1024);
m_statusList[count] = txt[i];
count++;
}
}
else
{
for (i = first; i <= last; i++)
{
ASSERT(count < 1024);
m_statusList[count] = txt[i];
count++;
}
}
}