<commit_before>var http = require('http');
var parser = require('./utils/xmltojson');
var request = require('request');
var WebSocketClient = require('websocket').client;

var KEYS = require('./utils/types').Keys;
var SOURCES = require('./utils/types').Source;

/**
 *
 * Device should at least contain
 * {
 *  name: 'Kitchen',
 *  ip: '',
 *  mac_address: ''
 * }
 *
 * @param device
 * @constructor
 */
var SoundTouchAPI = function(device) {
    device.url = "http://" + device.ip + ":" + device.port;
    device.ws_url = "ws://" + device.ip + ":" + '8080';

    this.device = device;
    this.name = device.name;

    this.socket = {
        source: undefined
    };
};

SoundTouchAPI.prototype.getDevice = function() {
    return this.device;
};

SoundTouchAPI.prototype.getMetaData = function() {
    return this.device;
};

SoundTouchAPI.prototype.getNowPlaying = function(handler) {
    this._getForDevice("now_playing", handler);
};

SoundTouchAPI.prototype.getTrackInfo = function(handler) {
    this._getForDevice("trackInfo", handler);
};

SoundTouchAPI.prototype.getPresets = function(handler) {
    this._getForDevice("presets", handler);
};

SoundTouchAPI.prototype.getSources = function(handler) {
    this._getForDevice("sources", handler);
};

SoundTouchAPI.prototype.getInfo = function(handler) {
    this._getForDevice("info", handler);
};

SoundTouchAPI.prototype.isAlive = function(handler) {
    this.getNowPlaying(function(json){
        if (json == undefined) {
            handler(false);
            return;
        }
        var isAlive =  json.nowPlaying.source != SOURCES.STANDBY;
        if (isAlive) {
            isAlive = json.nowPlaying.playStatus == 'PLAY_STATE';
        }
        handler(isAlive);
    });
};

SoundTouchAPI.prototype.isPoweredOn = function(handler) {
    this.getNowPlaying(function(json){
        if (json == undefined) {
            handler(false);
            return;
        }
        var isAlive =  json.nowPlaying.source != SOURCES.STANDBY;
        handler(isAlive);
    });
};


SoundTouchAPI.prototype.getVolume = function(handler) {
    this._getForDevice("volume", handler);
};

SoundTouchAPI.prototype.setVolume = function(volume, handler) {
    var data = "<volume>" + volume + "</volume>";
    this._setForDevice("volume", data, handler);
};


SoundTouchAPI.prototype.select = function(source, sourceAccount, location, handler) {
    if (source == undefined) {
        throw new Error("Source is not optional, provide a source from the SOURCES list.");
    }

    var data = '<ContentItem source="' + source + '" sourceAccount="' + sourceAccount + '" location="' + location + '">' +
        '<itemName>' + 'Select using API' + '</itemName>' +
        '</ContentItem>';

    this._setForDevice("select", data, handler);
};

SoundTouchAPI.prototype.setName = function(name, handler) {
    var data = "<name>" + name + "</name>";<commit_msg>Fix select API call by adding missing property<commit_after>var http = require('http');
var parser = require('./utils/xmltojson');
var request = require('request');
var WebSocketClient = require('websocket').client;

var KEYS = require('./utils/types').Keys;
var SOURCES = require('./utils/types').Source;

/**
 *
 * Device should at least contain
 * {
 *  name: 'Kitchen',
 *  ip: '',
 *  mac_address: ''
 * }
 *
 * @param device
 * @constructor
 */
var SoundTouchAPI = function(device) {
    device.url = "http://" + device.ip + ":" + device.port;
    device.ws_url = "ws://" + device.ip + ":" + '8080';

    this.device = device;
    this.name = device.name;

    this.socket = {
        source: undefined
    };
};

SoundTouchAPI.prototype.getDevice = function() {
    return this.device;
};

SoundTouchAPI.prototype.getMetaData = function() {
    return this.device;
};

SoundTouchAPI.prototype.getNowPlaying = function(handler) {
    this._getForDevice("now_playing", handler);
};

SoundTouchAPI.prototype.getTrackInfo = function(handler) {
    this._getForDevice("trackInfo", handler);
};

SoundTouchAPI.prototype.getPresets = function(handler) {
    this._getForDevice("presets", handler);
};

SoundTouchAPI.prototype.getSources = function(handler) {
    this._getForDevice("sources", handler);
};

SoundTouchAPI.prototype.getInfo = function(handler) {
    this._getForDevice("info", handler);
};

SoundTouchAPI.prototype.isAlive = function(handler) {
    this.getNowPlaying(function(json){
        if (json == undefined) {
            handler(false);
            return;
        }
        var isAlive =  json.nowPlaying.source != SOURCES.STANDBY;
        if (isAlive) {
            isAlive = json.nowPlaying.playStatus == 'PLAY_STATE';
        }
        handler(isAlive);
    });
};

SoundTouchAPI.prototype.isPoweredOn = function(handler) {
    this.getNowPlaying(function(json){
        if (json == undefined) {
            handler(false);
            return;
        }
        var isAlive =  json.nowPlaying.source != SOURCES.STANDBY;
        handler(isAlive);
    });
};


SoundTouchAPI.prototype.getVolume = function(handler) {
    this._getForDevice("volume", handler);
};

SoundTouchAPI.prototype.setVolume = function(volume, handler) {
    var data = "<volume>" + volume + "</volume>";
    this._setForDevice("volume", data, handler);
};


SoundTouchAPI.prototype.select = function(source, type, sourceAccount, location, handler) {
    if (source == undefined) {
        throw new Error("Source is not optional, provide a source from the SOURCES list.");
    }

    var data = '<ContentItem source="' + source + '" type="' + type + '" sourceAccount="' + sourceAccount + '" location="' + location + '">' +
        '<itemName>' + 'Select using API' + '</itemName>' +
        '</ContentItem>';

    this._setForDevice("select", data, handler);
};

SoundTouchAPI.prototype.setName = function(name, handler) {
    var data = "<name>" + name + "</name>";
{'fraction_non_alphanumeric': 0.15670243570090275, 'fraction_numerical': 0.0013626298756600238, 'mean_word_length': 3.3240058910162005, 'pattern_counts': {'":': 4, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3323164', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1741, 'n_words': 347}