Leosac  0.8.0
Open Source Access Control
AuthTarget.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 "AuthTarget.hpp"
21 #include "tools/log.hpp"
22 
23 using namespace Leosac::Auth;
24 
25 const std::string &AuthTarget::name() const
26 {
27  return name_;
28 }
29 
30 void AuthTarget::name(std::string const &param)
31 {
32  INFO("Changing target name (from " << name_ << " to " << param);
33  name_ = param;
34 }
35 
36 AuthTarget::AuthTarget(const std::string target_name)
37  : name_(target_name),
38  contact_duration_(15000)
39 {
40 }
41 
43 {
44  always_open_.push_back(sched);
45 }
46 
48 {
49  always_close_.push_back(sched);
50 }
51 
53 {
54  return gpio_.get();
55 }
56 
57 void AuthTarget::gpio(std::unique_ptr<Leosac::Hardware::FGPIO> new_gpio)
58 {
59  gpio_ = std::move(new_gpio);
60 }
61 
62 void AuthTarget::exitreq_gpio(std::unique_ptr<Leosac::Hardware::FGPIO> new_gpio)
63 {
64  exitreq_gpio_ = std::move(new_gpio);
65 }
66 
67 void AuthTarget::exitreq_duration(std::chrono::milliseconds duration)
68 {
69  exitreq_duration_ = duration;
70 }
71 
72 void AuthTarget::contact_gpio(std::unique_ptr<Leosac::Hardware::FGPIO> new_gpio)
73 {
74  contact_gpio_ = std::move(new_gpio);
75 }
76 
77 void AuthTarget::contact_duration(std::chrono::milliseconds duration)
78 {
79  contact_duration_ = duration;
80 }
81 
83  const std::chrono::system_clock::time_point &tp) const
84 {
85  for (const auto &sched : always_open_)
86  {
87  if (sched->is_in_schedule(tp))
88  return true;
89  }
90  return false;
91 }
92 
94  const std::chrono::system_clock::time_point &tp) const
95 {
96  for (const auto &sched : always_close_)
97  {
98  if (sched->is_in_schedule(tp))
99  return true;
100  }
101  return false;
102 }
103 
104 void AuthTarget::resetToExpectedState(const std::chrono::system_clock::time_point &tp)
105 {
106  if (is_always_open(tp) && is_always_closed(tp))
107  {
108  WARN("Oops, door "
109  << name()
110  << " is both always open and always close at the same time.");
111  }
112  else if (is_always_open(tp) && !gpio()->isOn())
113  {
114  gpio()->turnOn();
115  }
116  else if (is_always_closed(tp) && !gpio()->isOff())
117  {
118  gpio()->turnOff();
119  }
120 }
Leosac::Auth::AuthTarget::add_always_open_sched
void add_always_open_sched(const Tools::IScheduleCPtr &sched)
Definition: AuthTarget.cpp:42
Leosac::Auth
Holds classes relevant to the Authentication and Authorization subsystem.
Definition: AccessPoint.hpp:27
WARN
@ WARN
Definition: log.hpp:33
AuthTarget.hpp
Leosac::Auth::AuthTarget::exitreq_gpio
Hardware::FGPIO * exitreq_gpio()
Leosac::Auth::AuthTarget::always_close_
std::vector< Tools::IScheduleCPtr > always_close_
Definition: AuthTarget.hpp:98
Leosac::Auth::AuthTarget::contact_duration
std::chrono::milliseconds contact_duration()
INFO
@ INFO
Definition: log.hpp:34
Leosac::Auth::AuthTarget::name
const std::string & name() const
Definition: AuthTarget.cpp:25
Leosac::Auth::AuthTarget::gpio
Hardware::FGPIO * gpio()
Returns the pointer to the optional FGPIO associated with the door.
Definition: AuthTarget.cpp:52
Leosac::Hardware::FGPIO
A Facade to a GPIO object.
Definition: FGPIO.hpp:45
Leosac::Auth::AuthTarget::gpio_
std::unique_ptr< Hardware::FGPIO > gpio_
Optional GPIO associated with the door.
Definition: AuthTarget.hpp:103
Leosac::Tools::IScheduleCPtr
std::shared_ptr< const ISchedule > IScheduleCPtr
Definition: ToolsFwd.hpp:38
Leosac::Auth::AuthTarget::contact_gpio
Hardware::FGPIO * contact_gpio()
Leosac::Auth::AuthTarget::name_
std::string name_
Definition: AuthTarget.hpp:95
Leosac::Auth::AuthTarget::add_always_close_sched
void add_always_close_sched(const Tools::IScheduleCPtr &sched)
Definition: AuthTarget.cpp:47
Leosac::Auth::AuthTarget::exitreq_gpio_
std::unique_ptr< Hardware::FGPIO > exitreq_gpio_
Optional Exit Req GPIO associated with the door.
Definition: AuthTarget.hpp:108
Leosac::Auth::AuthTarget::exitreq_duration
std::chrono::milliseconds exitreq_duration()
Leosac::Auth::AuthTarget::always_open_
std::vector< Tools::IScheduleCPtr > always_open_
Definition: AuthTarget.hpp:97
Leosac::Auth::AuthTarget::resetToExpectedState
void resetToExpectedState(const std::chrono::system_clock::time_point &tp)
Definition: AuthTarget.cpp:104
log.hpp
Leosac::Auth::AuthTarget::is_always_closed
bool is_always_closed(const std::chrono::system_clock::time_point &tp) const
Check whether the door is in "always closed" mode at the given time point.
Definition: AuthTarget.cpp:93
Leosac::Hardware::FGPIO::turnOff
bool turnOff()
Turn the GPIO OFF by sending a message to the backend GPIO impl.
Definition: FGPIO.cpp:65
Leosac::Auth::AuthTarget::exitreq_duration_
std::chrono::milliseconds exitreq_duration_
Duration for the Exit Req to keep the door open.
Definition: AuthTarget.hpp:113
Leosac::Auth::AuthTarget::contact_duration_
std::chrono::milliseconds contact_duration_
Duration for the Contact Door Sensor to be ignored before triggering an alarm.
Definition: AuthTarget.hpp:123
Leosac::Auth::AuthTarget::AuthTarget
AuthTarget(const std::string target_name)
Definition: AuthTarget.cpp:36
Leosac::Auth::AuthTarget::contact_gpio_
std::unique_ptr< Hardware::FGPIO > contact_gpio_
Optional Contact Door Sensor GPIO associated with the door.
Definition: AuthTarget.hpp:118
Leosac::Auth::AuthTarget::is_always_open
bool is_always_open(const std::chrono::system_clock::time_point &tp) const
Check whether the door is in "always open" mode at the given time point.
Definition: AuthTarget.cpp:82
Leosac::Hardware::FGPIO::turnOn
bool turnOn(std::chrono::milliseconds duration)
Turn the GPIO ON and turn it OFF duration milliseconds later.
Definition: FGPIO.cpp:34