solarnetwork-api-core
    Preparing search index...

    Class ControlToggler

    Manage the state of a boolean control switch using SolarNetwork SetControlParameter instructions.

    Use an instance of this class to keep track of, and update the state of, a single switch-like control configured on a SolarNode. Because updating the state of a control is an asynchronous process involving multiple steps, this class simplifies this with a promise-based API that will be resolved when the control value changes.

    If the ControlToggler#start method is called, the toggler will make periodic calls to SolarNetwork to get the most recent value for the configured control ID, which it treats as a ControlDatum sourceId value. Thus if some other process changes the control, the toggler will eventually pick up that change and invoke the callback function.

    const auth = new AuthorizationV2Builder('token');
    auth.saveSigningKey('secret');

    const toggler = new ControlTogger(new SolarUserApi(), auth, 123, '/power/switch/1');
    toggler.callback = (error) => {
    // invoked when instruction states change, or the control value changes
    console.log(`Control ${toggler.controlId} value == ${toggler.value()}; pending == ${toggler.hasPendingStateChange}`);
    };

    // enable automatic keeping track of state and the callback hook
    toggler.start();

    // ... at some point later, maybe in response to a UI event, update the state;
    // the callback will be invoked then the value changes
    toggler.value(1);
    Index

    Constructors

    • Constructor.

      Parameters

      • api: SolarUserApi

        the URL helper to use

      • auth: AuthorizationV2Builder

        the auth builder to authenticate requests with; the required credentials must be set appropriately

      • nodeId: number

        the ID of the node with the control to manage

      • controlId: string

        the ID of the control to manage

      • OptionalqueryApi: SolarQueryApi

        a URL helper for accessing node datum via SolarQuery; if not provided one will be created using the environment from api. Useful in a development environment when the SolarUser and SolarQuery hosts are different.

      Returns ControlToggler

    Properties

    A callback function, which is called after the state of the control changes. The this reference will be set to this object. If an error has occurred, the error will be passed as the first argument.

    controlId: string

    The control ID to manage.

    nodeId: number

    The node ID to manage the control on.

    pendingRefreshMs: number = 5000

    The refresh rate, in milliseconds, when a toggle instruction is queued. Defaults to 5 seconds.

    refreshMs: number = 20000

    The refresh rate, in milliseconds. Defaults to 20 seconds.

    Accessors

    • get hasPendingStateChange(): boolean

      Test if a state change is pending confirmation.

      Returns boolean

      true if a state change is pending (not complete)

    Methods

    • Start automatically updating the status of the configured control.

      Parameters

      • Optionalwhen: number

        an optional offset in milliseconds to start at (defaults to 20)

      Returns this

      this object