RemoteSwitch Plugin

The RemoteSwitch plugin is a custom component designed for the Johnny-Five robotics and IoT library. It provides a streamlined way to manage on/off switch combinations, making it easier for developers to instantiate and control multiple switch pairs. This plugin encapsulates the behavior of these switches, allowing for modular and reusable code.

Overview

The RemoteSwitch plugin is particularly useful for scenarios where multiple switches need to be controlled programmatically. By encapsulating the switch behavior, it simplifies the process of managing these components within a larger application. The plugin is designed to be easily instantiated, providing a clean interface for controlling switch pairs.

Writing the RemoteSwitch Plugin

Creating the RemoteSwitch plugin involves writing a module that exports a function. This function returns a constructor for the RemoteSwitch component. The methods of the component are defined on its prototype, which allows for encapsulation and reuse. This design pattern is consistent with other components in the Johnny-Five library, ensuring that the RemoteSwitch plugin integrates seamlessly with existing codebases.

Refactoring to Use the RemoteSwitch Plugin

Refactoring an existing codebase to use the RemoteSwitch plugin involves replacing the current switch control logic with instances of the RemoteSwitch component. This refactoring leads to cleaner and more modular code, as the plugin handles the intricacies of switch management internally. By abstracting these details, developers can focus on higher-level application logic.

Queuing and Callbacks

The RemoteSwitch plugin incorporates a queuing mechanism to manage multiple toggle commands. Commands are stored in a queue and executed in order, ensuring that each toggle operation is completed before the next one begins. This is particularly useful in scenarios where multiple switches need to be toggled in quick succession.

Callbacks are also supported, allowing developers to register functions that are called once a switch command (on/off/toggle) is complete. This feature aligns with the behavior of other Johnny-Five components and provides additional flexibility for handling asynchronous operations.

[Listing 9.6. The Complete RemoteSwitch Plugin](https://livebook.manning.com/book/javascript-on-things/chapter-9/118)

Below is the complete implementation of the RemoteSwitch plugin, showcasing its structure and functionality:

module.exports = function (five) {
  return (function () {
    function RemoteSwitch (opts) {
      if (!(this instanceof RemoteSwitch)) {
        return new RemoteSwitch(opts);
      }
      five.Board.Component.call(this, opts = five.Board.Options(opts));

      this.pins     = opts.pins;
      this.duration = opts.duration || 500;
      this.isOn     = undefined;
      this.isActive = false;
      this.queue    = [];                                               1

      this.io.pinMode(this.pins.on, this.io.MODES.OUTPUT);
      this.io.pinMode(this.pins.off, this.io.MODES.OUTPUT);
    }

    RemoteSwitch.prototype.toggle = function (turnOn, callback) {       2
      if (this.isActive) {                                              3
        this.queue.push([turnOn, callback]);
        return;
      }
      this.isActive = true;
      if (typeof turnOn === 'undefined') {
        turnOn = !this.isOn;
      }
      const pin = (turnOn) ? this.pins.on : this.pins.off;
      this.io.digitalWrite(pin, 1);
      setTimeout(() => {
        this.io.digitalWrite(pin, 0);
        this.isActive = false;
        this.isOn = !!turnOn;
        if (typeof callback === 'function') {                           4
          callback();
        }
        if (this.queue.length) {                                        5
          this.toggle.apply(this, this.queue.shift());
        }
      }, this.duration);
    };

    RemoteSwitch.prototype.on = function (callback) {                   6
      this.toggle(true, callback);
    };
    RemoteSwitch.prototype.off = function (callback) {
      this.toggle(false, callback);
    };
    return RemoteSwitch;
  }());
};
  • 1 Instantiates an empty array to hold queued “commands”

This listing demonstrates how the RemoteSwitch plugin is structured, including its queuing mechanism and callback support. The plugin's methods, such as toggle, on, and off, provide a straightforward interface for controlling switch states.