<commit_before>var events = require('events');
var util = require('util');
var Service = require('./service');

function CdifDevice(spec) {
  this.spec = spec;
  this.deviceID = '';
  this.connected = false;
  this.initServices();

  this.getDeviceSpec = this.getDeviceSpec.bind(this);
  this.connect = this.connect.bind(this);
  this.disconnect = this.disconnect.bind(this);
  this.getHWAddress = this.getHWAddress.bind(this);
  this.deviceControl = this.deviceControl.bind(this);
  this.subscribeDeviceEvent = this.subscribeDeviceEvent.bind(this);
  this.unSubscribeDeviceEvent = this.unSubscribeDeviceEvent.bind(this);
}

util.inherits(CdifDevice, events.EventEmitter);

CdifDevice.prototype.setAction = function(serviceID, actionName, action) {
  var service = this.services[serviceID];
  if (service) {
    service.actions[actionName].invoke = action.bind(this);
  } else {
    console.error('cannot set action: ' + serviceID + ' ' + actionName);
  }
};

CdifDevice.prototype.initServices = function() {
  this.services = {};
  var serviceList = this.spec.device.serviceList;
  for (var i in serviceList) {
    var service_spec = serviceList[i];
    this.services[i] = new Service(i, service_spec);
  }
}

CdifDevice.prototype.getDeviceSpec = function(callback) {
  if (this.connected === false) {
    callback(new Error('device not connected'), null);
    return;
  }
  if (this.spec == null) {
    callback(new Error('cannot get device spec'), null);
  } else {
    console.log('device name: ' + this.spec.device.friendlyName + ' deviceID: ' + this.deviceID);
    callback(null, this.spec);
  }
};

CdifDevice.prototype.getDeviceState = function(serviceID, callback) {
  var service = this.services[serviceID];
  if (service == null) {
    callback(new Error('service not found: ' + serviceID), null);
    return;
  }
  if (this.connected === false) {
    callback(new Error('device not connected'), null);
    return;
  }
  service.getState(callback);
};

CdifDevice.prototype.connect = function(user, pass, callback) {
  var _this = this;
  if (this.connected === true) {
    //TODO: may need to increase connected count?
    if (this.spec.device.userAuth === true) {
      if (this.user === user && this.pass === pass) {
        callback (null);
      } else {
        callback (new Error('user not authenticated'));
      }
      return;
    } else {
      callback(null);
      return;
    }
  }
  if (this._connect) {
    if (this.spec.device.userAuth === true) {
      this._connect(user, pass, function(error) {
        if (!error) {
          _this.user = user;
          _this.pass = <PASSWORD>;
          _this.connected = true;
        }
        callback(error);
      });
    } else {
      this._connect('', '', function(error) {
        if (!error) {
          _this.connected = true;
        }
        callback(error);
      });
    }
  } else {
    if (this.spec.device.userAuth === true) {
      // TODO: may create auth strategy later on
      callback(new Error('cannot authenticate this device'));
    } else {
      this.connected = true;
      callback(null);
    }
  }
};

CdifDevice.prototype.disconnect = function(callback) {
  var _this = this;
  if (this.connected === true) {
    //TODO: may need to decrease connected count?    
    if (this._disconnect) {
      this._disconnect(function(error) {
        if (!error) {
          if (_this.spec.device.userAuth === true) {
            _this.user = ''; _this.pass = '';
          }
          _this.connected = false;
        }
        callback(error);
      });
    } else {
      if (this.spec.device.userAuth === true) {
        this.user = ''; this.pass = '';
      }
      this.connected = false;
      callback(null);
    }
  } else {
    callback(new Error('device not connected'));
  }
};

