<commit_before>'use strict';
import WebSocket from 'ws';

class UpdateListener {
  constructor(url, callback) {
    this.url = url;
    this.callback = callback;
  }

  startListener() {
    if (!this.url) {
      /* istanbul ignore next */
      if (process.env.NODE_ENV != 'test') {
        console.log("No URL defined, no listener attached.");
      }
      return;
    }
    let _this = this;
    let ws = new WebSocket(this.url.replace('http:', 'ws:') + '/state?type=json');
    ws.on('open', function() {
      console.log('open ws connection for url ' + _this.url);
    });
    ws.on('message', function(message) {
      _this.callback(message);
    });
  }

}

export { UpdateListener };<commit_msg>extend UpdateListener to recover from closed WebSocket connection to openHab. Fixes #5<commit_after>'use strict';
import WebSocket from 'ws';

class UpdateListener {
  constructor(url, callback) {
    this.url = url;
    this.callback = callback;
    this.ws = undefined;
  }

  startListener() {
    if (!this.url) {
      /* istanbul ignore next */
      if (process.env.NODE_ENV != 'test') {
        console.log("No URL defined, no listener attached.");
      }
      return;
    }

    this.openConnection();
  }

  registerWebSocketListeners() {
    let _this = this;
    this.ws.on('open', function() {
      /* istanbul ignore next */
      if (process.env.NODE_ENV != 'test') {
        console.log('open ws connection for url ' + _this.url);
      }
    });

    this.ws.on('message', function(message) {
      _this.callback(message);
    });

    this.ws.on('close', function() {
      /* istanbul ignore next */
      if (process.env.NODE_ENV != 'test') {
        console.log('ws disconnected for url ' + _this.url);
      }
      _this.reopenConnection();
    });

    this.ws.on('error', function() {
      /* istanbul ignore next */
      if (process.env.NODE_ENV != 'test') {
        console.log('error connecting to url ' + _this.url);
      }
      _this.reopenConnection();
    });
  }

  reopenConnection() {
    setTimeout(function (updateListener) {
      /* istanbul ignore next */
      if (process.env.NODE_ENV != 'test') {
        console.log('reopen ws connection for url ' + updateListener.url);
      }
      updateListener.openConnection();
    }, 10000, this);
  }

  openConnection() {
    this.ws = new WebSocket(this.url.replace('http:', 'ws:') + '/state?type=json');
    this.registerWebSocketListeners();
  }

}

export { UpdateListener };
{'fraction_non_alphanumeric': 0.15437323659814592, 'fraction_numerical': 0.0024183796856106408, 'mean_word_length': 2.5712230215827336, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '771481', 'n_tokens_mistral': 790, 'n_tokens_neox': 771, 'n_words': 172}