Building Simple Clients

There is example code for the node.js, Arduino and iOS platforms in our Git repository. Building simple clients is just that, simple.

NB: Some of the example from the repository are discussed elsewhere in the documentation where we say "Hello Lightbulbs".

Monitoring the event stream

If you want to monitor the event stream from the steward, for instance for weather changes reported by your weather station, or your Tesla finishing charging, you can do so by connecting to the console endpoint and watching for the relevant JSON message.

The node.js snippet below will grab each message as it passes and print it to the screen,

var util = require("util");
var WebSocket = require('ws');

var ws = new WebSocket('ws://127.0.0.1:8887/console');
console.log("Created websocket.");

ws.onopen = function(event) {
    console.log("Opened websocket to steward.");

};

ws.onmessage = function(event) {
    var str = JSON.stringify(JSON.parse(event.data), null, 2);
    console.log("Socket message: " + str);

};

ws.onclose = function(event) {
    console.log("Socket closed: " + event.wasClean );

};

ws.onerror = function(event) {
    console.log("Socket error: " + util.inspect(event, {depth: null}));
    try { 
        ws.close (); 
        console.log("Closed websocket.");
    } catch (ex) {}
};

Controlling devices

If you want to control a group of devices, for instance all the light bulbs, you can also do this via the manage end pint

var util = require("util");
var WebSocket = require('ws');

var ws = new WebSocket('ws://127.0.0.1:8887/manage');
console.log("Created websocket.");

ws.onopen = function(event) {
    console.log("Opened websocket to steward.");
    var json = JSON.stringify({ path      :'/api/v1/actor/perform/device/lighting', 
                            requestID :'3', 
                            perform   : 'on',
                parameter : JSON.stringify({ 
                            brightness: 100, 
                                color: { model: 'rgb', rgb: { r: 255, g: 255, b: 255 }}}) });
    ws.send(json);
};

ws.onmessage = function(event) {
    var str = JSON.stringify(JSON.parse(event.data), null, 2);
console.log("Socket message: " + str);
    ws.close();
};

ws.onclose = function(event) {
console.log("Socket closed: " + event.wasClean );
};

ws.onerror = function(event) {
console.log("Socket error: " + util.inspect(event, {depth: null}));
    try { 
        ws.close (); 
        console.log("Closed websocket.");
    } catch (ex) {}
};

Figuring out what devices are available.

If you want to control a single device rather than a class of devices-like all the lightbulbs- then you'll need the deviceID of the device you want. You can do an actor list call to the steward to figure out what devices are available by connecting to the manage endpoint.

var util = require("util");
var WebSocket = require('ws');

var ws = new WebSocket('ws://127.0.0.1:8887/manage');
console.log("Created websocket.");

ws.onopen = function(event) {
    console.log("Opened websocket to steward.");
    var json = JSON.stringify({ path:'/api/v1/actor/list', 
                                requestID :'1', 
                                options   :{ depth: 'all'  }  });
    ws.send(json);
};

ws.onmessage = function(event) {
    var str = JSON.stringify(JSON.parse(event.data), null, 2);
    console.log("Socket message: " + str);
    ws.close();
};

ws.onclose = function(event) {
    console.log("Socket closed: " + event.wasClean );
};

ws.onerror = function(event) {
    console.log("Socket error: " + util.inspect(event, {depth: null}));
    try { 
        ws.close (); 
        console.log("Closed websocket.");
    } catch (ex) {}
};

So if you wanted to manage the "Desk Lamp", the deviceID is '19' use

  /api/v1/device/foo/deviceID

where foo is 'list' or 'perform' or 'delete'. Beyond that control of a single device is similar to controlling multiple devices.

Implementing Magic

While simple clients usually require a lot of user interaction, effectively turning the steward into a sophisticated remote control for your devices, our philosophy is that more complicated clients probably shouldn't. They should effectively work like magic.