Skip to content

Instantly share code, notes, and snippets.

@kevinswiber
Last active June 5, 2016 08:34
Show Gist options
  • Save kevinswiber/0d77726e2d3783576aad to your computer and use it in GitHub Desktop.
Save kevinswiber/0d77726e2d3783576aad to your computer and use it in GitHub Desktop.
Zetta devices with documentation.
var util = require('util');
var Device = require('zetta').Device;
/// # heartbeat
///
/// A mock heartbeat sensor, often used as a driver example
/// for exposing streams.
///
/// This device has the following properties:
/// * `pulse`
/// * The latest value of the `pulse` stream.
///
/// ## Available Streams
///
/// ### pulse
/// A random number between 60 and 72, emitted every 10 seconds.
///
var Heartbeat = module.exports = function() {
this.pulse = null;
Device.call(this);
};
util.inherits(Heartbeat, Device);
Heartbeat.prototype.init = function(config) {
config
.type('heartbeat')
.monitor('pulse')
.doc(__filename);
var self = this;
setInterval(function() {
var min = 60;
var max = 72;
self.pulse = Math.round(Math.random() * (max - min) + min, 2);
}, 10000);
};
var util = require('util');
var Device = require('zetta').Device;
/// # led
///
/// A mock LED, often used as an example of a simple state machine,
/// allowing `turn-on` and `turn-off` transitions.
///
/// This device has the following properties:
/// * `state`
/// * The current state of the device.
///
var LED = module.exports = function() {
Device.call(this);
};
util.inherits(LED, Device);
LED.prototype.init = function(config) {
config
.type('led')
.state('off')
.when('off', { allow: ['turn-on'] })
.when('on', { allow: ['turn-off'] })
.map('turn-on', this.turnOn)
.map('turn-off', this.turnOff)
.doc(__filename);
};
/// ## Available Transitions
/// ### turn-on
///
/// Illuminates the LED. This transition has no fields.
///
LED.prototype.turnOn = function(cb) {
this.state = 'on';
cb();
};
/// ### turn-off
///
/// Darkens the LED. This transition has no fields.
///
LED.prototype.turnOff = function(cb) {
this.state = 'off';
cb();
};
HTTP/1.1 200 OK
Content-Type: application/vnd.siren+json
Access-Control-Allow-Origin: *
Content-Length: 935
Date: Wed, 10 Jun 2015 18:38:11 GMT
Connection: keep-alive
{
"class": [
"type"
],
"properties": {
"type": "led",
"properties": [
"id",
"name",
"type",
"state"
],
"streams": [
"state"
],
"transitions": [
{
"name": "turn-on"
},
{
"name": "turn-off"
}
],
"documentation": "# led\n\nA mock LED, often used as an example of a simple state machine,\nallowing `turn-on` and `turn-off` transitions.\n\nThis device has the following properties:\n* `state`\n * The current state of the device.\n\n## Available Transitions\n### turn-on\n\nIlluminates the LED. This transition has no fields.\n\n### turn-off\n\nDarkens the LED. This transition has no fields.\n"
},
"links": [
{
"rel": [
"self"
],
"href": "http://localhost:1337/servers/kevin-mbp.local/meta/led"
},
{
"title": "kevin-mbp.local",
"rel": [
"collection",
"http://rels.zettajs.io/metadata"
],
"href": "http://localhost:1337/servers/kevin-mbp.local/meta"
},
{
"rel": [
"http://rels.zettajs.io/instances",
"describes"
],
"href": "http://localhost:1337/servers/kevin-mbp.local?ql=where%20type%3D%22led%22"
}
]
}

led

A mock LED, often used as an example of a simple state machine, allowing turn-on and turn-off transitions.

This device has the following properties:

  • state
    • The current state of the device.

Available Transitions

turn-on

Illuminates the LED. This transition has no fields.

turn-off

Darkens the LED. This transition has no fields.

@zdne
Copy link

zdne commented Jun 11, 2015

just sketching what a resource blueprint for this could look like https://gist.github.com/zdne/752a3b240ec4d687c7ab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment