Tomáš Pecka | af7b704 | 2021-04-20 20:14:13 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "LED.h" |
| 9 | |
| 10 | #include <utility> |
| 11 | #include "utils/io.h" |
| 12 | #include "utils/log.h" |
| 13 | #include "utils/sysrepo.h" |
| 14 | |
| 15 | using namespace std::literals; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | const auto CZECHLIGHT_SYSTEM_MODULE_NAME = "czechlight-system"s; |
| 20 | const auto CZECHLIGHT_SYSTEM_LEDS_MODULE_PREFIX = "/"s + CZECHLIGHT_SYSTEM_MODULE_NAME + ":leds/"s; |
| 21 | |
| 22 | } |
| 23 | |
| 24 | namespace velia::system { |
| 25 | |
| 26 | LED::LED(const std::shared_ptr<::sysrepo::Connection>& srConn, std::filesystem::path sysfsLeds) |
| 27 | : m_log(spdlog::get("system")) |
| 28 | , m_sysfsLeds(std::move(sysfsLeds)) |
| 29 | , m_srSession(std::make_shared<::sysrepo::Session>(srConn)) |
| 30 | , m_srSubscribe(std::make_shared<::sysrepo::Subscribe>(m_srSession)) |
| 31 | { |
| 32 | utils::ensureModuleImplemented(m_srSession, CZECHLIGHT_SYSTEM_MODULE_NAME, "2021-01-13"); |
| 33 | |
| 34 | m_srSubscribe->oper_get_items_subscribe( |
| 35 | CZECHLIGHT_SYSTEM_MODULE_NAME.c_str(), |
| 36 | [this](auto session, auto, auto, auto, auto, auto& parent) { |
| 37 | std::map<std::string, std::string> data; |
| 38 | |
| 39 | for (const auto& entry : std::filesystem::directory_iterator(m_sysfsLeds)) { |
| 40 | if (!std::filesystem::is_directory(entry.path())) { |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | const std::string deviceName = entry.path().filename(); |
| 45 | |
| 46 | try { |
| 47 | /* actually just uint32_t is needed for the next two variables; but there is no harm in reading them as int64_t and downcasting them later (especially when the code for reading int64_t already exists) |
| 48 | * See https://github.com/torvalds/linux/commit/af0bfab907a011e146304d20d81dddce4e4d62d0 |
| 49 | */ |
| 50 | const auto brightness = velia::utils::readFileInt64(entry.path() / "brightness"); |
| 51 | const auto max_brightness = velia::utils::readFileInt64(entry.path() / "max_brightness"); |
| 52 | auto percent = brightness * 100 / max_brightness; |
| 53 | m_log->trace("Found LED '{}' with brightness of {} % (brightness {} out of {})", deviceName, percent, brightness, max_brightness); |
| 54 | |
| 55 | data[CZECHLIGHT_SYSTEM_LEDS_MODULE_PREFIX + "led[name='" + deviceName + "']/brightness"] = std::to_string(percent); |
| 56 | } catch (const std::invalid_argument& e) { |
| 57 | m_log->warn("Failed reading state of the LED '{}': {}", deviceName, e.what()); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | utils::valuesToYang(data, {}, session, parent); |
| 62 | return SR_ERR_OK; |
| 63 | }, |
| 64 | (CZECHLIGHT_SYSTEM_LEDS_MODULE_PREFIX + "*").c_str(), |
| 65 | SR_SUBSCR_PASSIVE | SR_SUBSCR_OPER_MERGE | SR_SUBSCR_CTX_REUSE); |
| 66 | } |
| 67 | |
| 68 | } |