55var 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