-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cpp
More file actions
365 lines (275 loc) · 12.4 KB
/
Main.cpp
File metadata and controls
365 lines (275 loc) · 12.4 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Name: BehavioralBoxLabjackController.cpp
* Desc: Main file
**/
//#ifdef _WIN32
//#include <winsock2.h>
//#include <ws2tcpip.h>
//#else
//#include <arpa/inet.h> // For inet_ntoa()
//#endif
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <LabJackM.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <chrono>
#include <thread>
//#include "../../C_C++_LJM_2019-05-20/LJM_Utilities.h"
#include "BehavioralBoxControllersManager.h"
#include "BehavioralBoxLabjack.h"
#include "LabjackHelpers.h"
#include "ConfigurationManager.h"
// Webserver functionality:
#if INCLUDE_WEB_SERVER_FILES
#include "LabjackControllerWebApplication.h"
#include <Wt/WServer.h>
// 1 Make the member a real variable not a pointer.
std::thread web_server_thread;
#endif // LAUNCH_WEB_SERVER
//BehavioralBoxControllersManager controller;
std::shared_ptr<BehavioralBoxControllersManager> controller = std::make_shared<BehavioralBoxControllersManager>();
// FUNCTION PROTOTYPES:
//bool waitForFoundLabjacks();
bool startWebserver(int argc, char** argv, const std::shared_ptr<BehavioralBoxControllersManager>* managerPtr);
int shutdownApplication(int shutdownCode);
// Interface:
void printComputerInformation();
void printFilesystemPaths();
void printConfiguration();
void printCommandsMenu();
int main(int argc, char** argv)
{
std::cout << "BehavioralBoxLabjackController:" << std::endl;
std::cout << "\t Version " << SOFTWARE_VERSION << std::endl;
std::cout << "\t Pho Hale 2021" << std::endl << std::endl;
std::shared_ptr<ConfigurationManager> configMan = std::make_shared<ConfigurationManager>();
//configMan->testJsonConfig();
// Get the hostname
//std::string foundHostName = configMan->getHostName();
//std::cout << "Found host name: " << foundHostName << std::endl;
//int computerIdentifierNumber = configMan->getNumericComputerIdentifier();
//std::cout << "Computer Identifier ID: " << computerIdentifierNumber << std::endl;
printConfiguration();
//TODO: this doesn't currently matter because the webserver reloads everything in TimeSeriesChart::buildHistoricDataModel() by calling the static BehavioralBoxControllersManager::loadAllHistoricalData() function.
// Eventually we weant to implement it in a singleton-like fashion.
#if LAUNCH_WEB_SERVER
const bool shouldStartWebServer = configMan->getLoadedConfig().launch_web_server;
if (shouldStartWebServer) {
// Run the webserver:
startWebserver(argc, argv, &controller);
}
#else
#endif
std::cout <<std::endl << "Scanning for attached Labjacks..." <<std::endl;
if (!controller->waitForFoundLabjacks()) {
// User wants to quit.
std::cout << "User chose to quit. Done." <<std::endl;
return shutdownApplication(LJME_NO_DEVICES_FOUND);
}
#if LAUNCH_WEB_SERVER
if (shouldStartWebServer) {
Wt::WServer::instance()->postAll(&LabjackControllerWebApplication::staticUpdateActiveLabjacks);
}
#endif
// TODO - READ ME: main run loop
// The LJM_StartInterval, LJM_WaitForNextInterval, and LJM_CleanInterval functions are used to efficiently execute the loop every so many milliseconds
// To permit multiple labjacks operating in a general way, we probably want this loop to be contained within a thread that is owned by the BehavioralBoxLabjack object.
// This main loop will simplely
// Each BehavioralBoxLabjack also needs to be responsible for writing out its own file.
// There are some archetecture decisions to be made.
// Perhaps turning the lights on and off should belong to the individual boxes as well.
// Main should have perhaps an array of things?
printCommandsMenu();
if (controller->getActiveLabjacks().size() > 0) {
std::cout << "Collecting data at " << (1000.0 / double(LABJACK_UPDATE_LOOP_FREQUENCY_MILLISEC)) << "Hz...." <<std::endl;
}
//WaitForUserIfWindows();
// Main run loop:
int terminateExecution = 0;
int character;
do {
/*
Here we will perform the reading of the LabJack inputs (sensor values, etc).
*/
// Read a character from the keyboard
character = _getch();
character = toupper(character);
if (character == 'Q') {
std::cout << "Quitting..." << std::endl;
terminateExecution = 1;
}
else if (character == 'S') {
// Show the data files:
std::cout << "Showing current log files..." << std::endl;
// Iterate through all found Labjacks
for (int i = 0; i < controller->getActiveLabjacks().size(); i++) {
std::string foundRelativeFilePathString = controller->getActiveLabjacks()[i]->getFullFilePath();
std::string fullFilePathString = LabjackHelpers::getFullPath(foundRelativeFilePathString);
std::cout << "\t Showing log file at " << fullFilePathString << std::endl;
LabjackHelpers::showInExplorer(fullFilePathString);
}
std::cout << "\t done." << std::endl;
}
else if (character == 'F') {
// Show the data files:
std::cout << "Showing current output directory..." << std::endl;
// Iterate through all found Labjacks
auto loaded_config = configMan->getLoadedConfig();
std::string fullOutputDirectoryPathString = configMan->getGeneratedActiveOutputDirectory();
std::cout << "\t Showing output file directory at " << fullOutputDirectoryPathString << std::endl;
LabjackHelpers::showInExplorer(fullOutputDirectoryPathString);
std::cout << "\t done." << std::endl;
}
else if (character == 'P') {
// Prints the current data
std::cout << "Printing current data..." << std::endl;
// Iterate through all found Labjacks
for (int i = 0; i < controller->getActiveLabjacks().size(); i++) {
//controller->getActiveLabjacks()[i]->diagnosticPrintLastValues();
}
std::cout << "\t done." << std::endl;
}
else if (character == 'R') {
std::cout << "Refreshing Labjacks..." << std::endl;
controller->scanForNewLabjacks();
//if (shouldStartWebServer) {
// Refresh the webserver
//WServer::instance()->postAll(&LabjackControllerWebApplication::staticUpdateActiveLabjacks);
//}
std::cout << "\t done." << std::endl;
}
else if (character == 'L') {
std::cout << "Toggling visible LED Light mode on all labjacks..." << std::endl;
// Iterate through all found Labjacks
for (int i = 0; i < controller->getActiveLabjacks().size(); i++) {
controller->getActiveLabjacks()[i]->toggleOverrideMode_VisibleLED();
}
std::cout << "\t done." << std::endl;
}
else if (character == 'A') {
std::cout << "Toggling attract mode on all Labjacks..." << std::endl;
// Iterate through all found Labjacks
for (int i = 0; i < controller->getActiveLabjacks().size(); i++) {
controller->getActiveLabjacks()[i]->toggleOverrideMode_AttractModeLEDs();
}
std::cout << "\t done." << std::endl;
}
else if (character == 'U') {
std::cout << "Utility mode:" << std::endl;
//TODO: utility mode.
// Export data as CSV
std::vector<std::string> exportPaths = controller->exportHistoricalDataAsCSV("C:/Common/data/", "export-HistoricalData");
if (exportPaths.empty()) {
std::cout << "Tried to export CSV files but had no historical data." << std::endl;
}
else {
int numExportPaths = exportPaths.size();
std::cout << "Export " << numExportPaths << " historical data .CSV files:" << std::endl;
for (size_t i = 0; i < numExportPaths; i++)
{
std::cout << "\t" << exportPaths[i] << std::endl;
}
}
std::cout << "\t done." << std::endl;
}
else {
printCommandsMenu();
}
} while (terminateExecution != 1);
return shutdownApplication(LJME_NOERROR);
}
bool startWebserver(int argc, char** argv, const std::shared_ptr<BehavioralBoxControllersManager>* managerPtr)
{
#if INCLUDE_WEB_SERVER_FILES
std::cout << "Starting the web server." << std::endl;
// WEB SERVER THREAD BLOCK:
web_server_thread = std::move(std::thread([=]() {
// Build the input arguments for the webserver
// Converted using https://stackoverflow.com/questions/26032039/convert-vectorstring-into-char-c
// Only works within the thread's block, when placed before it, it failed.
//std::vector<std::string> strings{ "C:\\Common\\repo\\phoBehavioralBoxLabjackController\\x64\\Release\\phoBehavioralBoxLabjackController.exe", "--docroot", ".", "--config", "C:\\Common\\repo\\phoBehavioralBoxLabjackController\\/ConfigFiles/wt_config.xml", "--http-address", "0.0.0.0", "--http-port", "8080", "-accesslog=C:\\Common\\repo\\phoBehavioralBoxLabjackController\\/logs/webServerAccessLog.log" };
std::vector<std::string> stringsWebserverArguments{ argv[0], "--docroot", ".", "--config", "C:/Common/config/phoBehavioralBoxLabjackController-WT_config.xml", "--http-address", "0.0.0.0", "--http-port", "8080", "-accesslog=C:/Common/info/webServerAccessLog.log" };
std::vector<char*> cstringsWebserverArguments{};
for (auto& currStr : stringsWebserverArguments) {
cstringsWebserverArguments.push_back(&currStr.front());
}
char** finalArgs = (char**)cstringsWebserverArguments.data();
labjackControllerApplicationWebServer(cstringsWebserverArguments.size(), finalArgs, managerPtr);
//WServer::instance()->postAll(&LabjackControllerWebApplication::staticUpdateActiveLabjacks);
return true;
}));
#endif
return true;
}
// Called when the application is being quit
int shutdownApplication(int shutdownCode)
{
std::cout << "Shutting down the application..." << std::endl;
//controller->shutdown();
std::shared_ptr<ConfigurationManager> configMan = std::make_shared<ConfigurationManager>();
#if INCLUDE_WEB_SERVER_FILES
const bool shouldStartWebServer = configMan->getLoadedConfig().launch_web_server;
if (shouldStartWebServer) {
std::cout << "Waiting on web server thread to quit..." << std::endl;
// As the thread is using members from this object
// We can not let this object be destroyed until the thread finishes executing.
web_server_thread.join();
}
#endif
printf("Done.");
// At this point the thread has finished.
// Destructor can now complete.
return shutdownCode;
}
// Print the list of options to execute
void printCommandsMenu() {
std::cout << "Commands: " << std::endl;
std::cout << "\t Press [p] at any time to print the most recently read values for all labjacks." << std::endl;
std::cout << "\t Press [f] at any time to show the current output file directory." << std::endl;
std::cout << "\t Press [s] at any time to show the current log files for all labjacks." << std::endl;
std::cout << "\t Press [r] at any time to refresh and scan for more labjacks." << std::endl;
std::cout << "\t Press [l] at any time to toggle visible LED Light mode for all labjacks." << std::endl;
std::cout << "\t Press [a] at any time to toggle Attract mode for all labjacks." <<std::endl;
std::cout << "\t Press [u] at any time to display utility options." <<std::endl;
std::cout << "\t Press [q] at any time to quit." <<std::endl;
std::cout << "\t Press any other key at any time to show this list of commands." <<std::endl;
}
void printFilesystemPaths() {
std::cout << "Filesystem: " <<std::endl;
std::shared_ptr<ConfigurationManager> configMan = std::make_shared<ConfigurationManager>();
auto loaded_config = configMan->getLoadedConfig();
std::cout << "\t Output Directory: " << configMan->getGeneratedActiveOutputDirectory() <<std::endl;
if (loaded_config.enableHistoricalDataLoading) {
std::cout << "\t Historical data loading is enabled and the output directory is " << configMan->getGeneratedActiveHistoricalSearchDirectory() <<std::endl;
}
else {
std::cout << "\t Historical data loading is disabled." <<std::endl;
}
}
void printComputerInformation() {
std::shared_ptr<ConfigurationManager> configMan = std::make_shared<ConfigurationManager>();
std::cout << "Computer Hostname: " << configMan->getHostName() <<std::endl;
}
void printConfiguration() {
std::shared_ptr<ConfigurationManager> configMan = std::make_shared<ConfigurationManager>();
auto loaded_config = configMan->getLoadedConfig();
std::cout << "CURRENT CONFIGURATION: ==============================" <<std::endl;
printComputerInformation();
std::cout << "\t Experiment Name: " << loaded_config.experimentName <<std::endl;
std::cout << "\t Cohort Name: " << loaded_config.cohortName <<std::endl;
std::cout << "\t Animal Name: " << loaded_config.animalName <<std::endl;
printFilesystemPaths();
std::cout << "Webserver is ";
if (loaded_config.launch_web_server) {
std::cout << "enabled. It can be accessed via a web browser at URL http://127.0.0.1:8080" <<std::endl;
}
else {
std::cout << "disabled." <<std::endl;
}
std::cout << "========================== END CURRENT CONFIGURATION" <<std::endl;
}