1- /* *
2- * Copyright (C) 2018 Guillem Castro
3- *
4- * This program is free software: you can redistribute it and/or modify
5- * it under the terms of the GNU General Public License as published by
6- * the Free Software Foundation, either version 3 of the License, or
7- * (at your option) any later version.
8- *
9- * This program is distributed in the hope that it will be useful,
10- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12- * GNU General Public License for more details.
13- *
14- * You should have received a copy of the GNU General Public License
15- * along with this program. If not, see <http://www.gnu.org/licenses/>.
16- */
17-
18- #include " Application.h"
19- // #include "StdoutPrinter.h"
20-
21- #include " LogService.h"
22-
23- #include < iostream>
24- #include < string>
25- #include < memory>
26- #include < thread>
27- #include < chrono>
28- #include < ctime>
29-
30- // Default number of threads
31- #ifndef THREADS
32- #define THREADS 4
33- #endif
34-
35- Application::Application (Configuration config) : config(config) {
36-
37- }
38-
39- /* *
40- * Setup the application, parse the configuration and initialize the Dispatcher and Listeners.
41- */
42- void Application::setup () {
43-
44- std::cout << " Starting application 1" << std::endl;
45-
46- std::string log_dir = config[" log_dir" ].get <std::string>();
47-
48- std::cout << " Starting application 2, log_dir was " << log_dir << std::endl;
49-
50- bool debug = config[" debug" ].get <bool >();
51-
52- logService.initializeLogging (" unknown" , log_dir, debug);
53-
54- manager.setDispatcher (&dispatcher);
55- manager.addSensor (std::make_shared<AnalogSensor>(config[" thermocoupler_1" ]));
56- manager.addSensor (std::make_shared<AnalogSensor>(config[" thermocoupler_2" ]));
57- manager.addSensor (std::make_shared<AnalogSensor>(config[" thermocoupler_3" ]));
58- manager.addSensor (std::make_shared<AnalogSensor>(config[" load_cell" ]));
59- manager.addSensor (std::make_shared<AnalogSensor>(config[" pressure" ]));
60-
61- std::shared_ptr<Listener> dblistener = std::make_shared<DbListener>(&dispatcher);
62- try {
63- dispatcher.subscribe (" read_thermocoupler" , dblistener);
64- dispatcher.subscribe (" read_load_cell" , dblistener);
65- dispatcher.subscribe (" read_pressure" , dblistener);
66- }
67- catch (std::exception& ex) {
68- LOG (WARNING ) << ex.what ();
69- }
70-
71- try {
72- dispatcher.start ();
73- manager.start ();
74- }
75- catch (std::exception& ex) {
76- LOG (WARNING ) << ex.what ();
77- }
78-
79- LOG (INFO ) << " Application started successfully" ;
80- }
81-
82- /* *
83- * A function that will be called in a loop. Best for testing and debugging. Not to be used in production.
84- * This function must return once per loop.
85- */
86- void Application::run () {
87- std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
88- }
89-
90- /* *
91- * Stop the application. Save context (TODO) and stop Dispatcher.
92- */
93- void Application::stop () {
94- LOG (INFO ) << " Stopping application..." ;
95- // dispatcher.stop();
96- // manager.stop();
1+ /* *
2+ * Copyright (C) 2018 Guillem Castro
3+ *
4+ * This program is free software: you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation, either version 3 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+ #include " Application.h"
19+ // #include "StdoutPrinter.h"
20+
21+ #include " LogService.h"
22+
23+ #include < iostream>
24+ #include < string>
25+ #include < memory>
26+ #include < thread>
27+ #include < chrono>
28+ #include < ctime>
29+
30+ // Default number of threads
31+ #ifndef THREADS
32+ #define THREADS 4
33+ #endif
34+
35+ Application::Application (Configuration config) : config(config) {
36+
37+ }
38+
39+ /* *
40+ * Setup the application, parse the configuration and initialize the Dispatcher and Listeners.
41+ */
42+ void Application::setup () {
43+
44+ std::cout << " Starting application 1" << std::endl;
45+
46+ std::string log_dir = config[" log_dir" ].get <std::string>();
47+
48+ std::cout << " Starting application 2, log_dir was " << log_dir << std::endl;
49+
50+ bool debug = config[" debug" ].get <bool >();
51+
52+ logService.initializeLogging (" unknown" , log_dir, debug);
53+
54+ manager.setDispatcher (&dispatcher);
55+ manager.addSensor (std::make_shared<AnalogSensor>(config[" thermocoupler_1" ]));
56+ manager.addSensor (std::make_shared<AnalogSensor>(config[" thermocoupler_2" ]));
57+ manager.addSensor (std::make_shared<AnalogSensor>(config[" thermocoupler_3" ]));
58+ manager.addSensor (std::make_shared<AnalogSensor>(config[" load_cell" ]));
59+ manager.addSensor (std::make_shared<AnalogSensor>(config[" pressure" ]));
60+
61+ std::shared_ptr<Listener> dblistener = std::make_shared<DbListener>(&dispatcher);
62+ try {
63+ dispatcher.subscribe (" read_thermocoupler" , dblistener);
64+ dispatcher.subscribe (" read_load_cell" , dblistener);
65+ dispatcher.subscribe (" read_pressure" , dblistener);
66+ }
67+ catch (std::exception& ex) {
68+ LOG (WARNING ) << ex.what ();
69+ }
70+
71+ try {
72+ dispatcher.start ();
73+ manager.start ();
74+ }
75+ catch (std::exception& ex) {
76+ LOG (WARNING ) << ex.what ();
77+ }
78+
79+ LOG (INFO ) << " Application started successfully" ;
80+ }
81+
82+ /* *
83+ * A function that will be called in a loop. Best for testing and debugging. Not to be used in production.
84+ * This function must return once per loop.
85+ */
86+ void Application::run () {
87+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
88+ }
89+
90+ /* *
91+ * Stop the application. Save context (TODO) and stop Dispatcher.
92+ */
93+ void Application::stop () {
94+ LOG (INFO ) << " Stopping application..." ;
95+ dispatcher.stop ();
96+ manager.stop ();
9797}
0 commit comments