herald  2.0.0
sensor_array.h
1 // Copyright 2020-2021 Herald Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #ifndef HERALD_SENSOR_ARRAY_H
6 #define HERALD_SENSOR_ARRAY_H
7 
8 #include "data/sensor_logger.h"
9 #include "sensor_delegate.h"
10 #include "sensor.h"
11 #include "context.h"
12 #include "payload/payload_data_supplier.h"
13 #include "datatype/payload_timestamp.h"
14 #include "datatype/data.h"
15 #include "datatype/target_identifier.h"
16 #include "datatype/payload_data.h"
17 #include "ble/ble_concrete.h"
18 #include "engine/coordinator.h"
19 
20 #include <memory>
21 #include <string>
22 #include <array>
23 #include <variant>
24 #include <functional>
25 
26 namespace herald {
27 
28 using namespace ble;
29 using namespace data;
30 using namespace datatype;
31 using namespace payload;
32 using namespace engine;
33 
44 template <typename ContextT, typename PayloadDataSupplierT, typename... SensorTs>
45 class SensorArray {
46 public:
47  static constexpr std::size_t Size = sizeof...(SensorTs);
48 
50  SensorArray(ContextT& ctx, PayloadDataSupplierT& payloadDataSupplier, SensorTs&... sensors)
51  : mContext(ctx),
52  mPayloadDataSupplier(payloadDataSupplier),
53  mSensorArray(
54  std::array<
55  std::variant<std::reference_wrapper<SensorTs>...>
56  ,Size
57  >({std::variant<std::reference_wrapper<SensorTs>...>(sensors)...})
58  ),
59  engine(ctx),
60  deviceDescription("")
61  HLOGGERINIT(mContext, "Sensor", "SensorArray")
62  {
63  // addSensors(0,sensors...);
64  }
65 
66  ~SensorArray() = default;
67 
68  // TODO add immediate send support back in when template mechanism determined
69  // SENSOR ARRAY METHODS
70  // bool immediateSend(Data data, const TargetIdentifier& targetIdentifier) {
71  // return concrete->immediateSend(data, targetIdentifier);
72  // }
73 
74  // bool immediateSendAll(Data data) {
75  // return concrete->immediateSendAll(data);
76  // }
77 
78  PayloadData payloadData() {
79  return mPayloadDataSupplier.payload(PayloadTimestamp());
80  }
81 
83  // void add(Sensor& sensor) {
84  // mSensorArray.emplace_back(sensor); // adds in links to BLE transmitter, receiver
85  // engine.add(sensor);
86  // }
87 
88  // SENSOR OVERRIDES
89  void start() {
90  for (auto& sensor: mSensorArray) {
91  std::visit([](auto&& arg) {
92  ((decltype(arg))arg).get().start(); // cast to call derived class function
93  }, sensor);
94  }
95  engine.start();
96  }
97 
98  void stop() {
99  engine.stop();
100  for (auto& sensor: mSensorArray) {
101  std::visit([](auto&& arg) {
102  ((decltype(arg))arg).get().stop(); // cast to call derived class function
103  }, sensor);
104  }
105  }
106 
107  std::optional<std::reference_wrapper<CoordinationProvider>> coordinationProvider() {
108  return {};
109  }
110 
112  void iteration(const TimeInterval sinceLastCompleted) {
113  // TODO ensure this works for continuous evaluation with minimal overhead or battery
114  engine.iteration();
115  }
116 
117 private:
118  // Initialised on entry to Impl constructor:-
119  ContextT& mContext;
120  PayloadDataSupplierT& mPayloadDataSupplier;
121  std::array<std::variant<std::reference_wrapper<SensorTs>...>,Size> mSensorArray;
122 
123  Coordinator<ContextT> engine;
124 
125  // Not initialised (and thus optional):-
126  std::string deviceDescription;
127 
128  HLOGGER(ContextT);
129 
130  template <typename LastT>
131  constexpr void addSensors(int nextPos,LastT& last) {
132  mSensorArray[nextPos] = std::reference_wrapper<LastT>(last);
133  }
134 
135  template <typename FirstT, typename SecondT, typename... RestT>
136  constexpr void addSensors(int nextPos,FirstT& first, SecondT& second, RestT&... rest) {
137  mSensorArray[nextPos] = std::reference_wrapper<FirstT>(first);
138  ++nextPos;
139  addSensors(nextPos,second,rest...);
140  }
141 };
142 
143 
144 
145 
146 } // end namespace
147 
148 #endif
Manages all Sensors and sensor delegates for Herald.
Definition: sensor_array.h:45
void start()
Adds a new sensor to the array, and add its coordination provider to the engine.
Definition: sensor_array.h:89
void iteration(const TimeInterval sinceLastCompleted)
Scheduling activities from external OS thread wakes - Since v1.2-beta3.
Definition: sensor_array.h:112
SensorArray(ContextT &ctx, PayloadDataSupplierT &payloadDataSupplier, SensorTs &... sensors)
Takes ownership of payloadDataSupplier (std::move)
Definition: sensor_array.h:50
Definition: time_interval.h:19
Coordinates all connection and activities used across all sensors within Herald.
Definition: coordinator.h:39
Acts as a non-global memory arena for arbitrary classes.
Definition: aggregates.h:15
Definition: data.h:562