Code Monkey home page Code Monkey logo

node-owfs's Introduction

Build Status Dependencies

owfs

A node.js client library for 1-wire devices using the owserver protocol. Requires node.js version 6 or higher.

Installation

npm install owfs

Examples

First of all you need a connection to a running owserver:

var Client = require("owfs").Client
var con = new Client(HOST, PORT)

or with the default port 4304

var con = new Client(HOST)

read

Reads a value of the specified path and passes the value to the callback.

con.read("/10.E89C8A020800/temperature", function(err, result){
  console.log(result)
})

write

Writes a value to the specified path and passes the raw owserver message to the callback.

con.write("/3A.378A06000000/PIO.A", 1, function(err, message){
  console.log(message)
})

Directory listings

According to OWFS message types there are several methods for directory listings. They all have the same argument list but behave a bit different.

Lists all the children of the supplied path as an array passed to the callback.

con.dir("/", function(err, directories){
  console.log(directories)
})

con.dirall("/", function(err, directories){
  console.log(directories)
})

con.get("/", function(err, directories){
  console.log(directories)
})

con.dirallslash("/", function(err, directories){
  console.log(directories)
})

con.getslash("/", function(err, directories){
  console.log(directories)
})

Device presence

Check the availability of a single 1-Wire device using the OWFS message type presence. The device presence state is reported as boolean to the callback.

con.presence("/81.365F2F000000", function(err, result){
  console.log(result)
})

Debug

We are using debug for debugging output. If you are using owfs in your app, start the debug mode like this:

DEBUG=owfs* node app.js

owserver Documentation

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/njh/node-owfs.

Please run npm test before submitting a Pull Request and add tests for any new functionality.

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

node-owfs's People

Contributors

benediktarnold avatar colinl avatar giermann avatar njh avatar vaceslav avatar waldbaer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-owfs's Issues

Getting empty strings back when no devices

Hello,

I have just been testing the behaviour of making requests to owserver, when there are no devices connected. I have been doing this using the fake parameter to owserver:

owserver \
  --foreground \
  --Celsius \
  --error_print=2 \
  --error_level=4 \
  --fake=

I am not expecting any "device: " or "property: " lines being logged, but I am:

var owfs = require("owfs");
var async = require("async");


var client = new owfs.Client('localhost', 4304);
client.dirall("/",function(error, directories) {
    if (!error) {
        async.mapSeries(directories,
            function(directory,cb) {
                client.dirall(directory,cb);
            },
            function(error, results) {
                if (!error) {
                    results.forEach(function(device) {
                        console.log("device: "+device);
                        device.forEach(function(property) {
                            console.log("property: "+property);
                        });
                    });
                } else {
                    console.log("Failed to get properties for device: "+error);
                }
                console.log("Done.");
            }
        );
    } else {
        console.log("Failed to get list of devices: "+error);
    }
});

Is this due to a protocol parse error, or is owserver sending back bad data?

Writing Error on port XX.XXXXX/PIO.BYTE on DS2408

When I try to write on my DS2408 port, this raises an error :
owfs:communication Sending { path: '/29.09491D000000/PIO.BYTE\u00004',
command: 3,
data_len: 1,
server: 'raspberrypi.local',
port: '4304' } +25s
owfs:communication Receiving header { version: 0,
payload: 0,
ret: -22,
controlflags: 32,
size: 0,
offset: 0 } +371ms
owfs:communication Receiving payload +1ms

