From 870ae6b9ee7942f295e916d4d2dc8b217d51147c Mon Sep 17 00:00:00 2001 From: Ivan Panarin Date: Fri, 27 Jan 2023 23:58:36 +0200 Subject: [PATCH] feat(*): timezone configuration add timezone configuration plugin and each task --- README.md | 19 +++++++++++-------- config.schema.json | 12 ++++++++++++ src/configs/config.ts | 3 +++ src/configs/task.config.ts | 3 +++ src/task.handler.ts | 2 ++ 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 59d68b8..8f2ff14 100644 --- a/README.md +++ b/README.md @@ -84,11 +84,12 @@ sudo npm install -g --unsafe-perm homebridge-cron-scheduler@latest } ``` -| Config Field | Description | Default | Required | -|--------------|---------------------------------------|-------------------|----------| -| platform | Must always be `CronScheduler`. | `"CronScheduler"` | Yes | -| debug | Enable for displaying debug messages. | `false` | No | -| tasks | Array of cron tasks. | `[]` | No | +| Config Field | Description | Default | Required | +|--------------|---------------------------------------------------------------------------------------------------|-------------------|----------| +| platform | Must always be `CronScheduler`. | `"CronScheduler"` | Yes | +| debug | Enable for displaying debug messages. | `false` | No | +| timezone | Timezone in 'Europe/Kyiv' format to use for all tasks. Leave blank for using the system timezone. | `undefined` | No | +| tasks | Array of cron tasks. | `[]` | No | | Task Config Field | Description | Default | Required | |------------------------|-------------------------------------------------------------------------------------------------------------------------------------------|----------------|----------| @@ -99,13 +100,15 @@ sudo npm install -g --unsafe-perm homebridge-cron-scheduler@latest | taskStateResetInterval | The interval in minutes after which the task state will be reset. Leave '0' for immediate reset, change to '-1' for enabling toggle mode. | `0` | No | | taskStartAt | Time at which the task should start. Leave blank for immediate start. ISO 8601 formatted datetime (2021-10-17T23:43:00) in local time. | `undefined` | No | | taskStopAt | Time at which the task should stop. Leave blank for no stop. ISO 8601 formatted datetime (2021-10-17T23:43:00) in local time. | `undefined` | No | +| timezone | Timezone override in 'Europe/Kyiv' format to use for this tasks. Leave blank for using the global timezone. | `undefined` | No | ## Cron Expression -* Cron expressions support the following additional modifiers - - *?* A question mark is substituted with cron initialization time, as an example - `? * * * *` would be substituted with `8 * * * *` if time is `:08`. The question mark can be used in any field. - - *L* L can be used in the day of month field, to specify the last day of the month. +* Cron expressions support the following additional modifiers +- *?* A question mark is substituted with cron initialization time, as an example - `? * * * *` would be substituted + with `8 * * * *` if time is `:08`. The question mark can be used in any field. +- *L* L can be used in the day of month field, to specify the last day of the month. ```javascript // ┌────────────── minute (0 - 59) diff --git a/config.schema.json b/config.schema.json index 333c21e..1c57234 100644 --- a/config.schema.json +++ b/config.schema.json @@ -13,6 +13,12 @@ "description": "Enable for displaying debug messages.", "default": false }, + "timezone": { + "title": "Global Timezone", + "description": "Timezone in 'Europe/Kyiv' format to use for all tasks. Leave blank for using the system timezone.", + "placeholder": "Europe/Kyiv", + "type": "string" + }, "tasks": { "title": "Tasks", "type": "array", @@ -62,6 +68,12 @@ "title": "Task Stop At", "description": "Time at which the task should stop. Leave blank for no stop. ISO 8601 formatted datetime (2021-10-17T23:43:00) in local time.", "type": "string" + }, + "timezone": { + "title": "Task Timezone", + "description": "Timezone override in 'Europe/Kyiv' format to use for this tasks. Leave blank for using the global timezone.", + "placeholder": "Europe/Kyiv", + "type": "string" } } }, diff --git a/src/configs/config.ts b/src/configs/config.ts index 46b6783..a7a919a 100644 --- a/src/configs/config.ts +++ b/src/configs/config.ts @@ -4,6 +4,7 @@ import { TaskConfig, ITaskConfig } from './task.config'; export interface IConfig extends PlatformConfig { debug?: boolean; + timezone?: string; tasks?: ITaskConfig[]; } @@ -14,11 +15,13 @@ export const CONFIG_DEFAULT: IConfig = { export class Config implements IConfig { readonly platform: string; readonly debug: boolean; + readonly timezone?: string; readonly tasks: TaskConfig[]; constructor(config: IConfig = CONFIG_DEFAULT) { this.platform = config.platform; this.debug = !!config.debug; + this.timezone = config.timezone; this.tasks = (config.tasks || []) .map((task) => { return new TaskConfig(this, task); diff --git a/src/configs/task.config.ts b/src/configs/task.config.ts index a02e733..1bd2dfb 100644 --- a/src/configs/task.config.ts +++ b/src/configs/task.config.ts @@ -8,6 +8,7 @@ export interface ITaskConfig { taskStateResetInterval?: number; taskStartAt?: string; taskStopAt?: string; + timezone?: string; } export class TaskConfig implements ITaskConfig { @@ -18,6 +19,7 @@ export class TaskConfig implements ITaskConfig { readonly taskStateResetInterval: number; readonly taskStartAt?: string; readonly taskStopAt?: string; + readonly timezone?: string; constructor(_config: Config, task: ITaskConfig) { this.taskActive = task.taskActive !== undefined ? task.taskActive : true; @@ -27,6 +29,7 @@ export class TaskConfig implements ITaskConfig { this.taskStateResetInterval = task.taskStateResetInterval || 0; this.taskStartAt = task.taskStartAt; this.taskStopAt = task.taskStartAt; + this.timezone = task.timezone || _config.timezone; } get id(): string { diff --git a/src/task.handler.ts b/src/task.handler.ts index 9d218ee..1363a3e 100644 --- a/src/task.handler.ts +++ b/src/task.handler.ts @@ -61,6 +61,7 @@ export class TaskHandler { maxRuns: this.taskConfig.taskMaxRuns, startAt: this.taskConfig.taskStartAt, stopAt: this.taskConfig.taskStopAt, + timezone: this.taskConfig.timezone, }, this._handleCronJob.bind(this), ); @@ -108,6 +109,7 @@ export class TaskHandler { legacyMode: false, paused: false, maxRuns: 1, + timezone: this.taskConfig.timezone, }, () => { this.$_logger && this.$_logger.debug(