Leosac  0.8.0
Open Source Access Control
LEDBuzzerModule.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "LEDBuzzerModule.hpp"
21 #include "core/kernel.hpp"
22 #include "hardware/Buzzer_odb.h"
23 #include "hardware/GPIO.hpp"
24 #include "hardware/LED_odb.h"
25 #include "tools/db/database.hpp"
26 #include "tools/timeout.hpp"
27 #include "ws/WSHelperThread.hpp"
28 #include <boost/iterator/transform_iterator.hpp>
29 
30 namespace Leosac
31 {
32 namespace Module
33 {
34 namespace LedBuzzer
35 {
36 
37 LEDBuzzerModule::LEDBuzzerModule(zmqpp::context &ctx, zmqpp::socket *pipe,
38  boost::property_tree::ptree const &cfg,
39  CoreUtilsPtr utils)
40  : BaseModule(ctx, pipe, cfg, utils)
41 {
43  for (auto &led : leds_and_buzzers_)
44  {
45  reactor_.add(led->frontend(),
46  std::bind(&LedBuzzerImpl::handle_message, led.get()));
47  }
48 }
49 
51 {
52 }
53 
55 {
56  if (config_.get_child("module_config").get<bool>("use_database", false))
57  {
58  ws_helper_thread_ = std::make_unique<WSHelperThread>(utils_);
59  ws_helper_thread_->start_running();
60  }
61  while (is_running_)
62  {
63  auto itr_transform = [](const std::shared_ptr<LedBuzzerImpl> &lb) {
64  return lb->next_update();
65  };
67  boost::make_transform_iterator(leds_and_buzzers_.begin(), itr_transform),
68  boost::make_transform_iterator(leds_and_buzzers_.end(), itr_transform)));
69  for (auto &led : leds_and_buzzers_)
70  {
71  if (led->next_update() <= std::chrono::system_clock::now())
72  led->update();
73  }
74  }
76  if (ws_service && ws_helper_thread_)
77  ws_helper_thread_->unregister_ws_handlers(*ws_service);
78 }
79 
81 {
82  boost::property_tree::ptree module_config = config_.get_child("module_config");
83 
84  if (module_config.get<bool>("use_database", false))
85  {
87  }
88  else
89  {
91  }
92 }
93 
95 {
96  boost::property_tree::ptree module_config = config_.get_child("module_config");
97 
98  if (const auto &leds_node = module_config.get_child_optional("leds"))
99  {
100  for (auto &node : *leds_node)
101  {
102  boost::property_tree::ptree led_cfg = node.second;
103 
104  std::string led_name = led_cfg.get<std::string>("name");
105  std::string gpio_name = led_cfg.get<std::string>("gpio");
106  int default_blink_duration =
107  led_cfg.get<int>("default_blink_duration", 1000);
108  int default_blink_speed = led_cfg.get<int>("default_blink_speed", 200);
109 
110  INFO("Creating LED " << led_name << ", linked to GPIO " << gpio_name);
112 
113  leds_and_buzzers_.push_back(std::make_shared<LedBuzzerImpl>(
114  ctx_, led_name, gpio_name, default_blink_duration,
115  default_blink_speed));
116  utils_->config_checker().register_object(led_name,
118  }
119  }
120 
121  if (const auto &buzzers_node = module_config.get_child_optional("buzzers"))
122  {
123  for (auto &node : *buzzers_node)
124  {
125  boost::property_tree::ptree buzzer_cfg = node.second;
126 
127  std::string buzzer_name = buzzer_cfg.get<std::string>("name");
128  std::string gpio_name = buzzer_cfg.get<std::string>("gpio");
129  int default_blink_duration =
130  buzzer_cfg.get<int>("default_blink_duration", 1000);
131  int default_blink_speed =
132  buzzer_cfg.get<int>("default_blink_speed", 200);
133 
134  INFO("Creating Buzzer " << buzzer_name << ", linked to GPIO "
135  << gpio_name);
137 
138  // internally we do not care if its a buzzer or a led.
139  leds_and_buzzers_.push_back(std::make_shared<LedBuzzerImpl>(
140  ctx_, buzzer_name, gpio_name, default_blink_duration,
141  default_blink_speed));
142  utils_->config_checker().register_object(
144  }
145  }
146 }
147 
149 {
150  using namespace odb;
151  using namespace odb::core;
152  auto db = utils_->database();
153 
154  // Load leds
155  {
156  odb::transaction t(utils_->database()->begin());
157  odb::result<Hardware::LED> result(
158  utils_->database()->query<Hardware::LED>());
159  for (const auto &led : result)
160  {
161  if (!led.enabled())
162  continue;
163  if (!led.gpio())
164  {
165  WARN("No GPIO associated with device " << led.name()
166  << ". Skipping.");
167  continue;
168  }
169  leds_and_buzzers_.push_back(std::make_shared<LedBuzzerImpl>(
170  ctx_, led.name(), led.gpio()->name(), led.default_blink_duration(),
171  led.default_blink_speed()));
172  utils_->config_checker().register_object(led.name(),
174  }
175  t.commit();
176  }
177 
178  // Load buzzer
179  {
180  odb::transaction t(utils_->database()->begin());
181  odb::result<Hardware::Buzzer> result(
182  utils_->database()->query<Hardware::Buzzer>());
183  for (const auto &buzzer : result)
184  {
185  if (!buzzer.enabled())
186  continue;
187  if (!buzzer.gpio())
188  {
189  WARN("No GPIO associated with device " << buzzer.name()
190  << ". Skipping.");
191  continue;
192  }
193  leds_and_buzzers_.push_back(std::make_shared<LedBuzzerImpl>(
194  ctx_, buzzer.name(), buzzer.gpio()->name(),
195  buzzer.default_blink_duration(), buzzer.default_blink_speed()));
196  utils_->config_checker().register_object(
198  }
199  t.commit();
200  }
201 
202  INFO("LEDBuzzer module using SQL database for configuration.");
203 }
204 }
205 }
206 }
Leosac::Module::LedBuzzer::LEDBuzzerModule::load_xml_config
void load_xml_config()
Definition: LEDBuzzerModule.cpp:94
Leosac::Module::BaseModule
Base class for module implementation.
Definition: BaseModule.hpp:110
Leosac::Tools::compute_timeout
int compute_timeout(InputIterator begin, InputIterator end)
Compute the time until the next timeout from a collection of time point.
Definition: timeout.hpp:39
WARN
@ WARN
Definition: log.hpp:33
Leosac::Module::LedBuzzer::LEDBuzzerModule::load_db_config
void load_db_config()
Definition: LEDBuzzerModule.cpp:148
Leosac::Module::BaseModule::config_check
void config_check(const std::string &obj_name, Leosac::Hardware::DeviceClass type)
An helper that checks configuration the existence of some objects.
Definition: BaseModule.cpp:143
database.hpp
Leosac::get_service_registry
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
Leosac::Module::LedBuzzer::LEDBuzzerModule::leds_and_buzzers_
std::vector< std::shared_ptr< LedBuzzerImpl > > leds_and_buzzers_
Definition: LEDBuzzerModule.hpp:80
Leosac::Hardware::DeviceClass::GPIO
@ GPIO
timeout.hpp
INFO
@ INFO
Definition: log.hpp:34
Leosac::Hardware::DeviceClass::BUZZER
@ BUZZER
odb
Provide ODB magic to be able to store an Leosac::Audit::EventType (FlagSet) object.
Definition: AuditEventMaskODB.hpp:31
Leosac::Module::LedBuzzer::LEDBuzzerModule::process_config
void process_config()
Definition: LEDBuzzerModule.cpp:80
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::WebSockAPI::Service
A service object provided by the Websocket module.
Definition: Service.hpp:45
kernel.hpp
Leosac::Module::BaseModule::config_
boost::property_tree::ptree config_
The configuration tree passed to the start_module function.
Definition: BaseModule.hpp:193
Leosac::Module::BaseModule::reactor_
zmqpp::reactor reactor_
The reactor object we poll() on in the main loop.
Definition: BaseModule.hpp:214
Leosac::Module::LedBuzzer::LedBuzzerImpl::handle_message
void handle_message()
Message received on the rep_ socket.
Definition: LedBuzzerImpl.cpp:47
Leosac::Module::BaseModule::utils_
CoreUtilsPtr utils_
Pointer to the core utils, which gives access to scheduler and others.
Definition: BaseModule.hpp:198
GPIO.hpp
Leosac::Module::BaseModule::is_running_
bool is_running_
Boolean indicating whether the main loop should run or not.
Definition: BaseModule.hpp:203
Leosac::Hardware::LED
Abstraction of LED device attributes.
Definition: LED.hpp:36
Leosac::Module::LedBuzzer::LEDBuzzerModule::ws_helper_thread_
std::unique_ptr< WSHelperThread > ws_helper_thread_
Definition: LEDBuzzerModule.hpp:81
Leosac::ServiceRegistry::get_service
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
Definition: ServiceRegistry.hpp:290
LEDBuzzerModule.hpp
Leosac::Module::LedBuzzer::LEDBuzzerModule::LEDBuzzerModule
LEDBuzzerModule(zmqpp::context &ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
Definition: LEDBuzzerModule.cpp:37
Leosac::Hardware::Buzzer
Abstraction of Buzzer device attributes.
Definition: Buzzer.hpp:42
Leosac::Module::BaseModule::ctx_
zmqpp::context & ctx_
A reference to the ZeroMQ context in case you need it to create additional socket.
Definition: BaseModule.hpp:183
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
Leosac::Hardware::DeviceClass::LED
@ LED
Leosac::Module::LedBuzzer::LEDBuzzerModule::~LEDBuzzerModule
virtual ~LEDBuzzerModule()
Definition: LEDBuzzerModule.cpp:50
Leosac::Module::LedBuzzer::LEDBuzzerModule::run
void run() override
This is the main loop of the module.
Definition: LEDBuzzerModule.cpp:54