Skip to content

Commit

Permalink
feat(*): timezone configuration
Browse files Browse the repository at this point in the history
add timezone configuration plugin and each task
  • Loading branch information
uamanager committed Jan 27, 2023
1 parent 2a3ee68 commit 870ae6b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|------------------------|-------------------------------------------------------------------------------------------------------------------------------------------|----------------|----------|
Expand All @@ -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 `<any hour>: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 `<any hour>: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)
Expand Down
12 changes: 12 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions src/configs/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TaskConfig, ITaskConfig } from './task.config';

export interface IConfig extends PlatformConfig {
debug?: boolean;
timezone?: string;
tasks?: ITaskConfig[];
}

Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/configs/task.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ITaskConfig {
taskStateResetInterval?: number;
taskStartAt?: string;
taskStopAt?: string;
timezone?: string;
}

export class TaskConfig implements ITaskConfig {
Expand All @@ -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;
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/task.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
Expand Down Expand Up @@ -108,6 +109,7 @@ export class TaskHandler {
legacyMode: false,
paused: false,
maxRuns: 1,
timezone: this.taskConfig.timezone,
},
() => {
this.$_logger && this.$_logger.debug(
Expand Down

0 comments on commit 870ae6b

Please sign in to comment.