<commit_before>var request = require("request");
var Service, Characteristic;

module.exports = function(homebridge) {
  Service = homebridge.hap.Service;
  Characteristic = homebridge.hap.Characteristic;

  homebridge.registerAccessory("homebridge-sonybraviatv", "SonyBraviaTV", SonyBraviaTVAccessory);
}

function SonyBraviaTVAccessory(log, config) {
  this.log = log;

  this.name = config["name"];
  this.psk = config["presharedkey"];
  this.ipaddress = config["ipaddress"];


  this.service = new Service.Switch(this.name);

     this.service
        .getCharacteristic(Characteristic.On)
        .on('get', this.getOn.bind(this))
        .on('set', this.setOn.bind(this));

}

SonyBraviaTVAccessory.prototype.getOn = function(callback)
{
  this.log("Getting whether Sony TV is on...");

  var postData = {
    method: 'getPowerStatus',
    params: [],
    id: 1,
    version: '1.0'
  };

  request.post({
    url: "http://"+this.ipaddress + "/sony/system",
    headers: {'X-Auth-PSK': this.psk},
    form: postData
  }, function(err, response, body) {

    if (!err && response.statusCode == 200) {
      var json = JSON.parse(body);
      var status = json.result[0].status;
      this.log("TV status is %s", status);
      var isOn = state == "active";
      callback(null, isOn); // success
    }
    else {
      this.log("Error getting TV status (status code %s): %s", response.statusCode, err);
      callback(err);
    }
  }.bind(this));
}

SonyBraviaTVAccessory.prototype.setOn = function(value, callback) {
  this.log("Set value to %s", value);

  var postData = {
    method: 'setPowerStatus',
    params: [{'status':value}],
    id: 1,
    version: '1.0'
  };


  request.post({
    url: "http://"+this.ipaddress + "/sony/system",
    headers: {'X-Auth-PSK': this.psk},
    form: postData
  }, function(err, response, body) {

    if (!err && response.statusCode == 200) {
      callback(); // success
    }
    else {
      this.log("Error '%s' setting lock state. Response: %s", err, body);
      callback(err || new Error("Error setting lock state."));
    }
  }.bind(this));
}

SonyBraviaTVAccessory.prototype.getServices = function() {
  return [this.service];
}<commit_msg>Fixed so that the post data is being passed as a JSON string<commit_after>var request = require("request");
var Service, Characteristic;

module.exports = function(homebridge) {
  Service = homebridge.hap.Service;
  Characteristic = homebridge.hap.Characteristic;

  homebridge.registerAccessory("homebridge-sonybraviatv", "SonyBraviaTV", SonyBraviaTVAccessory);
}

function SonyBraviaTVAccessory(log, config) {
  this.log = log;

  this.name = config["name"];
  this.psk = config["presharedkey"];
  this.ipaddress = config["ipaddress"];


  this.service = new Service.Switch(this.name);

     this.service
        .getCharacteristic(Characteristic.On)
        .on('get', this.getOn.bind(this))
        .on('set', this.setOn.bind(this));

}

SonyBraviaTVAccessory.prototype.getOn = function(callback)
{
  this.log("Getting whether Sony TV is on...");

  var postData = JSON.stringify({
    method: 'getPowerStatus',
    params: [],
    id: 1,
    version: '1.0'
  });

  request.post({
    url: "http://"+this.ipaddress + "/sony/system",
    headers: {'X-Auth-PSK': this.psk},
    form: postData
  }, function(err, response, body) {

    if (!err && response.statusCode == 200) {
      var json = JSON.parse(body);
      var status = json.result[0].status;
      this.log("TV status is %s", status);
      var isOn = status == "active";
      callback(null, isOn); // success
    }
    else {
      this.log("Error getting TV status (status code %s): %s", response.statusCode, err);
      callback(err);
    }
  }.bind(this));
}

SonyBraviaTVAccessory.prototype.setOn = function(value, callback) {
  this.log("Set value to %s", value);

  var postData = JSON.stringify({
    method: 'setPowerStatus',
    params: [{'status':value}],
    id: 1,
    version: '1.0'
  });


  request.post({
    url: "http://"+this.ipaddress + "/sony/system",
    headers: {'X-Auth-PSK': this.psk},
    form: postData
  }, function(err, response, body) {

    if (!err && response.statusCode == 200) {
      callback(); // success
    }
    else {
      this.log("Error '%s' setting lock state. Response: %s", err, body);
      callback(err || new Error("Error setting lock state."));
    }
  }.bind(this));
}

SonyBraviaTVAccessory.prototype.getServices = function() {
  return [this.service];
}
{'fraction_non_alphanumeric': 0.16457960644007155, 'fraction_numerical': 0.005813953488372093, 'mean_word_length': 3.2559467174119887, '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': '689426', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1481, 'n_words': 326}