factoryyyyyN

This commit is contained in:
Jonas Zeunert
2018-12-10 23:49:29 +01:00
parent d65d87b8f1
commit 6367e79898
16 changed files with 99 additions and 69 deletions

View File

@@ -1,4 +1,5 @@
{ {
"deactivation_time_milliseconds" : 10,
"solenoids" : "solenoids" :
[ [
{ // todo is this solenoid? { // todo is this solenoid?

View File

@@ -17,7 +17,8 @@ namespace flippR_driver
std::shared_ptr<output::IOutputDriver> get_OutputDriver(std::istream &output_pin_config, std::shared_ptr<output::IOutputDriver> get_OutputDriver(std::istream &output_pin_config,
std::istream &lamp_config, std::istream &lamp_config,
std::istream &solenoid_config, std::istream &solenoid_config,
std::istream &sound_config); std::istream &sound_config,
std::istream &display_config);
} }
#endif //flippR_driver_DRIVERFACTORY_H #endif //flippR_driver_DRIVERFACTORY_H

View File

@@ -22,6 +22,7 @@ class ISolenoid : public IItem
public: public:
~ISolenoid() ~ISolenoid()
{}; {};
virtual void trigger() = 0; virtual void trigger() = 0;
}; };

View File

@@ -17,8 +17,9 @@ namespace flippR_driver
std::shared_ptr<output::IOutputDriver> get_OutputDriver(std::istream &output_pin_config, std::shared_ptr<output::IOutputDriver> get_OutputDriver(std::istream &output_pin_config,
std::istream &lamp_config, std::istream &lamp_config,
std::istream &solenoid_config, std::istream &solenoid_config,
std::istream &sound_config) std::istream &sound_config,
std::istream &display_config)
{ {
return output::OutputDriverFactory::get_OutputDriver(output_pin_config, lamp_config, solenoid_config, sound_config); return output::OutputDriverFactory::get_OutputDriver(output_pin_config, lamp_config, solenoid_config, sound_config, display_config);
} }
} }

View File

@@ -7,7 +7,7 @@
#include "IOutputGPIOInterface.h" #include "IOutputGPIOInterface.h"
#include "output/items/IDriverBoardItem.h" #include "output/items/IItem.h"
#include "output/items/ISound.h" #include "output/items/ISound.h"
#include "output/items/IDisplay.h" #include "output/items/IDisplay.h"
#include <memory> #include <memory>
@@ -22,10 +22,10 @@ class IOutputGPIOInterface
public: public:
virtual ~IOutputGPIOInterface() = default; virtual ~IOutputGPIOInterface() = default;
virtual void activate(items::IDriverBoardItem *driver_board_item) = 0; virtual void activate(items::IItem *driver_board_item) = 0;
virtual void activate(items::ISound *sound) = 0; virtual void activate(items::ISound *sound) = 0;
virtual void deactivate(items::IDriverBoardItem *driver_board_item) = 0; virtual void deactivate(items::IItem *driver_board_item) = 0;
virtual void deactivate(items::ISound *sound) = 0; virtual void deactivate(items::ISound *sound) = 0;
virtual void write_display(std::shared_ptr<output::items::IDisplay> display) = 0; virtual void write_display(std::shared_ptr<output::items::IDisplay> display) = 0;

View File

@@ -17,8 +17,8 @@ namespace output
using namespace items; using namespace items;
OutputDriver::OutputDriver(std::map<std::string, std::shared_ptr<ISolenoid>> solenoids, std::map<std::string, std::shared_ptr<ILamp>> lamps, std::map<char, std::shared_ptr<IDisplay>> displays, std::map<std::string, std::shared_ptr<ISound>> sounds) OutputDriver::OutputDriver(std::map<std::string, std::shared_ptr<ISolenoid>> solenoids, std::map<std::string, std::shared_ptr<ILamp>> lamps, std::map<std::string, std::shared_ptr<ISound>> sounds, std::map<char, std::shared_ptr<IDisplay>> displays)
: solenoids(std::move(solenoids)), lamps(std::move(lamps)), displays(std::move(displays)), sounds(std::move(sounds)) : solenoids(std::move(solenoids)), lamps(std::move(lamps)), sounds(std::move(sounds)), displays(std::move(displays))
{} {}

