-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
qingping_pc.h
53 lines (43 loc) · 1.26 KB
/
qingping_pc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include <cmath>
#include <type_traits>
#include "qingping_beacon.h"
namespace esphome {
namespace qingping {
namespace property_controller {
template<class T> struct ValueHolder {
using data_type = T;
const T *value;
};
struct RawValueHolder : ValueHolder<QPDataRaw> {
const QPID type;
template<class T> const T *cast() const {
if (T::type != this->type) {
return nullptr;
}
return reinterpret_cast<const T *>(this->value);
}
};
template<class T> struct BinaryState : T {
bool valid() const { return T::value != nullptr; }
bool state() const { return T::state(); }
};
template<class T> struct State : T {
bool valid() const { return T::value != nullptr; }
float state() const { return T::state(); }
};
template<class T> BinaryState<T> make_binary_state(const qingping::QPDataPoint &dp) {
const auto *value = dp.cast<typename T::data_type>();
return BinaryState<T>{value};
}
template<class T> State<T> make_state(const qingping::QPDataPoint &dp) {
const auto *value = dp.cast<typename T::data_type>();
if constexpr (std::is_base_of<T, RawValueHolder>::value) {
return State<T>{value, dp.type};
} else {
return State<T>{value};
}
}
} // namespace property_controller
} // namespace qingping
} // namespace esphome