If I modify as follow the file owfs, it works:
Client.prototype.write = function(path, payload, callback) {
var command;

  command = {
    path: path + "\u0000" + payload,
    command: 3,
    //data_len: payload.toString().length,
    data_len: payload.toString().length+1, // modification performed
    server: this.server,
    port: this.port
  };
  return this.communication.sendCommand(command, callback);

Question 1 : has it been tested with something to write larger than a byte ?
Comment: I did not fully understand the code here so I am pretty sure the fix here is not properly done, do you have any advice ?

tks,
jean

problems with concurrent requests to same device

Hi
I am loopeing thru a dir request in order to analyse the devices.
if a DS1820 I wnat to read the temperature which goes well

if a DS2438 I want to read Temperature, VAD and VDD - this will fail with the following:

/volume2/server/owfstest/node_modules/owfs/build/base/convert.js:19
result = messageToUse.payload.replace(new RegExp(" ", "g"), "");
^
TypeError: Cannot call method 'replace' of undefined
at /volume2/server/owfstest/node_modules/owfs/build/base/convert.js:19:39
at callbackOnce (/volume2/server/owfstest/node_modules/owfs/build/base/communication.js:23:9)
at Socket. (/volume2/server/owfstest/node_modules/owfs/build/base/communication.js:32:14)
at Socket.emit (events.js:117:20)
at _stream_readable.js:943:16

at process._tickCallback (node.js:419:13)

What can I do?

I tried to make the read for VAD inside the result for Temerature read result and the read for VDD inside the read for VAD result in order to prevent multible outstanding requests for one device, but it still fails

Attempting to read devices that do not respond

Hi,

In my application occasionally my devices are not responding to read requests (bad wiring I think or someone unplugs a device).

The read callback does not execute (no error parameter either to check if it did). I see this on the console:

[TypeError: Cannot read property 'payload' of undefined]

I would really like if the callback always get's called, with an err parameter to check.

Thanks!

Todd

Convert string responses to native int/float

This is what I was doing in node-red-contrib-owfs:

    function parseResult(result) {
        if (typeof result.match === 'function') {
            if (result.match(/^\-?\d+\.\d+$/)) {
                return parseFloat(result);
            } else if (result.match(/^\-?\d+$/)) {
                return parseInt(result);
            }
        }
        return result;
    }

Incorrect decoding of 1-wire message

Occasionally, requesting a temperature from a remote ds18b20, I see a result that is a string containing, in hex, stuff like:
30 0 0 0 0 0 0 0 c 0 0 0 c 0 0 0 0 0 0 c 0 0 0 0 33 37 2e 31 38 37 35
The sensor value is actually the bit on the end (37.1875) and the start appears to be from the 1-wire protocol header.
Having enabled debug for the convert module I see, leading up to the error:

owfs:convert Checking Header payload > 0 -1 +0ms
owfs:convert Checking Header payload > 0 12 +0ms
owfs:convert message to use [ { header:
{ version: 0,
payload: 12,
ret: 12,
controlflags: 32,
size: 12,
offset: 0 },
payload: ' 42.125' } ] +0ms
owfs:convert Received multiple messages in simple read [ { header:
{ version: 0,
payload: -1,
ret: 0,
controlflags: 0,
size: 0,
offset: 0 },
payload: '' },
{ header:
{ version: 0,
payload: 12,
ret: 12,
controlflags: 32,
size: 12,
offset: 0 },
payload: ' 21.125' } ] +10s
owfs:convert Checking Header payload > 0 -1 +0ms
owfs:convert Checking Header payload > 0 12 +0ms
owfs:convert message to use [ { header:
{ version: 0,
payload: 12,
ret: 12,
controlflags: 32,
size: 12,
offset: 0 },
payload: ' 21.125' } ] +0ms
owfs:convert message to use [ { header:
{ version: 0,
payload: -1,
ret: 0,
controlflags: 0,
size: 0,
offset: 0 },
payload: '\u0000\u0000\u0000\u0000\u0000\u0000\u0000\f\u0000\u0000\u0000\f\u0000\u0000\u0000 \u0000\u0000\u0000\f\u0000\u0000\u0000\u0000 42.5' } ] +124ms

Feature request: readRaw()

Thank you for your great library!
I want to read the whole scratchpad from an OneWire sensor, for example:

con.read("/28.0000B218DA00/scratchpad", function(err, result){
  console.log( result )
})

Sadly, your library converts every payload to an utf8-String. Because the scratchpad of the sensor is no valid utf-8 string, some bytes get lost and there is no way to get them back.
It would be great if there is a function like "readRaw", which bypassed every string conversion in the code.

Background:
I simulate a DS18B20 with an arduino and the library "OneWireHub" and modify some bytes to get a higher resolution. To get this modifed bytes, I need to read the full "scratchpad".

Obsolete files

It seems, that /build/debug.js and /build/debug.js.map are old debug/test files...
If I'm right, please remove those or renew them from 'winston' to 'debug' and maybe move them to test.

Occational bug reading payload.

It might be from calling dirall, dir or read. I haven't found out which one yet.
The error message is...

/node_modules/owfs/owfs.js:105
var result = messageToUse[0].payload.replace(new RegExp(" ", "g"), "")
^
TypeError: Cannot read property 'payload' of undefined
at Client.read (/node_modules/owfs/owfs.js:1
at Socket. (/node_modules/owfs/ow
at Socket.EventEmitter.emit (events.js:93:17)
at TCP.onread (net.js:418:51)

problem getting device id in callback

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!
        });
}

buffertools is not defined

I get this error:

/home/per/projects/senserve/node_modules/owfs/owfs.js:53
        var bres = buffertools.concat(msg, path+'\x00');
                   ^
ReferenceError: buffertools is not defined
    at Socket.<anonymous> (/home/per/projects/senserve/node_modules/owfs/owfs.js:53:20)
    at Socket.g (events.js:192:14)
    at Socket.EventEmitter.emit (events.js:93:17)
    at Object.afterConnect [as oncomplete] (net.js:752:10)

I suppose it's because buffertools is required at the beginning of the file but not assigned to a variable...

'write' return -90

this code always return "-90", is it correct?
ubuntu 12.04, owfs 2.8p13

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

con.write("/05.000005FA0100/PIO",1,function(message){
    console.log(message)
})

drago@ubuntu:~$ node test.js The "sys" module is now called "util". It should have a similar interface. [ { header: { version: 0, payload: 0, ret: -90, controlflags: 32, size: 0, offset: 0 }, payload: '' } ]

Received multiple messages in simple read from temperature sensor DS18B20

