UNPKG

fritzbox-api

Version:

Straightforward, lightweight and extendable Node.js library to communicate with FRITZ!Box devices

101 lines (77 loc) 2.07 kB
// Copyright (c) 2026, Thorsten A. Weintz. All rights reserved. // Licensed under the MIT license. See LICENSE in the project root for license information. import FritzBoxApi, { url, setCredentials } from './config.mjs'; /** * Writes a message to the console. */ const log = console.log; /** * Initializes new instance of @see FritzBoxApi and sets options. */ const fritzBoxApi = new FritzBoxApi({ url }); /** * Sets the credentials used for authentication against the FRITZ!Box. */ setCredentials(fritzBoxApi); /** * The device identifier for the call (e.g., phone number, extension, or device ID). */ const number = '**610'; /** * Indicates whether the alarm loop is active. */ let running = false; /** * Delays execution for a given number of milliseconds. * * @param {*} ms Delay in milliseconds. * @returns {Promise<void>} */ const sleep = (ms) => new Promise(r => setTimeout(r, ms)); /** * Continuously triggers calls to simulate an alarm. * * @param {*} number Target device identifier. */ const alarmLoop = async (number) => { log('Alarm started'); while (running) { try { await fritzBoxApi.hangup(); await sleep(300); await fritzBoxApi.callDevice(number); } catch { } await sleep(10000); } log('Alarm stopped'); }; /** * Starts the alarm loop if not already running. * * @param {*} number Target device identifier. * @returns {void} */ const startAlarm = (number) => { if (running) return; running = true; alarmLoop(number); }; /** * Stops the alarm loop and hangs up any active call. */ const stopAlarm = () => { running = false; fritzBoxApi.hangup().catch(() => {}); }; /** * Handles process termination (Ctrl+C). */ process.on('SIGINT', () => { log('Shutting down...'); stopAlarm(); setTimeout(() => process.exit(0), 1000); }); /** * Starts the alarm on script execution. */ startAlarm(number);