$darkmode
delay_action.hpp
1 /*
2  * Copyright 2024 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  */
18 
19 #pragma once
20 
21 #include <memory>
22 #include <thread>
23 
24 #include <cloe/trigger.hpp>
25 #include <fable/utility/chrono.hpp>
26 
27 namespace cloe::actions {
28 
29 class WallClockDelay : public Action {
30  public:
31  WallClockDelay(const std::string& name, cloe::Duration sleep_for,
32  std::function<bool(const Sync&)> func)
33  : Action(name), sleep_for_(sleep_for), func_(std::move(func)) {}
34 
35  [[nodiscard]] ActionPtr clone() const override {
36  return std::make_unique<WallClockDelay>(name(), sleep_for_, func_);
37  }
38 
39  CallbackResult operator()(const Sync& sync, TriggerRegistrar& /*reg*/) override {
40  if (func_ && !func_(sync)) {
41  return CallbackResult::Ok;
42  }
43 
44  std::this_thread::sleep_for(sleep_for_);
45  return CallbackResult::Ok;
46  }
47 
48  [[nodiscard]] bool is_significant() const override { return true; }
49 
50  protected:
51  void to_json(Json& j) const override {
52  j = Json{
53  {"sleep_for", fable::to_string(sleep_for_)},
54  };
55  }
56 
57  private:
58  cloe::Duration sleep_for_;
59  std::function<bool(const Sync&)> func_;
60 };
61 
63  public:
64  using ActionType = WallClockDelay;
65 
66  WallClockDelayFactory(std::string name, std::string desc, std::function<bool(const Sync&)> func)
67  : ActionFactory(std::move(name), std::move(desc)), func_(std::move(func)) {}
68 
69  [[nodiscard]] TriggerSchema schema() const override {
70  return TriggerSchema{
71  this->name(), this->description(), InlineSchema("time to delay for", "duration", true),
73  {"sleep_for", make_prototype<std::string>("time to delay for").require()},
74  }};
75  }
76 
77  [[nodiscard]] ActionPtr make(const Conf& c) const override {
78  return std::make_unique<WallClockDelay>(
79  name(), fable::parse_duration<cloe::Duration>(c.get<std::string>("sleep_for")), func_);
80  }
81 
82  [[nodiscard]] ActionPtr make(const std::string& s) const override {
83  return make(Conf(Json{
84  {"sleep_for", s},
85  }));
86  }
87 
88  private:
89  std::function<bool(const Sync&)> func_;
90 };
91 
92 } // namespace cloe::actions
Definition: trigger.hpp:619
const std::string & description() const
Definition: entity.hpp:90
const std::string & name() const
Definition: entity.hpp:67
Definition: sync.hpp:34
Definition: trigger.hpp:290
Definition: trigger.hpp:437
Definition: delay_action.hpp:62
ActionPtr make(const Conf &c) const override
Definition: delay_action.hpp:77
ActionPtr make(const std::string &s) const override
Definition: delay_action.hpp:82
TriggerSchema schema() const override
Definition: delay_action.hpp:69
Definition: delay_action.hpp:29
bool is_significant() const override
Definition: delay_action.hpp:48
ActionPtr clone() const override
Definition: delay_action.hpp:35
CallbackResult operator()(const Sync &sync, TriggerRegistrar &) override
Definition: delay_action.hpp:39
Definition: conf.hpp:81
T get() const
Definition: conf.hpp:297
Definition: schema.hpp:173
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36
Definition: trigger.hpp:87
Definition: trigger.hpp:207
CallbackResult
Definition: trigger.hpp:514