11/* jshint -W097 */ // jshint strict:false
22/*jslint node: true */
33
4+ // This is file, that makes all communication with controller. All options are optional except name.
5+ // following options are available:
6+ // name: name of the adapter. Must be exactly the same as directory name.
7+ // dirname: adapter directory name
8+ // instance: instance number of adapter
9+ // objects: true or false, if desired to have oObjects. This is a list with all states, channels and devices of this adapter and it will be updated automatically.
10+ // states: true or false, if desired to have oStates. This is a list with all states values and it will be updated automatically.
11+ // systemConfig: if required system configuration. Store it in systemConfig attribute
12+ // objectChange: callback function (id, obj) that will be called if object changed
13+ // stateChange: callback function (id, obj) that will be called if state changed
14+ // message: callback to inform about new message the adapter
15+ // unload: callback to stop the adapter
16+ // config: configuration of the connection to controller
17+ // noNamespace: return short names of objects and states in objectChange and in stateChange
18+
419var net = require ( 'net' ) ;
520var fs = require ( 'fs' ) ;
621var extend = require ( 'node.extend' ) ;
@@ -376,28 +391,39 @@ function Adapter(options) {
376391 }
377392 that . inited = true ;
378393
379- initStates ( function ( ) {
380- that . getForeignState ( 'system.adapter.' + that . namespace + '.alive' , function ( err , res ) {
381- if ( options . instance !== undefined ) {
382- initAdapter ( options ) ;
383- } else
384- if ( ! isInstall && res && res . val === true ) {
385- logger . error ( options . name + '.' + instance + ' already running' ) ;
386- process . exit ( 7 ) ;
387- } else {
388- that . getForeignObject ( 'system.adapter.' + that . namespace , function ( err , res ) {
389- if ( ( err || ! res ) && ! isInstall ) {
390- logger . error ( options . name + '.' + instance + ' invalid config' ) ;
391- process . exit ( 2 ) ;
392- } else {
393- initAdapter ( res ) ;
394- }
395- } ) ;
396- }
394+ // auto oObjects
395+ if ( options . objects ) {
396+ that . getAdapterObjects ( function ( objs ) {
397+ that . oObjects = objs ;
398+ that . subscribeObjects ( '*' ) ;
399+ initStates ( prepareInitAdapter ) ;
397400 } ) ;
398- } ) ;
401+ } else {
402+ initStates ( prepareInitAdapter ) ;
403+ }
399404 } ) ;
400405
406+ function prepareInitAdapter ( ) {
407+ that . getForeignState ( 'system.adapter.' + that . namespace + '.alive' , function ( err , res ) {
408+ if ( options . instance !== undefined ) {
409+ initAdapter ( options ) ;
410+ } else
411+ if ( ! isInstall && res && res . val === true ) {
412+ logger . error ( options . name + '.' + instance + ' already running' ) ;
413+ process . exit ( 7 ) ;
414+ } else {
415+ that . getForeignObject ( 'system.adapter.' + that . namespace , function ( err , res ) {
416+ if ( ( err || ! res ) && ! isInstall ) {
417+ logger . error ( options . name + '.' + instance + ' invalid config' ) ;
418+ process . exit ( 2 ) ;
419+ } else {
420+ initAdapter ( res ) ;
421+ }
422+ } ) ;
423+ }
424+ } ) ;
425+ }
426+
401427 function initObjects ( cb ) {
402428 that . objects = new Objects ( {
403429 connection : config . objects ,
@@ -435,18 +461,26 @@ function Adapter(options) {
435461 }
436462 }
437463
438- if ( id . slice ( that . namespace . length ) === that . namespace ) {
439- if ( typeof options . objectChange === 'function' ) options . objectChange ( id . slice ( that . namespace . length + 1 ) , obj ) ;
464+ // update oObjects structure if desired
465+ if ( that . oObjects ) {
466+ if ( obj ) {
467+ that . oObjects [ id ] = obj ;
468+ } else {
469+ delete that . oObjects [ id ] ;
470+ }
471+ }
440472
473+ // It was an error in the calculation
474+ if ( ( options . noNamespace || config . noNamespace ) && that . _namespaceRegExp . test ( id ) ) {
441475 // emit 'objectChange' event instantly
442476 setTimeout ( function ( ) {
443- that . emit ( 'objectChange' , id . slice ( that . namespace . length + 1 ) , obj ) ;
477+ if ( typeof options . objectChange === 'function' ) options . objectChange ( id . substring ( that . namespace . length + 1 ) , obj ) ;
478+ that . emit ( 'objectChange' , id . substring ( that . namespace . length + 1 ) , obj ) ;
444479 } , 0 ) ;
445480 } else {
446- if ( typeof options . objectChange === 'function' ) options . objectChange ( id , obj ) ;
447-
448- // emit 'objectChange' event instantly
449481 setTimeout ( function ( ) {
482+ if ( typeof options . objectChange === 'function' ) options . objectChange ( id , obj ) ;
483+ // emit 'objectChange' event instantly
450484 that . emit ( 'objectChange' , id , obj ) ;
451485 } , 0 ) ;
452486 }
@@ -534,6 +568,35 @@ function Adapter(options) {
534568 }
535569 } ;
536570
571+ that . getAdapterObjects = function ( callback ) {
572+ var objects = { } ;
573+
574+ that . objects . getObjectView ( 'system' , 'state' , { startkey : that . namespace + '.' , endkey : that . namespace + '.\u9999' , include_docs : true } , function ( err , _states ) {
575+ that . objects . getObjectView ( 'system' , 'channel' , { startkey : that . namespace + '.' , endkey : that . namespace + '.\u9999' , include_docs : true } , function ( err , _channels ) {
576+ that . objects . getObjectView ( 'system' , 'device' , { startkey : that . namespace + '.' , endkey : that . namespace + '.\u9999' , include_docs : true } , function ( err , _devices ) {
577+ if ( _channels ) {
578+ for ( var c = _channels . rows . length - 1 ; c >= 0 ; c -- ) {
579+ objects [ _channels . rows [ c ] . id ] = _channels . rows [ c ] . value ;
580+ }
581+ }
582+ if ( _devices ) {
583+ for ( var d = _devices . rows . length - 1 ; d >= 0 ; d -- ) {
584+ objects [ _devices . rows [ d ] . id ] = _devices . rows [ d ] . value ;
585+ }
586+ }
587+ if ( _states ) {
588+ if ( options . states ) that . oStates = { } ;
589+ for ( var s = _states . rows . length - 1 ; s >= 0 ; s -- ) {
590+ objects [ _states . rows [ s ] . id ] = _states . rows [ s ] . value ;
591+ if ( that . oStates ) that . oStates [ id ] = null ;
592+ }
593+ }
594+ if ( typeof callback === 'function' ) callback ( objects ) ;
595+ } ) ;
596+ } ) ;
597+ } ) ;
598+ } ;
599+
537600 that . extendObject = function extendObject ( id , obj , options , callback ) {
538601 if ( typeof options === 'function' ) {
539602 callback = options ;
@@ -2107,8 +2170,16 @@ function Adapter(options) {
21072170 }
21082171 }
21092172 } else {
2173+ if ( that . oStates ) {
2174+ if ( ! state ) {
2175+ delete that . oStates [ id ] ;
2176+ } else {
2177+ that . oStates [ id ] = state ;
2178+ }
2179+ }
2180+
21102181 // It was an error in the calculation
2111- if ( config . noNamespace && that . _namespaceRegExp . test ( id ) ) {
2182+ if ( ( options . noNamespace || config . noNamespace ) && that . _namespaceRegExp . test ( id ) ) {
21122183 if ( typeof options . stateChange === 'function' ) {
21132184 options . stateChange ( id . substring ( that . namespace . length + 1 ) , state ) ;
21142185 } else {
@@ -2869,7 +2940,7 @@ function Adapter(options) {
28692940
28702941 if ( ! isInstall && ( ! process . argv || process . argv . indexOf ( '--force' ) === - 1 ) ) {
28712942 var id = 'system.adapter.' + that . namespace ;
2872- that . states . setState ( id + '.alive' , { val : true , ack : true , expire : 30 , from : id } ) ;
2943+ that . states . setState ( id + '.alive' , { val : true , ack : true , expire : 30 , from : id } ) ;
28732944 that . states . setState ( id + '.connected' , { val : true , ack : true , expire : 30 , from : id } , function ( ) {
28742945 process . exit ( 3 ) ;
28752946 } ) ;
@@ -2896,26 +2967,26 @@ function Adapter(options) {
28962967 process . exit ( 5 ) ;
28972968 return ;
28982969 }
2899- name = tmp [ 1 ] ;
2900- instance = tmp [ 2 ] ;
2970+ name = tmp [ 1 ] ;
2971+ instance = tmp [ 2 ] ;
29012972 } else {
2902- name = options . name ;
2903- instance = 0 ;
2973+ name = options . name ;
2974+ instance = 0 ;
29042975 adapterConfig = adapterConfig || { common : { mode : 'once' , name : name } , native : { } } ;
29052976 }
29062977
29072978 for ( var tp in logger . transports ) {
29082979 logger . transports [ tp ] . level = adapterConfig . common . logLevel || 'info' ;
29092980 }
29102981
2911- that . name = adapterConfig . common . name ;
2912- that . instance = instance ;
2982+ that . name = adapterConfig . common . name ;
2983+ that . instance = instance ;
29132984 that . namespace = name + '.' + instance ;
2914- process . title = 'io.' + that . namespace ;
2985+ process . title = 'io.' + that . namespace ;
29152986
2916- that . config = adapterConfig . native ;
2917- that . host = adapterConfig . common . host ;
2918- that . common = adapterConfig . common ;
2987+ that . config = adapterConfig . native ;
2988+ that . host = adapterConfig . common . host ;
2989+ that . common = adapterConfig . common ;
29192990
29202991 if ( adapterConfig . common . mode === 'subscribe' ||
29212992 adapterConfig . common . mode === 'schedule' ||
@@ -2930,8 +3001,7 @@ function Adapter(options) {
29303001
29313002 if ( typeof options . message === 'function' && ! adapterConfig . common . messagebox ) {
29323003 logger . error ( that . namespace + ' : message handler implemented, but messagebox not enabled. Define common.messagebox in io-package.json for adapter or delete message handler.' ) ;
2933- } else
2934- if ( /*typeof options.message === 'function' && */ adapterConfig . common . messagebox ) {
3004+ } else if ( /*typeof options.message === 'function' && */ adapterConfig . common . messagebox ) {
29353005 that . mboxSubscribed = true ;
29363006 that . states . subscribeMessage ( 'system.adapter.' + that . namespace ) ;
29373007 }
@@ -2952,9 +3022,10 @@ function Adapter(options) {
29523022 that . host = that . common . host || tools . getHostName ( ) || require ( 'os' ) . hostname ( ) ;
29533023 }
29543024
2955- var Log = function ( ) { } ;
3025+ var Log = function ( ) {
3026+ } ;
29563027 if ( process . argv . indexOf ( '--logs' ) !== - 1 ) {
2957- Log . prototype . info = function ( msg ) {
3028+ Log . prototype . info = function ( msg ) {
29583029 console . log ( msg ) ;
29593030 logger . info ( that . namespace + ' ' + msg ) ;
29603031 } ;
@@ -2966,12 +3037,12 @@ function Adapter(options) {
29663037 console . error ( msg ) ;
29673038 logger . error ( that . namespace + ' ' + msg ) ;
29683039 } ;
2969- Log . prototype . warn = function ( msg ) {
3040+ Log . prototype . warn = function ( msg ) {
29703041 console . warn ( msg ) ;
29713042 logger . warn ( that . namespace + ' ' + msg ) ;
29723043 } ;
29733044 } else {
2974- Log . prototype . info = function ( msg ) {
3045+ Log . prototype . info = function ( msg ) {
29753046 logger . info ( that . namespace + ' ' + msg ) ;
29763047 } ;
29773048 Log . prototype . debug = function ( msg ) {
@@ -2980,11 +3051,11 @@ function Adapter(options) {
29803051 Log . prototype . error = function ( msg ) {
29813052 logger . error ( that . namespace + ' ' + msg ) ;
29823053 } ;
2983- Log . prototype . warn = function ( msg ) {
3054+ Log . prototype . warn = function ( msg ) {
29843055 logger . warn ( that . namespace + ' ' + msg ) ;
29853056 } ;
29863057 }
2987-
3058+
29883059 that . log = new Log ( ) ;
29893060 if ( options . instance === undefined ) {
29903061 that . version = ( that . pack && that . pack . version ) ? that . pack . version : ( ( that . ioPack && that . ioPack . common ) ? that . ioPack . common . version : 'unknown' ) ;
@@ -3008,9 +3079,19 @@ function Adapter(options) {
30083079 } ) ;
30093080 }
30103081 }
3011-
3012- if ( typeof options . ready === 'function' ) options . ready ( ) ;
3013- that . emit ( 'ready' ) ;
3082+
3083+ // auto oStates
3084+ if ( options . states ) {
3085+ that . getStates ( '*' , function ( err , _states ) {
3086+ that . oStates = _states ;
3087+ that . subscribeStates ( '*' ) ;
3088+ if ( typeof options . ready === 'function' ) options . ready ( ) ;
3089+ that . emit ( 'ready' ) ;
3090+ } ) ;
3091+ } else {
3092+ if ( typeof options . ready === 'function' ) options . ready ( ) ;
3093+ that . emit ( 'ready' ) ;
3094+ }
30143095 }
30153096
30163097 function reportStatus ( ) {
0 commit comments