obmon  0.0.0
 All Classes Functions Variables Enumerations Groups Pages
ObSensor.cpp
1 #include "ObSensor.h"
2 
3 ObSensor::ObSensor(std::string name) : _name{name} {
7 
8  _logger = spdlog::get("console");
9 }
14 }
15 
20 
21  for (auto s : _sensors) {
22  s->init();
23  }
24 }
25 
26 void ObSensor::update(int timeout) {
30 
31  // switch pointers
32  if (_first) {
33  auto s_ptr = _first;
34  _first = _second;
35  _second = s_ptr;
36  }
37  for (auto s : _sensors) {
38  if (s->_first) {
39  s->_first->type(SensorType::VALUE);
40  s->_first->process();
41  }
42  if (s->_change) {
43  s->_change->type(SensorType::SPEED);
44  s->_change->speed(s->_second, s->_first, timeout);
45  }
46  s->update(timeout);
47  }
48 }
49 
50 std::string ObSensor::json(const std::string name) const {
54 
55  std::string json;
56 
57  json += "\"";
58  if (name.empty())
59  json += _name;
60  else
61  json += name;
62  json += "\" : {";
63  for (auto s : _sensors) {
64  json += "\"";
65  json += s->name();
66  json += "\" : {";
67  if (s->_first) {
68  json += s->_first->json("value");
69  } else {
70  json += s->json(s->name());
71  }
72  json += ",";
73  if (s->_change) {
74  json += s->_change->json("change");
75  } else {
76  json += s->json(s->name());
77  }
78  json += ",";
79  if (json.back() == ',')
80  json.pop_back();
81  json += "}";
82  json += ",";
83  }
84 
85  if (json.back() == ',')
86  json.pop_back();
87 
88  json += "}";
89 
90  return json;
91 }
ObSensor(std::string name={"sensor"})
Definition: ObSensor.cpp:3
virtual void init()
Definition: ObSensor.cpp:16
std::string name() const
Returns name of sensor.
Definition: ObSensor.h:39
std::vector< ObSensor * > _sensors
List of subsensors.
Definition: ObSensor.h:57
ObSensor * _first
Pointer to first sensor.
Definition: ObSensor.h:54
virtual std::string json(const std::string name={}) const
Definition: ObSensor.cpp:50
ObSensor * _second
Pointer to second sensor.
Definition: ObSensor.h:55
virtual void update(int timeout) final
Definition: ObSensor.cpp:26
virtual ~ObSensor()
Definition: ObSensor.cpp:10
std::string _name
Sensor name.
Definition: ObSensor.h:52
void type(SensorType t)
Sets sensor type.
Definition: ObSensor.h:42