Leosac  0.8.0
Open Source Access Control
LEDSerializer.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/GPIO_odb.h"
25 #include "hardware/LED.hpp"
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 {
37  json serialized = DeviceSerializer::serialize(in, sc);
38  ASSERT_LOG(serialized.at("type").is_string(),
39  "Base device serialization did something unexpected.");
40 
41  // Override object type
42  serialized["type"] = "led";
43  serialized["attributes"]["default-blink-duration"] = in.default_blink_duration();
44  serialized["attributes"]["default-blink-speed"] = in.default_blink_speed();
45 
46  auto hwd_service =
48  ASSERT_LOG(hwd_service, "No hardware service.");
49 
50  serialized["relationships"]["gpio"] =
51  hwd_service->serialize_device_metadata(in.gpio());
52 
53  return serialized;
54 }
55 
57  const SecurityContext &sc)
58 {
59  using namespace JSONUtil;
60  DeviceSerializer::unserialize(out, in, sc);
61 
62  out.default_blink_duration(extract_with_default(in, "default-blink-duration",
63  out.default_blink_duration()));
65  extract_with_default(in, "default-blink-speed", out.default_blink_speed()));
66 
67  Hardware::DeviceId gpio_id = extract_with_default(in, "gpio_id", UUID{});
69  if (!gpio_id.is_nil())
70  {
71  out.gpio(odb::lazy_shared_ptr<Hardware::GPIO>(*db->db(), gpio_id).load());
72  }
73 }
74 }
75 }
LEDSerializer.hpp
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::Hardware::LEDSerializer::unserialize
static void unserialize(Hardware::LED &out, const json &in, const SecurityContext &sc)
Definition: LEDSerializer.cpp:56
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::Hardware::LED::gpio
const GPIOPtr & gpio() const
Definition: LED.cpp:38
Leosac::Hardware::LED::default_blink_speed
int64_t default_blink_speed() const
Definition: LED.cpp:58
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
Leosac::Hardware::LEDSerializer::serialize
static json serialize(const Hardware::LED &in, const SecurityContext &sc)
Definition: LEDSerializer.cpp:35
Leosac::Hardware::LED::default_blink_duration
int64_t default_blink_duration() const
Definition: LED.cpp:48
JSONUtils.hpp
Leosac::UUID::is_nil
bool is_nil() const
Definition: Uuid.hpp:57
Leosac::Hardware::LED
Abstraction of LED device attributes.
Definition: LED.hpp:36
HardwareService.hpp
LED.hpp
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::DeviceSerializer::serialize
static json serialize(const Hardware::Device &in, const SecurityContext &sc)
Definition: DeviceSerializer.cpp:28
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