Pergunta

I am building a time-lapse camera web application using Raspberry Pi and the Raspberry Pi Camera Module. So far I have built a web application (using NodeJS, Express, AngularJS, and BootStrap 3) that can interact with the Raspberry Camera Module using an open source NodeJS module (https://www.npmjs.org/package/raspicam).

I have a global variable called "setting" that will always change whenever the user changes the camera settings:

var setting = {
    mode: "timelapse",
    output: "public/images/image%d.jpg", // image1, image2, image3, etc...
    encoding: "jpg",
    timelapse: 3000, // take a picture every 3 seconds
    timeout: 12000 // take a total of 4 pictures over 12 seconds
}

I have three functions in Express that can:

set Camera settings

exports.setCamera = function(req, res) {
    setting = {
        mode: req.body.mode,
        output: req.body.output,
        encoding: req.body.encoding,
        timelapse: req.body.timelapse,
        timeout: req.body.timeout
    }
    res.json(setting, 200);
    console.log('SET CAMERA - ' + JSON.stringify(setting));
}

start the Camera

exports.startCamera = function(req, res) {
    camera = new RaspiCam(setting);
    camera.on("start", function( err, timestamp ){
      console.log("timelapse started at " + timestamp);
    });

    camera.on("read", function( err, timestamp, filename ){
      console.log("timelapse image captured with filename: " + filename);
    });

    camera.on("exit", function( timestamp ){
      console.log("timelapse child process has exited");
      res.json(setting, 200);
    });

    camera.on("stop", function( err, timestamp ){
      console.log("timelapse child process has been stopped at " + timestamp);
    });

    camera.start();

    setTimeout(function(){
      camera.stop();
    }, setting.timeout + 1000);

    console.log('START CAMERA - ' + JSON.stringify(setting));
}

stop the Camera

exports.stopCamera = function(req, res) {
    camera.stop();
    res.json(setting, 200);
    console.log('STOP CAMERA - ' + JSON.stringify(setting));
}

As you can see in the "startCamera" function, I am creating a new RaspiCam object called "camera" that passes in the global variable "setting" (which can always change). When the camera object is created, I am also creating "start", "read", "exist", and "stop" functions for it. The problem is that since I am not setting the camera object as a global variable, when the user decides to click Stop halfway during the session, the "stopCamera" function gets called but it does not know what camera.stop() is and says it is undefined. Is there a way I can allow the "stopCamera" function to know what camera.stop() is (which was created in the "startCamera" function)?

Sorry if this is confusing, I don't know how else to describe my problem.. :(

Foi útil?

Solução

I think you have a problem with how this is architected, but the simple solution to your question is check to see if the camera object has been initialized.

exports.stopCamera = function(req, res) {
    if(camera && typeof(camera.stop) == "function") {
        camera.stop();
        console.log('STOP CAMERA - ' + JSON.stringify(setting));    
    }

    res.json(setting, 200); 
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top