1- /* jshint -W097 */ // jshint strict:false
2- /*jslint node: true */
1+ /* jshint -W097 */
2+ /* jshint strict: false */
3+ /* jslint node: true */
34
45'use strict' ;
5- var Cul = process . env . DEBUG ? require ( __dirname + ' /lib/debugCul.js') : require ( 'cul' ) ;
6+ var Cul = process . env . DEBUG ? require ( '. /lib/debugCul.js') : require ( 'cul' ) ;
67
78// you have to require the utils module and call adapter function
89var utils = require ( '@iobroker/adapter-core' ) ; // Get common adapter utils
2728}
2829
2930adapter . on ( 'stateChange' , function ( id , state ) {
30- //if (cul) cul.cmd();
31+ if ( ! state . ack ) {
32+ adapter . log . debug ( 'State Change ' + JSON . stringify ( id ) + ', State: ' + JSON . stringify ( state ) ) ;
33+ // State Change "cul.0.FS20.123401.cmd" State: {"val":2,"ack":false,"ts":1581365531968,"q":0,"from":"system.adapter.admin.0","user":"system.user.admin","lc":1581365531968}
34+ var oAddr = id . split ( '.' ) ;
35+ // 0: cul; 1:0; 2:FS20; 3:123401; 4:cmd;
36+ var sHousecode = oAddr [ 3 ] . substring ( 0 , 4 ) ;
37+ var sAddress = oAddr [ 3 ] . substring ( 4 , 6 ) ;
38+ if ( oAddr [ 2 ] === 'FS20' || adapter . config . experimental === true || adapter . config . experimental === 'true' ) {
39+ switch ( oAddr [ 4 ] ) {
40+ case 'cmdRaw' :
41+ sendCommand ( { protocol : oAddr [ 2 ] , housecode : sHousecode , address : sAddress , command : state . val } ) ;
42+ break ;
43+
44+ deafult:
45+ adapter . log . error ( 'Write of State ' + oAddr [ 4 ] + ' currently not implemented' ) ;
46+ break ;
47+ }
48+ } else {
49+ dapter . log . error ( 'Only FS20 Devices are tested. Please contribute here: https://github.com/ioBroker/ioBroker.cul' ) ;
50+ }
51+ }
3152} ) ;
3253
3354adapter . on ( 'unload' , function ( callback ) {
@@ -42,7 +63,8 @@ adapter.on('unload', function (callback) {
4263} ) ;
4364
4465adapter . on ( 'ready' , function ( ) {
45- adapter . setState ( 'info.connection' , false , false ) ;
66+ adapter . setState ( 'info.connection' , false , true ) ;
67+
4668 checkPort ( function ( err ) {
4769 if ( ! err || process . env . DEBUG ) {
4870 main ( ) ;
@@ -68,25 +90,44 @@ adapter.on('message', function (obj) {
6890 adapter . sendTo ( obj . from , obj . command , [ { comName : 'Not available' } ] , obj . callback ) ;
6991 }
7092 }
71-
93+ break ;
94+
95+ case 'send' :
96+ sendCommand ( { protocol : obj . message . protocol , housecode : obj . message . housecode , address : obj . message . address , command : obj . message . command } ) ;
97+ break ;
98+
99+ default :
100+ adapter . log . error ( 'No such command: ' + obj . command ) ;
72101 break ;
73102 }
74103 }
75104} ) ;
76105
106+ /***
107+ * Send a command to the cul module
108+ * @param {obj.message.protocol, obj.message.housecode, obj.message.address, obj.message.command }
109+ */
110+ function sendCommand ( o ) {
111+ adapter . log . info ( 'Send command received. Housecode: ' + o . housecode + '; address: ' + o . address + '; command: ' + o . command ) ;
112+ cul . cmd ( o . protocol , o . housecode , o . address , o . command ) ;
113+ }
114+
77115function checkConnection ( host , port , timeout , callback ) {
78116 timeout = timeout || 10000 ; //default 10 seconds
117+
79118 var timer = setTimeout ( function ( ) {
80119 socket . end ( ) ;
81- callback ( " Timeout" ) ;
120+ callback ( ' Timeout' ) ;
82121 callback = null ;
83122 } , timeout ) ;
123+
84124 var socket = Net . createConnection ( port , host , function ( ) {
85125 clearTimeout ( timer ) ;
86126 socket . end ( ) ;
87127 callback ( null ) ;
88128 callback = null ;
89129 } ) ;
130+
90131 socket . on ( 'error' , function ( err ) {
91132 clearTimeout ( timer ) ;
92133 socket . end ( ) ;
@@ -98,12 +139,12 @@ function checkConnection(host, port, timeout, callback) {
98139function checkPort ( callback ) {
99140 if ( adapter . config . type === 'cuno' ) {
100141 checkConnection ( adapter . config . ip , adapter . config . port , 10000 , function ( err ) {
101- if ( callback ) callback ( err ) ;
142+ callback && callback ( err ) ;
102143 callback = null ;
103144 } ) ;
104145 } else {
105146 if ( ! adapter . config . serialport ) {
106- if ( callback ) callback ( 'Port is not selected' ) ;
147+ callback && callback ( 'Port is not selected' ) ;
107148 return ;
108149 }
109150 var sPort ;
@@ -114,24 +155,23 @@ function checkPort(callback) {
114155 } ) ;
115156 sPort . on ( 'error' , function ( err ) {
116157 if ( sPort . isOpen ) sPort . close ( ) ;
117- if ( callback ) callback ( err ) ;
158+ callback && callback ( err ) ;
118159 callback = null ;
119160 } ) ;
120161
121162 sPort . open ( function ( err ) {
122- if ( sPort . isOpen ) sPort . close ( ) ;
123-
124- if ( callback ) callback ( err ) ;
163+ sPort . isOpen && sPort . close ( ) ;
164+ callback && callback ( err ) ;
125165 callback = null ;
126166 } ) ;
127167 } catch ( e ) {
128168 adapter . log . error ( 'Cannot open port: ' + e ) ;
129169 try {
130- if ( sPort . isOpen ) sPort . close ( ) ;
170+ sPort . isOpen && sPort . close ( ) ;
131171 } catch ( ee ) {
132172
133173 }
134- if ( callback ) callback ( e ) ;
174+ callback && callback ( e ) ;
135175 }
136176 }
137177}
@@ -141,6 +181,7 @@ var tasks = [];
141181function processTasks ( ) {
142182 if ( tasks . length ) {
143183 var task = tasks . shift ( ) ;
184+
144185 if ( task . type === 'state' ) {
145186 adapter . setForeignState ( task . id , task . val , true , function ( ) {
146187 setTimeout ( processTasks , 0 ) ;
@@ -178,7 +219,9 @@ function setStates(obj) {
178219 var isStart = ! tasks . length ;
179220
180221 for ( var state in obj . data ) {
181- if ( ! obj . data . hasOwnProperty ( state ) ) continue ;
222+ if ( ! obj . data . hasOwnProperty ( state ) ) {
223+ continue ;
224+ }
182225 var oid = adapter . namespace + '.' + id + '.' + state ;
183226 var meta = objects [ oid ] ;
184227 var val = obj . data [ state ] ;
@@ -193,7 +236,7 @@ function setStates(obj) {
193236 }
194237 tasks . push ( { type : 'state' , id : oid , val : val } ) ;
195238 }
196- if ( isStart ) processTasks ( ) ;
239+ isStart && processTasks ( ) ;
197240}
198241
199242function connect ( ) {
@@ -207,7 +250,7 @@ function connect() {
207250 host : adapter . config . ip ,
208251 port : adapter . config . port ,
209252 debug : true ,
210- logger : adapter . log . info
253+ logger : adapter . log . debug
211254 } ;
212255
213256 cul = new Cul ( options ) ;
@@ -276,22 +319,16 @@ function connect() {
276319 }
277320
278321 setStates ( obj ) ;
279- if ( isStart ) processTasks ( ) ;
322+ isStart && processTasks ( ) ;
280323 } ) ;
281324
282325}
283326
284- function insertObjects ( objs , cb ) {
285- if ( objs && objs . length ) {
286- var newObject = objs . pop ( ) ;
287-
288- } else if ( cb ) {
289- cb ( ) ;
290- }
291- }
292-
293327function main ( ) {
294-
328+
329+ // in this template all states changes inside the adapters namespace are subscribed
330+ adapter . subscribeStates ( '*' ) ;
331+
295332 adapter . getForeignObject ( 'cul.meta.roles' , function ( err , res ) {
296333 metaRoles = res . native ;
297334 adapter . getObjectView ( 'system' , 'device' , { startkey : adapter . namespace + '.' , endkey : adapter . namespace + '.\u9999' } , function ( err , res ) {
0 commit comments