Debian Codegraph @

src/core/components/sensors/sensorgraphitem.h

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #pragma once
5
6 #include "core/exportable.h"
7 #include "graphitem.h"
8 #include "sensor.h"
9 #include <functional>
10 #include <optional>
11 #include <string_view>
12 #include <units.h>
13 #include <utility>
14
15 template<typename Unit, typename T>
16 class SensorReader final : public Sensor<Unit, T>::Exporter
17 {
18 public:
19 SensorReader(
20 std::string_view id, std::function<void(Unit)> &&onValue,
21 std::function<void(std::optional<std::pair<Unit, Unit>> const &)> &&onRange)
22 : id_(id)
23 , onValue_(std::move(onValue))
24 , onRange_(std::move(onRange))
25 {
26 }
27
28 std::optional<std::reference_wrapper<Exportable::Exporter>>
29 provideExporter(Item const &i) override
30 {
31 if (i.ID() == id_)
32 return *this;
33
34 return {};
35 }
36
37 void takeValue(Unit value) override
38 {
39 onValue_(value);
40 }
41
42 void takeRange(std::optional<std::pair<Unit, Unit>> const &range) override
43 {
44 onRange_(range);
45 }
46
47 private:
48 std::string_view const id_;
49 std::function<void(Unit)> const onValue_;
50 std::function<void(std::optional<std::pair<Unit, Unit>> const &)> const onRange_;
51 };
52
53 template<typename Unit, typename T>
54 class SensorGraphItem final : public GraphItem
55 {
56 public:
57 SensorGraphItem(std::string_view id, std::string_view unit) noexcept
58 : GraphItem(id, unit)
59 , valueReader_(
60 id, [&](Unit value) { this->updateGraph(value.template to<double>()); },
61 [](std::optional<std::pair<Unit, Unit>> const &) {})
62 , rangeReader_(
63 id, [&](Unit) {},
64 [&](std::optional<std::pair<Unit, Unit>> const &range) {
65 if (range.has_value()) {
66 this->initialRange(range.value().first.template to<double>(),
67 range.value().second.template to<double>());
68 }
69 })
70 {
71 }
72
73 void init(Exportable const *sensor) override
74 {
75 sensor_ = sensor;
76 sensor_->exportWith(rangeReader_);
77 }
78
79 void update() override
80 {
81 sensor_->exportWith(valueReader_);
82 }
83
84 private:
85 SensorReader<Unit, T> valueReader_;
86 SensorReader<Unit, T> rangeReader_;
87
88 Exportable const *sensor_;
89 };