Created
October 3, 2014 14:39
-
-
Save timfpark/f632aeba7048886cca4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Store = require('nitrogen-leveldb-store'), | |
nitrogen = require('nitrogen'); | |
var config = { | |
host: process.env.HOST_NAME || 'api.nitrogen.io', | |
http_port: process.env.PORT || 443, | |
protocol: process.env.PROTOCOL || 'https', | |
api_key: "75fecc9fef5fb9ba488e987216442595" | |
}; | |
config.store = new Store(config); | |
var simpleLED = new nitrogen.Device({ | |
nickname: 'simpleLED', | |
tags: ['sends:_isOn', 'executes:_lightOn'], | |
api_key: config.api_key | |
}); | |
function simpleLEDManager() { | |
nitrogen.CommandManager.apply(this, arguments); | |
} | |
simpleLEDManager.prototype = Object.create(nitrogen.CommandManager.prototype); | |
simpleLEDManager.prototype.constructor = simpleLEDManager; | |
simpleLEDManager.prototype.isRelevant = function(message) { | |
return (message.is('_lightOn') && (!this.device || message.from === this.device.id || message.to == this.device.id)); | |
}; | |
simpleLEDManager.prototype.isCommand = function(message) { | |
return message.is('_lightOn'); | |
}; | |
simpleLEDManager.prototype.obsoletes = function(downstreamMsg, upstreamMsg) { | |
if (nitrogen.CommandManager.obsoletes(downstreamMsg, upstreamMsg)) return true; | |
return upstreamMsg.is('_lightOn') && downstreamMsg.body.command === "cancel"; | |
}; | |
simpleLEDManager.prototype.executeQueue = function(callback) { | |
if (!this.device) return callback(new Error('no icontrol attached to control manager.')); | |
var activeCommands = this.activeCommands(); | |
if (activeCommands.length === 0) { | |
this.session.log.warn('simpleLEDManager::executeQueue: no active commands to execute.'); | |
return callback(); | |
} | |
activeCommands.forEach(function(activeCommand) { | |
console.log("wow"); | |
activeCommand.body.command = "cancel"; | |
console.log("activeCommand: " + JSON.stringify(activeCommand)); | |
var lightOn = activeCommand.body.value; | |
var message = new nitrogen.Message({ | |
type: '_isOn', | |
body: { | |
command: { | |
message: "Light (" + simpleLED.id + ") is " + JSON.stringify(lightOn) + " at " + Date.now() | |
} | |
} | |
}); | |
message.send(_session); | |
}); | |
} | |
var _session; | |
simpleLEDManager.prototype.start = function(session, callback) { | |
var filter = { | |
type: "_lightOn" | |
}; | |
_session = session; | |
return nitrogen.CommandManager.prototype.start.call(this, session, filter, callback); | |
}; | |
var service = new nitrogen.Service(config); | |
service.connect(simpleLED, function(err, session, simpleLED) { | |
if (err) return console.log('failed to connect simpleLED: ' + err); | |
new simpleLEDManager(simpleLED).start(session, function(err, message) { | |
if (err) return session.log.error(JSON.stringify(err)); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment