$darkmode
example_actions.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
26 #pragma once
27 
28 #include <memory> // for unique_ptr<>
29 #include <string> // for string
30 #include <utility> // for move
31 #include <vector> // for vector<>
32 
33 #include <cloe/trigger.hpp> // for Action, ActionFactory
34 
35 namespace cloe {
36 namespace actions {
37 
38 // ----------------------------------------------------------------------- Log
39 class Log : public Action {
40  public:
41  Log(const std::string& name, LogLevel level, const std::string& msg)
42  : Action(name), level_(level), msg_(msg) {}
43  ActionPtr clone() const override { return std::make_unique<Log>(name(), level_, msg_); }
44  void operator()(const Sync&, TriggerRegistrar&) override { logger()->log(level_, msg_.c_str()); }
45  bool is_significant() const override { return false; }
46 
47  protected:
48  void to_json(Json& j) const override {
49  j = Json{
50  {"level", logger::to_string(level_)},
51  {"msg", msg_},
52  };
53  }
54 
55  private:
56  LogLevel level_;
57  std::string msg_;
58 };
59 
60 class LogFactory : public ActionFactory {
61  public:
62  using ActionType = Log;
63  LogFactory() : ActionFactory("log", "log a message with a severity") {}
64  TriggerSchema schema() const override;
65  ActionPtr make(const Conf& c) const override;
66  ActionPtr make(const std::string& s) const override;
67 };
68 
69 // -------------------------------------------------------------------- Bundle
70 class Bundle : public Action {
71  public:
72  Bundle(const std::string& name, std::vector<ActionPtr>&& actions);
73  ActionPtr clone() const override;
74  void operator()(const Sync& s, TriggerRegistrar& r) override;
75  bool is_significant() const override;
76 
77  protected:
78  void to_json(Json& j) const override {
79  j = Json{
80  {"actions", repr_},
81  };
82  }
83 
84  private:
85  std::vector<ActionPtr> actions_;
86  Json repr_;
87 };
88 
89 class BundleFactory : public ActionFactory {
90  public:
91  using ActionType = Bundle;
92  explicit BundleFactory(std::shared_ptr<TriggerRegistrar> r)
93  : ActionFactory("bundle", "run a set of actions"), registrar_(r) {}
94  TriggerSchema schema() const override;
95  ActionPtr make(const Conf& c) const override;
96 
97  private:
98  std::shared_ptr<TriggerRegistrar> registrar_;
99 };
100 
101 // -------------------------------------------------------------------- Insert
102 class Insert : public Action {
103  public:
104  Insert(const std::string& name, const Conf& triggers) : Action(name), triggers_(triggers) {}
105  ActionPtr clone() const override { return std::make_unique<Insert>(name(), triggers_); }
106  void operator()(const Sync& s, TriggerRegistrar& r) override;
107 
108  protected:
109  void to_json(Json& j) const override;
110 
111  private:
112  Conf triggers_;
113 };
114 
115 class InsertFactory : public ActionFactory {
116  public:
117  using ActionType = Insert;
118  explicit InsertFactory(std::shared_ptr<TriggerRegistrar> r)
119  : ActionFactory("insert", "insert a new trigger"), registrar_(r) {}
120  TriggerSchema schema() const override;
121  ActionPtr make(const Conf& c) const override;
122 
123  private:
124  std::shared_ptr<TriggerRegistrar> registrar_;
125 };
126 
127 // --------------------------------------------------------------- PushRelease
128 class PushRelease : public Action {
129  public:
130  PushRelease(const std::string& name, Duration dur, ActionPtr&& push, ActionPtr&& release,
131  const Json& repr)
132  : Action(name)
133  , duration_(dur)
134  , push_(push.release())
135  , release_(release.release())
136  , repr_(repr) {}
137  ActionPtr clone() const override {
138  return std::make_unique<PushRelease>(name(), duration_, push_->clone(), release_->clone(),
139  repr_);
140  }
141  void operator()(const Sync&, TriggerRegistrar&) override;
142 
143  protected:
144  void to_json(Json& j) const override { j = repr_; }
145 
146  private:
147  Duration duration_;
148  ActionPtr push_;
149  ActionPtr release_;
150  Json repr_;
151 };
152 
154  public:
155  using ActionType = PushRelease;
156  explicit PushReleaseFactory(std::shared_ptr<TriggerRegistrar> r)
157  : ActionFactory("push_release", "push and release one or more buttons"), registrar_(r) {}
158  TriggerSchema schema() const override;
159  ActionPtr make(const Conf& c) const override;
160 
161  private:
162  std::shared_ptr<TriggerRegistrar> registrar_;
163 };
164 
165 } // namespace actions
166 } // namespace cloe
Definition: trigger.hpp:607
Logger logger() const
Definition: trigger.hpp:646
const std::string & name() const
Definition: entity.hpp:67
Definition: sync.hpp:34
Definition: trigger.hpp:290
Definition: trigger.hpp:433
Definition: example_actions.hpp:89
ActionPtr make(const Conf &c) const override
Definition: example_actions.cpp:128
TriggerSchema schema() const override
Definition: example_actions.cpp:118
Definition: example_actions.hpp:70
ActionPtr clone() const override
Definition: example_actions.cpp:102
bool is_significant() const override
Definition: example_actions.cpp:93
void operator()(const Sync &s, TriggerRegistrar &r) override
Definition: example_actions.cpp:111
Definition: example_actions.hpp:115
TriggerSchema schema() const override
Definition: example_actions.cpp:151
ActionPtr make(const Conf &c) const override
Definition: example_actions.cpp:162
Definition: example_actions.hpp:102
void operator()(const Sync &s, TriggerRegistrar &r) override
Definition: example_actions.cpp:144
ActionPtr clone() const override
Definition: example_actions.hpp:105
Definition: example_actions.hpp:60
ActionPtr make(const Conf &c) const override
Definition: example_actions.cpp:50
TriggerSchema schema() const override
Definition: example_actions.cpp:38
Definition: example_actions.hpp:39
bool is_significant() const override
Definition: example_actions.hpp:45
ActionPtr clone() const override
Definition: example_actions.hpp:43
void operator()(const Sync &, TriggerRegistrar &) override
Definition: example_actions.hpp:44
Definition: example_actions.hpp:153
TriggerSchema schema() const override
Definition: example_actions.cpp:193
ActionPtr make(const Conf &c) const override
Definition: example_actions.cpp:207
Definition: example_actions.hpp:128
ActionPtr clone() const override
Definition: example_actions.hpp:137
void operator()(const Sync &, TriggerRegistrar &) override
Definition: example_actions.cpp:173
Definition: conf.hpp:82
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36
Definition: trigger.hpp:207