240 lines
7.6 KiB
C++
240 lines
7.6 KiB
C++
//
|
|
// Created by rhetenor on 04.10.18.
|
|
//
|
|
|
|
#include "OutputDriverFactory.h"
|
|
|
|
#include "utility/LoggerFactory.h"
|
|
|
|
#include "output/impl/OutputDriver.h"
|
|
#include "output/impl/OutputPinController.h"
|
|
|
|
namespace flippR_driver
|
|
{
|
|
namespace output
|
|
{
|
|
namespace OutputDriverFactory
|
|
{
|
|
|
|
namespace
|
|
{
|
|
|
|
using namespace nlohmann;
|
|
|
|
std::shared_ptr<OutputDriver> get_OutputDriver(std::istream &output_pin_config, std::istream &solenoid_config, std::istream &lamp_config, std::istream &sound_config, std::istream &display_config)
|
|
{
|
|
utility::LoggerFactory::CreateOutputLogger();
|
|
|
|
std::shared_ptr<OutputPinController> output_gpio_interface = std::shared_ptr<OutputPinController>(create_OutputGPIOInterface(output_pin_config));
|
|
|
|
auto solenoids = create_solenoids(solenoid_config, output_gpio_interface);
|
|
auto lamps = create_lamps(lamp_config, output_gpio_interface);
|
|
auto sounds = create_sounds(sound_config, output_gpio_interface);
|
|
|
|
auto displays = create_displays(display_config, output_gpio_interface);
|
|
|
|
return std::make_shared<OutputDriver>(solenoids, lamps, sounds, displays);
|
|
}
|
|
|
|
OutputPinController* create_OutputGPIOInterface(std::istream &output_pin_config)
|
|
{
|
|
json output_config;
|
|
output_pin_config >> output_config;
|
|
|
|
return new OutputGPIOInterface(parse_pins_driver_board(output_config.at("driver_board")),
|
|
parse_pins_sound(output_config.at("sound_board")),
|
|
parse_pins_sound(output_config.at("display_board")));
|
|
}
|
|
|
|
std::map<std::string, uint8_t> parse_pins_driver_board(json &driver_board_config)
|
|
{
|
|
std::map<std::string, uint8_t> pins_driver_board;
|
|
|
|
try
|
|
{
|
|
pins_driver_board["i2c_address"] = driver_board_config.at("i2c_address").get<uint8_t>();
|
|
pins_driver_board["pin_base"] = driver_board_config.at("pin_base").get<uint8_t>();
|
|
pins_driver_board["data"] = driver_board_config.at("data").get<uint8_t>();
|
|
pins_driver_board["CL"] = driver_board_config.at("CL").get<uint8_t>();
|
|
|
|
json pin_select = driver_board_config.at("pin-select");
|
|
pins_driver_board["pin-select-A"] = pin_select.at("A").get<uint8_t>();
|
|
pins_driver_board["pin-select-B"] = pin_select.at("B").get<uint8_t>();
|
|
pins_driver_board["pin-select-C"] = pin_select.at("C").get<uint8_t>();
|
|
|
|
json latch_select = driver_board_config.at("latch-select");
|
|
pins_driver_board["mux1"] = latch_select.at("mux1").get<uint8_t>();
|
|
pins_driver_board["mux2"] = latch_select.at("mux2").get<uint8_t>();
|
|
pins_driver_board["latch-select-A"] = latch_select.at("A").get<uint8_t>();
|
|
pins_driver_board["latch-select-B"] = latch_select.at("B").get<uint8_t>();
|
|
pins_driver_board["latch-select-C"] = latch_select.at("C").get<uint8_t>();
|
|
}
|
|
catch(json::exception &e)
|
|
{
|
|
CLOG(ERROR, OUTPUT_LOGGER) << "Output pin config file at driver_board corrupted: " << e.what();
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
return pins_driver_board;
|
|
}
|
|
|
|
std::map<std::string, uint8_t> parse_pins_sound(json &sound_board_config)
|
|
{
|
|
std::map<std::string, uint8_t> pins_sound;
|
|
try
|
|
{
|
|
pins_sound["i2c_address"] = sound_board_config.at("i2c_address").get<uint8_t>();
|
|
pins_sound["pin_base"] = sound_board_config.at("pin_base").get<uint8_t>();
|
|
pins_sound["fire"] = sound_board_config.at("fire").get<uint8_t>();
|
|
|
|
json sound_address = sound_board_config.at("sound_address_select");
|
|
pins_sound["A"] = sound_address.at("A").get<uint8_t>();
|
|
pins_sound["B"] = sound_address.at("B").get<uint8_t>();
|
|
pins_sound["C"] = sound_address.at("C").get<uint8_t>();
|
|
pins_sound["D"] = sound_address.at("D").get<uint8_t>();
|
|
pins_sound["E"] = sound_address.at("E").get<uint8_t>();
|
|
pins_sound["F"] = sound_address.at("F").get<uint8_t>();
|
|
pins_sound["G"] = sound_address.at("G").get<uint8_t>();
|
|
}
|
|
catch(json::exception &e)
|
|
{
|
|
CLOG(ERROR, OUTPUT_LOGGER) << "Output pin config file at sound_board corrupted: " << e.what();
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
return pins_sound;
|
|
}
|
|
|
|
std::map<std::string, uint8_t> parse_pins_display(json &display_board_config)
|
|
{
|
|
std::map<std::string, uint8_t> pins_display;
|
|
try
|
|
{
|
|
pins_display["run"] = display_board_config.at("run");
|
|
}
|
|
catch(json::exception &e)
|
|
{
|
|
CLOG(ERROR, OUTPUT_LOGGER) << "Output pin config file at display_board corrupted: " << e.what();
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
std::map<std::string, std::shared_ptr<items::ISolenoid>> create_solenoids(std::istream &solenoid_config, std::shared_ptr<OutputPinController> output_gpio_interface)
|
|
{
|
|
std::map<std::string, std::shared_ptr<items::ISolenoid>> solenoids;
|
|
|
|
json solenoids_json;
|
|
solenoid_config >> solenoids_json;
|
|
|
|
auto deactivation_time = get_deactivation_time(solenoids_json);
|
|
|
|
for(auto &solenoid_json : solenoids_json)
|
|
{
|
|
auto solenoid = create_solenoid(solenoid_json, output_gpio_interface, deactivation_time);
|
|
solenoids.emplace(solenoid->get_name(), solenoid);
|
|
}
|
|
return solenoids;
|
|
}
|
|
|
|
std::map<std::string, std::shared_ptr<items::ILamp>> create_lamps(std::istream &lamp_config, std::shared_ptr<OutputPinController> output_gpio_interface)
|
|
{
|
|
std::map<std::string, std::shared_ptr<items::ILamp>> lamps;
|
|
|
|
json lamps_json;
|
|
lamp_config >> lamps_json;
|
|
|
|
for(auto &lamp_json : lamps_json)
|
|
{
|
|
auto lamp = create_lamp(lamp_json, output_gpio_interface);
|
|
lamps.emplace(lamp->get_name(), lamp);
|
|
}
|
|
|
|
return lamps;
|
|
}
|
|
|
|
std::map<std::string, std::shared_ptr<items::ISound>> create_sounds(std::istream &sound_config, std::shared_ptr<OutputPinController> output_gpio_interface)
|
|
{
|
|
std::map<std::string, std::shared_ptr<items::ISound>> sounds;
|
|
|
|
json sounds_json;
|
|
sound_config >> sounds_json;
|
|
|
|
auto deactivation_time = get_deactivation_time(sounds_json);
|
|
|
|
for(auto &sound_json : sounds_json)
|
|
{
|
|
auto sound = create_sound(sound_json, output_gpio_interface, deactivation_time);
|
|
sounds.emplace(sound->Item::get_name(), sound);
|
|
}
|
|
|
|
return sounds;
|
|
}
|
|
|
|
std::chrono::milliseconds get_deactivation_time(nlohmann::json &json)
|
|
{
|
|
try
|
|
{
|
|
auto deactivation_time = json.at("deactivation_time_milliseconds").get<uint>();
|
|
return std::chrono::milliseconds(deactivation_time);
|
|
}
|
|
catch(json::type_error &e)
|
|
{
|
|
// todo log and exit
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<items::Solenoid> create_solenoid(nlohmann::json &solenoid_json, std::shared_ptr<OutputPinController> output_gpio_interface, std::chrono::milliseconds deactivation_time)
|
|
{
|
|
try
|
|
{
|
|
std::string name = solenoid_json.at("name");
|
|
auto address = solenoid_json.at("address").get<uint8_t>();
|
|
|
|
if(solenoid_json.find("deactivation_time_milliseconds") != solenoid_json.end())
|
|
{
|
|
deactivation_time = get_deactivation_time(solenoid_json);
|
|
}
|
|
|
|
return std::make_shared<items::Solenoid>(output_gpio_interface, address, name, deactivation_time);
|
|
}
|
|
catch(json::type_error &e)
|
|
{
|
|
// todo log and exit
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<items::Lamp> create_lamp(nlohmann::json &lamp_json, std::shared_ptr<OutputPinController> output_gpio_interface)
|
|
{
|
|
try
|
|
{
|
|
std::string name = lamp_json.at("name");
|
|
auto address = lamp_json.at("address").get<uint8_t>();
|
|
return std::make_shared<items::Lamp>(output_gpio_interface, address, name);
|
|
}
|
|
catch(json::type_error &e)
|
|
{
|
|
// todo log and exit
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<items::Sound> create_sound(nlohmann::json &sound_json, std::shared_ptr<OutputPinController> &output_gpio_interface, std::chrono::milliseconds deactivation_time)
|
|
{
|
|
try
|
|
{
|
|
auto id = sound_json.at("id").get<uint>();
|
|
std::string description = sound_json.at("description");
|
|
auto address = sound_json.at("address").get<uint8_t>();
|
|
return std::make_shared<items::Sound>(output_gpio_interface, address, description, deactivation_time, id);
|
|
}
|
|
catch(json::type_error &e)
|
|
{
|
|
// todo log and exit
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
} |