Skip to content

Commit 7e65cfc

Browse files
author
greguz
committed
updated README and package.json
fix to readynas.js
1 parent 50b03fb commit 7e65cfc

4 files changed

Lines changed: 92 additions & 72 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.idea
3-
dump
3+
dump
4+
npm-debug.log

README.md

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
# node-raidar: a node.js NetGear RADIar library
1+
# node-raidar: a node.js NetGear RAIDar library
22

33
`node-raidar` is a simple to use NetGear RAIDar implementation for node.js.
44

5-
# Example
5+
## Installing
6+
7+
```
8+
npm install --save raidar
9+
```
10+
11+
## Get device list
612

713
``` js
814
var raidar = require('raidar');
@@ -16,38 +22,51 @@ raidar.request(10000, function(err, devices) {
1622
});
1723
```
1824

19-
# Methods
25+
## Event definition
26+
27+
``` js
28+
var raidar = require('raidar');
29+
30+
// set event for particular device
31+
raidar.on('192.168.1.160', function(device) {
32+
console.log('Response from:', device.ip, device.mac, device.hostname);
33+
});
34+
35+
raidar.request();
36+
```
37+
38+
## Methods
2039

2140
``` js
22-
var raidar = require('raidar')
41+
var raidar = require('raidar');
2342
```
2443

25-
## raidar.open(opt:Object, cb:Function)
44+
### raidar.open(opt:Object, cb:Function)
45+
46+
Open or reset the UDP socket with given `opt` options and fire `cb` callback when socket is open. All arguments are optional.
2647

27-
Open new UDP socket with given `opt` options and fire `cb` callback when socket is open.
28-
All arguments are optional.
2948
Default options:
3049
- `socketType` : socket type to use ('udp4' or 'udp6'), default `udp4`
3150
- `portToListen` : port to listen, default `57877`
3251
- `targetHost` : target host to send "request info" packet, default `255.255.255.255`
3352
- `targetPort` : target port to send "request info" packet, default `22081`
3453

35-
## raidar.close()
54+
### raidar.close()
3655

3756
Close UDP socket.
3857

39-
## raidar.request(timeout:Number, callback:Function)
58+
### raidar.request(timeout:Number, callback:Function)
4059

4160
Send "request info" broadcast packet.
4261
If at least `callback` function is passed, it will execute after `timeout` ms, default 5000 ms.
4362
The event fires with `callback(err, devices)`.
4463
`err` is set in case of errors and `devices` is an array of all devices found.
4564

46-
## raidar.on(event:String, callback:Function)
65+
### raidar.on(event:String, callback:Function)
4766

4867
Set an event callback.
4968

50-
## raidar.once(event:String, callback:Function)
69+
### raidar.once(event:String, callback:Function)
5170

5271
Set an event callback that execute only one time.
5372

@@ -75,18 +94,22 @@ This event fires with `callback(err)` where `err` is an instance of `Error` clas
7594
Fail to parse response data.
7695
This event fires with `callback(msg, err)` where `msg` is the response buffer and `err` is an instance of `Error` class.
7796

78-
# ReadyNAS class
97+
## ReadyNAS class
7998

80-
...
99+
It is the representation of a ReadyNAS device.
81100

82-
## Property
101+
### device.ip()
83102

84-
...
103+
Return device IP address.
85104

86-
## Methods
105+
### device.hostname()
106+
107+
Return Device Hostname.
108+
109+
### device.mac()
87110

88-
...
111+
Return device MAC address.
89112

90-
# License
113+
## License
91114

92115
MIT

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
"keywords": [
2626
"netgear",
2727
"raidar",
28-
"socket"
28+
"readynas"
2929
]
3030
}

readynas.js

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
var ReadyNAS = function(buffer) {
66
if (!buffer instanceof Buffer) throw new Error('RAIDar buffer message required!');
77

8-
this.buffer = buffer;
9-
this.message = buffer.toString('utf8');
8+
this._buffer = buffer;
9+
this._message = buffer.toString('utf8');
1010

11-
this.header = null;
11+
this._header = null;
1212

13-
this.mac = null;
14-
this.hostname = null;
15-
this.ip = null;
13+
this._mac = null;
14+
this._hostname = null;
15+
this._ip = null;
1616

17-
this.info = {};
18-
19-
this.init();
17+
this._data = {};
18+
this._info = {};
2019

20+
this._init();
2121
return this;
2222
};
2323

