Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem getting device id in callback #34

Open
palsbo opened this issue Apr 15, 2018 · 5 comments
Open

problem getting device id in callback #34

palsbo opened this issue Apr 15, 2018 · 5 comments

Comments

@palsbo
Copy link

palsbo commented Apr 15, 2018

When I want to issue a serie of reads to several devices, the callback gives no way of telling which device is delivering result.
What to do?

var Client = require("owfs").Client;
var con = new Client('localhost');

var dev = [
        {'id':'/10.243EE400080086/temperature', 'topic':'/lolland/stuen'},
        {'id':'/10.8E6521010800E3/temperature', 'topic':'/lolland/vaerksted'},
        {'id':'/10.FD622101080073/temperature', 'topic':'/lolland/orangeri'},
        {'id':'/10.AFCE3503080058/temperature', 'topic':'/lolland/depot'},
]

for (i in dev) {
        con.read(dev[i].id, function(err, result) {
               console.log(dev[i].id,dev[i].topic, result);  //  this will not work!
        });
}
@njh
Copy link
Owner

njh commented Apr 16, 2018

Hi,

You say "this will not work" - does it not work?
Or do you just think it won't won't?

I think it should work - in JavaScript dev[i].topic should be in scope within that callback.

It probably doesn't help, because it is a bit complex but this is the code I wrote that uses the owfs.js library:

https://github.com/njh/node-red-contrib-owfs/blob/master/owfs.js

nick.

@njh
Copy link
Owner

njh commented Apr 16, 2018

I havn't tested it but something like this should work:

function lookupTemp(hostname, path) {

    var Client = require('owfs').Client
    var client = new Client(hostname, 4304)

    client.read(path, function (err, result) {
        if (err) {
            console.log(err)
        } else {
            console.log("path: " + path)
            console.log("result: " + result)
        }
    })

}

@palsbo
Copy link
Author

palsbo commented Apr 16, 2018

No it does not work!. It works for one device, but when issuing read for several devices, the path in the callback will be the last used path - for all events. The path must be delivered as an argument in the callback to identify the path.

@njh
Copy link
Owner

njh commented Apr 16, 2018 via email

@palsbo
Copy link
Author

palsbo commented Apr 16, 2018

See this example:

var Client = require('owfs').Client
var client = new Client('localhost', 4304)

var dev = [
	{'id':'/10.243EE400080086/temperature', 'topic':'/lolland/stuen'},
	{'id':'/10.8E6521010800E3/temperature', 'topic':'/lolland/vaerksted'},
	{'id':'/10.FD622101080073/temperature', 'topic':'/lolland/orangeri'},
	{'id':'/10.AFCE3503080058/temperature', 'topic':'/lolland/depot',},
]

function lookupTemp(path) {
    client.read(path, function (err, result) {
        if (err) {
            console.log(err)
        } else {
            console.log("path: " + path)
            console.log("result: " + result)
        }
    })
}
for (i in dev) {
	lookupTemp(dev[i].id);
}

The result is (correct!);

path: /10.243EE400080086/temperature
result: 19.1875
path: /10.FD622101080073/temperature
result: 16.375
path: /10.8E6521010800E3/temperature
result: 16.5625
path: /10.AFCE3503080058/temperature
result: 19.875

Then changing the code like this:

var Client = require('owfs').Client
var client = new Client('localhost', 4304)

var dev = [
	{'id':'/10.243EE400080086/temperature', 'topic':'/lolland/stuen'},
	{'id':'/10.8E6521010800E3/temperature', 'topic':'/lolland/vaerksted'},
	{'id':'/10.FD622101080073/temperature', 'topic':'/lolland/orangeri'},
	{'id':'/10.AFCE3503080058/temperature', 'topic':'/lolland/depot',},
]


for (i in dev) {
	var path = dev[i].id;
    client.read(path, function (err, result) {
        if (err) {
            console.log(err)
        } else {
            console.log("path: " + path)
            console.log("result: " + result)
        }
    })
}

The result is (I expected a duplication of the old result):

path: /10.AFCE3503080058/temperature
result: 19.1875
path: /10.AFCE3503080058/temperature
result: 16.625
path: /10.AFCE3503080058/temperature
result: 19.875
path: /10.AFCE3503080058/temperature
result: 16.25

```

Results are correct, buit path is duplicated.
I understand that this is async code. This is why you cannot count on the initial values in the callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants