11/**
2- * 's7': snap7 wrapper to communicate with Siemens PLC (S7)
2+ * 's7': snap7 wrapper to communicate with Siemens PLC (S7)
33 */
44
55var snap7 ;
@@ -93,10 +93,10 @@ function S7client(_data, _logger, _events, _runtime) {
9393 }
9494
9595 /**
96- * Read values in polling mode
96+ * Read values in polling mode
9797 * Update the tags values list, save in DAQ if value changed or in interval and emit values to clients
9898 */
99- this . polling = function ( ) {
99+ this . polling = async function ( ) {
100100 if ( _checkWorking ( true ) ) {
101101 var readVarsfnc = [ ] ;
102102 for ( var dbnum in db ) {
@@ -107,10 +107,11 @@ function S7client(_data, _logger, _events, _runtime) {
107107 readVarsfnc . push ( _readVars ( chunk ) ) ;
108108 } )
109109 }
110- Promise . all ( readVarsfnc ) . then ( result => {
110+ try {
111+ const result = await Promise . all ( readVarsfnc ) ;
111112 _checkWorking ( false ) ;
112113 if ( result . length ) {
113- let varsValueChanged = _updateVarsValue ( result ) ;
114+ let varsValueChanged = await _updateVarsValue ( result ) ;
114115 lastTimestampValue = new Date ( ) . getTime ( ) ;
115116 _emitValues ( varsValue ) ;
116117 if ( this . addDaq && ! utils . isEmptyObject ( varsValueChanged ) ) {
@@ -122,14 +123,14 @@ function S7client(_data, _logger, _events, _runtime) {
122123 if ( lastStatus !== 'connect-ok' ) {
123124 _emitStatus ( 'connect-ok' ) ;
124125 }
125- } , reason => {
126+ } catch ( reason ) {
126127 if ( reason && reason . stack ) {
127128 logger . error ( `'${ data . name } ' _readVars error! ${ reason . stack } ` ) ;
128129 } else {
129130 logger . error ( `'${ data . name } ' _readVars error! ${ reason } ` ) ;
130131 }
131132 _checkWorking ( false ) ;
132- } ) ;
133+ } ;
133134 } else {
134135 _emitStatus ( 'connect-busy' ) ;
135136 }
@@ -220,7 +221,7 @@ function S7client(_data, _logger, _events, _runtime) {
220221
221222 /**
222223 * Set the Tag value
223- * Read the current Tag object, write the value in object and send to SPS
224+ * Read the current Tag object, write the value in object and send to SPS
224225 */
225226 this . setValue = async function ( sigid , value ) {
226227 var item = _getTagItem ( data . tags [ sigid ] ) ;
@@ -260,23 +261,23 @@ function S7client(_data, _logger, _events, _runtime) {
260261
261262 /**
262263 * Return the timestamp of last read tag operation on polling
263- * @returns
264+ * @returns
264265 */
265266 this . lastReadTimestamp = ( ) => {
266267 return lastTimestampValue ;
267268 }
268269
269270 /**
270271 * Return the Daq settings of Tag
271- * @returns
272+ * @returns
272273 */
273274 this . getTagDaqSettings = ( tagId ) => {
274275 return data . tags [ tagId ] ? data . tags [ tagId ] . daq : null ;
275276 }
276277
277278 /**
278279 * Set Daq settings of Tag
279- * @returns
280+ * @returns
280281 */
281282 this . setTagDaqSettings = ( tagId , settings ) => {
282283 if ( data . tags [ tagId ] ) {
@@ -305,7 +306,7 @@ function S7client(_data, _logger, _events, _runtime) {
305306
306307 /**
307308 * Update the Tags values read
308- * @param {* } vars
309+ * @param {* } vars
309310 */
310311 var _updateVarsValue = async ( vars ) => {
311312 var someval = false ;
@@ -392,15 +393,15 @@ function S7client(_data, _logger, _events, _runtime) {
392393
393394 /**
394395 * Emit the PLC Tags values array { id: <name>, value: <value>, type: <type> }
395- * @param {* } values
396+ * @param {* } values
396397 */
397398 var _emitValues = function ( values ) {
398399 events . emit ( 'device-value:changed' , { id : data . id , values : values } ) ;
399400 }
400401
401402 /**
402403 * Emit the PLC connection status
403- * @param {* } status
404+ * @param {* } status
404405 */
405406 var _emitStatus = function ( status ) {
406407 lastStatus = status ;
@@ -409,7 +410,7 @@ function S7client(_data, _logger, _events, _runtime) {
409410
410411 /**
411412 * Used to manage the async connection and polling automation (that not overloading)
412- * @param {* } check
413+ * @param {* } check
413414 */
414415 var _checkWorking = function ( check ) {
415416 if ( check && working ) {
@@ -455,7 +456,7 @@ function S7client(_data, _logger, _events, _runtime) {
455456 vars . map ( v => {
456457 let value = null ;
457458 if ( v . type === 'BOOL' ) {
458- // check the full byte and send all bit if there is a change
459+ // check the full byte and send all bit if there is a change
459460 value = datatypes [ 'BYTE' ] . parser ( res , v . Start - offset , - 1 ) ;
460461 } else {
461462 value = datatypes [ v . type ] . parser ( res , v . Start - offset , v . bit ) ;
@@ -472,7 +473,7 @@ function S7client(_data, _logger, _events, _runtime) {
472473
473474 /**
474475 * Read multiple Vars
475- * @param {* } vars
476+ * @param {* } vars
476477 */
477478 var _readVars = function ( vars ) {
478479 return new Promise ( ( resolve , reject ) => {
@@ -486,7 +487,7 @@ function S7client(_data, _logger, _events, _runtime) {
486487 } else {
487488 try {
488489 if ( v . type === 'BOOL' ) {
489- // check the full byte and send all bit if there is a change
490+ // check the full byte and send all bit if there is a change
490491 value = datatypes [ 'BYTE' ] . parser ( res [ i ] . Data ) ; //, v.Start, -1);
491492 } else {
492493 value = datatypes [ v . type ] . parser ( res [ i ] . Data ) ;
@@ -575,7 +576,7 @@ function S7client(_data, _logger, _events, _runtime) {
575576 if ( variable ) {
576577 var prefix = variable . substring ( 0 , 2 ) ;
577578 if ( prefix === 'DB' ) {
578- // DB[n]"
579+ // DB[n]"
579580 var startpos = variable . indexOf ( '.' ) ;
580581 var dbNum = parseInt ( variable . substring ( 2 , startpos ) ) ;
581582 if ( dbNum >= 0 ) {
@@ -665,7 +666,7 @@ function S7client(_data, _logger, _events, _runtime) {
665666
666667 /**
667668 * Return error message, from error code
668- * @param {* } s7err
669+ * @param {* } s7err
669670 */
670671 var _getErr = function ( s7err ) {
671672 if ( Array . isArray ( s7err ) ) return new Error ( 'Errors: ' + s7err . join ( '; ' ) ) ;
0 commit comments