Search code examples
javascriptgoogle-chromeeventsdom-eventsevent-dispatching

chrome Event is not dispatched


I'm having a little problem. I thought I had understood Event Handling, but now I don't think so anymore.

I've created a Chrome Event():

this.onReadLine = new chrome.Event();

this event is dispatched in a function:

this.onReadLine.dispatch(line);

before the dispatch instruction I've tried to log 'line', the argument of the dispatch instruction. No problem, 'line' exists.

Going straight down with the code you will find this part:

connection.onReadLine.addListener(function(line) {                  
    logJSON(line);                                                  
}); 

this is what must be fired every time the onReadLine event is dispatched.

The problem is that the Event onReadLine is only dispatched when I push or release the button '#dimmer1_Chrome_Input' defined at the end of my code. Where I'm wrong?

My full code here. The parts related to problem are highlighted with ////\///\/\///\\ lines.

// Serial used from Arduino board
const Arduino_COM = 'COM3'; // PC

var SerialConnection = function() {                                 
  this.connectionId = -1;                                           
  this.lineBuffer = "";
  this.boundOnDataReceiving = this.onDataReceiving.bind(this);                  
  this.boundOnDataReceivingError = this.onDataReceivingError.bind(this);
  this.onConnect = new chrome.Event();

///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

  this.onReadLine = new chrome.Event();

///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

  this.onError = new chrome.Event();
};

SerialConnection.prototype.connect = function(Serial_COM_Port) {
  chrome.serial.connect(Serial_COM_Port, this.onConnectComplete.bind(this));
};

SerialConnection.prototype.onConnectComplete = function(connectionInfo) {
  if (!connectionInfo) {
    log("Connection failed.");
    return;
  }
  this.connectionId = connectionInfo.connectionId;
  chrome.serial.onReceive.addListener(this.boundOnDataReceiving);
  chrome.serial.onReceiveError.addListener(this.boundOnDataReceivingError);
  this.onConnect.dispatch();
};

SerialConnection.prototype.send = function(msg) {
  if (this.connectionId < 0) {
    throw 'Invalid connection';
  }
  chrome.serial.send(this.connectionId, String_to_ArrayBuffer(msg), function() {});     
};

SerialConnection.prototype.onDataReceiving = function(receiveInfo) {
  if (receiveInfo.connectionId !== this.connectionId) {
    return;
  }
  this.lineBuffer += ArrayBuffer_to_String(receiveInfo.data);

  var index;
  while ((index = this.lineBuffer.indexOf('\n')) >= 0) {    
    var line = this.lineBuffer.substr(0, index + 1);        

    console.log(line);
    ///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

    this.onReadLine.dispatch(line);
///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

    this.lineBuffer = this.lineBuffer.substr(index + 1);    
  }
};

SerialConnection.prototype.onDataReceivingError = function(errorInfo) {
  if (errorInfo.connectionId === this.connectionId) {
    this.onError.dispatch(errorInfo.error);
  }
};

SerialConnection.prototype.disconnect = function() {
  if (this.connectionId < 0) {
    throw 'Invalid connection';
  }
  chrome.serial.disconnect(this.connectionId, function() {});
};

var connection = new SerialConnection();    
connection.onConnect.addListener(function() {                       
    log('connected to: ' + Arduino_COM);                            
});                                                                 

///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

connection.onReadLine.addListener(function(line) {                  
    logJSON(line);                                                  
});                                                                     
///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

connection.connect(Arduino_COM);

function logJSON(result) {
    
    var response = jQuery.parseJSON( result );
    
    dimmer1_state = response.dimmer1_state;
    dimmer1_value = response.dimmer1_value;
    SerialIn = response.SerialIn;
    dimmer1_Chrome_Input = response.dimmer1_Chrome_Input;
    temperature1_value = response.temperature1_value;

    s=Math.round(dimmer1_value * 80 / 255 + 20);
    hsl='hsl(115,'+s+'%,60%)';
    
    if (dimmer1_state == 0)
    {
        $('#statusCircle').css('fill','hsl(115,20%,60%)');
    }
    else
    {
        $('#statusCircle').css('fill', hsl);
    };

    // Print led Status to HTML buffer area
    messaggio = "dimmer1 state: " + dimmer1_state 
                + "<br />dimmer1 value: " + dimmer1_value   
                + "<br />SerialIn: " + SerialIn 
                + "<br />dimmer1_Chrome_Input: " + dimmer1_Chrome_Input 
                + "<br />temperature1_value: " + temperature1_value + " °C";
    log(messaggio);
};


function log(msg) {
    $('#buffer').html(msg);
};

$(function(){
    $('#dimmer1_Chrome_Input')  .button()
                                .mousedown(function() {
                                    connection.send("101");
                                })
                                .mouseup(function() {
                                    connection.send("100");
                                });
});

Solution

  • The code is correct, the error was in another part of program