You are here

Smart Home Library Manual



STASH JavaScript Library

The STASH (SmartTV Alliance Smart Home) JavaScript Library abstracts the network protocols defined in the Smart Home specification and gives an easy handle for application developers to use Smart Home functionality in their applications.

Overview

This library has following logical entities:

  • an Endpoint is the entity the client connects to and has a set of devices
  • a Device is bound to an endpoint and consists of properties
  • a Property is bound to a device and can be of various types (like String, Integer, ...)

Examples

In the following sections the basic operations of the library are explained.

Using Network Service Discovery (NSD) API to find Endpoints

The library contains a method discoverEndpoints to find Endpoints in the local network. The NSD API is called with a search target of '''urn:smarttv-alliance-org:service:smarthome''' to find these. The example below shows how to trigger the discovery and to handle the results.

// trigger search of endpoints
var successCb = function(eps) {
    var discoveredEndpoints = eps;
    // this can also be used:
    // var discoveredEndpoints = stash.getDiscoveredEndpoints();
    if (discoveredEndpoints.length > 0) {
        log("Discovered " + discoveredEndpoints.length + " endpoints", discoveredEndpoints);
    }
};

var errorCb = function(error) {
    log("Could not discover endpoints!", error);
};

var discoverResult = stash.discoverEndpoints(successCb, errorCb);
if (discoverResult === true) {
    log("Triggered discovering of endpoints...");
} else {
    log("Could not trigger discovering of endpoints, most probably browser does not support the NSD API!");
}

Working with endpoints

Retrieve information about all registered endpoints:

var allEndpoints = stash.getEndpoints();
for ( var ep in allEndpoints) {
    if (allEndpoints[ep].name == epName) {
        log("Endpoint name already in use");
        return;
    } else if (allEndpoints[ep].endpointAddress == "ws://" + epIP) {
        log("Endpoint with address: " + epIP + " already added");
        return;
    }
}

Add and remove endpoints:

stash.addEndpoint("SampleEndpoint", "ws://192.168.0.123:8080", "obix.v2");
stash.removeEndpoint("SampleEndpoint");

Manually trigger polling

var endpoint = stash.getEndpoint(name);
if (typeof (endpoint) != 'undefined') {
    endpoint.poll();
}

Implementing a callback function for continuous updates:

var endpoint = stash.getEndpoint(name);
if (endpoint != null) {
    endpoint.connect(function() {
        if (endpoint.watch) {
           try { endpoint.watch(); } catch (e) { };
        }
        ...
    });
}

Working with devices

var devices = stash.getDevices();
console.log("Devices", devices);
for (var i = 0; i < devices.length; i++) {
    if (devices.hasOwnProperty(i)) {
        console.log(" - ", devices[i].name);
        for ( var j = 0; j < devices[i].properties.length; j++) {
            console.log(" -- ", devices[i].properties[j].name, devices[i].properties[j].obix, 
                devices[i].properties[j].value, devices[i].properties[j].writeable);
            ...
        }
    }
}

Debugging

Debug logging can be enabled with setting the debug property to true

stash.debug = true;

Links