Leosac  0.8.0
Open Source Access Control
HardwareService.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 
21 #include "core/SecurityContext.hpp"
22 #include "hardware/Buzzer.hpp"
23 #include "hardware/Device_odb.h"
24 #include "hardware/LED.hpp"
28 
29 namespace Leosac
30 {
31 namespace Hardware
32 {
34  : dbservice_(dbservice)
35 {
36  register_serializer<Hardware::Buzzer>(&BuzzerSerializer::serialize);
37  register_serializer<Hardware::LED>(&LEDSerializer::serialize);
38 }
39 
40 std::string
42 {
43  auto serialized = serialize(device, SystemSecurityContext::instance());
44  ASSERT_LOG(serialized.at("type").is_string(),
45  "Underlying serialization did something incorrect.");
46 
47  return serialized.at("type").get<std::string>();
48 }
49 
51  const Hardware::DevicePtr &device) const
52 {
53  if (!device)
54  return json{{"data", {}}};
55 
56  std::string type = hardware_device_type(*device);
57  json dev_metadata = {{"data", {{"id", device->id()}, {"type", type}}}};
58  return dev_metadata;
59 }
60 
61 DevicePtr HardwareService::find_device_by_name(const std::string &dev_name) const
62 {
63  using Query = odb::query<Hardware::Device>;
64  Query q(Query::name == dev_name);
65 
66  db::OptionalTransaction t(dbservice_->db()->begin());
67 
68  auto result = dbservice_->db()->query_one<Hardware::Device>(q);
69  t.commit();
70  return result;
71 }
72 }
73 }
Leosac::db::OptionalTransaction
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
Definition: OptionalTransaction.hpp:43
Leosac::db::OptionalTransaction::commit
void commit()
Commit the transaction, if there was no currently active transaction at the time of this object's cre...
Definition: OptionalTransaction.cpp:38
Leosac::SystemSecurityContext::instance
static SecurityContext & instance()
Definition: SecurityContext.cpp:64
LEDSerializer.hpp
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
Leosac::Hardware::HardwareService::dbservice_
DBServicePtr dbservice_
Definition: HardwareService.hpp:62
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Query
odb::query< Tools::LogEntry > Query
Definition: LogEntry.cpp:36
Leosac::Hardware::Device
Base class for hardware devices.
Definition: Device.hpp:44
Leosac::Hardware::HardwareService::hardware_device_type
std::string hardware_device_type(const Hardware::Device &device) const
Return the name of real type of a device.
Definition: HardwareService.cpp:41
BuzzerSerializer.hpp
SecurityContext.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
OptionalTransaction.hpp
Leosac::DBServicePtr
std::shared_ptr< DBService > DBServicePtr
Definition: db_fwd.hpp:34
Leosac::Hardware::LEDSerializer::serialize
static json serialize(const Hardware::LED &in, const SecurityContext &sc)
Definition: LEDSerializer.cpp:35
Leosac::Hardware::HardwareService::serialize_device_metadata
json serialize_device_metadata(const Hardware::DevicePtr &device) const
Serialize as json the metadata of a device.
Definition: HardwareService.cpp:50
Leosac::Hardware::DevicePtr
std::shared_ptr< Device > DevicePtr
Definition: HardwareFwd.hpp:55
Leosac::Hardware::HardwareService::find_device_by_name
DevicePtr find_device_by_name(const std::string &dev_name) const
Definition: HardwareService.cpp:61
Buzzer.hpp
Leosac::Hardware::HardwareService::HardwareService
HardwareService(const Leosac::DBServicePtr &dbservice)
Definition: HardwareService.cpp:33
HardwareService.hpp
LED.hpp
Leosac::Hardware::BuzzerSerializer::serialize
static json serialize(const Hardware::Buzzer &in, const SecurityContext &sc)
Definition: BuzzerSerializer.cpp:35
Leosac::ExtensibleSerializer< json, Hardware::Device, const SecurityContext & >::serialize
json serialize(const Hardware::Device &input, AdditionalArgs &&... args) const
Definition: ExtensibleSerializer.hpp:56