nitrogen-camera
Version:
This module adds support for cameraCommand and cameraStatus messages to a Nitrogen application or service. It also provides the CameraManager class for interpreting message streams and driving the attached device.
1 lines • 2.12 kB
JavaScript
(function(){"use strict";function e(){nitrogen.CommandManager.apply(this,arguments)}e.prototype=Object.create(nitrogen.CommandManager.prototype),e.prototype.constructor=e,e.prototype.executeQueue=function(e){if(!this.device)return e(new Error("no camera attached to camera manager."));var t=this.activeCommands();if(t.length===0)return this.session.log.warn("CameraManager::executeQueue: no active commands to execute."),e();var n=t[0],r=n.body.options||{},i=this,s=[];this.device.snapshot(r,function(r,s){var o={};o.response_to=[],t.forEach(function(e){if(n.body.command===e.body.command){i.session.log.info("CameraManager::executeQueue: command message satisfied: "+JSON.stringify(e)),o.response_to.push(e.id);if(e.body.message)for(var t in e.body.message)o[t]=e.body.message[t]}}),i.sendResponse(r,s,o,function(t,n){if(t)return e(t);i.process(n),e()})})},e.prototype.isRelevant=function(e){return(e.is("cameraCommand")||e.is("image"))&&(!this.device||e.from===this.device.id||e.to==this.device.id)},e.prototype.isCommand=function(e){return e.is("cameraCommand")},e.prototype.obsoletes=function(e,t){return nitrogen.CommandManager.obsoletes(e,t)?!0:e.is("cameraCommand")&&e.body.command==="cancel"&&e.isResponseTo(t.id)||e.is("image")&&e.isResponseTo(t)&&t.body.command==="snapshot"},e.prototype.sendResponse=function(e,t,n,r){var i=this,s=new nitrogen.Blob({content_type:t.content_type});s.save(i.session,e,function(e,t){if(e)return r(e);var s=new nitrogen.Message({type:"image",link:t.link,tags:[],body:{url:t.url}});for(var o in n)s[o]=n[o];s.tags.push(nitrogen.CommandManager.commandTag(i.device.id)),i.session.log.info("CameraManager::sendImage: sending message: "+JSON.stringify(s)),s.send(i.session,function(e,t){if(e)return r("CameraManager::sendImage: failed to send message for image: "+e);i.session.log.info("CameraManager::sendImage: image sent: "+JSON.stringify(t)),r(null,t[0])})})},e.prototype.start=function(e,t){var n={tags:nitrogen.CommandManager.commandTag(this.device.id)};return nitrogen.CommandManager.prototype.start.call(this,e,n,t)},setTimeout(function(){window.nitrogen.CameraManager=e},1)})();