Hi
I'm trying to use owfs with a DS18B20 connected to a RPI2 I2C to 1-Wire Host Adapter from http://www.sheepwalkelectronics.co.uk which is based around a DS2482-100 I2C to 1-Wire IC.

If I list the devices I can see the temperature sensor 28.94083F040000

I'm using the code:

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

con.read("/28.94083F040000/temperature", function(err, result){
    console.log(result);
})

Running this with debug I can see this error:

 owfs:communication Sending[object Object] +0ms
  owfs:communication Receiving header [object Object] +1s
  owfs:communication Receiving payload  +7ms
  owfs:communication Receiving header [object Object] +87ms
  owfs:communication Receiving payload        22.25 +2ms
  owfs:convert Received multiple messages in simple read[object Object],[object Object] +29ms

/home/pi/node_modules/owfs/build/base/convert.js:19
        result = messageToUse.payload.replace(new RegExp(" ", "g"), "");
                                      ^
TypeError: Cannot call method 'replace' of undefined
    at /home/pi/node_modules/owfs/build/base/convert.js:19:39
    at callbackOnce (/home/pi/node_modules/owfs/build/base/communication.js:23:9)
    at Socket.<anonymous> (/home/pi/node_modules/owfs/build/base/communication.js:32:14)
    at Socket.emit (events.js:117:20)
    at _stream_readable.js:943:16
    at process._tickCallback (node.js:419:13)

By adding some console.log's I can see the two message's contain this:

Message Length: 2
{ header:
   { version: 0,
     payload: -1,
     ret: 0,
     controlflags: 0,
     size: 0,
     offset: 0 },
  payload: '' }
--------
{ header:
   { version: 0,
     payload: 12,
     ret: 12,
     controlflags: 32,
     size: 12,
     offset: 0 },
  payload: '      22.375' }

Do you have idea idea why I'm seeing two messages back from the sensor? Thanks

Write support not in npm

I have been trying to implement support for writing in my node-red-contrib-owfs package but was unable to get it to work.

I have since found that the fixes that @Giermann provided in #14 and #15 fixed the CoffeeScript but not in the generated JavaScript.

I also don't think there has been release to npm since the fixes were applied?

Directory Listing Payload

The current directory listing code has an example owserver response payload of:

var payloadResult = '/01.A7F1D92A82C8\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0011\u0000\u0000\u0000\u0000\u0000\u0000\u0000 \u0000\u0000\u0000\u0010\u0000\u0000\u0000\u0000/10.D8FE434D9855\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0011\u0000\u0000\u0000\u0000\u0000\u0000\u0000 \u0000\u0000\u0000\u0010\u0000\u0000\u0000\u0000/22.8CE2B3471711\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0011\u0000\u0000\u0000\u0000\u0000\u0000\u0000 \u0000\u0000\u0000\u0010\u0000\u0000\u0000\u0000/29.98542F112D05\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 \u0000\u0000\u0000\u0000\u0000\u0000';

I have never seen anything like this in a real-world situation. And from reading the protocol documentation - I can't se how it is a valid response.

@colinl have you ever seen anything like this in the payload field?

If not, I think I will remove the code and tests that handle this.
I have a feeling that it was due to a bug elsewhere in the code.

Listing all devices and properties

Hello,

I am trying to create a complete list of devices and the properties for each device. However I am having problems with second call to dirall throwing EADDRNOTAVAIL.

Here is my code:

var owfs = require("owfs");
var async = require("async");

var client = new owfs.Client('localhost');
client.dirall("/",function(error, directories){
    if (!error) {
        console.log("Top no error");
        async.mapSeries(directories, client.dirall,
            function(error, results) {
                console.log("Inner Error: "+error);
                console.log("Inner Results: "+results);
            }
        );
    } else {
        console.log("Top error: "+error);
    }
});

This is the output:

Top no error
error:  code=EADDRNOTAVAIL, errno=EADDRNOTAVAIL, syscall=connect
Inner Error: Error: connect EADDRNOTAVAIL
Inner Results: 

I am using async.mapSeries in an attempt to make a sequence of calls to owserver and avoid making a large number of calls in parallel.

My theory is the while each call to dirall should be creating its own socket and connection to owserver. By the time the second socket is created, the first socket is only half-closed and node.js tries to re-use the same local port and then fails.

Can you suggest a solution to this problem?

Thanks,

nick.

Hardware Information

I'm a little bewildered on how to get started. There is nothing listed about hardware. Was this built for one specific platform like raspberry pie? Or not? I bought an intel edison with the minibreakout board. Would it work out of the box?

Automatic retry on read error

Hello,

I used your owfs library inside my Node-RED owfs node:
https://github.com/njh/node-red-contrib-owfs

I have a feature request to perform automatic retries, when there is an occasional error - in particular when the special value of 85 is read from a DS18B20. I have been trying to seperate concerns, and only include code relating to Node-RED and its UI in my module

Would you accept a Pull Request that adds this functionality to your library?
It would involve somehow passing a retries and interval values, when creating the client.

nick.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.