Leosac  0.8.0
Open Source Access Control
LedBuzzerImpl.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 "LedBuzzerImpl.hpp"
21 #include "tools/log.hpp"
22 
23 using namespace Leosac::Module::LedBuzzer;
24 
25 LedBuzzerImpl::LedBuzzerImpl(zmqpp::context &ctx, std::string const &led_name,
26  std::string const &gpio_name, int blink_duration,
27  int blink_speed)
28  : ctx_(ctx)
29  , frontend_(ctx, zmqpp::socket_type::rep)
30  , backend_(ctx, zmqpp::socket_type::req)
31  , gpio_(ctx, gpio_name)
32  , default_blink_duration_(blink_duration)
33  , default_blink_speed_(blink_speed)
34  , stmachine_(std::ref(gpio_))
35 {
36  frontend_.bind("inproc://" + led_name);
37  backend_.connect("inproc://" + gpio_name);
38 
39  stmachine_.start();
40 }
41 
42 zmqpp::socket &LedBuzzerImpl::frontend()
43 {
44  return frontend_;
45 }
46 
48 {
49  zmqpp::message_t msg;
50  zmqpp::message_t rep;
51  std::string frame1;
52 
53  frontend_.receive(msg);
54  msg >> frame1;
55  bool ok = false;
56  if (frame1 == "STATE")
57  {
58  return send_state();
59  }
60  if (frame1 == "ON" || frame1 == "OFF" || frame1 == "TOGGLE")
61  {
62  // simply forward message to GPIO
63  rep = send_to_backend(msg);
64  frontend_.send(rep);
65  return;
66  }
67  else if (frame1 == "BLINK")
68  ok = start_blink(&msg);
69  else if (frame1 == "FAST_TO_SLOW")
70  {
71  ok = true;
73  e.pattern = {
74  {5000, 1000}, {2100, 700}, {1000, 100},
75  };
76  stmachine_.process_event(e);
77  }
78  else // invalid cmd
79  assert(0);
80  frontend_.send(ok ? "OK" : "KO");
81 }
82 
84 {
85  DEBUG("UPDATING LED");
86  stmachine_.process_event(SM::EventUpdate());
87 }
88 
89 std::chrono::system_clock::time_point LedBuzzerImpl::next_update()
90 {
91  return stmachine_.next_update();
92 }
93 
94 zmqpp::message LedBuzzerImpl::send_to_backend(zmqpp::message &msg)
95 {
96  zmqpp::message rep;
97  backend_.send(msg);
98 
99  backend_.receive(rep);
100  return rep;
101 }
102 
103 bool LedBuzzerImpl::start_blink(zmqpp::message *msg)
104 {
105  std::string tmp;
106  SM::EventBlink event_blink;
107  event_blink.duration = default_blink_duration_;
108  event_blink.speed = default_blink_speed_;
109 
110  if (msg->parts() > 1)
111  {
112  *msg >> event_blink.duration;
113  }
114 
115  if (msg->parts() > 2)
116  {
117  *msg >> event_blink.speed;
118  }
119  assert(event_blink.speed <= event_blink.duration);
120  stmachine_.process_event(event_blink);
121 
122  return true;
123 }
124 
126 {
127  zmqpp::message st;
128  if (stmachine_.led_state_.st == Hardware::FLED::State::BLINKING)
129  {
130  st << "BLINKING";
131  st << static_cast<int64_t>(stmachine_.led_state_.duration)
132  << static_cast<int64_t>(stmachine_.led_state_.speed);
133  }
134  st << (gpio_.isOn() ? "ON" : "OFF");
135  frontend_.send(st);
136 }
Leosac::Module::LedBuzzer::LedBuzzerImpl::default_blink_speed_
int64_t default_blink_speed_
Definition: LedBuzzerImpl.hpp:111
Leosac::Module::LedBuzzer::SM::EventPlayingPattern::pattern
std::vector< std::pair< int, int > > pattern
A vector of <duration, speed> that represents our blinking pattern.
Definition: LedBuzzerSM.hpp:80
zmqpp
Definition: CoreUtils.hpp:27
Leosac::Module::LedBuzzer::LedBuzzerImpl::stmachine_
LedBuzzerSM stmachine_
Our state machine that handle blinking, blinking in pattern or doing nothing.
Definition: LedBuzzerImpl.hpp:117
DEBUG
@ DEBUG
Definition: log.hpp:35
Leosac::Hardware::FLED::State::BLINKING
@ BLINKING
Definition: FLED.hpp:64
Leosac::Module::LedBuzzer::LedBuzzerImpl::default_blink_duration_
int64_t default_blink_duration_
Definition: LedBuzzerImpl.hpp:110
Leosac::Module::LedBuzzer::LedBuzzerImpl::next_update
std::chrono::system_clock::time_point next_update()
Time point of the next wanted update.
Definition: LedBuzzerImpl.cpp:89
Leosac::Module::LedBuzzer::LedBuzzerImpl::update
void update()
Update the object.
Definition: LedBuzzerImpl.cpp:83
Leosac::Module::LedBuzzer::LedBuzzerImpl::send_state
void send_state()
Write the current state of the LED device (according to specs) to the frontend_ socket.
Definition: LedBuzzerImpl.cpp:125
Leosac::Module::LedBuzzer::LedBuzzerImpl::handle_message
void handle_message()
Message received on the rep_ socket.
Definition: LedBuzzerImpl.cpp:47
Leosac::Module::LedBuzzer::LedBuzzerImpl::frontend
zmqpp::socket & frontend()
Return the frontend_ socket.
Definition: LedBuzzerImpl.cpp:42
Leosac::Module::LedBuzzer::LedBuzzerImpl::send_to_backend
zmqpp::message send_to_backend(zmqpp::message &msg)
Send a message to the backend object (used for ON, OFF, TOGGLE).
Definition: LedBuzzerImpl.cpp:94
Leosac::Module::LedBuzzer
Namespace where implementation of Led (or buzzer) support is done.
Definition: LedBuzzerImpl.hpp:32
Leosac::Module::LedBuzzer::LedBuzzerImpl::start_blink
bool start_blink(zmqpp::message *msg)
Start blinking, this stores the blink_end timepoint and send commands for blinking to happen.
Definition: LedBuzzerImpl.cpp:103
Leosac::Module::LedBuzzer::LedBuzzerImpl::frontend_
zmqpp::socket frontend_
REP socket to receive LED command.
Definition: LedBuzzerImpl.hpp:96
Leosac::Hardware::FGPIO::isOn
bool isOn()
Query the value of the GPIO and returns true if the LED is ON.
Definition: FGPIO.cpp:93
Leosac::Module::LedBuzzer::LedBuzzerImpl::gpio_
Hardware::FGPIO gpio_
Facade to the GPIO we use with this LED.
Definition: LedBuzzerImpl.hpp:108
Leosac::Module::LedBuzzer::LedBuzzerImpl::backend_
zmqpp::socket backend_
REQ socket to the backend GPIO.
Definition: LedBuzzerImpl.hpp:101
log.hpp
Leosac::Module::LedBuzzer::SM::EventUpdate
Fired when we update the state machine.
Definition: LedBuzzerSM.hpp:42
Leosac::Module::LedBuzzer::LedBuzzerImpl::LedBuzzerImpl
LedBuzzerImpl(zmqpp::context &ctx, const std::string &led_name, const std::string &gpio_name, int blink_duration, int blink_speed)
Definition: LedBuzzerImpl.cpp:25
LedBuzzerImpl.hpp
Leosac::Module::LedBuzzer::SM::EventPlayingPattern
Fired when we want to "play" a pattern.
Definition: LedBuzzerSM.hpp:72