question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connect to multiple devices

See original GitHub issue

Hi! I recently install this plugin and im trying to use with express. I want to manipulate the camera from mobile app as client. The problem is that i need to connect multiple devices at the same time, depend of the client and available ip for his user. I want to know if it is possible and if there is some way to handle this situation. I recently was trying to return the cam instance as response, but later i cannot get uri.

var onvif = require('onvif');
var app = require('express')();
var bodyParser = require('body-parser')
var port = 3000

app.use(function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});
app.use(bodyParser.json());

app.get('/connect',(req,res)=>{
    var body = req.body;
    var camData = body['camData'];
    return new onvif.Cam({
        hostname: camData.ip,
        port:camData.port,
        username: camData.username,
        password: camData.password
      }, function(err) {
        return res.send(this);
      });
})
app.post('/getUri',(req,res)=>{
    var body = req.body;
    var cam = body['cam'];
    console.log(cam);
    cam.getStreamUri({}, function(err, stream) {
        return res.send(stream);
    })
})
app.listen(port, console.log);

Another question is, do i need to connect in every request? is the only solution? Im really noob at working with ONVIF protocol, so sorry if im not explaining well, but thank you anyway!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Motoralfacommented, Sep 7, 2020

@Motoralfa you can use a Map in which you can store different cams and in the /getUri endpoint send also info which cam you want. Or store in the user’s session this info. Or something else… I don’t know your current case. Simple example with host-port info:

...
let cams = {};

app.get('/connect',(req,res)=>{
   ...
    cams[`${camData.ip}-${camData.port}`] = new onvif.Cam({
        hostname: camData.ip,
        port: camData.port,
        username: camData.username,
        password: camData.password
    }, function(err) {
      if (err) {return res.send(err.message);}
      res.send('connected');
    });
});
...
app.post('/getUri', (req,res)=>{
    var body = req.body;
    const camData = body.camData;
    const camInfo = `${camData.ip}-${camData.port}`;
    const cams = cams[camInfo]; 
    if (!cam) {
      return res.send('not connected');
    }
    cam.getStreamUri({}, function(err, stream) {
        return res.send(stream);
    });
})

Thank you so much @agsh . Seriously this help me a lot, i was thinking in implement something like that but i didnt know if it was right. Now i have an idea about how to face the situation! 👍

1reaction
agshcommented, Sep 7, 2020

@Motoralfa you can use a Map in which you can store different cams and in the /getUri endpoint send also info which cam you want. Or store in the user’s session this info. Or something else… I don’t know your current case. Simple example with host-port info:

...
let cams = {};

app.get('/connect',(req,res)=>{
   ...
    cams[`${camData.ip}-${camData.port}`] = new onvif.Cam({
        hostname: camData.ip,
        port: camData.port,
        username: camData.username,
        password: camData.password
    }, function(err) {
      if (err) {return res.send(err.message);}
      res.send('connected');
    });
});
...
app.post('/getUri', (req,res)=>{
    var body = req.body;
    const camData = body.camData;
    const camInfo = `${camData.ip}-${camData.port}`;
    const cams = cams[camInfo]; 
    if (!cam) {
      return res.send('not connected');
    }
    cam.getStreamUri({}, function(err, stream) {
        return res.send(stream);
    });
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Connect Multiple Bluetooth Devices At Once: 5+1+1
Bluetooth multipoint allows a single peripheral, typically a headset, to connect to multiple primary Bluetooth devices like two smartphones. The two host ...
Read more >
How to Connect Multiple Bluetooth Speakers to One Device
Go to Settings > Connections > Bluetooth. · Tap Advanced. · Turn on the Dual Audio toggle switch. · To use Dual Audio,...
Read more >
How to Connect Multiple Bluetooth Speakers ... - Gadgets 360
Android users need to go to Bluetooth Settings and pair either Bluetooth headphones or speakers one by one. Once connected, tap the three-dot ......
Read more >
What is Bluetooth multipoint, and why isn't it more popular?
It's a feature that allows a single Bluetooth headset to maintain simultaneous connections to at least two source devices like a laptop and ......
Read more >
How to Connect Multiple Bluetooth Audio Devices on Windows
How to Connect Multiple Bluetooth Audio Devices on Windows ; Press Win + X and select Device Manager from the options. Double-click on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found