Leosac  0.8.0
Open Source Access Control
BuzzerSerializer.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 
20 
23 #include "hardware/Buzzer.hpp"
24 #include "hardware/GPIO_odb.h"
27 #include "tools/JSONUtils.hpp"
28 #include "tools/db/DBService.hpp"
29 #include "tools/log.hpp"
30 
31 namespace Leosac
32 {
33 namespace Hardware
34 {
36  const SecurityContext &sc)
37 {
38  json serialized = DeviceSerializer::serialize(in, sc);
39  ASSERT_LOG(serialized.at("type").is_string(),
40  "Base device serialization did something unexpected.");
41 
42  // Override object type
43  serialized["type"] = "buzzer";
44  serialized["attributes"]["default-blink-duration"] = in.default_blink_duration();
45  serialized["attributes"]["default-blink-speed"] = in.default_blink_speed();
46 
47  auto hwd_service =
49  ASSERT_LOG(hwd_service, "No hardware service.");
50 
51  serialized["relationships"]["gpio"] =
52  hwd_service->serialize_device_metadata(in.gpio());
53 
54  return serialized;
55 }
56 
58  const SecurityContext &sc)
59 {
60  using namespace JSONUtil;
61  DeviceSerializer::unserialize(out, in, sc);
62 
63  out.default_blink_duration(extract_with_default(in, "default-blink-duration",
64  out.default_blink_duration()));
66  extract_with_default(in, "default-blink-speed", out.default_blink_speed()));
67 
68  Hardware::DeviceId gpio_id = extract_with_default(in, "gpio_id", UUID{});
70  if (!gpio_id.is_nil())
71  {
72  out.gpio(odb::lazy_shared_ptr<Hardware::GPIO>(*db->db(), gpio_id).load());
73  }
74 }
75 }
76 }
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
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
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::DBService
Provides various database-related services to consumer.
Definition: DBService.hpp:34
BuzzerSerializer.hpp
Leosac::Hardware::Buzzer::gpio
const GPIOPtr & gpio() const
Definition: Buzzer.cpp:38
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
GetServiceRegistry.hpp
Leosac::Hardware::Buzzer::default_blink_speed
int64_t default_blink_speed() const
Definition: Buzzer.cpp:58
JSONUtils.hpp
Leosac::UUID::is_nil
bool is_nil() const
Definition: Uuid.hpp:57
Buzzer.hpp
Leosac::Hardware::BuzzerSerializer::unserialize
static void unserialize(Hardware::Buzzer &out, const json &in, const SecurityContext &sc)
Definition: BuzzerSerializer.cpp:57
HardwareService.hpp
Leosac::Hardware::Buzzer::default_blink_duration
int64_t default_blink_duration() const
Definition: Buzzer.cpp:48
DBService.hpp
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
log.hpp
Leosac::Hardware::BuzzerSerializer::serialize
static json serialize(const Hardware::Buzzer &in, const SecurityContext &sc)
Definition: BuzzerSerializer.cpp:35
Leosac::Hardware::DeviceSerializer::serialize
static json serialize(const Hardware::Device &in, const SecurityContext &sc)
Definition: DeviceSerializer.cpp:28
Leosac::Hardware::Buzzer
Abstraction of Buzzer device attributes.
Definition: Buzzer.hpp:42
Leosac::Hardware::DeviceSerializer::unserialize
static void unserialize(Hardware::Device &out, const json &in, const SecurityContext &sc)
Definition: DeviceSerializer.cpp:40
DeviceSerializer.hpp
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