1+ // #########################################################################
2+ // class definition
3+ // #########################################################################
4+
5+ var ReadyNAS = function ( buffer ) {
6+ if ( ! buffer instanceof Buffer ) throw new Error ( 'RAIDar buffer message required!' ) ;
7+
8+ this . buffer = buffer ;
9+ this . message = buffer . toString ( 'utf8' ) ;
10+
11+ this . header = null ;
12+
13+ this . mac = null ;
14+ this . hostname = null ;
15+ this . ip = null ;
16+
17+ this . info = { } ;
18+
19+ this . initInfo ( ) ;
20+
21+ return this ;
22+ } ;
23+
24+
25+
26+ // #########################################################################
27+ // private
28+ // #########################################################################
29+
30+ ReadyNAS . prototype . initInfo = function ( ) {
31+ this . initHeaderInfo ( ) ;
32+ this . initBodyInfo ( ) ;
33+ this . initFooterInfo ( ) ;
34+ } ;
35+
36+ ReadyNAS . prototype . initHeaderInfo = function ( ) {
37+ this . header = this . message . substr ( 0 , 28 ) ;
38+
39+ var self = this
40+ , firstLine = this . message . substring ( 28 , this . message . indexOf ( '\n' ) )
41+ , firstInfo = firstLine . split ( '\t' )
42+ , otherInfo = firstInfo . slice ( 3 ) ;
43+
44+ this . mac = firstInfo [ 0 ] ;
45+ this . hostname = firstInfo [ 1 ] ;
46+ this . ip = firstInfo [ 2 ] ;
47+
48+ otherInfo . forEach ( function ( info ) {
49+ self . parseInfo ( info ) ;
50+ } ) ;
51+ } ;
52+
53+ ReadyNAS . prototype . initBodyInfo = function ( ) {
54+ var self = this
55+ , lines = this . message . split ( '\n' ) . slice ( 1 ) ;
56+
57+ lines . forEach ( function ( line ) {
58+ if ( line [ 0 ] === '\t' ) return ;
59+ self . parseInfo ( line ) ;
60+ } ) ;
61+ } ;
62+
63+ ReadyNAS . prototype . initFooterInfo = function ( ) {
64+
65+ // TODO write footer data initialization
66+
67+ } ;
68+
69+ ReadyNAS . prototype . parseInfo = function ( str ) {
70+ var arr = str . split ( '!!' )
71+ , name = arr [ 0 ]
72+ , info = { _info : arr [ 1 ] } ;
73+
74+ if ( ! this . info [ name ] ) this . info [ name ] = [ ] ;
75+ if ( ! arr [ 2 ] ) return this . info [ name ] . push ( info ) ;
76+
77+ var otherInfo = arr [ 2 ] . split ( '::' ) ;
78+
79+ otherInfo . forEach ( function ( str ) {
80+ var arr = str . split ( '=' )
81+ , field = arr [ 0 ]
82+ , value = arr [ 1 ] ;
83+
84+ if ( value ) {
85+ info [ field ] = value ;
86+ } else {
87+ info [ '_status' ] = field ;
88+ }
89+ } ) ;
90+
91+ return this . info [ name ] . push ( info ) ;
92+ } ;
93+
94+
95+
96+ // #########################################################################
97+ // public
98+ // #########################################################################
99+
100+ ReadyNAS . prototype . toJSON = function ( string ) {
101+ var res = {
102+ message : this . message ,
103+ mac : this . mac ,
104+ hostname : this . hostname ,
105+ ip : this . ip ,
106+ info : this . info
107+ } ;
108+
109+ return string === true ? JSON . stringify ( res ) : res ;
110+ } ;
111+
112+
113+
114+ // #########################################################################
115+ // export
116+ // #########################################################################
117+
118+ module . exports = ReadyNAS ;
0 commit comments