5 #ifndef HERALD_COORDINATOR_H
6 #define HERALD_COORDINATOR_H
8 #include "../context.h"
9 #include "activities.h"
10 #include "../data/sensor_logger.h"
38 template <
typename ContextT>
46 HLOGGERINIT(ctx,
"engine",
"coordinator")
52 template <
typename SensorT>
53 void add(SensorT& sensor) {
54 HTDBG(
"Adding sensor");
55 auto prov = sensor.coordinationProvider();
56 if (prov.has_value()) {
57 HTDBG(
"Sensor has Provider implementation");
58 providers.push_back(prov.value());
62 template <
typename SensorT>
70 HTDBG(
"Start called");
72 featureProviders.clear();
74 for (
auto prov: providers) {
75 auto myFeatures = prov.get().connectionsProvided();
76 for (
auto feature : myFeatures) {
77 featureProviders.emplace(feature,prov);
81 HTDBG(
"Start returning");
87 HTDBG(
"Coordinator not running. Returning from iteration having done nothing.");
90 HTDBG(
"################# ITERATION #################");
93 std::map<std::reference_wrapper<CoordinationProvider>,std::vector<PrioritisedPrerequisite>> assignPrereqs;
94 for (
auto& prov : providers) {
95 assignPrereqs.emplace(prov,std::vector<PrioritisedPrerequisite>());
100 std::vector<PrioritisedPrerequisite> connsRequired;
102 for (
auto& prov : providers) {
103 auto myConns = prov.get().requiredConnections();
104 std::copy(myConns.begin(),myConns.end(),
105 std::back_insert_iterator<std::vector<PrioritisedPrerequisite>>(connsRequired));
111 for (
auto& p : connsRequired) {
112 auto el = featureProviders.find(std::get<0>(p));
113 if (featureProviders.end() != el) {
114 assignPrereqs[el->second].push_back(p);
128 std::vector<PrioritisedPrerequisite> provisioned;
129 for (
auto& prov : assignPrereqs) {
144 std::vector<PrioritisedPrerequisite> myProvisioned = prov.first.get().provision(prov.second);
145 std::copy(myProvisioned.begin(),myProvisioned.end(),
146 std::back_insert_iterator<std::vector<PrioritisedPrerequisite>>(provisioned));
152 for (
auto& prov : providers) {
153 auto maxActs = prov.get().requiredActivities();
155 for (
auto& act : maxActs) {
156 std::string san(
"Activity ");
161 bool allFound =
true;
162 for (
auto& pre : act.prerequisites) {
163 bool myFound =
false;
164 for (
auto& exists : provisioned) {
165 if (std::get<0>(pre) == std::get<0>(exists) &&
166 std::get<1>(pre) == std::get<2>(exists)) {
170 allFound = allFound & myFound;
172 HTDBG(
" - Prereq satisfied");
174 HTDBG(
" - Prereq NOT SATISFIED");
179 HTDBG(
"All satisfied, calling activity");
190 std::optional<Activity> followOn = act.executor(act);
196 HTDBG(
"################# END #################");
206 std::vector<std::reference_wrapper<CoordinationProvider>> providers;
207 std::map<FeatureTag,std::reference_wrapper<CoordinationProvider>> featureProviders;
215 bool operator<(
const std::reference_wrapper<CoordinationProvider> first,
const std::reference_wrapper<CoordinationProvider> second);
Coordinates all connection and activities used across all sensors within Herald.
Definition: coordinator.h:39
Coordinator(ContextT &ctx)
Default constructor. Receives a configured platform-specific context instance.
Definition: coordinator.h:42
void stop()
Closes out any existing connections/activities.
Definition: coordinator.h:199
void start()
Prepares for iterations to be called (may pre-emptively make calls)
Definition: coordinator.h:69
void iteration()
Execute an iteration of activity, according to settings.
Definition: coordinator.h:85
void remove(SensorT &sensor)
Remove from iteration planning.
Definition: coordinator.h:63
void add(SensorT &sensor)
Introspect and include in iteration planning.
Definition: coordinator.h:53
Acts as a non-global memory arena for arbitrary classes.
Definition: aggregates.h:15