@@ -4,6 +4,7 @@ const gwPort = 9999;
44
55//const gwType = 'Serial';
66//const gwPort = 'COM4';
7+ //const gwPort = '/dev/ttyUSB0';
78//const gwBaud = 115200;
89
910const dbAddress = '127.0.0.1' ;
@@ -116,6 +117,7 @@ const P_ULONG32 = 5;
116117const P_CUSTOM = 6 ;
117118
118119var fs = require ( 'fs' ) ;
120+ var appendedString = "" ;
119121
120122function crcUpdate ( old , value ) {
121123 var c = old ^ value ;
@@ -516,6 +518,22 @@ function sendRebootMessage(destination, gw) {
516518 gw . write ( td ) ;
517519}
518520
521+
522+ function appendData ( str , db , gw ) {
523+ pos = 0 ;
524+ while ( str . charAt ( pos ) != '\n' && pos < str . length ) {
525+ appendedString = appendedString + str . charAt ( pos ) ;
526+ pos ++ ;
527+ }
528+ if ( str . charAt ( pos ) == '\n' ) {
529+ rfReceived ( appendedString . trim ( ) , db , gw ) ;
530+ appendedString = "" ;
531+ }
532+ if ( pos < str . length ) {
533+ appendData ( str . substr ( pos + 1 , str . length - pos - 1 ) , db , gw ) ;
534+ }
535+ }
536+
519537function rfReceived ( data , db , gw ) {
520538 if ( ( data != null ) && ( data != "" ) ) {
521539 console . log ( '<- ' + data ) ;
@@ -526,7 +544,10 @@ function rfReceived(data, db, gw) {
526544 var command = + datas [ 2 ] ;
527545 var ack = + datas [ 3 ] ;
528546 var type = + datas [ 4 ] ;
529- var rawpayload = datas [ 5 ] . trim ( ) ;
547+ var rawpayload = "" ;
548+ if ( datas [ 5 ] ) {
549+ rawpayload = datas [ 5 ] . trim ( ) ;
550+ }
530551 var payload ;
531552 if ( command == C_STREAM ) {
532553 payload = [ ] ;
@@ -635,9 +656,7 @@ dbc.connect('mongodb://' + dbAddress + ':' + dbPort + '/' + dbName, function(err
635656 gw . on ( 'connect' , function ( ) {
636657 console . log ( 'connected to ethernet gateway at ' + gwAddress + ":" + gwPort ) ;
637658 } ) . on ( 'data' , function ( rd ) {
638- var rds = rd . toString ( ) . split ( '\n' ) ;
639- for ( var i = 0 ; i < rds . length ; i ++ )
640- rfReceived ( rds [ i ] . trim ( ) , db , gw ) ;
659+ appendData ( rd . toString ( ) , db , gw ) ;
641660 } ) . on ( 'end' , function ( ) {
642661 console . log ( 'disconnected from gateway' ) ;
643662 } ) . on ( 'error' , function ( ) {
@@ -646,14 +665,13 @@ dbc.connect('mongodb://' + dbAddress + ':' + dbPort + '/' + dbName, function(err
646665 gw . setEncoding ( 'ascii' ) ;
647666 } ) ;
648667 } else if ( gwType == 'Serial' ) {
649- gw = require ( 'serialport' ) . SerialPort ( gwPort , { baudrate : gwBaud } , false ) ;
668+ var SerialPort = require ( 'serialport' ) . SerialPort ;
669+ gw = new SerialPort ( gwPort , { baudrate : gwBaud } ) ;
650670 gw . open ( ) ;
651671 gw . on ( 'open' , function ( ) {
652672 console . log ( 'connected to serial gateway at ' + gwPort ) ;
653673 } ) . on ( 'data' , function ( rd ) {
654- var rds = rd . toString ( ) . split ( '\n' ) ;
655- for ( var i = 0 ; i < rds . length ; i ++ )
656- rfReceived ( rds [ i ] . trim ( ) , db , gw ) ;
674+ appendData ( rd . toString ( ) , db , gw ) ;
657675 } ) . on ( 'end' , function ( ) {
658676 console . log ( 'disconnected from gateway' ) ;
659677 } ) . on ( 'error' , function ( ) {
0 commit comments