From 4324b79ae2929c11b02839dbe395cff98abb8784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristofer=20Borgstr=C3=B6m?= Date: Sun, 14 Nov 2021 20:30:00 +0100 Subject: [PATCH] feat: custom dimmer interval, extended testing and default config --- Dockerfile | 3 +- config-defaults.yaml | 19 + config.yaml | 5 +- src/sunrise-timer.ts | 4 +- .../__snapshots__/timer.service.test.ts.snap | 1313 ++++++++++++++++- src/svc/config.service.ts | 8 +- src/svc/timer.service.test.ts | 45 +- src/svc/timer.service.ts | 49 +- 8 files changed, 1330 insertions(+), 116 deletions(-) create mode 100644 config-defaults.yaml diff --git a/Dockerfile b/Dockerfile index 212f2149..a0d34ba1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,8 +7,9 @@ RUN mkdir -p /opt/app WORKDIR /opt/app COPY *.json yarn.lock *.yaml /opt/app/ -COPY src /opt/app/src RUN yarn --pure-lockfile +COPY src /opt/app/src + CMD yarn start diff --git a/config-defaults.yaml b/config-defaults.yaml new file mode 100644 index 00000000..9f7890f8 --- /dev/null +++ b/config-defaults.yaml @@ -0,0 +1,19 @@ +timezone: Europe/Stockholm + +# Location & max sun angle for artificial light +max_sun_angle: 7 # degrees over the horizon at which time light can be turned off as it's light-enough anyway +latitude: 60.025369 +longitude: 17.669363 + +fake_noon_hour: 11 # Treat as mid-day +hours_of_full_light: 10 +minutes_of_sunrise: 120 # interval for dimming between 0 and 100% (both sunrise and sundown) + +dimmer_step: 3 +dimmer_min: 0 # Start dimming upwards from here. Although 0 is 0 +dimmer_max: 100 # Start dimming downwards from here. Although 100 is 100. +dimmer_controller: telldus + +telldus: + dimmer_id: 111 + diff --git a/config.yaml b/config.yaml index e29fb6c6..b69a8c6b 100644 --- a/config.yaml +++ b/config.yaml @@ -1,5 +1,4 @@ # Location & max sun angle for artificial light -max_sun_angle: 7 # degrees over the horizon at which time light can be turned off as it's light-enough anyway latitude: 60.025369 longitude: 17.669363 @@ -7,9 +6,9 @@ fake_noon_hour: 11 # Treat as mid-day hours_of_full_light: 10 minutes_of_sunrise: 120 # interval for dimming between 0 and 100% (both sunrise and sundown) +dimmer_step: 2 dimmer_controller: telldus -dimmer_step: 3 +dimmer_max: 50 # Start dimming downwards from here. Although 100 is 100. -timezone: Europe/Stockholm telldus: dimmer_id: 9611251 diff --git a/src/sunrise-timer.ts b/src/sunrise-timer.ts index a5e47743..50b75f3c 100644 --- a/src/sunrise-timer.ts +++ b/src/sunrise-timer.ts @@ -19,12 +19,14 @@ export function runScript(fn: (...args: any[]) => any): void { })() } +process.env['TZ'] = config.timezone runScript(async () => { console.log("Start sunrise timer") console.log(`Using controller: ${config.dimmer_controller}`) + console.log(`Timezone: ${process.env['TZ']}`) - await run() + await run(config) }) diff --git a/src/svc/__snapshots__/timer.service.test.ts.snap b/src/svc/__snapshots__/timer.service.test.ts.snap index 14467783..06f67b6c 100644 --- a/src/svc/__snapshots__/timer.service.test.ts.snap +++ b/src/svc/__snapshots__/timer.service.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`timer.runner snapshot 1`] = ` +exports[`timer.service with configs Custom dimmer interval config 1`] = ` Array [ Array [ "00:00", @@ -200,95 +200,95 @@ Array [ ], Array [ "04:05", - 48003, + 12, ], Array [ "04:10", - 48007, + 16, ], Array [ "04:15", - 48011, + 18, ], Array [ "04:20", - 48015, + 22, ], Array [ "04:25", - 48020, + 26, ], Array [ "04:30", - 48024, + 30, ], Array [ "04:35", - 48028, + 32, ], Array [ "04:40", - 48032, + 36, ], Array [ "04:45", - 48036, + 38, ], Array [ "04:50", - 48040, + 42, ], Array [ "04:55", - 48045, + 46, ], Array [ "05:00", - 48049, + 50, ], Array [ "05:05", - 48053, + 52, ], Array [ "05:10", - 48057, + 56, ], Array [ "05:15", - 48061, + 58, ], Array [ "05:20", - 48065, + 62, ], Array [ "05:25", - 48070, + 66, ], Array [ "05:30", - 48074, + 70, ], Array [ "05:35", - 48078, + 72, ], Array [ "05:40", - 48082, + 76, ], Array [ "05:45", - 48086, + 78, ], Array [ "05:50", - 48090, + 82, ], Array [ "05:55", - 48095, + 86, ], Array [ "06:00", @@ -452,132 +452,1289 @@ Array [ ], Array [ "09:20", + 0, + ], + Array [ + "09:25", + 0, + ], + Array [ + "09:30", + 0, + ], + Array [ + "09:35", + 0, + ], + Array [ + "09:40", + 0, + ], + Array [ + "09:45", + 0, + ], + Array [ + "09:50", + 0, + ], + Array [ + "09:55", + 0, + ], + Array [ + "10:00", + 0, + ], + Array [ + "10:05", + 0, + ], + Array [ + "10:10", + 0, + ], + Array [ + "10:15", + 0, + ], + Array [ + "10:20", + 0, + ], + Array [ + "10:25", + 0, + ], + Array [ + "10:30", + 0, + ], + Array [ + "10:35", + 0, + ], + Array [ + "10:40", + 0, + ], + Array [ + "10:45", + 0, + ], + Array [ + "10:50", + 0, + ], + Array [ + "10:55", + 0, + ], + Array [ + "11:00", + 0, + ], + Array [ + "11:05", + 0, + ], + Array [ + "11:10", + 0, + ], + Array [ + "11:15", + 0, + ], + Array [ + "11:20", + 0, + ], + Array [ + "11:25", + 0, + ], + Array [ + "11:30", + 0, + ], + Array [ + "11:35", + 0, + ], + Array [ + "11:40", + 0, + ], + Array [ + "11:45", + 0, + ], + Array [ + "11:50", + 0, + ], + Array [ + "11:55", + 0, + ], + Array [ + "12:00", + 100, + ], + Array [ + "12:05", + 100, + ], + Array [ + "12:10", + 100, + ], + Array [ + "12:15", + 100, + ], + Array [ + "12:20", + 100, + ], + Array [ + "12:25", + 100, + ], + Array [ + "12:30", + 100, + ], + Array [ + "12:35", + 100, + ], + Array [ + "12:40", + 100, + ], + Array [ + "12:45", + 100, + ], + Array [ + "12:50", + 100, + ], + Array [ + "12:55", + 100, + ], + Array [ + "13:00", + 100, + ], + Array [ + "13:05", + 100, + ], + Array [ + "13:10", + 100, + ], + Array [ + "13:15", + 100, + ], + Array [ + "13:20", + 100, + ], + Array [ + "13:25", + 100, + ], + Array [ + "13:30", + 100, + ], + Array [ + "13:35", + 100, + ], + Array [ + "13:40", + 100, + ], + Array [ + "13:45", + 100, + ], + Array [ + "13:50", + 100, + ], + Array [ + "13:55", + 100, + ], + Array [ + "14:00", + 100, + ], + Array [ + "14:05", + 100, + ], + Array [ + "14:10", + 100, + ], + Array [ + "14:15", + 100, + ], + Array [ + "14:20", + 100, + ], + Array [ + "14:25", + 100, + ], + Array [ + "14:30", + 100, + ], + Array [ + "14:35", + 100, + ], + Array [ + "14:40", + 100, + ], + Array [ + "14:45", + 100, + ], + Array [ + "14:50", + 100, + ], + Array [ + "14:55", + 100, + ], + Array [ + "15:00", + 100, + ], + Array [ + "15:05", + 100, + ], + Array [ + "15:10", + 100, + ], + Array [ + "15:15", + 100, + ], + Array [ + "15:20", + 100, + ], + Array [ + "15:25", + 100, + ], + Array [ + "15:30", + 100, + ], + Array [ + "15:35", + 100, + ], + Array [ + "15:40", + 100, + ], + Array [ + "15:45", + 100, + ], + Array [ + "15:50", + 100, + ], + Array [ + "15:55", + 100, + ], + Array [ + "16:00", + 0, + ], + Array [ + "16:05", + 86, + ], + Array [ + "16:10", + 82, + ], + Array [ + "16:15", + 78, + ], + Array [ + "16:20", + 76, + ], + Array [ + "16:25", + 72, + ], + Array [ + "16:30", + 70, + ], + Array [ + "16:35", + 66, + ], + Array [ + "16:40", + 62, + ], + Array [ + "16:45", + 58, + ], + Array [ + "16:50", + 56, + ], + Array [ + "16:55", + 52, + ], + Array [ + "17:00", + 50, + ], + Array [ + "17:05", + 46, + ], + Array [ + "17:10", + 42, + ], + Array [ + "17:15", + 38, + ], + Array [ + "17:20", + 36, + ], + Array [ + "17:25", + 32, + ], + Array [ + "17:30", + 30, + ], + Array [ + "17:35", + 26, + ], + Array [ + "17:40", + 22, + ], + Array [ + "17:45", + 18, + ], + Array [ + "17:50", + 16, + ], + Array [ + "17:55", + 12, + ], + Array [ + "18:00", + 0, + ], + Array [ + "18:05", + 0, + ], + Array [ + "18:10", + 0, + ], + Array [ + "18:15", + 0, + ], + Array [ + "18:20", + 0, + ], + Array [ + "18:25", + 0, + ], + Array [ + "18:30", + 0, + ], + Array [ + "18:35", + 0, + ], + Array [ + "18:40", + 0, + ], + Array [ + "18:45", + 0, + ], + Array [ + "18:50", + 0, + ], + Array [ + "18:55", + 0, + ], + Array [ + "19:00", + 0, + ], + Array [ + "19:05", + 0, + ], + Array [ + "19:10", + 0, + ], + Array [ + "19:15", + 0, + ], + Array [ + "19:20", + 0, + ], + Array [ + "19:25", + 0, + ], + Array [ + "19:30", + 0, + ], + Array [ + "19:35", + 0, + ], + Array [ + "19:40", + 0, + ], + Array [ + "19:45", + 0, + ], + Array [ + "19:50", + 0, + ], + Array [ + "19:55", + 0, + ], + Array [ + "20:00", + 0, + ], + Array [ + "20:05", + 0, + ], + Array [ + "20:10", + 0, + ], + Array [ + "20:15", + 0, + ], + Array [ + "20:20", + 0, + ], + Array [ + "20:25", + 0, + ], + Array [ + "20:30", + 0, + ], + Array [ + "20:35", + 0, + ], + Array [ + "20:40", + 0, + ], + Array [ + "20:45", + 0, + ], + Array [ + "20:50", + 0, + ], + Array [ + "20:55", + 0, + ], + Array [ + "21:00", + 0, + ], + Array [ + "21:05", + 0, + ], + Array [ + "21:10", + 0, + ], + Array [ + "21:15", + 0, + ], + Array [ + "21:20", + 0, + ], + Array [ + "21:25", + 0, + ], + Array [ + "21:30", + 0, + ], + Array [ + "21:35", + 0, + ], + Array [ + "21:40", + 0, + ], + Array [ + "21:45", + 0, + ], + Array [ + "21:50", + 0, + ], + Array [ + "21:55", + 0, + ], + Array [ + "22:00", + 0, + ], + Array [ + "22:05", + 0, + ], + Array [ + "22:10", + 0, + ], + Array [ + "22:15", + 0, + ], + Array [ + "22:20", + 0, + ], + Array [ + "22:25", + 0, + ], + Array [ + "22:30", + 0, + ], + Array [ + "22:35", + 0, + ], + Array [ + "22:40", + 0, + ], + Array [ + "22:45", + 0, + ], + Array [ + "22:50", + 0, + ], + Array [ + "22:55", + 0, + ], + Array [ + "23:00", + 0, + ], + Array [ + "23:05", + 0, + ], + Array [ + "23:10", + 0, + ], + Array [ + "23:15", + 0, + ], + Array [ + "23:20", + 0, + ], + Array [ + "23:25", + 0, + ], + Array [ + "23:30", + 0, + ], + Array [ + "23:35", + 0, + ], + Array [ + "23:40", + 0, + ], + Array [ + "23:45", + 0, + ], + Array [ + "23:50", + 0, + ], + Array [ + "23:55", + 0, + ], +] +`; + +exports[`timer.service with configs Default config 1`] = ` +Array [ + Array [ + "00:00", + 0, + ], + Array [ + "00:05", + 0, + ], + Array [ + "00:10", + 0, + ], + Array [ + "00:15", + 0, + ], + Array [ + "00:20", + 0, + ], + Array [ + "00:25", + 0, + ], + Array [ + "00:30", + 0, + ], + Array [ + "00:35", + 0, + ], + Array [ + "00:40", + 0, + ], + Array [ + "00:45", + 0, + ], + Array [ + "00:50", + 0, + ], + Array [ + "00:55", + 0, + ], + Array [ + "01:00", + 0, + ], + Array [ + "01:05", + 0, + ], + Array [ + "01:10", + 0, + ], + Array [ + "01:15", + 0, + ], + Array [ + "01:20", + 0, + ], + Array [ + "01:25", + 0, + ], + Array [ + "01:30", + 0, + ], + Array [ + "01:35", + 0, + ], + Array [ + "01:40", + 0, + ], + Array [ + "01:45", + 0, + ], + Array [ + "01:50", + 0, + ], + Array [ + "01:55", + 0, + ], + Array [ + "02:00", + 0, + ], + Array [ + "02:05", + 0, + ], + Array [ + "02:10", + 0, + ], + Array [ + "02:15", + 0, + ], + Array [ + "02:20", + 0, + ], + Array [ + "02:25", + 0, + ], + Array [ + "02:30", + 0, + ], + Array [ + "02:35", + 0, + ], + Array [ + "02:40", + 0, + ], + Array [ + "02:45", + 0, + ], + Array [ + "02:50", + 0, + ], + Array [ + "02:55", + 0, + ], + Array [ + "03:00", + 0, + ], + Array [ + "03:05", + 0, + ], + Array [ + "03:10", + 0, + ], + Array [ + "03:15", + 0, + ], + Array [ + "03:20", + 0, + ], + Array [ + "03:25", + 0, + ], + Array [ + "03:30", + 0, + ], + Array [ + "03:35", + 0, + ], + Array [ + "03:40", + 0, + ], + Array [ + "03:45", + 0, + ], + Array [ + "03:50", + 0, + ], + Array [ + "03:55", + 0, + ], + Array [ + "04:00", + 0, + ], + Array [ + "04:05", + 4, + ], + Array [ + "04:10", + 8, + ], + Array [ + "04:15", + 12, + ], + Array [ + "04:20", + 16, + ], + Array [ + "04:25", + 20, + ], + Array [ + "04:30", + 24, + ], + Array [ + "04:35", + 28, + ], + Array [ + "04:40", + 32, + ], + Array [ + "04:45", + 36, + ], + Array [ + "04:50", + 40, + ], + Array [ + "04:55", + 44, + ], + Array [ + "05:00", + 50, + ], + Array [ + "05:05", + 54, + ], + Array [ + "05:10", + 58, + ], + Array [ + "05:15", + 62, + ], + Array [ + "05:20", + 66, + ], + Array [ + "05:25", + 70, + ], + Array [ + "05:30", + 74, + ], + Array [ + "05:35", + 78, + ], + Array [ + "05:40", + 82, + ], + Array [ + "05:45", + 86, + ], + Array [ + "05:50", + 90, + ], + Array [ + "05:55", + 94, + ], + Array [ + "06:00", 100, ], Array [ - "09:25", + "06:05", 100, ], Array [ - "09:30", + "06:10", 100, ], Array [ - "09:35", + "06:15", 100, ], Array [ - "09:40", + "06:20", 100, ], Array [ - "09:45", + "06:25", 100, ], Array [ - "09:50", + "06:30", 100, ], Array [ - "09:55", + "06:35", 100, ], Array [ - "10:00", + "06:40", 100, ], Array [ - "10:05", + "06:45", 100, ], Array [ - "10:10", + "06:50", 100, ], Array [ - "10:15", + "06:55", 100, ], Array [ - "10:20", + "07:00", 100, ], Array [ - "10:25", + "07:05", 100, ], Array [ - "10:30", + "07:10", 100, ], Array [ - "10:35", + "07:15", 100, ], Array [ - "10:40", + "07:20", 100, ], Array [ - "10:45", + "07:25", 100, ], Array [ - "10:50", + "07:30", 100, ], Array [ - "10:55", + "07:35", 100, ], Array [ - "11:00", + "07:40", 100, ], Array [ - "11:05", + "07:45", 100, ], Array [ - "11:10", + "07:50", 100, ], Array [ - "11:15", + "07:55", 100, ], Array [ - "11:20", + "08:00", 100, ], Array [ - "11:25", + "08:05", 100, ], Array [ - "11:30", + "08:10", 100, ], Array [ - "11:35", + "08:15", 100, ], Array [ - "11:40", + "08:20", 100, ], Array [ - "11:45", + "08:25", 100, ], Array [ - "11:50", + "08:30", 100, ], Array [ - "11:55", + "08:35", + 100, + ], + Array [ + "08:40", + 100, + ], + Array [ + "08:45", + 100, + ], + Array [ + "08:50", + 100, + ], + Array [ + "08:55", + 100, + ], + Array [ + "09:00", + 100, + ], + Array [ + "09:05", + 100, + ], + Array [ + "09:10", + 100, + ], + Array [ + "09:15", 100, ], + Array [ + "09:20", + 0, + ], + Array [ + "09:25", + 0, + ], + Array [ + "09:30", + 0, + ], + Array [ + "09:35", + 0, + ], + Array [ + "09:40", + 0, + ], + Array [ + "09:45", + 0, + ], + Array [ + "09:50", + 0, + ], + Array [ + "09:55", + 0, + ], + Array [ + "10:00", + 0, + ], + Array [ + "10:05", + 0, + ], + Array [ + "10:10", + 0, + ], + Array [ + "10:15", + 0, + ], + Array [ + "10:20", + 0, + ], + Array [ + "10:25", + 0, + ], + Array [ + "10:30", + 0, + ], + Array [ + "10:35", + 0, + ], + Array [ + "10:40", + 0, + ], + Array [ + "10:45", + 0, + ], + Array [ + "10:50", + 0, + ], + Array [ + "10:55", + 0, + ], + Array [ + "11:00", + 0, + ], + Array [ + "11:05", + 0, + ], + Array [ + "11:10", + 0, + ], + Array [ + "11:15", + 0, + ], + Array [ + "11:20", + 0, + ], + Array [ + "11:25", + 0, + ], + Array [ + "11:30", + 0, + ], + Array [ + "11:35", + 0, + ], + Array [ + "11:40", + 0, + ], + Array [ + "11:45", + 0, + ], + Array [ + "11:50", + 0, + ], + Array [ + "11:55", + 0, + ], Array [ "12:00", 100, @@ -776,95 +1933,95 @@ Array [ ], Array [ "16:05", - 47903, + 94, ], Array [ "16:10", - 47907, + 90, ], Array [ "16:15", - 47911, + 86, ], Array [ "16:20", - 47915, + 82, ], Array [ "16:25", - 47920, + 78, ], Array [ "16:30", - 47924, + 74, ], Array [ "16:35", - 47928, + 70, ], Array [ "16:40", - 47932, + 66, ], Array [ "16:45", - 47936, + 62, ], Array [ "16:50", - 47940, + 58, ], Array [ "16:55", - 47945, + 54, ], Array [ "17:00", - 47949, + 50, ], Array [ "17:05", - 47953, + 44, ], Array [ "17:10", - 47957, + 40, ], Array [ "17:15", - 47961, + 36, ], Array [ "17:20", - 47965, + 32, ], Array [ "17:25", - 47970, + 28, ], Array [ "17:30", - 47974, + 24, ], Array [ "17:35", - 47978, + 20, ], Array [ "17:40", - 47982, + 16, ], Array [ "17:45", - 47986, + 12, ], Array [ "17:50", - 47990, + 8, ], Array [ "17:55", - 47995, + 4, ], Array [ "18:00", diff --git a/src/svc/config.service.ts b/src/svc/config.service.ts index 26211710..0fafed6f 100644 --- a/src/svc/config.service.ts +++ b/src/svc/config.service.ts @@ -10,6 +10,8 @@ export interface SunriseTimerConfig { fake_noon_hour: number dimmer_controller: string dimmer_step: number + dimmer_max: number + dimmer_min: number timezone: string telldus?: TelldusConfig } @@ -17,4 +19,8 @@ export interface SunriseTimerConfig { export interface TelldusConfig { dimmer_id: string } -export const config: SunriseTimerConfig = yaml.load(fs.readFileSync('config.yaml', 'utf8')) as SunriseTimerConfig + +export const config: SunriseTimerConfig = { + ...(yaml.load(fs.readFileSync('config-defaults.yaml', 'utf8')) as SunriseTimerConfig), + ...(yaml.load(fs.readFileSync('config.yaml', 'utf8')) as SunriseTimerConfig) +} as SunriseTimerConfig diff --git a/src/svc/timer.service.test.ts b/src/svc/timer.service.test.ts index 93f241c9..29c8a77d 100644 --- a/src/svc/timer.service.test.ts +++ b/src/svc/timer.service.test.ts @@ -1,25 +1,48 @@ import {dayjs, IDayjs} from "@naturalcycles/time-lib"; -import {determineDimLevel} from "./timer.service"; -import {config} from "./config.service"; +import {_singleExec, determineDimLevel, run} from "./timer.service"; +import {config, SunriseTimerConfig} from "./config.service"; +import {mockTime} from "@naturalcycles/dev-lib/dist/testing"; +import {controller} from "../controller/telldus"; +import * as yaml from "js-yaml"; +import * as fs from 'fs' +import {pMap} from "@naturalcycles/js-lib"; beforeEach(() => { - jest.restoreAllMocks() + jest.restoreAllMocks() + mockTime(dayjs('2021-11-28').unix()) }) - - -test('timer.runner snapshot', async () => { - - //Lazy time mock - const today = dayjs().year(2021).month(11).day(11) +describe('timer.service with configs', () => { + const testCases: [string, SunriseTimerConfig][] = [ + // Req: Track to fb with db data if user is logged in and paid less than 7 days ago + [ + 'Default config', + yaml.load(fs.readFileSync('config-defaults.yaml', 'utf8')) as SunriseTimerConfig, + ],[ + 'Custom dimmer interval config', + { + ...(yaml.load(fs.readFileSync('config-defaults.yaml', 'utf8')) as SunriseTimerConfig), + dimmer_min: 10, + dimmer_max: 90 + } + ], + ] + test.each(testCases)('%s', async (_, cfg) => { + + jest.spyOn(controller, 'toggleLight').mockImplementation() + jest.spyOn(controller, 'dimLight').mockImplementation() //Array with every 5 minutes of day const totalMinutes = [...Array(60*24/5)].map((_, i) => i * 5); - const dayJsArr: IDayjs[] = totalMinutes.map(totMin => today.hour(Math.floor(totMin / 60)).minute(totMin % 60)) + const dayJsArr: IDayjs[] = totalMinutes.map(totMin => dayjs().hour(Math.floor(totMin / 60)).minute(totMin % 60)) - const dimLevelArr = dayJsArr.map(now => [now.format("HH:mm"), determineDimLevel(now, config)]) + const dimLevelArr = await pMap(dayJsArr, async(now) => { + mockTime(now.unix()); + return [now.format("HH:mm"), await _singleExec(controller, cfg)] + }) expect(dimLevelArr).toMatchSnapshot() + }) }) diff --git a/src/svc/timer.service.ts b/src/svc/timer.service.ts index 821da73b..37c454bf 100644 --- a/src/svc/timer.service.ts +++ b/src/svc/timer.service.ts @@ -1,39 +1,46 @@ import {dayjs, IDayjs} from "@naturalcycles/time-lib"; import {config, SunriseTimerConfig} from "./config.service"; +import {Controller} from "../controller/controller"; -var suncalc = require('suncalc'); +const suncalc = require('suncalc'); +let dimState: number = -1 -export async function run(): Promise { +export async function run(cfg: SunriseTimerConfig): Promise { console.log("Timer starting") - let dimState: number = -1 // controller shall export "controller" const { controller } = await import(`../controller/${config.dimmer_controller}`) while (true) { - let dimLevel = determineDimLevel(dayjs(), config) + await _singleExec(controller, cfg) + //Evaluate again in 1 minute + await delay(1000 * 60) + } +} - // Handle increments - if (dimLevel > 1 && dimLevel < 100) { - dimLevel = dimLevel - dimLevel % config.dimmer_step - } +export async function _singleExec(controller: Controller, cfg: SunriseTimerConfig): Promise { + let dimLevel = determineDimLevel(dayjs(), config) - //Avoid unnecessary api calls - if (dimLevel !== dimState) { - if (dimLevel === 0) { - await controller.toggleLight(false, config) - } else if (dimLevel === 100) { - await controller.toggleLight(true, config) - } else { - await controller.dimLight(dimLevel, config) - } - dimState = dimLevel - } + // Handle increments + if (dimLevel > 0 && dimLevel < 100) { + const dimInterval: number = cfg.dimmer_max - cfg.dimmer_min + const dimLevelPrecise = cfg.dimmer_min + Math.floor(dimLevel * dimInterval / 100) + dimLevel = dimLevelPrecise - dimLevelPrecise % config.dimmer_step + } - //Evaluate again in 1 minute - await delay(1000 * 60) + //Avoid unnecessary api calls + if (dimLevel !== dimState) { + if (dimLevel === 0) { + await controller.toggleLight(false, config) + } else if (dimLevel === 100) { + await controller.toggleLight(true, config) + } else { + await controller.dimLight(dimLevel, config) } + dimState = dimLevel + } + return dimState } export function determineDimLevel(now: IDayjs, cfg: SunriseTimerConfig): number {