_connectToRadio() {
		log_info(`Radio[${this.radio_id}]._connectToRadio():`);
		if (this.connectionState === ConnectionStates.disconnected) {
			this._setConnectionState(ConnectionStates.connecting);

			const radio = this;
			this.connection = net.connect(radio.port, radio.host, function() {
				log_info(`Radio[${radio.radio_id}].connection.on('connect')`);

				Promise.all([radio._startRealtimeListener(), 
								radio._getMeterList(),
								// triggers new format UDP packets
								radio.send('client set enforce_network_mtu=1'),
								// triggers new API
								radio.send('client set send_reduced_bw_dax=0')
							])
					.then(function() {
						radio._setConnectionState(ConnectionStates.connected);
					});
			});

			this.connection.on('data', function(data) {
				radio._receiveData(data);
			});

			this.connection.on('error', function(error) {
				// Called when there is an error on the channel
				// MUST be handled by a listener somewhere or will
				// CRASH the program with an unhandled exception.
				console.error(`Radio[${radio.radio_id}].connection.on('error')`);
				try {
					radio.emit(MessageTypes.error, error);
				} catch (err) {
					console.log(err);	
				}
			});

			this.connection.on('close', function() {
				log_info(`Radio[${radio.radio_id}].connection.on('close')`);
				radio._stopRealtimeListener();
				radio._setConnectionState(ConnectionStates.disconnected);
			});
		}
	}
// Connect to radio and setup handlers for TCP/IP data and state changes.
Connect to radio and setup handlers for TCP/IP data and state changes.
Connect to radio and setup handlers for TCP / IP data and state changes .
_connectToRadio() {
		log_info(`Radio[${this.radio_id}]._connectToRadio():`);
		if (this.connectionState === ConnectionStates.disconnected) {
			this._setConnectionState(ConnectionStates.connecting);
			const radio = this;
			this.connection = net.connect(radio.port, radio.host, function() {
				log_info(`Radio[${radio.radio_id}].connection.on('connect')`);
				Promise.all([radio._startRealtimeListener(), 
								radio._getMeterList(),
								radio.send('client set enforce_network_mtu=1'),
								radio.send('client set send_reduced_bw_dax=0')
							])
					.then(function() {
						radio._setConnectionState(ConnectionStates.connected);
					});
			});
			this.connection.on('data', function(data) {
				radio._receiveData(data);
			});
			this.connection.on('error', function(error) {
				console.error(`Radio[${radio.radio_id}].connection.on('error')`);
				try {
					radio.emit(MessageTypes.error, error);
				} catch (err) {
					console.log(err);	
				}
			});
			this.connection.on('close', function() {
				log_info(`Radio[${radio.radio_id}].connection.on('close')`);
				radio._stopRealtimeListener();
				radio._setConnectionState(ConnectionStates.disconnected);
			});
		}
	}
_connectToRadio ( ) { log_info ( ` ${ this . radio_id } ` ) ; if ( this . connectionState === ConnectionStates . disconnected ) { this . _setConnectionState ( ConnectionStates . connecting ) ; const radio = this ; this . connection = net . connect ( radio . port , radio . host , function ( ) { log_info ( ` ${ radio . radio_id } ` ) ; Promise . all ( [ radio . _startRealtimeListener ( ) , radio . _getMeterList ( ) , radio . send ( 'client set enforce_network_mtu=1' ) , radio . send ( 'client set send_reduced_bw_dax=0' ) ] ) . then ( function ( ) { radio . _setConnectionState ( ConnectionStates . connected ) ; } ) ; } ) ; this . connection . on ( 'data' , function ( data ) { radio . _receiveData ( data ) ; } ) ; this . connection . on ( 'error' , function ( error ) { console . error ( ` ${ radio . radio_id } ` ) ; try { radio . emit ( MessageTypes . error , error ) ; } catch ( err ) { console . log ( err ) ; } } ) ; this . connection . on ( 'close' , function ( ) { log_info ( ` ${ radio . radio_id } ` ) ; radio . _stopRealtimeListener ( ) ; radio . _setConnectionState ( ConnectionStates . disconnected ) ; } ) ; } }
Connect to radio and setup handlers for TCP/IP data and state changes.
Connect to radio and setup handlers for TCP / IP data and state changes .
// triggers new format UDP packets // triggers new API // Called when there is an error on the channel // MUST be handled by a listener somewhere or will // CRASH the program with an unhandled exception.