function sendMessage(obj,callback) { var message = new nitrogen.Message(obj); message.to = nitrogenId; message.send(nitrogenSession, function (err,sentMessages) { callback(err,sentMessages); }); }
ServoManager.prototype.executeQueue = function(callback) { if (!this.device) return callback(new Error('no device attached to control manager.')); var self = this; // CommandManager.activeCommands() // Returns the array of commands that are currently active for this manager var activeCommands = this.activeCommands(); // Verify there are activeCommands if (activeCommands.length === 0) { this.session.log.warn('ServoManager::executeQueue: no active commands to execute.'); return callback(); } var commandIds = []; var pos; //console.log('activeCommands:'); console.dir(activeCommands); // Find the final state and collect all the active command ids // You will use them in a moment. activeCommands.forEach(function(activeCommand) { // Collect active command IDs commandIds.push(activeCommand.id); pos = activeCommand.body.command.position; }); if(pos > 180) pos = 180; else if(pos < 0) pos = 0; servo.to(pos); // This is the response to the _lightLevel command. // Notice the response_to is the array of command ids from above. This is used in the obsoletes method above as well. var servoMessage = new nitrogen.Message({ type: '_position', tags: nitrogen.CommandManager.commandTag(self.device.id), body: { command: { position: pos } }, response_to: commandIds }); servoMessage.send(this.session, function(err, message) { if (err) return callback(err); console.log("Message sent: " + JSON.stringify(message)); // let the command manager know we processed this message. self.process(new nitrogen.Message(message)); // need to callback if there aren't any issues so commandManager can proceed. return callback(); }); };
simpleManager.prototype.executeQueue = function(callback) { var self = this; if (!this.device) return callback(new Error('no device attached to control manager.')); // This looks at the list of active commands and returns if there's no commands to process. var activeCommands = this.activeCommands(); if (activeCommands.length === 0) { this.session.log.warn('simpleManager::executeQueue: no active commands to execute.'); return callback(); } var lightOn; var commandIds = []; // Here we are going to find the final state and but collect all the active command ids because we'll use them in a moment. activeCommands.forEach(function(activeCommand) { console.log("activeCommand: " + JSON.stringify(activeCommand)); try { lightOn = activeCommand.body.value; commandIds.push(activeCommand.id); if (lightOn == "true") { board.digitalWrite(LEDPIN, 1); } else { board.digitalWrite(LEDPIN, 0); } } catch (ex) { callback(ex); } }); // This is the response to the _lightOn command. // Notice the response_to is the array of command ids from above. This is used in the obsoletes method above as well. var message = new nitrogen.Message({ type: '_isOn', tags: nitrogen.CommandManager.commandTag(self.device.id), body: { command: { message: "Light (" + simpleLED.id + ") is " + JSON.stringify(lightOn) + " at " + Date.now() } }, response_to: commandIds }); message.send(this.session, function(err, message) { if (err) return callback(err); // let the command manager know we processed this _lightOn message by passing it the _isOn message. self.process(new nitrogen.Message(message)); // need to callback if there aren't any issues so commandManager can proceed. return callback(); }); }
photoresistor.on('data', function() { // Capture the ambient light level from the photoresistor var lightLevel = this.value; // Create a Nitrogen message var message = new nitrogen.Message({ type: '_lightLevel', body: { ambientLight: lightLevel } }); // Log the light level value for debugging session.log.info('Sending ambientLight: ' + lightLevel); // Send the message message.send(session); });
light.on("change", function() { var lightValue = Math.round(this.value * .1); var message = new nitrogen.Message({ type: '_lightLevel', body: { command: { 'light': lightValue } } }); console.log("Sending: " + JSON.stringify(message)); message.send(session); });
setTimeout(function() { nitrogen.Message.find(session, { from: camera.id }, {}, function(err, messages) { assert.ifError(err); callback(messages.length); }); }, 500);
this.device.set(this.state, function(err) { if (err) return callback(err); var message = new nitrogen.Message({ type: 'lightState', tags: [ nitrogen.CommandManager.commandTag(self.device.id) ], response_to: commandIds, body: self.state }); message.send(self.session, function(err, messages) { if (err) return callback(err); self.process(messages[0]); callback(); }); });
light.on("change", function() { var lightValue = Math.round(this.value * .1); var message = new nitrogen.Message({ type: '_lightLevel', tags: nitrogen.CommandManager.commandTag(simpleLightSensor.id), body: { command: { 'light': lightValue } }, to: simpleLightSensor.id }); console.log("Sending: " + JSON.stringify(message)); message.send(session); });
service.connect(simpleLED, function(err, session, simpleLED) { console.log("Connected to Nitrogen"); var message = new nitrogen.Message({ type: '_isOn', body: { command: { message: "Light (" + simpleLED.id + ") is On at " + Date.now() } } }); message.send(session); new simpleManager(simpleLED).start(session, function(err, message) { if (err) return session.log.error(JSON.stringify(err)); }); });
photoresistor.on('data', function() { // Capture the ambient light level from the photo resistor var lightLevel = this.value; // Create a Nitrogen Message to send the _lightLevel var ambientLightMessage = new nitrogen.Message({ type: '_lightLevel', tags: nitrogen.CommandManager.commandTag(lightSensor.id), body: { command: { ambientLight: lightLevel } }, to: lightSensor.id }); // Send the message ambientLightMessage.send(session); console.log("Message sent: " + JSON.stringify(ambientLightMessage)); });
photoresistor.on('data', function() { // Capture the ambient light level from the photo resistor var lightLevel = this.value; // Create a Nitrogen Message to send the _lightLevel var ambientLightMessage = new nitrogen.Message({ type: '_lightLevel', // Specify a command tag that you can scope to // This will enable you to filter out non-relevant messages tags: nitrogen.CommandManager.commandTag('lab06'), body: { command: { ambientLight: lightLevel } } }); // Send the message ambientLightMessage.send(session); // Show the message in the console console.log("Message sent: " + JSON.stringify(ambientLightMessage)); });
principals.forEach(function(camera) { if (!cameras[camera.id]) { nitrogen.Message.find(session, { type: 'image' }, { limit: 1, sort: { ts: -1 } }, function(err, messages) { if (err) return session.log.error('message request failed: ' + err); if (messages.length) { session.log.info('last image for camera: ' + JSON.stringify(messages)); sendImage(session, messages[0]); } }); sendCamera(camera); } });
photoresistor.on('data', function() { // Capture the ambient light level from the photo resistor var lightLevel = this.value; var pos = map(lightLevel, 0, 1023, 0, 180); pos = constrain(pos, 0, 180); // Create a Nitrogen Message to send the _lightLevel var ambientLightMessage = new nitrogen.Message({ type: '_setPosition', tags: nitrogen.CommandManager.commandTag(servoDevice.id), body: { command: { position: pos } }, to: servoDevice.id }); // Send the message ambientLightMessage.send(session); console.log("Message sent: " + JSON.stringify(ambientLightMessage)); });
LightManager.prototype.executeQueue = function(callback) { if (!this.device) return callback(new Error('no device attached to control manager.')); var self = this; // CommandManager.activeCommands() // Returns the array of commands that are currently active for this manager var activeCommands = this.activeCommands(); // Verify there are activeCommands if (activeCommands.length === 0) { this.session.log.warn('LightManager::executeQueue: no active commands to execute.'); return callback(); } var commandIds = []; var lightOn; // Find the final state and collect all the active command ids // You will use them in a moment. activeCommands.forEach(function(activeCommand) { // Collect active command IDs commandIds.push(activeCommand.id); var light = activeCommand.body.command.ambientLight; // Determine the final state of the light (on/true or off/false) lightOn = light > 950; }); // Turn the light on or off based on final state if (led != null) { // Make sure the led sensor is available before using it if(lightOn) { led.on(); } else { led.off(); } } else { console.log("ERROR: 'led' was null."); } // This is the response to the _lightLevel command. // Notice the response_to is the array of command ids from above. This is used in the obsoletes method above as well. var lightMessage = new nitrogen.Message({ type: '_lightState', tags: nitrogen.CommandManager.commandTag(self.device.id), body: { command: { on: lightOn } }, response_to: commandIds }); lightMessage.send(this.session, function(err, message) { if (err) return callback(err); console.log("Message sent: " + JSON.stringify(message)); // let the command manager know we processed this message. self.process(new nitrogen.Message(message)); // need to callback if there aren't any issues so commandManager can proceed. return callback(); }); };
LightManager.prototype.executeQueue = function(callback) { if (!this.device) return callback(new Error('no device attached to control manager.')); var self = this; // CommandManager.activeCommands() // Returns the array of commands that are currently active for this manager var activeCommands = this.activeCommands(); // Verify there are activeCommands if (activeCommands.length === 0) { this.session.log.warn('LightManager::executeQueue: no active commands to execute.'); return callback(); } var commandIds = []; // An array to collect Command IDs var r, g, b, color; // Variables for the Red, Green and Blue values // Find the final state and collect all the active command ids // You will use them in a moment. activeCommands.forEach(function(activeCommand) { // Collect active command IDs commandIds.push(activeCommand.id); // Collect the ambient light level from the message var light = activeCommand.body.command.ambientLight; // Set the Red, Green, and Blue values based on the ambient light level. // In bright light it will glow green, and in dim light it will glow white. // Increase the Red value for levels above 350 r = map(light, 350, 1023, 0, 255); // increase the Green value for all light levels g = map(light, 0, 1023, 0, 255); // Increase the Blue value for light levels above 650 b = map(light, 650, 1023, 0, 255); // Constrain the values to eliminate negative values and values beyond the upper bound r = constrain(r, 0, 255).toString(16); //.toString(16) converts the value to Hexidecimal g = constrain(g, 0, 255).toString(16); b = constrain(b, 0, 255).toString(16); // Add a leading '0' as needed to have a two-digit hex value if(r.length < 2) r = '0' + r; if(g.length < 2) g = '0' + g; if(b.length < 2) b = '0' + b; }); color = r + g + b; // If the LED is present, set its color value if(led != null) { led.color(color); } // This is the response to the _lightLevel command. // Notice the response_to is the array of command ids from above. This is used in the obsoletes method above as well. var lightMessage = new nitrogen.Message({ type: '_color', tags: nitrogen.CommandManager.commandTag(cmdTag), body: { command: { color: color } }, response_to: commandIds }); lightMessage.send(this.session, function(err, message) { if (err) return callback(err); console.log("Message sent: " + JSON.stringify(message)); // let the command manager know we processed this message. self.process(new nitrogen.Message(message)); // need to callback if there aren't any issues so commandManager can proceed. return callback(); }); };