<commit_before>'use strict';

const sonosModule = require('sonos');
const UP = 'up';
const DOWN = 'down';

const setVolume = (sonos, type) => {
  return new Promise(function(resolve, reject) {
    if (type === UP) {
      sonos.getVolume(function(err, volume) {
        volume += 15;
        if (volume > 100) {
          volume = 100;
        }
        sonos.setVolume(volume, function(err, data) {
          return resolve(data);
        });
      });
    }
    if (type === DOWN) {
      sonos.getVolume(function(err, volume) {
        volume -= 15;
        if (volume < 0) {
          volume = 5;
        }
        sonos.setVolume(volume, function(err, data) {
          return resolve(data);
        });
      });
    }
  });
};

const pause = (sonos) => {
  return new Promise(function(resolve, reject) {
    sonos.pause(function(err, data) {
      return resolve(data);
    });
  });
};

const resume = (sonos) => {
  return new Promise(function(resolve, reject) {
    sonos.play(function(err, data) {
      return resolve(data);
    });
  });
};

const next = (sonos) => {
  return new Promise(function(resolve, reject) {
    sonos.next(function(err, data) {
      return resolve(data);
    });
  });
};

module.exports = {run: function(state, config) {
  return new Promise(function(resolve, reject) {
    const zones = state.getSonosZoneList();
    console.log(zones);

    if (zones.length < 1) {
      state.setFinal('I can\'t find any Sonos devices on your network, sorry!');
      resolve(state);
    } else {
      let statement = state.getStatement();

      // normalize things to resume
      statement = statement.replace(/play music/i, 'resume');

      // if we are not talking about playing anything change back on for resume
      // "turn the music back on"
      if (statement.match(/play/i) === null) {
        statement = statement.replace(/back on/i, 'resume');
        statement = statement.replace(/music on/i, 'resume');
        statement = statement.replace(/on the /i, 'resume');
      }

      // if there is a follow up question, this was the original intent to sonos
      if (state.getField('sonosAction') === undefined) {
        state.setField('sonosAction', statement);
      }

      // figure out what zone we are going to talk to
      let currentZone = undefined;

      // check to see if the zone was said in the statment
      for (let i = 0; i < zones.length; i++) {
        const re = new RegExp(zones[i].name, 'gi');
        if (statement.match(re)) {
          currentZone = zones[i];
        }
      }

      // if we don't have a zone yet, pick the first one
      currentZone = zones[0];

      // do single zone actions that are not playing something
      if (currentZone !== undefined && state.getIntent() !== 'play') {
        const sonos = new sonosModule.Sonos(currentZone.ip);

        // turn volume up default 20 percent
        if (statement.match(/ up/i)) {
          return setVolume(sonos, UP).then(() => {
            resolve(state.setFinal(''));
          });
        }

        // turn volume up default 20 percent
        console.log(statement);
        if (statement.match(/(down|lower)/i)) {
          return setVolume(sonos, DOWN).then(() => {
            resolve(state.setFinal(''));
          });
        }

        // pause
        if (statement.match(/pause/i)) {
          return pause(sonos).then(() => {
            return resolve(state.setFinal(''));
          });
        }

        // resume
        if (statement.match(/resume/i)) {
          return resume(sonos).then(() => {
            return resolve(state.setFinal(''));
          });
        }<commit_msg>cleaned up debug and fixed "play music" bug<commit_after>'use strict';

const sonosModule = require('sonos');
const UP = 'up';
const DOWN = 'down';

const setVolume = (sonos, type) => {
  return new Promise(function(resolve, reject) {
    if (type === UP) {
      sonos.getVolume(function(err, volume) {
        volume += 15;
        if (volume > 100) {
          volume = 100;
        }
        sonos.setVolume(volume, function(err, data) {
          return resolve(data);
        });
      });
    }
    if (type === DOWN) {
      sonos.getVolume(function(err, volume) {
        volume -= 15;
        if (volume < 0) {
          volume = 5;
        }
        sonos.setVolume(volume, function(err, data) {
          return resolve(data);
        });
      });
    }
  });
};

const pause = (sonos) => {
  return new Promise(function(resolve, reject) {
    sonos.pause(function(err, data) {
      return resolve(data);
    });
  });
};

const resume = (sonos) => {
  return new Promise(function(resolve, reject) {
    sonos.play(function(err, data) {
      return resolve(data);
    });
  });
};

const next = (sonos) => {
  return new Promise(function(resolve, reject) {
    sonos.next(function(err, data) {
      return resolve(data);
    });
  });
};

module.exports = {run: function(state, config) {
  return new Promise(function(resolve, reject) {
    const zones = state.getSonosZoneList();

    if (zones.length < 1) {
      state.setFinal('I can\'t find any Sonos devices on your network, sorry!');
      resolve(state);
    } else {
      let statement = state.getStatement();

      // normalize things to resume
      if (statement.replace(/play music/i)) {
        statement = statement.replace(/play music/i, 'resume');
        state.setIntent('resume');
      }

      // if we are not talking about playing anything change back on for resume
      // "turn the music back on"
      if (statement.match(/play/i) === null) {
        statement = statement.replace(/back on/i, 'resume');
        statement = statement.replace(/music on/i, 'resume');
        statement = statement.replace(/on the /i, 'resume');
      }

      // if there is a follow up question, this was the original intent to sonos
      if (state.getField('sonosAction') === undefined) {
        state.setField('sonosAction', statement);
      }

      // figure out what zone we are going to talk to
      let currentZone = undefined;

      // check to see if the zone was said in the statment
      for (let i = 0; i < zones.length; i++) {
        const re = new RegExp(zones[i].name, 'gi');
        if (statement.match(re)) {
          currentZone = zones[i];
        }
      }

      // if we don't have a zone yet, pick the first one
      currentZone = zones[0];

      // do single zone actions that are not playing something
      if (currentZone !== undefined && state.getIntent() !== 'play') {
        const sonos = new sonosModule.Sonos(currentZone.ip);

        // turn volume up default 20 percent
        if (statement.match(/ up/i)) {
          return setVolume(sonos, UP).then(() => {
            resolve(state.setFinal(''));
          });
        }

        // turn volume up default 20 percent
        if (statement.match(/(down|lower)/i)) {
          return setVolume(sonos, DOWN).then(() => {
            resolve(state.setFinal(''));
          });
        }

        // pause
        if (statement.match(/pause/i)) {
          return pause(sonos).then(() => {
            return resolve(state.setFinal(''));
          });
        }

        // resume
        if (statement.match(/resume/i)) {
          return resume(sonos).then(() => {
            return resolve(state.setFinal(''));
          });
        }
{'fraction_non_alphanumeric': 0.14561522773623387, 'fraction_numerical': 0.005166553365057783, 'mean_word_length': 2.08816120906801, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '3585550', 'n_tokens_mistral': 2192, 'n_tokens_neox': 2125, 'n_words': 585}