26 #include "modules/wiegand/WiegandConfig_odb.h" 31 #include <boost/property_tree/ptree.hpp> 33 #include <zmqpp/context.hpp> 34 #include <zmqpp/message.hpp> 41 return "WIEGAND_READER";
50 start_module(
zmqpp::socket *pipe, boost::property_tree::ptree cfg,
53 return Leosac::Module::start_module_helper<WiegandReaderModule>(pipe, cfg,
58 boost::property_tree::ptree
const &cfg,
83 boost::property_tree::ptree module_config =
config_.get_child(
"module_config");
85 if (module_config.get<
bool>(
"use_database",
false))
89 ASSERT_LOG(hwd_service,
"No hardware service but we have database.");
102 using namespace Colorize;
103 INFO(
"Creating WiegandReader: " 106 <<
"\n\t Buzzer: " <<
green(
underline(reader_config->buzzer_name()))
107 <<
"\n\t GPIO Low: " <<
green(
underline(reader_config->gpio_low_name()))
108 <<
"\n\t GPIO High: " 113 ctx_, reader_config->name(), reader_config->gpio_high_name(),
114 reader_config->gpio_low_name(), reader_config->green_led_name(),
115 reader_config->buzzer_name(),
create_strategy(*reader_config, &reader));
116 utils_->config_checker().register_object(reader.name(),
118 readers_.push_back(std::move(reader));
124 if (
config_.get_child(
"module_config").get<
bool>(
"use_database",
false))
146 using namespace Auth;
147 using namespace Strategy;
149 auto simple_wiegand =
150 std::unique_ptr<CardReading>(
new SimpleWiegandStrategy(reader));
151 auto pin_4bits = std::unique_ptr<PinReading>(
new WiegandPinNBitsOnly<4>(
153 auto pin_8bits = std::unique_ptr<PinReading>(
new WiegandPinNBitsOnly<8>(
155 auto pin_buffered = std::unique_ptr<PinReading>(
new WiegandPinBuffered(reader));
157 if (reader_cfg.
mode ==
"SIMPLE_WIEGAND")
158 return std::move(simple_wiegand);
159 else if (reader_cfg.
mode ==
"WIEGAND_PIN_4BITS")
160 return std::move(pin_4bits);
161 else if (reader_cfg.
mode ==
"WIEGAND_PIN_8BITS")
162 return std::move(pin_8bits);
163 else if (reader_cfg.
mode ==
"WIEGAND_PIN_BUFFERED")
164 return std::move(pin_buffered);
165 else if (reader_cfg.
mode ==
"WIEGAND_CARD_PIN_4BITS")
167 return std::unique_ptr<WiegandStrategy>(
168 new WiegandCardAndPin(reader, std::move(simple_wiegand),
171 else if (reader_cfg.
mode ==
"WIEGAND_CARD_PIN_8BITS")
173 return std::unique_ptr<WiegandStrategy>(
174 new WiegandCardAndPin(reader, std::move(simple_wiegand),
177 else if (reader_cfg.
mode ==
"WIEGAND_CARD_PIN_BUFFERED")
179 return std::unique_ptr<WiegandStrategy>(
180 new WiegandCardAndPin(reader, std::move(simple_wiegand),
183 else if (reader_cfg.
mode ==
"AUTODETECT")
185 return std::unique_ptr<WiegandStrategy>(
190 ERROR(
"Wiegand mode " << reader_cfg.
mode <<
" is not a valid mode.");
192 throw std::runtime_error(
"Invalid wiegand mode " + reader_cfg.
mode);
200 using namespace odb::core;
201 auto db =
utils_->database();
204 schema_version v = db->schema_version(
"module_wiegand");
205 schema_version cv(schema_catalog::current_version(*db,
"module_wiegand"));
208 transaction t(db->begin());
209 INFO(
"Attempt to create module_wiegand SQL schema.");
210 schema_catalog::create_schema(*db,
"module_wiegand");
215 INFO(
"Wiegand Module performing database migration. Going from version " 216 << v <<
" to version " << cv);
217 transaction t(db->begin());
218 schema_catalog::migrate(*db, cv,
"module_wiegand");
226 odb::transaction t(
utils_->database()->begin());
227 odb::result<WiegandReaderConfig> result(
229 for (
const auto &reader : result)
233 ASSERT_LOG(reader_ptr,
"Loading from database/cache failed");
234 if (reader_ptr->enabled())
238 INFO(
"Wiegand module using SQL database for configuration.");
242 const boost::property_tree::ptree &module_config)
245 for (
auto &node : module_config.get_child(
"readers"))
247 auto reader_config = std::make_shared<WiegandReaderConfig>();
248 boost::property_tree::ptree xml_reader_cfg = node.second;
254 auto gpio_high = std::make_shared<Hardware::GPIO>();
255 gpio_high->name(xml_reader_cfg.get<std::string>(
"high"));
257 auto gpio_low = std::make_shared<Hardware::GPIO>();
258 gpio_low->name(xml_reader_cfg.get<std::string>(
"low"));
260 auto green_led = std::make_shared<Hardware::LED>();
261 green_led->name(xml_reader_cfg.get<std::string>(
"green_led",
""));
263 auto buzzer = std::make_shared<Hardware::Buzzer>();
264 buzzer->name(xml_reader_cfg.get<std::string>(
"buzzer",
""));
266 reader_config->name(xml_reader_cfg.get_child(
"name").data());
267 reader_config->gpio_high_ = gpio_high;
268 reader_config->gpio_low_ = gpio_low;
269 reader_config->buzzer_ = buzzer;
270 reader_config->green_led_ = green_led;
272 reader_config->mode =
273 xml_reader_cfg.get<std::string>(
"mode",
"SIMPLE_WIEGAND");
274 reader_config->pin_timeout =
275 std::chrono::milliseconds(xml_reader_cfg.get<
int>(
"pin_timeout", 2500));
276 reader_config->pin_key_end = xml_reader_cfg.get<
char>(
"pin_key_end",
'#');
283 if (!reader_config->green_led_name().empty())
286 if (!reader_config->buzzer_name().empty())
WiegandReaderModule(zmqpp::context &ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
void load_xml_config(const boost::property_tree::ptree &module_config)
Load the module configuration from the XML configuration object.
An instance of this class represents the configuration of one Wiegand reader.
void handle_bus_msg()
Something happened on the bus.
std::unique_ptr< WiegandStrategy > WiegandStrategyUPtr
A service object provided by the Websocket module.
An implementation class that represents a Wiegand Reader.
This is the header file for a generated source file, GitSHA1.cpp.
std::shared_ptr< WiegandReaderConfig > WiegandReaderConfigPtr
std::chrono::milliseconds pin_timeout
std::unique_ptr< WiegandConfig > wiegand_config_
Configuration object for the module.
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
__attribute__((visibility("default"))) bool start_module(zmqpp
Entry point of wiegand module.
CoreUtilsPtr utils_
Pointer to the core utils, which gives access to scheduler and others.
void process_config()
Create wiegand reader instances based on configuration.
Strategy::WiegandStrategyUPtr create_strategy(const WiegandReaderConfig &reader_config, WiegandReaderImpl *reader)
Internal factory that build a strategy object based upon a reader configuration.
std::string underline(const T &in)
std::vector< WiegandReaderImpl > readers_
Vector of wiegand reader managed by this module.
bool is_running_
Boolean indicating whether the main loop should run or not.
~WiegandReaderModule() override
virtual void run() override
Module's main loop.
std::string green(const T &in)
Database aware Hardware Service.
void config_check(const std::string &obj_name, ConfigChecker::ObjectType type)
An helper that checks configuration the existence of some objects.
Base class for module implementation.
zmqpp::context & ctx_
A reference to the ZeroMQ context in case you need it to create additional socket.
#define ASSERT_LOG(cond, msg)
Provide ODB magic to be able to store an Leosac::Audit::EventType (FlagSet) object.
zmqpp::reactor reactor_
The reactor object we poll() on in the main loop.
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
std::shared_ptr< CoreUtils > CoreUtilsPtr
const char * get_module_name()
boost::property_tree::ptree config_
The configuration tree passed to the start_module function.
Provide support for Wiegand devices.
void load_db_config()
Load the module configuration from the database.
std::unique_ptr< WSHelperThread > ws_helper_thread_
static json serialize(const WiegandReaderConfig &in, const SecurityContext &sc)
void handle_request()
Someone sent a request.
void unregister_serializer()