View File

@@ -20,7 +20,7 @@ namespace output
class OutputDriver : public IOutputDriver class OutputDriver : public IOutputDriver
{ {
public: public:
OutputDriver(std::map<std::string, std::shared_ptr<items::ISolenoid>> solenoids, std::map<std::string, std::shared_ptr<items::ILamp>> lamps, std::map<char, std::shared_ptr<items::IDisplay>> displays, std::map<std::string, std::shared_ptr<items::ISound>> sounds); OutputDriver(std::map<std::string, std::shared_ptr<items::ISolenoid>> solenoids, std::map<std::string, std::shared_ptr<items::ILamp>> lamps, std::map<std::string, std::shared_ptr<items::ISound>> sounds, std::map<char, std::shared_ptr<items::IDisplay>> displays);
~OutputDriver() override = default; ~OutputDriver() override = default;
// todo what is flipper_relay ? // todo what is flipper_relay ?
@@ -37,8 +37,9 @@ public:
private: private:
const std::map<std::string, std::shared_ptr<items::ILamp>> lamps; const std::map<std::string, std::shared_ptr<items::ILamp>> lamps;
const std::map<std::string, std::shared_ptr<items::ISolenoid>> solenoids; const std::map<std::string, std::shared_ptr<items::ISolenoid>> solenoids;
const std::map<char, std::shared_ptr<items::IDisplay>> displays;
const std::map<std::string, std::shared_ptr<items::ISound>> sounds; const std::map<std::string, std::shared_ptr<items::ISound>> sounds;
const std::map<char, std::shared_ptr<items::IDisplay>> displays;
}; };
} /* namespace output */ } /* namespace output */

View File

