@@ -12,11 +12,41 @@ var logger = Logger.get('hyperdeck.HyperdeckCore');
1212 * Allows you to make requests to the hyperdeck and get its parsed responses.
1313 * This chains the requests so only one is sent at a time.
1414 * You can also listen for asynchronous events sent from the hyperdeck.
15- * @param ip, The IP address of the hyperdeck.
15+ * @param config hyperdeck configuration
16+ * config = 'ip address string[string]'
17+ * or
18+ * config = {
19+ * ip: 'ip address string[string]',
20+ * [ pingInterval: ping interval in miliseconds[int][default = 10000] ]
21+ * }
1622 **/
17- function HyperdeckCore ( ip ) {
23+ function HyperdeckCore ( config ) {
24+
25+ /**
26+ * validate configuration
27+ *
28+ * @param {* } config
29+ */
30+ function isConfigValid ( config ) {
31+ return (
32+ config !== undefined &&
33+ config !== null &&
34+ ( typeof config === 'string' || ! ! config . ip )
35+ ) ;
36+ }
37+
38+ // check for valid configuration
39+ if ( ! isConfigValid ( config ) ) {
40+ throw new Error ( 'Invalid Configuration, please refer to documentations.' ) ;
41+ }
42+
1843 var self = this ;
1944
45+ if ( typeof config === 'string' ) {
46+ config = { ip : config } ;
47+ }
48+ config = Object . assign ( { pingInterval : 10000 } , config ) ;
49+
2050 function onConnectionStateChange ( state ) {
2151 if ( ! state . connected ) {
2252 publicNotifier . emit ( 'connectionLost' ) ;
@@ -36,7 +66,9 @@ function HyperdeckCore(ip) {
3666 connecting = false ;
3767 registerAsyncResponseListener ( ) ;
3868 notifier . emit ( 'connectionStateChange' , { connected : true } ) ;
39- pingTimerId = setInterval ( ping , 10000 ) ;
69+ if ( config . pingInterval > 0 ) {
70+ pingTimerId = setInterval ( ping , config . pingInterval ) ;
71+ }
4072 // a request might have been queued whilst the connection
4173 // was being made
4274 performNextRequest ( ) ;
@@ -197,7 +229,7 @@ function HyperdeckCore(ip) {
197229 notifier . on ( 'connectionStateChange' , onConnectionStateChange ) ;
198230
199231 var client = net . connect ( {
200- host : ip ,
232+ host : config . ip ,
201233 port : 9993
202234 } , function ( ) {
203235 logger . info ( 'Socket connected.' ) ;
@@ -206,7 +238,8 @@ function HyperdeckCore(ip) {
206238 handleConnectionResponse ( ) ;
207239 } ) ;
208240 client . setEncoding ( 'utf8' ) ;
209- client . on ( 'error' , function ( e ) {
241+
242+ client . on ( 'error' , function ( e ) {
210243 logger . warn ( 'Socket error.' , e ) ;
211244 } ) ;
212245 // when the connection closes handle this
0 commit comments