<commit_before>var telldus = require("telldus");
var _ = require("lodash");
var WebSocket = require("ws");
var winston = require("winston");
var logger = new (winston.Logger)({
    transports: [
      new (winston.transports.Console)(),
      new (winston.transports.File)({ filename: "nexa-adapter.log", level: "debug" })
    ]
  });
var devices = telldus.getDevicesSync(function(err, devices) {
                if ( err ) {
                   logger.error("Failed to read devices from TellStick!", err);
                   process.exit(1);
                 } else {
                   return JSON.stringify(devices);
                 }
               });
var houmioServer = process.env.HORSELIGHTS_SERVER || "wss://houm.herokuapp.com";
var houmioSitekey = process.env.HORSELIGHTS_SITEKEY || "";
var ws = new WebSocket(houmioServer);

ws.on("open", function() {
  ws.send(JSON.stringify({ command: "publish", data: { sitekey: houmioSitekey, vendor: "nexa" } }));
  logger.info("Connected to " + houmioServer);
});

ws.on("message", function(msg) {
  logger.debug("received message %s", msg);
  handleMessage(JSON.parse(msg));
})

function isDimmer(deviceId) {
  return _.some(devices, function(device) {
    return (device.id == deviceId) && device.model.indexOf("dimmer") > 0;
  });
}

function parseOnOffValue(message) {
  return message.data.on === true ? 1 : 0;
}

function handleMessage(message) {
  var deviceId = parseInt(message.data.devaddr);
  var dimmer = isDimmer(deviceId);
  var value = dimmer ? parseInt(message.data.bri) : parseOnOffValue(message);
  if (dimmer && value > 0 && value < 255) {
    telldus.dimSync(deviceId, value);
  } else if (value === 0) {
    telldus.turnOffSync(deviceId);
  } else {
    telldus.turnOnSync(deviceId);
  }
}

ws.on("ping", function(ping) {
  logger.debug("ping received");
  ws.pong();
});

ws.on("error", function(error) {
  logger.info("Error received: %s -> exiting", error);
  process.exit(1);
});

ws.on("close", function() {
  logger.info("Connection closed -> exiting");
  process.exit(1);
});<commit_msg>Pass site configuration and logger in constructor<commit_after>var telldus = require("telldus");
var _ = require("lodash");
var WebSocket = require("ws");

module.exports = function(server, sitekey, logger) {
  var ws = new WebSocket(server);  
  var devices = telldus.getDevicesSync(function(err, devices) {
                  if (err) {
                    logger.error("Failed to read devices from TellStick!", err);
                    process.exit(1);
                  } else {
                    return JSON.stringify(devices);
                  }
                });

  ws.on("open", function() {
    ws.send(JSON.stringify({ command: "publish", data: { sitekey: sitekey, vendor: "nexa" } }));
    logger.info("Connected to " + server);
  });

  ws.on("message", function(msg) {
    logger.debug("received message %s", msg);
    handleMessage(JSON.parse(msg));
  });

  function isDimmer(deviceId) {
    return _.some(devices, function(device) {
      return (device.id == deviceId) && device.model.indexOf("dimmer") > 0;
    });
  }

  function parseOnOffValue(message) {
    return message.data.on === true ? 1 : 0;
  }

  function handleMessage(message) {
    var deviceId = parseInt(message.data.devaddr);
    var dimmer = isDimmer(deviceId);
    var value = dimmer ? parseInt(message.data.bri) : parseOnOffValue(message);
    if (dimmer && value > 0 && value < 255) {
      logger.info("setting dim level to: " + value);
      telldus.dimSync(deviceId, value);
    } else if (value === 0) {
      telldus.turnOffSync(deviceId);
    } else {
      telldus.turnOnSync(deviceId);
    }
  }

  ws.on("ping", function(ping) {
    logger.debug("ping received");
    ws.pong();
  });

  ws.on("error", function(error) {
    logger.info("Error received: %s -> exiting", error);
    process.exit(1);
  });

  ws.on("close", function() {
    logger.info("Connection closed -> exiting");
    process.exit(1);
  });
}
{'fraction_non_alphanumeric': 0.1636546184738956, 'fraction_numerical': 0.005522088353413655, 'mean_word_length': 3.0621814475025486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '5744780', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1278, 'n_words': 264}