@@ -4,6 +4,9 @@
#include "OutputDriverFactory.h" #include "OutputDriverFactory.h"
#include "OutputDriver.h"
namespace flippR_driver namespace flippR_driver
{ {
namespace output namespace output
@@ -16,7 +19,7 @@ namespace
using namespace nlohmann; using namespace nlohmann;
std::shared_ptr<IOutputDriver> get_OutputDriver(std::istream &output_pin_config, std::istream &solenoid_config, std::istream &lamp_config, std::istream &sound_config) std::shared_ptr<IOutputDriver> get_OutputDriver(std::istream &output_pin_config, std::istream &solenoid_config, std::istream &lamp_config, std::istream &sound_config, std::istream &display_config)
{ {
auto output_gpio_interface = create_OutputGPIOInterface(output_pin_config); auto output_gpio_interface = create_OutputGPIOInterface(output_pin_config);
@@ -24,7 +27,9 @@ std::shared_ptr<IOutputDriver> get_OutputDriver(std::istream &output_pin_config,
auto lamps = create_lamps(lamp_config, output_gpio_interface); auto lamps = create_lamps(lamp_config, output_gpio_interface);
auto sounds = create_sounds(sound_config, output_gpio_interface); auto sounds = create_sounds(sound_config, output_gpio_interface);
return std::make_shared<IOutputDriver>(solenoids, lamps, sounds); auto displays = create_displays(display_config, output_gpio_interface);
return std::make_shared<OutputDriver>(solenoids, lamps, sounds, displays);
} }
std::shared_ptr<OutputGPIOInterface> create_OutputGPIOInterface(std::istream &output_pin_config) std::shared_ptr<OutputGPIOInterface> create_OutputGPIOInterface(std::istream &output_pin_config)
@@ -88,39 +93,85 @@ std::map<std::string, uint8_t> parse_pins_display(json &display_board_config)
return std::map<std::string, uint8_t>(); return std::map<std::string, uint8_t>();
} }
std::map<std::string, std::shared_ptr<items::ISolenoid>> create_solenoids(std::istream &solenoid_config, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface) std::map<std::string, std::shared_ptr<items::ISolenoid>> create_solenoids(std::istream &solenoid_config, std::shared_ptr<OutputGPIOInterface> output_gpio_interface)
{ {
std::map<std::string, items::ISolenoid> solenoids; 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->Item::get_name(), solenoid_json);
}
return solenoids; return solenoids;
} }
std::map<std::string, std::shared_ptr<items::ILamp>> create_lamps(std::istream &lamp_config, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface) std::map<std::string, std::shared_ptr<items::ILamp>> create_lamps(std::istream &lamp_config, std::shared_ptr<OutputGPIOInterface> output_gpio_interface)
{ {
std::map<std::string, items::ILamp> lamps; 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->Item::get_name(), lamp);
}
return lamps; return lamps;
} }
std::map<std::string, std::shared_ptr<items::ISound>> create_sounds(std::istream &sound_config, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface) std::map<std::string, std::shared_ptr<items::ISound>> create_sounds(std::istream &sound_config, std::shared_ptr<OutputGPIOInterface> output_gpio_interface)
{ {
std::map<std::string, std::shared_ptr<items::ISound>> sounds; std::map<std::string, std::shared_ptr<items::ISound>> sounds;
json sounds_json; json sounds_json;
sound_config >> sounds_json; sound_config >> sounds_json;
auto deactivation_time = get_deactivation_time(sounds_json);
for(auto &sound_json : sounds_json) for(auto &sound_json : sounds_json)
{ {
auto sound = create_sound(sound_json); auto sound = create_sound(sound_json, output_gpio_interface, deactivation_time);
sounds.emplace(sound->get_name(), sound); sounds.emplace(sound->Item::get_name(), sound);
} }
return sounds; return sounds;
} }
std::shared_ptr<items::ISolenoid> create_solenoid(nlohmann::json &solenoid_json, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface)
std::chrono::milliseconds get_deactivation_time(nlohmann::json &json)
{ {
return std::shared_ptr<items::ISolenoid>(); try
{
uint deactivation_time = json.at("deactivation_time_milliseconds").get<uint>();
return std::chrono::milliseconds(deactivation_time);
}
catch(json::type_error &e)
{
// todo log and exit
}
} }
std::shared_ptr<items::Sound> create_sound(nlohmann::json &sound_json, std::shared_ptr<IOutputGPIOInterface> &output_gpio_interface, std::chrono::milliseconds deactivation_time) std::shared_ptr<items::Solenoid> create_solenoid(nlohmann::json &solenoid_json, std::shared_ptr<OutputGPIOInterface> output_gpio_interface, std::chrono::milliseconds deactivation_time)
{
try
{
std::string name = solenoid_json.at("name");
uint8_t address = solenoid_json.at("address").get<uint8_t>();
return std::make_shared<items::Solenoid>(output_gpio_interface, address, name, deactivation_time);
}
catch(json::type_error &e)
{
// todo log and exit
}
}
std::shared_ptr<items::Sound> create_sound(nlohmann::json &sound_json, std::shared_ptr<OutputGPIOInterface> &output_gpio_interface, std::chrono::milliseconds deactivation_time)
{ {
try try
{ {

View File

@@ -19,18 +19,22 @@ namespace output
{ {
namespace OutputDriverFactory namespace OutputDriverFactory
{ {
std::shared_ptr<IOutputDriver> get_OutputDriver(std::istream &output_pin_config, std::istream &solenoid_config, std::istream &lamp_config, std::istream &sound_config); std::shared_ptr<IOutputDriver> get_OutputDriver(std::istream &output_pin_config, std::istream &solenoid_config, std::istream &lamp_config, std::istream &sound_config, std::istream &display_config);
namespace namespace
{ {
std::map<std::string, std::shared_ptr<items::ISolenoid>> create_solenoids(std::istream &solenoid_config, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface); std::map<std::string, std::shared_ptr<items::ISolenoid>> create_solenoids(std::istream &solenoid_config, std::shared_ptr<OutputGPIOInterface> output_gpio_interface);
std::shared_ptr<items::ISolenoid> create_solenoid(nlohmann::json &solenoid_json, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface); std::shared_ptr<items::Solenoid> create_solenoid(nlohmann::json &solenoid_json, std::shared_ptr<OutputGPIOInterface> output_gpio_interface, std::chrono::milliseconds deactivation_time);
std::map<std::string, std::shared_ptr<items::ILamp>> create_lamps(std::istream &lamp_config, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface); std::map<std::string, std::shared_ptr<items::ILamp>> create_lamps(std::istream &lamp_config, std::shared_ptr<OutputGPIOInterface> output_gpio_interface);
std::shared_ptr<items::ILamp> create_lamp(nlohmann::json &lamp_json, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface); std::shared_ptr<items::Lamp> create_lamp(nlohmann::json &lamp_json, std::shared_ptr<OutputGPIOInterface> output_gpio_interface);
std::map<std::string, std::shared_ptr<items::ISound>> create_sounds(std::istream &sound_config, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface); std::map<std::string, std::shared_ptr<items::ISound>> create_sounds(std::istream &sound_config, std::shared_ptr<OutputGPIOInterface> output_gpio_interface);
std::shared_ptr<items::ISound> create_sound(nlohmann::json &sound_json, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface, std::chrono::milliseconds deactivation_time); std::shared_ptr<items::Sound> create_sound(nlohmann::json &sound_json, std::shared_ptr<OutputGPIOInterface> output_gpio_interface, std::chrono::milliseconds deactivation_time);
std::chrono::milliseconds get_deactivation_time(nlohmann::json &json);
std::map<char, std::shared_ptr<items::IDisplay>> create_displays(std::istream &display_config, std::shared_ptr<OutputGPIOInterface> output_gpio_interface);
std::shared_ptr<OutputGPIOInterface> create_OutputGPIOInterface(std::istream &output_pin_config); std::shared_ptr<OutputGPIOInterface> create_OutputGPIOInterface(std::istream &output_pin_config);
std::map<std::string, uint8_t> parse_pins_driver_board(nlohmann::json &driver_board_config); std::map<std::string, uint8_t> parse_pins_driver_board(nlohmann::json &driver_board_config);

View File

@@ -45,7 +45,7 @@ void OutputGPIOInterface::initialize_i2c_addresses()
mcp23017Setup(pins_display.at("pin_base"), pins_display.at("i2c_address")); mcp23017Setup(pins_display.at("pin_base"), pins_display.at("i2c_address"));
} }
void OutputGPIOInterface::activate(output::items::ISoundItem *sound) void OutputGPIOInterface::activate(output::items::ISound *sound)
{ {
std::lock_guard<std::mutex> guard(output_item_mutex); std::lock_guard<std::mutex> guard(output_item_mutex);
@@ -54,7 +54,7 @@ void OutputGPIOInterface::activate(output::items::ISoundItem *sound)
fire_sound(true); fire_sound(true);
} }
void OutputGPIOInterface::activate(output::items::IDriverBoardItem *driver_board_item) void OutputGPIOInterface::activate(output::items::IItem *driver_board_item)
{ {
std::lock_guard<std::mutex> guard(output_item_mutex); std::lock_guard<std::mutex> guard(output_item_mutex);
@@ -63,7 +63,7 @@ void OutputGPIOInterface::activate(output::items::IDriverBoardItem *driver_board
write_data(true); write_data(true);
} }
void OutputGPIOInterface::deactivate(output::items::IDriverBoardItem *driver_board_item) void OutputGPIOInterface::deactivate(output::items::IItem *driver_board_item)
{ {
std::lock_guard<std::mutex> guard(output_item_mutex); std::lock_guard<std::mutex> guard(output_item_mutex);
@@ -72,7 +72,7 @@ void OutputGPIOInterface::deactivate(output::items::IDriverBoardItem *driver_boa
write_data(false); write_data(false);
} }
void OutputGPIOInterface::deactivate(output::items::ISoundItem *sound) void OutputGPIOInterface::deactivate(output::items::ISound *sound)
{ {
std::lock_guard<std::mutex> guard(output_item_mutex); std::lock_guard<std::mutex> guard(output_item_mutex);

View File

@@ -32,10 +32,10 @@ public:
~OutputGPIOInterface() override = default; ~OutputGPIOInterface() override = default;
void activate(items::IDriverBoardItem *driver_board_item) override; void activate(items::IItem *driver_board_item) override;
void activate(items::ISound *sound) override; void activate(items::ISound *sound) override;
void deactivate(items::IDriverBoardItem *driver_board_item) override; void deactivate(items::IItem *driver_board_item) override;
void deactivate(items::ISound *sound) override; void deactivate(items::ISound *sound) override;
void write_display(std::shared_ptr<output::items::IDisplay> display) override; void write_display(std::shared_ptr<output::items::IDisplay> display) override;

View File

@@ -1,29 +0,0 @@
//
// Created by rhetenor on 23.11.18.
//
#ifndef FLIPPR_DRIVER_IDRIVERBOARDITEM_H
#define FLIPPR_DRIVER_IDRIVERBOARDITEM_H
#include "output/items/IItem.h"
#include <cstdint>
namespace flippR_driver
{
namespace output
{
namespace items
{
class IDriverBoardItem : public IItem
{
public:
~IDriverBoardItem() override = default;
};
}
}
}
#endif //FLIPPR_DRIVER_IDRIVERBOARDITEM_H

View File

@@ -21,6 +21,7 @@ uint8_t Item::get_address()
{ {
return this->address; return this->address;
} }
std::string Item::get_name() std::string Item::get_name()
{ {
return this->name; return this->name;

View File

@@ -10,7 +10,6 @@
#include "Item.h" #include "Item.h"
#include "output/items/ILamp.h" #include "output/items/ILamp.h"
#include "IDriverBoardItem.h"
namespace flippR_driver namespace flippR_driver
{ {
@@ -19,7 +18,7 @@ namespace output
namespace items namespace items
{ {
class Lamp : public Item, ILamp, IDriverBoardItem class Lamp : public Item, public ILamp
{ {
public: public:
Lamp(std::shared_ptr<IOutputGPIOInterface> output_gpio_interface, uint8_t address, std::string name); Lamp(std::shared_ptr<IOutputGPIOInterface> output_gpio_interface, uint8_t address, std::string name);

View File

@@ -10,7 +10,6 @@
#include "output/items/ISolenoid.h" #include "output/items/ISolenoid.h"
#include "Item.h" #include "Item.h"
#include "IDriverBoardItem.h"
#include <future> #include <future>
#include <chrono> #include <chrono>
@@ -22,7 +21,7 @@ namespace output
namespace items namespace items
{ {
class Solenoid : public Item, ISolenoid, IDriverBoardItem class Solenoid : public Item, public ISolenoid
{ {
public: public:
Solenoid(std::shared_ptr<IOutputGPIOInterface> output_gpio_interface, u_int8_t address, std::string name, std::chrono::milliseconds deactivation_time); Solenoid(std::shared_ptr<IOutputGPIOInterface> output_gpio_interface, u_int8_t address, std::string name, std::chrono::milliseconds deactivation_time);

View File

@@ -24,7 +24,7 @@ namespace output
namespace items namespace items
{ {
class Sound : public ISound, Item class Sound : public ISound, public Item
{ {
public: public:
u_int id; u_int id;