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",
'#');
277 reader_config->nowait = xml_reader_cfg.get<
bool>(
"nowait", 0);
284 if (!reader_config->green_led_name().empty())
287 if (!reader_config->buzzer_name().empty())