#pragma once #include #include #include "esphome/core/automation.h" #include "esphome/core/component.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" namespace esphome { namespace script { class ScriptLogger { protected: #ifdef USE_STORE_LOG_STR_IN_FLASH void esp_logw_(int line, const __FlashStringHelper *format, const char *param) { esp_log_(ESPHOME_LOG_LEVEL_WARN, line, format, param); } void esp_logd_(int line, const __FlashStringHelper *format, const char *param) { esp_log_(ESPHOME_LOG_LEVEL_DEBUG, line, format, param); } void esp_log_(int level, int line, const __FlashStringHelper *format, const char *param); #else void esp_logw_(int line, const char *format, const char *param) { esp_log_(ESPHOME_LOG_LEVEL_WARN, line, format, param); } void esp_logd_(int line, const char *format, const char *param) { esp_log_(ESPHOME_LOG_LEVEL_DEBUG, line, format, param); } void esp_log_(int level, int line, const char *format, const char *param); #endif }; /// The abstract base class for all script types. template class Script : public ScriptLogger, public Trigger { public: /** Execute a new instance of this script. * * The behavior of this function when a script is already running is defined by the subtypes */ virtual void execute(Ts...) = 0; /// Check if any instance of this script is currently running. virtual bool is_running() { return this->is_action_running(); } /// Stop all instances of this script. virtual void stop() { this->stop_action(); } // execute this script using a tuple that contains the arguments void execute_tuple(const std::tuple &tuple) { this->execute_tuple_(tuple, typename gens::type()); } // Internal function to give scripts readable names. void set_name(const LogString *name) { name_ = name; } protected: template void execute_tuple_(const std::tuple &tuple, seq /*unused*/) { this->execute(std::get(tuple)...); } const LogString *name_{nullptr}; }; /** A script type for which only a single instance at a time is allowed. * * If a new instance is executed while the previous one hasn't finished yet, * a warning is printed and the new instance is discarded. */ template class SingleScript : public Script { public: void execute(Ts... x) override { if (this->is_action_running()) { this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' is already running! (mode: single)"), LOG_STR_ARG(this->name_)); return; } this->trigger(x...); } }; /** A script type that restarts scripts from the beginning when a new instance is started. * * If a new instance is started but another one is already running, the existing * script is stopped and the new instance starts from the beginning. */ template class RestartScript : public Script { public: void execute(Ts... x) override { if (this->is_action_running()) { this->esp_logd_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' restarting (mode: restart)"), LOG_STR_ARG(this->name_)); this->stop_action(); } this->trigger(x...); } }; /** A script type that queues new instances that are created. * * Only one instance of the script can be active at a time. * * Ring buffer implementation: * - num_queued_ tracks the number of queued (waiting) instances, NOT including the currently running one * - queue_front_ points to the next item to execute (read position) * - Buffer size is max_runs_ - 1 (max total instances minus the running one) * - Write position is calculated as: (queue_front_ + num_queued_) % (max_runs_ - 1) * - When an item finishes, queue_front_ advances: (queue_front_ + 1) % (max_runs_ - 1) * - First execute() runs immediately without queuing (num_queued_ stays 0) * - Subsequent executes while running are queued starting at position 0 * - Maximum total instances = max_runs_ (includes 1 running + (max_runs_ - 1) queued) */ template class QueueingScript : public Script, public Component { public: void execute(Ts... x) override { if (this->is_action_running() || this->num_queued_ > 0) { // num_queued_ is the number of *queued* instances (waiting, not including currently running) // max_runs_ is the maximum *total* instances (running + queued) // So we reject when num_queued_ + 1 >= max_runs_ (queued + running >= max) if (this->num_queued_ + 1 >= this->max_runs_) { this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' max instances (running + queued) reached!"), LOG_STR_ARG(this->name_)); return; } // Initialize queue on first queued item (after capacity check) this->lazy_init_queue_(); this->esp_logd_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' queueing new instance (mode: queued)"), LOG_STR_ARG(this->name_)); // Ring buffer: write to (queue_front_ + num_queued_) % queue_capacity const size_t queue_capacity = static_cast(this->max_runs_ - 1); size_t write_pos = (this->queue_front_ + this->num_queued_) % queue_capacity; // Use std::make_unique to replace the unique_ptr this->var_queue_[write_pos] = std::make_unique>(x...); this->num_queued_++; return; } this->trigger(x...); // Check if the trigger was immediate and we can continue right away. this->loop(); } void stop() override { // Clear all queued items to free memory immediately if (this->var_queue_) { const size_t queue_capacity = static_cast(this->max_runs_ - 1); for (size_t i = 0; i < queue_capacity; i++) { this->var_queue_[i].reset(); } this->var_queue_.reset(); } this->num_queued_ = 0; this->queue_front_ = 0; Script::stop(); } void loop() override { if (this->num_queued_ != 0 && !this->is_action_running()) { // Dequeue: decrement count, move tuple out (frees slot), advance read position this->num_queued_--; const size_t queue_capacity = static_cast(this->max_runs_ - 1); auto tuple_ptr = std::move(this->var_queue_[this->queue_front_]); this->queue_front_ = (this->queue_front_ + 1) % queue_capacity; this->trigger_tuple_(*tuple_ptr, typename gens::type()); } } void set_max_runs(int max_runs) { max_runs_ = max_runs; } protected: // Lazy init queue on first use - avoids setup() ordering issues and saves memory // if script is never executed during this boot cycle inline void lazy_init_queue_() { if (!this->var_queue_) { // Allocate array of max_runs_ - 1 slots for queued items (running item is separate) // unique_ptr array is zero-initialized, so all slots start as nullptr this->var_queue_ = std::make_unique>[]>(this->max_runs_ - 1); } } template void trigger_tuple_(const std::tuple &tuple, seq /*unused*/) { this->trigger(std::get(tuple)...); } int num_queued_ = 0; // Number of queued instances (not including currently running) int max_runs_ = 0; // Maximum total instances (running + queued) size_t queue_front_ = 0; // Ring buffer read position (next item to execute) std::unique_ptr>[]> var_queue_; // Ring buffer of queued parameters }; /** A script type that executes new instances in parallel. * * If a new instance is started while previous ones haven't finished yet, * the new one is executed in parallel to the other instances. */ template class ParallelScript : public Script { public: void execute(Ts... x) override { if (this->max_runs_ != 0 && this->automation_parent_->num_running() >= this->max_runs_) { this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' maximum number of parallel runs exceeded!"), LOG_STR_ARG(this->name_)); return; } this->trigger(x...); } void set_max_runs(int max_runs) { max_runs_ = max_runs; } protected: int max_runs_ = 0; }; template class ScriptExecuteAction; template class ScriptExecuteAction, Ts...> : public Action { public: ScriptExecuteAction(Script *script) : script_(script) {} using Args = std::tuple...>; template void set_args(F... x) { args_ = Args{x...}; } void play(Ts... x) override { this->script_->execute_tuple(this->eval_args_(x...)); } protected: // NOTE: // `eval_args_impl` functions evaluates `I`th the functions in `args` member. // and then recursively calls `eval_args_impl` for the `I+1`th arg. // if `I` = `N` all args have been stored, and nothing is done. template void eval_args_impl_(std::tuple & /*unused*/, std::integral_constant /*unused*/, std::integral_constant /*unused*/, Ts... /*unused*/) {} template void eval_args_impl_(std::tuple &evaled_args, std::integral_constant /*unused*/, std::integral_constant n, Ts... x) { std::get(evaled_args) = std::get(args_).value(x...); // NOTE: evaluate `i`th arg, and store in tuple. eval_args_impl_(evaled_args, std::integral_constant{}, n, x...); // NOTE: recurse to next index. } std::tuple eval_args_(Ts... x) { std::tuple evaled_args; eval_args_impl_(evaled_args, std::integral_constant{}, std::tuple_size{}, x...); return evaled_args; } Script *script_; Args args_; }; template class ScriptStopAction : public Action { public: ScriptStopAction(C *script) : script_(script) {} void play(Ts... x) override { this->script_->stop(); } protected: C *script_; }; template class IsRunningCondition : public Condition { public: explicit IsRunningCondition(C *parent) : parent_(parent) {} bool check(Ts... x) override { return this->parent_->is_running(); } protected: C *parent_; }; template class ScriptWaitAction : public Action, public Component { public: ScriptWaitAction(C *script) : script_(script) {} void play_complex(Ts... x) override { this->num_running_++; // Check if we can continue immediately. if (!this->script_->is_running()) { this->play_next_(x...); return; } this->var_ = std::make_tuple(x...); this->loop(); } void loop() override { if (this->num_running_ == 0) return; if (this->script_->is_running()) return; this->play_next_tuple_(this->var_); } void play(Ts... x) override { /* ignore - see play_complex */ } protected: C *script_; std::tuple var_{}; }; } // namespace script } // namespace esphome