$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_); }
45  logger()->log(level_, msg_.c_str());
46  return CallbackResult::Ok;
47  }
48  bool is_significant() const override { return false; }
49 
50  protected:
51  void to_json(Json& j) const override {
52  j = Json{
53  {"level", logger::to_string(level_)},
54  {"msg", msg_},
55  };
56  }
57 
58  private:
59  LogLevel level_;
60  std::string msg_;
61 };
62 
63 class LogFactory : public ActionFactory {
64  public:
65  using ActionType = Log;
66  LogFactory() : ActionFactory("log", "log a message with a severity") {}
67  TriggerSchema schema() const override;
68  ActionPtr make(const Conf& c) const override;
69  ActionPtr make(const std::string& s) const override;
70 };
71 
72 // -------------------------------------------------------------------- Bundle
73 class Bundle : public Action {
74  public:
75  Bundle(const std::string& name, std::vector<ActionPtr>&& actions);
76  ActionPtr clone() const override;
77  CallbackResult operator()(const Sync& s, TriggerRegistrar& r) override;
78  bool is_significant() const override;
79 
80  protected:
81  void to_json(Json& j) const override {
82  j = Json{
83  {"actions", repr_},
84  };
85  }
86 
87  private:
88  std::vector<ActionPtr> actions_;
89  Json repr_;
90 };
91 
92 class BundleFactory : public ActionFactory {
93  public:
94  using ActionType = Bundle;
95  explicit BundleFactory(std::shared_ptr<TriggerRegistrar> r)
96  : ActionFactory("bundle", "run a set of actions"), registrar_(r) {}
97  TriggerSchema schema() const override;
98  ActionPtr make(const Conf& c) const override;
99 
100  private:
101  std::shared_ptr<TriggerRegistrar> registrar_;
102 };
103 
104 // -------------------------------------------------------------------- Insert
105 class Insert : public Action {
106  public:
107  Insert(const std::string& name, const Conf& triggers) : Action(name), triggers_(triggers) {}
108  ActionPtr clone() const override { return std::make_unique<Insert>(name(), triggers_); }
109  CallbackResult operator()(const Sync& s, TriggerRegistrar& r) override;
110 
111  protected:
112  void to_json(Json& j) const override;
113 
114  private:
115  Conf triggers_;
116 };
117 
118 class InsertFactory : public ActionFactory {
119  public:
120  using ActionType = Insert;
121  explicit InsertFactory(std::shared_ptr<TriggerRegistrar> r)
122  : ActionFactory("insert", "insert a new trigger"), registrar_(r) {}
123  TriggerSchema schema() const override;
124  ActionPtr make(const Conf& c) const override;
125 
126  private:
127  std::shared_ptr<TriggerRegistrar> registrar_;
128 };
129 
130 // --------------------------------------------------------------- PushRelease
131 class PushRelease : public Action {
132  public:
133  PushRelease(const std::string& name, Duration dur, ActionPtr&& push, ActionPtr&& release,
134  const Json& repr)
135  : Action(name)
136  , duration_(dur)
137  , push_(push.release())
138  , release_(release.release())
139  , repr_(repr) {}
140  ActionPtr clone() const override {
141  return std::make_unique<PushRelease>(name(), duration_, push_->clone(), release_->clone(),
142  repr_);
143  }
144  CallbackResult operator()(const Sync&, TriggerRegistrar&) override;
145 
146  protected:
147  void to_json(Json& j) const override { j = repr_; }
148 
149  private:
150  Duration duration_;
151  ActionPtr push_;
152  ActionPtr release_;
153  Json repr_;
154 };
155 
157  public:
158  using ActionType = PushRelease;
159  explicit PushReleaseFactory(std::shared_ptr<TriggerRegistrar> r)
160  : ActionFactory("push_release", "push and release one or more buttons"), registrar_(r) {}
161  TriggerSchema schema() const override;
162  ActionPtr make(const Conf& c) const override;
163 
164  private:
165  std::shared_ptr<TriggerRegistrar> registrar_;
166 };
167 
168 } // namespace actions
169 } // namespace cloe
Definition: trigger.hpp:619
Logger logger() const
Definition: trigger.hpp:658
const std::string & name() const
Definition: entity.hpp:67
Definition: sync.hpp:34
Definition: trigger.hpp:290
Definition: trigger.hpp:437
Definition: example_actions.hpp:92
ActionPtr make(const Conf &c) const override
Definition: example_actions.cpp:132
TriggerSchema schema() const override
Definition: example_actions.cpp:122
Definition: example_actions.hpp:73
ActionPtr clone() const override
Definition: example_actions.cpp:101
bool is_significant() const override
Definition: example_actions.cpp:92
CallbackResult operator()(const Sync &s, TriggerRegistrar &r) override
Definition: example_actions.cpp:110
Definition: example_actions.hpp:118
TriggerSchema schema() const override
Definition: example_actions.cpp:156
ActionPtr make(const Conf &c) const override
Definition: example_actions.cpp:167
Definition: example_actions.hpp:105
CallbackResult operator()(const Sync &s, TriggerRegistrar &r) override
Definition: example_actions.cpp:148
ActionPtr clone() const override
Definition: example_actions.hpp:108
Definition: example_actions.hpp:63
ActionPtr make(const Conf &c) const override
Definition: example_actions.cpp:49
TriggerSchema schema() const override
Definition: example_actions.cpp:37
Definition: example_actions.hpp:39
bool is_significant() const override
Definition: example_actions.hpp:48
CallbackResult operator()(const Sync &, TriggerRegistrar &) override
Definition: example_actions.hpp:44
ActionPtr clone() const override
Definition: example_actions.hpp:43
Definition: example_actions.hpp:156
TriggerSchema schema() const override
Definition: example_actions.cpp:199
ActionPtr make(const Conf &c) const override
Definition: example_actions.cpp:213
Definition: example_actions.hpp:131
ActionPtr clone() const override
Definition: example_actions.hpp:140
CallbackResult operator()(const Sync &, TriggerRegistrar &) override
Definition: example_actions.cpp:178
Definition: conf.hpp:76
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36
Definition: trigger.hpp:207
CallbackResult
Definition: trigger.hpp:514