-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
executable file
·195 lines (160 loc) · 6.86 KB
/
main.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
/*
* This file is part of FactoryController project.
*
* factorycontroller is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* factorycontroller is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with factorycontroller; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Copyright (C) Dmitry Ognyannikov, 2012-2017
*/
#include "factorycontroller/factorycontroller.h"
#include "factory-hardware-emulator/factory-hardware-emulator.h"
// command line argument parser
#include <boost/program_options.hpp>
#include <boost/program_options/cmdline.hpp>
#include <stdlib.h>
#include <log4cxx/logstring.h>
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/consoleappender.h>
#include <log4cxx/simplelayout.h>
#include <log4cxx/logmanager.h>
#include <log4cxx/ndc.h>
using namespace std;
void init_logging()
{
// BasicConfigurator replaced with PropertyConfigurator.
//PropertyConfigurator::configure("log4cxx.properties");
log4cxx::BasicConfigurator::configure();
//log4cxx::LoggerPtr logger = log4cxx::Logger::getRootLogger();
}
void factorycontroller_exit_handler(void) {
LOG4CXX_INFO(log4cxx::Logger::getRootLogger(), "normal program exit");
}
void factorycontroller_terminate_handler(void) {
LOG4CXX_INFO(log4cxx::Logger::getRootLogger(), "terminate event");
}
void factorycontroller_unexpected_exception_handler(void) {
LOG4CXX_ERROR(log4cxx::Logger::getRootLogger(), "unexpected exception");
}
int main(int argc, char *argv[])
{
// command line parser
boost::program_options::options_description desc("General options");
desc.add_options()
("help,h", "produce help message")
("version", "print program version")
("config,c", boost::program_options::value<std::string>(), "initialize with configuration file")
("emulation,e", "validate input and emulate without hardware")
("test,t", "run internal tests")
("project,p", boost::program_options::value<std::vector< std::string > >(), "project configuration file(s), by default test hardware if empty")
("reset,r", "reset all active projects and project tasks (or continue last projects otherwise)")
;
// without unnamed parameters
//boost::program_options::variables_map vm;
//boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
//boost::program_options::notify(vm);
// with unnamed parameters
boost::program_options::positional_options_description p;
p.add("project", -1);
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
boost::program_options::notify(vm);
// init log
init_logging();
// event handlers
atexit(factorycontroller_exit_handler);
set_terminate(factorycontroller_terminate_handler);
set_unexpected(factorycontroller_unexpected_exception_handler);
// print general program description
LOG4CXX_INFO(log4cxx::Logger::getRootLogger(), "factorycontroller - Manufacturing Execution System");
LOG4CXX_INFO(log4cxx::Logger::getRootLogger(), "Copyright (C) Dmitry Ognyannikov, 2012-2017");
if (vm.count("help"))
{
std::cout << desc << std::endl;
return 1;
}
if (vm.count("version"))
{
std::cout << "version 0.1 alpha" << std::endl;
return 1;
}
// Global Factory object
std::shared_ptr<oldportal::fc::factory::manufacturing::Factory> factory;
// Global EmulatorApplication object
std::shared_ptr<oldportal::fhe::EmulatorApplication> emulatorApplication;
// init hardware modbus network emulator
if (vm.count("emulation"))
{
emulatorApplication = std::make_shared<oldportal::fhe::EmulatorApplication>();
emulatorApplication->_network = std::make_shared<oldportal::fhe::network::ModbusNetworkController>(emulatorApplication);
std::shared_ptr<oldportal::fhe::device::Device> device = std::make_shared<oldportal::fhe::hardware::mechatronics::StepMotor>();
device->_modbus_address = 18;
emulatorApplication->_devices.push_back(device);
LOG4CXX_INFO(log4cxx::Logger::getRootLogger(), "StepMotor emulated device added, modbus_address: 18");
emulatorApplication->_network->init();
// start emulator thread
emulatorApplication->_network->start();
}
// init factory
if (vm.count("config"))
{
// file configuration loader
std::string config_filename = vm["config"].as<std::string>();
std::cout << "Initialize with configuration file: " << config_filename << std::endl;
// init loader
auto loader = std::make_shared<oldportal::fc::factory::manufacturing::proc::FactoryConfigurationFileLoader>(config_filename);
loader->init();
factory = std::make_shared<oldportal::fc::factory::manufacturing::Factory>
(std::static_pointer_cast<oldportal::fc::factory::manufacturing::FactoryLoader>(loader));
// free loader
loader.reset();
}
else
{
// pure program loader as default
std::cout << "Initialize with default configuration (pure program loader)" << std::endl;
// init loader
auto loader = std::make_shared<oldportal::fc::factory::manufacturing::proc::FactoryPureProgramLoader>();
factory = std::make_shared<oldportal::fc::factory::manufacturing::Factory>
(std::static_pointer_cast<oldportal::fc::factory::manufacturing::FactoryLoader>(loader));
loader->start_test_processes();
// free loader
loader.reset();
}
if (vm.count("test"))
{
std::cerr << "test mode is not supported yet" << std::endl;
//TODO: run tests mode
return 1;
}
if (vm.count("project"))
{
std::vector<std::string> project_filenames = vm["project"].as< std::vector<std::string> >();
//TODO: add and run projects
std::cerr << "project mode is not supported yet" << std::endl;
return 1;
}
if (vm.count("reset"))
{
//TODO: clear running projects
std::cerr << "reset mode is not supported yet" << std::endl;
return 1;
}
// start the Factory cycle
factory->start();
return 0;
}