Skip to content
wwitman edited this page Feb 4, 2016 · 4 revisions

Zetta class

This is the base module for Zetta. It is accessed by using require('zetta') in node. This class exposes functionality for standing up your Zetta server, and linking to the cloud.

Methods

Zetta.name(string)

Gives the Zetta instance a human-readable name. This name is exposed in the API through the name property. The name is important for clients crawling the API.

Arguments

  • name - A name for the server.
var zetta = require('zetta');

zetta()
  .name('detroit');

Zetta.silent()

Suppresses logging messages from the Zetta server.

Arguments

None.

var zetta = require('zetta');

zetta()
  .silent()

Zetta.logger(logFunction)

Implements a custom logger for Zetta.

Arguments

  • logFunction - A function that handles logging messages.
var zetta = require('zetta');

zetta()
  .logger(function(log) {
    log.on('message', function(level, event, msg, data) {
      //Intercept logging messages.  
    });
  })

Zetta.use(constructor, [args], [options])

Instantiates and loads device drivers and scouts into Zetta.

You can write custom device drivers, or find existing drivers on NPM. (Give link to modules search and device topic here.)

Arguments

  • constructor - The constructor function for the device or scout.
  • [args] - (optional) A comma-separated list of arguments to pass to the constructor. Typically used to set properties on the object.
  • [options] - (optional) An object specifying options to put on the device or scout object.
var zetta = require('zetta');
var myDevice = require('./device.js');
var myScout = require('./scout.js');

zetta()
  .use(myScout)
  .use(myDevice, ['pin_12'])

Zetta.use(app)

Loads a Zetta app module. Apps are stateless JavaScript modules that allow you to wire up interactions between devices. Apps can also be used to create server extensions.

Arguments

  • app - An exported function with the signature function(server).

Example

//index.js
var zetta = require('zetta');
var myApp = require('./myapp.js');

zetta()
  .use(myApp);

Sample app

Here's a sample app. Notice the function signature takes a server argument. This is the required pattern for apps. This app queries for devices and changes their state. (more)

//app.js
module.exports = function(server) {

    var StateMachine_1_Query = server.where({type: 'state_machine', name: 'machine_1'});
    var StateMachine_2_Query = server.where({type: 'state_machine', name: 'machine_2'});
    var StateMachine_3_Query = server.where({type: 'state_machine', name: 'machine_3'});

    server.observe([StateMachine_1_Query, StateMachine_2_Query, StateMachine_3_Query], function(machine_1, machine_2, machine_3) {
        console.log("State Machine came online: " + machine_1.name + ", " + machine_2.name + ", " + machine_3.name);

        machine_1.on('turn-off', function() {
            machine_2.call('turn-off');
            machine_3.call('turn-off');
        });

        machine_1.on('turn-on', function() {
            machine_2.call('turn-on');
            machine_3.call('turn-on');
        });
    });
}

Zetta.link(host)

Link to another Zetta instance. Typically this is used to link your local instance of Zetta to the cloud.

  • host - A string or array of strings representing the target URL(s).
var zetta = require('zetta');

zetta();
  .link('http://zettajs.io/cloud');

Zetta.listen(port, [hostname], [callback])

Select which host and port your instance of Zetta will listen on locally.

Arguments

  • port - The port number for the server.
  • hostname - (Optional) The hostname for the server. Default: localhost.
  • callback - (Optional) A function that executes when the server starts successfully.
var zetta = require('zetta');

zetta()
  .listen(3000, function(err) {
    console.log('Server listening on port 3000');
  });

Zetta Options

You can pass these options to the Zetta server.

  • registry - DeviceRegistry Class - Used to override default DeviceRegistry. See zetta-memory-registry for example.
  • peerRegistry - PeerRegistry Class - Used to override default PeerRegistry. See zetta-memory-registry for example.
  • tls - Object - Include TLS options to the http/https server exposed by zetta.
var zetta = require('zetta');

var options = {
  tls: {
    key: fs.readFileSync('./key.pem'),
    cert: fs.readFileSync('./cert.pem')
  }
};

zetta(options)
  .listen(443)
Clone this wiki locally