@@ -27,52 +27,57 @@ var ReadyNAS = function(buffer) {
2727
// private
2828
// #########################################################################
2929

30-
ReadyNAS.prototype.init = function() {
31-
this.initHeaderInfo();
32-
this.initBodyInfo();
33-
this.initFooterInfo();
30+
ReadyNAS.prototype._init = function() {
31+
this._initHeaderData();
32+
this._initBodyData();
33+
this._initFooterData();
3434
};
3535

36-
ReadyNAS.prototype.initHeaderInfo = function() {
37-
this.header = this.message.substr(0, 28);
36+
ReadyNAS.prototype._initHeaderData = function() {
37+
this._header = this._message.substr(0, 28);
3838

3939
var self = this
40-
, firstLine = this.message.substring(28, this.message.indexOf('\n'))
40+
, firstLine = this._message.substring(28, this._message.indexOf('\n'))
4141
, firstInfo = firstLine.split('\t')
4242
, otherInfo = firstInfo.slice(3);
4343

44-
this.mac = firstInfo[0];
45-
this.hostname = firstInfo[1];
46-
this.ip = firstInfo[2];
44+
this._mac = firstInfo[0];
45+
this._hostname = firstInfo[1];
46+
this._ip = firstInfo[2];
4747

48-
otherInfo.forEach(function(info) {
49-
self.parseInfo(info);
48+
otherInfo.forEach(function(ini) {
49+
var info = self._parseInfo(ini);
50+
if (!self._data[info.name]) self._data[info.name] = [];
51+
self._data[info.name].push(info);
5052
});
5153
};
5254

53-
ReadyNAS.prototype.initBodyInfo = function() {
55+
ReadyNAS.prototype._initBodyData = function() {
5456
var self = this
55-
, lines = this.message.split('\n').slice(1);
57+
, lines = this._message.split('\n').slice(1);
5658

5759
lines.forEach(function(line) {
5860
if (line[0] === '\t') return;
59-
self.parseInfo(line);
61+
62+
var info = self._parseInfo(line);
63+
if (!self._data[info.name]) self._data[info.name] = [];
64+
self._data[info.name].push(info);
6065
});
6166
};
6267

63-
ReadyNAS.prototype.initFooterInfo = function() {
64-
65-
// TODO write footer data initialization
68+
ReadyNAS.prototype._initFooterData = function() {
69+
var lines = this._message.split('\n')
70+
, line = lines[ lines.length - 2 ];
6671

72+
this._info = this._parseInfo(line.split('\t').join(''));
6773
};
6874

69-
ReadyNAS.prototype.parseInfo = function(str) {
70-
var arr = str.split('!!')
71-
, name = arr[0]
72-
, info = { _info : arr[1] };
75+
ReadyNAS.prototype._parseInfo = function(str) {
76+
var arr = str.trim().split('!!')
77+
, res = { name: arr[0], _status : arr[1] };
7378

74-
if (!this.info[name]) this.info[name] = [];
75-
if (!arr[2]) return this.info[name].push(info);
79+
if (!arr[2])
80+
return res;
7681

7782
var otherInfo = arr[2].split('::');
7883

@@ -82,13 +87,13 @@ ReadyNAS.prototype.parseInfo = function(str) {
8287
, value = arr[1];
8388

8489
if (value) {
85-
info[field] = value;
90+
res[field] = value;
8691
} else {
87-
info['_status'] = field;
92+
res['_status'] = field;
8893
}
8994
});
9095

91-
return this.info[name].push(info);
96+
return res;
9297
};
9398

9499

@@ -97,25 +102,16 @@ ReadyNAS.prototype.parseInfo = function(str) {
97102
// public
98103
// #########################################################################
99104

100-
ReadyNAS.prototype.disk = function(index) {
101-
if (!this.info['disk']) return;
102-
103-
if (typeof index === 'number')
104-
return this.info['disk'][index];
105-
else
106-
return this.info['disk'];
105+
ReadyNAS.prototype.ip = function() {
106+
return this._ip;
107107
};
108108

109-
ReadyNAS.prototype.toJSON = function(string) {
110-
var res = {
111-
message : this.message,
112-
mac : this.mac,
113-
hostname : this.hostname,
114-
ip : this.ip,
115-
info : this.info
116-
};
109+
ReadyNAS.prototype.hostname = function() {
110+
return this._hostname;
111+
};
117112

118-
return string === true ? JSON.stringify(res) : res;
113+
ReadyNAS.prototype.mac = function() {
114+
return this._mac;
119115
};
120116

121117

0 commit comments

Comments
 (0)