CdifDevice.prototype.getHWAddress = function(callback) {
  if (this._getHWAddress) {
    this._getHWAddress(function(error, data) {
      callback(error, data);<commit_msg>minor update<commit_after>var events = require('events');
var util = require('util');
var Service = require('./service');

function CdifDevice(spec) {
  this.spec = spec;
  this.deviceID = '';
  this.connected = false;
  this.initServices();

  this.getDeviceSpec = this.getDeviceSpec.bind(this);
  this.connect = this.connect.bind(this);
  this.disconnect = this.disconnect.bind(this);
  this.getHWAddress = this.getHWAddress.bind(this);
  this.deviceControl = this.deviceControl.bind(this);
  this.subscribeDeviceEvent = this.subscribeDeviceEvent.bind(this);
  this.unSubscribeDeviceEvent = this.unSubscribeDeviceEvent.bind(this);
}

util.inherits(CdifDevice, events.EventEmitter);

CdifDevice.prototype.setAction = function(serviceID, actionName, action) {
  var service = this.services[serviceID];
  if (service) {
    service.actions[actionName].invoke = action.bind(this);
  } else {
    console.error('cannot set action: ' + serviceID + ' ' + actionName);
  }
};

CdifDevice.prototype.initServices = function() {
  this.services = {};
  var serviceList = this.spec.device.serviceList;
  for (var i in serviceList) {
    var service_spec = serviceList[i];
    this.services[i] = new Service(i, service_spec);
  }
}

CdifDevice.prototype.getDeviceSpec = function(callback) {
  if (this.connected === false) {
    callback(new Error('device not connected'), null);
    return;
  }
  if (this.spec == null) {
    callback(new Error('cannot get device spec'), null);
  } else {
    console.log('device name: ' + this.spec.device.friendlyName + ' deviceID: ' + this.deviceID);
    callback(null, this.spec);
  }
};

CdifDevice.prototype.getDeviceState = function(serviceID, callback) {
  var service = this.services[serviceID];
  if (service == null) {
    callback(new Error('service not found: ' + serviceID), null);
    return;
  }
  if (this.connected === false) {
    callback(new Error('device not connected'), null);
    return;
  }
  service.getState(callback);
};

CdifDevice.prototype.connect = function(user, pass, callback) {
  var _this = this;
  //TODO: ref count this call?
  if (this.connected === true) {
    if (this.spec.device.userAuth === true) {
      if (this.user === user && this.pass === pass) {
        callback (null);
      } else {
        callback (new Error('user not authenticated'));
      }
      return;
    } else {
      callback(null);
      return;
    }
  }
  if (this._connect) {
    if (this.spec.device.userAuth === true) {
      this._connect(user, pass, function(error) {
        if (!error) {
          _this.user = user;
          _this.pass = <PASSWORD>;
          _this.connected = true;
        }
        callback(error);
      });
    } else {
      this._connect('', '', function(error) {
        if (!error) {
          _this.connected = true;
        }
        callback(error);
      });
    }
  } else {
    if (this.spec.device.userAuth === true) {
      // TODO: may create auth strategy later on
      callback(new Error('cannot authenticate this device'));
    } else {
      this.connected = true;
      callback(null);
    }
  }
};

CdifDevice.prototype.disconnect = function(callback) {
  var _this = this;
  //TODO: ref count this call?
  if (this.connected === true) {
    if (this._disconnect) {
      this._disconnect(function(error) {
        if (!error) {
          if (_this.spec.device.userAuth === true) {
            _this.user = ''; _this.pass = '';
          }
          _this.connected = false;
        }
        callback(error);
      });
    } else {
      if (this.spec.device.userAuth === true) {
        this.user = ''; this.pass = '';
      }
      this.connected = false;
      callback(null);
    }
  } else {
    callback(new Error('device not connected'));
  }
};

CdifDevice.prototype.getHWAddress = function(callback) {
  if (this._getHWAddress) {
    this._getHWAddress(function(error, data) {
      callback(error, data);
{'fraction_non_alphanumeric': 0.14719715266302275, 'fraction_numerical': 0.0, 'mean_word_length': 2.834307992202729, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2836320', 'n_tokens_mistral': 2460, 'n_tokens_neox': 2424, 'n_words': 504}