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