feat: Use ActionCallback for schedule
All checks were successful
Build and deploy / build (push) Successful in 11m12s
Build and deploy / Deploy container (push) Has been skipped

This has two advantages:
- Each schedule entry can take either a single function or table of
  functions.
- We get a better type definition.
This commit is contained in:
2025-10-19 02:43:24 +02:00
parent 92a0bff8c4
commit 64c4a47c6f
3 changed files with 6 additions and 4 deletions

View File

@@ -10,5 +10,5 @@ local FulfillmentConfig
---@class Config ---@class Config
---@field fulfillment FulfillmentConfig ---@field fulfillment FulfillmentConfig
---@field devices DeviceInterface[]? ---@field devices DeviceInterface[]?
---@field schedule table<string, function>? ---@field schedule table<string, fun() | fun()[]>?
local Config local Config

View File

@@ -1,6 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::net::{Ipv4Addr, SocketAddr}; use std::net::{Ipv4Addr, SocketAddr};
use automation_lib::action_callback::ActionCallback;
use automation_lib::device::Device; use automation_lib::device::Device;
use automation_macro::LuaDeviceConfig; use automation_macro::LuaDeviceConfig;
use lua_typed::Typed; use lua_typed::Typed;
@@ -39,7 +40,7 @@ pub struct Config {
pub devices: Vec<Box<dyn Device>>, pub devices: Vec<Box<dyn Device>>,
#[device_config(from_lua, default)] #[device_config(from_lua, default)]
#[typed(default)] #[typed(default)]
pub schedule: HashMap<String, mlua::Function>, pub schedule: HashMap<String, ActionCallback<()>>,
} }
impl From<FulfillmentConfig> for SocketAddr { impl From<FulfillmentConfig> for SocketAddr {

View File

@@ -1,10 +1,11 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::pin::Pin; use std::pin::Pin;
use automation_lib::action_callback::ActionCallback;
use tokio_cron_scheduler::{Job, JobScheduler, JobSchedulerError}; use tokio_cron_scheduler::{Job, JobScheduler, JobSchedulerError};
pub async fn start_scheduler( pub async fn start_scheduler(
schedule: HashMap<String, mlua::Function>, schedule: HashMap<String, ActionCallback<()>>,
) -> Result<(), JobSchedulerError> { ) -> Result<(), JobSchedulerError> {
let scheduler = JobScheduler::new().await?; let scheduler = JobScheduler::new().await?;
@@ -14,7 +15,7 @@ pub async fn start_scheduler(
let f = f.clone(); let f = f.clone();
Box::pin(async move { Box::pin(async move {
f.call_async::<()>(()).await.unwrap(); f.call(()).await;
}) })
} }
}; };