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.

Example

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);

Constructors

  • Constructor.

    Parameters

    • api: default

      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

    • Optional queryApi: default

      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

#api: default
#auth: AuthorizationV2Builder
#lastKnownDatum?: ControlDatum

The last known instruction status. The val property indicates the control value.

#lastKnownInstruction?: default

The last known instruction object.

#queryApi: default
#queryAuth: AuthorizationV2Builder
#timer?: any

A timeout identifier.

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

  • Private

    Calculate the refresh rate to use.

    Returns number

    the refresh rate to use, in milliseconds

  • Private

    Fetch a URL.

    Type Parameters

    • T

      the expected result type

    Parameters

    • method: HttpMethod

      the HTTP method to use

    • url: string

      the URL to request

    • auth: AuthorizationV2Builder

    Returns Promise<T>

    promise of the results

  • Private

    Find an active SetControlParameter Instruction for the configured controlId.

    Parameters

    • data: undefined | InstructionInfo[]

      array of instructions

    Returns undefined | default

    the active instruction, or undefined

  • Private

    Return the value from either the controlStatus or the first parameter value of an instruction, whichever is valid and more recent.

    Parameters

    • Optional controlDatum: ControlDatum

      a control status object

    • Optional instruction: default

      an instruction object

    Returns undefined | ControlValueType

    the control status value, or undefined if not known

  • Start automatically updating the status of the configured control.

    Parameters

    • Optional when: number

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

    Returns this

    this object

  • Refresh the control state from SolarNetwork.

    Once the ControlToggler#start method is called, this method is invoked periodically automatically. Only call this directly if you need to manually update the state of the control.

    Returns Promise<undefined | void | ControlValueType>

    promise that resolves after getting the updated state