Leosac  0.8.0
Open Source Access Control
WiegandConfigSerializer.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2017 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 
22 #include "hardware/Buzzer_odb.h"
23 #include "hardware/GPIO_odb.h"
25 #include "hardware/LED_odb.h"
27 #include "tools/JSONUtils.hpp"
28 
29 namespace Leosac
30 {
31 namespace Module
32 {
33 namespace Wiegand
34 {
35 
37  const SecurityContext &sc)
38 {
40  // Now we override the type.
41  ASSERT_LOG(serialized.at("type").is_string(),
42  "Base GPIO serialization did something unexpected.");
43 
44  serialized["type"] = "wiegand-reader";
45  serialized["attributes"]["mode"] = in.mode;
46 
47  serialized["relationships"] = json{};
48 
49  auto hwd_service =
51  ASSERT_LOG(hwd_service, "No hardware service.");
52 
53  serialized["relationships"]["gpio-high"] =
54  hwd_service->serialize_device_metadata(in.gpio_high_);
55  serialized["relationships"]["gpio-low"] =
56  hwd_service->serialize_device_metadata(in.gpio_low_);
57  serialized["relationships"]["buzzer"] =
58  hwd_service->serialize_device_metadata(in.buzzer_);
59  serialized["relationships"]["green-led"] =
60  hwd_service->serialize_device_metadata(in.green_led_);
61 
62  return serialized;
63 }
64 
66  const json &in,
67  const SecurityContext &sc)
68 {
69  using namespace JSONUtil;
71 
72  out.mode = extract_with_default(in, "mode", out.mode);
74 
75  // High GPIO
76  Hardware::DeviceId device_id = extract_with_default(in, "gpio_high_id", UUID{});
77  if (!device_id.is_nil())
78  {
79  out.gpio_high_ =
80  odb::lazy_shared_ptr<Hardware::GPIO>(*db->db(), device_id).load();
81  }
82 
83  // Low GPIO
84  device_id = extract_with_default(in, "gpio_low_id", UUID{});
85  if (!device_id.is_nil())
86  {
87  out.gpio_low_ =
88  odb::lazy_shared_ptr<Hardware::GPIO>(*db->db(), device_id).load();
89  }
90 
91  // Buzzer
92  device_id = extract_with_default(in, "buzzer_id", UUID{});
93  if (!device_id.is_nil())
94  {
95  out.buzzer_ =
96  odb::lazy_shared_ptr<Hardware::Buzzer>(*db->db(), device_id).load();
97  }
98 
99  // Green Led
100  device_id = extract_with_default(in, "green_led_id", UUID{});
101  if (!device_id.is_nil())
102  {
103  out.green_led_ =
104  odb::lazy_shared_ptr<Hardware::LED>(*db->db(), device_id).load();
105  }
106 }
107 }
108 }
109 }
RFIDReaderSerializer.hpp
Leosac::Module::Wiegand::WiegandReaderConfig::gpio_low_
Hardware::GPIOPtr gpio_low_
Definition: WiegandConfig.hpp:93
WiegandConfigSerializer.hpp
Leosac::Module::Wiegand::WiegandReaderConfig::buzzer_
Hardware::BuzzerPtr buzzer_
Definition: WiegandConfig.hpp:95
Leosac::Module::Wiegand::WiegandReaderConfigSerializer::serialize
static json serialize(const WiegandReaderConfig &in, const SecurityContext &sc)
Definition: WiegandConfigSerializer.cpp:36
Leosac::JSONUtil::extract_with_default
std::chrono::system_clock::time_point extract_with_default(const nlohmann::json &obj, const std::string &key, const std::chrono::system_clock::time_point &tp)
Extract an ISO 8601 datetime string from a json object.
Definition: JSONUtils.cpp:45
Leosac::Hardware::HardwareService
Database aware Hardware Service.
Definition: HardwareService.hpp:39
Leosac::get_service_registry
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
Leosac::Module::Wiegand::WiegandReaderConfig::green_led_
Hardware::LEDPtr green_led_
Definition: WiegandConfig.hpp:94
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Module::Wiegand::json
nlohmann::json json
Definition: WiegandConfigSerializer.hpp:32
Leosac::DBService
Provides various database-related services to consumer.
Definition: DBService.hpp:34
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
GetServiceRegistry.hpp
JSONUtils.hpp
HardwareService.hpp
Leosac::Module::Wiegand::WiegandReaderConfig::mode
std::string mode
Definition: WiegandConfig.hpp:96
Leosac::Hardware::RFIDReaderSerializer::unserialize
static void unserialize(Hardware::RFIDReader &out, const json &in, const SecurityContext &sc)
Definition: RFIDReaderSerializer.cpp:42
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
Leosac::Module::Wiegand::WiegandReaderConfigSerializer::unserialize
static void unserialize(WiegandReaderConfig &out, const json &in, const SecurityContext &sc)
Definition: WiegandConfigSerializer.cpp:65
Leosac::Module::Wiegand::WiegandReaderConfig::gpio_high_
Hardware::GPIOPtr gpio_high_
Definition: WiegandConfig.hpp:92
Leosac::Module::Wiegand::WiegandReaderConfig
An instance of this class represents the configuration of one Wiegand reader.
Definition: WiegandConfig.hpp:46
Leosac::Hardware::RFIDReaderSerializer::serialize
static json serialize(const Hardware::RFIDReader &in, const SecurityContext &sc)
Definition: RFIDReaderSerializer.cpp:30
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40
Leosac::UUID
Thin wrapper around boost::uuids::uuid.
Definition: Uuid.hpp:35