UNPKG

tibber-api

Version:

Node.js module for connecting to Tibber API and extract data from your connected homes, including realtime data from Tibber Pulse.

198 lines (197 loc) 6.34 kB
import { EventEmitter } from 'events'; import { IConfig } from '../models/IConfig'; import WebSocket from 'ws'; import { TibberQueryBase } from './TibberQueryBase'; export declare class TibberFeed extends EventEmitter { private _operationId; private _feedConnectionTimeout; private _feedIdleTimeout; private _config; private _active; private _isConnected; private _isConnecting; private _gql; private _webSocket; private _tibberQuery; private _isClosing; private _isUnauthenticated; private _headerManager; private _lastRetry; private _retryBackoff; private _connectionAttempts; private _backoffDelayBase; private _backoffDelayMax; private _realTimeConsumptionEnabled?; private _failedAttempts; private _maxFailedConnectionAttempts; private _timeoutCount; private _webSocketFactory; private _timeouts; private _lastConnectedAt; private _rateLimitUntil; private _autoReconnect; get timeoutCount(): number; get maxFailedConnectionAttempts(): number; set maxFailedConnectionAttempts(value: number); /** * Constructor for creating a new instance if TibberFeed. * @constructor * @param {TibberQueryBase} tibberQuery TibberQueryBase object. * @param {number} timeout Feed idle timeout in milliseconds. The feed will reconnect after being idle for more than the specified number of milliseconds. Min 5000 ms. * @param {boolean} returnAllFields Specify if you want to return all fields from the data feed. * @param {number} connectionTimeout Feed connection timeout. * @see {@linkcode TibberQueryBase} */ constructor(tibberQuery: TibberQueryBase, timeout?: number, returnAllFields?: boolean, connectionTimeout?: number, autoReconnect?: boolean, webSocketFactory?: (url: string, protocols: string[], options: any) => WebSocket); get active(): boolean; set active(value: boolean); get connected(): boolean; get feedIdleTimeout(): number; set feedIdleTimeout(value: number); get feedConnectionTimeout(): number; set feedConnectionTimeout(value: number); get queryRequestTimeout(): number; set queryRequestTimeout(value: number); get config(): IConfig; set config(value: IConfig); private get canConnect(); /** * PUBLIC METHODS * ---------------- * These methods are used to interact with the TibberFeed. * ---------------- * */ /** * Connect to Tibber feed. */ connect(): Promise<void>; /** * Close the Tibber feed. */ close(): void; /** * Heartbeat function used to keep connection alive. * Mostly for internal use, even if it is public. */ heartbeat(): void; /** * PRIVATE METHODS * ---------------- * These methods are used internally by the TibberFeed. * ---------------- * */ /** * Generate random number with a max value. * @param {number} max Maximum number * @returns {number} Random number. */ private getRandomInt; /** * Exponential backoff with jitter * @param {number} attempt Connection attempt * @returns {number} */ private getBackoffWithJitter; /** * Decreases the connection backoff. */ private decreaseConnectionBackoff; /** * addTimeout * Adds a timeout to the list of timeouts. * @param {string} name Name of the timeout. * @param {() => void} fn Function to call when timeout is reached. * @param {number} ms Delay in milliseconds before callback will be called. */ protected addTimeout(name: string, fn: () => void, ms: number): void; /** * cancelTimeout * Cancels a timeout with the specified name. * @param name Name of the timeout to cancel. */ protected cancelTimeout(name: string): void; /** * Connect to feed with built in delay, timeout and backoff. */ private connectWithTimeout; private updateBackoff; private incrementFailedAttempts; private resetFailedAttempts; private hardReset; /** * Connect with a delay if the feed is still active. */ private connectWithDelayWorker; /** * Internal connection method that handles the communication with tibber feed. */ private internalConnect; private attachWebSocketHandlers; /** * Event: onWebSocketOpen * Called when websocket connection is established. */ private onWebSocketOpen; /** * Event: onWebSocketClose * Called when feed is closed. * @param {WebSocket.CloseEvent} event Close event */ private onWebSocketClose; /** * Event: onWebSocketMessage * Called when data is received from the feed. * @param {WebSocket.MessageEvent} event Message event */ private onWebSocketMessage; /** * Event: onWebSocketError * Called when an error has occurred. * @param {WebSocket.ErrorEvent} event Error event */ private onWebSocketError; /** * Gracefully close connection with Tibber. */ private closeConnection; /** * Forcefully terminate connection with Tibber. */ private terminateConnection; /** * Initialize connection with Tibber. */ private initConnection; /** * Subscribe to a specified resource. * @param subscription @typedef string Name of the resource to subscribe to. * @param variables @typedef Record<string, unknown> Variable to use with the resource. */ private startSubscription; /** * Stops subscribing to a resource with a specified operation Id * @param {number} operationId Operation Id to stop subscribing to. */ private stopSubscription; /** * Send websocket query to Tibber * @param {IQuery} query Tibber GQL query */ private sendQuery; /** * Log function to emit log data to subscribers. * @param {string} message Log message */ private log; /** * Log function to emit warning log data to subscribers. * @param {string} message Log message */ private warn; /** * Log function to emit error log data to subscribers. * @param {any} message Log message */ private error; private handleConnectionError; }