Skip to content

Commit

Permalink
handle JSON errors, remove request timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
LeJeko committed Feb 26, 2021
1 parent 1f9a4bb commit 2dde4f2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 63 deletions.
121 changes: 59 additions & 62 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { debug } = require('console');

var Service, Characteristic;

module.exports = function(homebridge) {
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-enlighten-power", "enlighten-power", AirQualityAccessory);
Expand All @@ -16,76 +16,76 @@ function AirQualityAccessory(log, config) {
}

this.log = log

this.log("Initialising...")

this.name = config["name"];
this.connection = config["connection"] || "bonjour"; // local or api
if (this.connection == "api") {
this.api_key = config["api_key"];
this.api_user_id = config["api_user_id"];
this.site_id = config["site_id"];
this.url = "https://api.enphaseenergy.com/api/v2/systems/"+this.site_id+"/summary?key="+this.api_key+"&user_id="+this.api_user_id;
this.updateInterval = config['update_interval'] || "5"; // every 5 min
}
if (this.connection == "api") {
this.api_key = config["api_key"];
this.api_user_id = config["api_user_id"];
this.site_id = config["site_id"];
this.url = "https://api.enphaseenergy.com/api/v2/systems/" + this.site_id + "/summary?key=" + this.api_key + "&user_id=" + this.api_user_id;
this.updateInterval = config['update_interval'] || "5"; // every 5 min
}
if (this.connection == "bonjour") {
this.url = "http://envoy.local/production.json";
this.updateInterval = config['update_interval'] || "1"; // every minutes
// allow change production type and handling current default to 1
temp_type = config["type"] || "eim" ;
temp_type = config["type"] || "eim";
this.type = (temp_type == "eim") ? 1 : 0;
}
if (this.connection == "url") {
this.url = config["url"];
if (this.connection == "url") {
this.url = config["url"];
this.updateInterval = config['update_interval'] || "1"; // every minutes
// allow change production type and handling current default to 1
temp_type = config["type"] || "eim" ;
temp_type = config["type"] || "eim";
this.type = (temp_type == "eim") ? 1 : 0;
}
}
this.co2Threshold = config['power_threshold'];
this.co2CurrentLevel = 0;
this.co2Detected = 0;
this.co2LevelUpdated = false;

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

this.service
.getCharacteristic(Characteristic.CarbonDioxideLevel)
.on('get', this.getCo2Level.bind(this));

this.service
.getCharacteristic(Characteristic.CarbonDioxideDetected)
.on('get', this.getCo2Detected.bind(this));

this.informationService = new Service.AccessoryInformation();

this.informationService
.setCharacteristic(Characteristic.Manufacturer, "Homebridge")
.setCharacteristic(Characteristic.Model, "Enlighten")
.setCharacteristic(Characteristic.SerialNumber, "0000030");

setInterval(function() {
this.getCo2Level(function(err, value) {
if (err) {
value = err;
}
this.service
.getCharacteristic(Characteristic.CarbonDioxideLevel).updateValue(value);
}.bind(this));
}.bind(this), this.updateInterval * 60000);

this.getCo2Level(function(err, value) {
this.service
.setCharacteristic(Characteristic.CarbonDioxideLevel, value);
}.bind(this));
.setCharacteristic(Characteristic.Manufacturer, "Homebridge")
.setCharacteristic(Characteristic.Model, "Enlighten")
.setCharacteristic(Characteristic.SerialNumber, "0000030");

setInterval(function () {
this.getCo2Level(function (err, value) {
if (err) {
value = err;
}
this.service
.getCharacteristic(Characteristic.CarbonDioxideLevel).updateValue(value);
}.bind(this));
}.bind(this), this.updateInterval * 60000);

this.getCo2Level(function (err, value) {
this.service
.setCharacteristic(Characteristic.CarbonDioxideLevel, value);
}.bind(this));
}

AirQualityAccessory.prototype.setCo2Level = function() {
AirQualityAccessory.prototype.setCo2Level = function () {
this.service
.setCharacteristic(Characteristic.CarbonDioxideLevel, this.co2CurrentLevel);
.setCharacteristic(Characteristic.CarbonDioxideLevel, this.co2CurrentLevel);
}

AirQualityAccessory.prototype.getCo2Level = function(callback) {
AirQualityAccessory.prototype.getCo2Level = function (callback) {

let url = new URL(this.url)
this.log.debug(url)
Expand All @@ -112,6 +112,14 @@ AirQualityAccessory.prototype.getCo2Level = function(callback) {
resp.on('end', () => {

if (resp.statusCode == 200) {
try {
JSON.parse(data)
} catch (e) {
this.log("Error: not JSON!");
callback(null, 0)
return
}

var json = JSON.parse(data);
this.log.debug("JSON: %s", json)
if (this.connection == "api") {
Expand All @@ -126,51 +134,40 @@ AirQualityAccessory.prototype.getCo2Level = function(callback) {
this.setCo2Level();
this.setCo2Detected();
callback(null, this.co2CurrentLevel);

} else {
this.log("Error getting current power: %s : %s",resp.statusCode, resp.statusMessage)
this.log("Error getting current power: %s : %s", resp.statusCode, resp.statusMessage)
callback(null, 0)
}

})
})

req.on("error", (err) => {
this.log("Error getting current power: %s - %s : %s",err.code, err.status, err.message)
callback(null, 0 )
})

req.on('timeout', function () {
// Timeout happend. Server received request, but not handled it
// (i.e. doesn't send any response or it took to long).
// You don't know what happend.
// It will emit 'error' message as well (with ECONNRESET code).

this.log('timeout')
req.destroy
this.log("Error getting current power: %s - %s : %s", err.code, err.status, err.message)
callback(null, 0)
})

req.setTimeout(5000)
req.end()

// callback(null, this.co2CurrentLevel);
}

AirQualityAccessory.prototype.setCo2Detected = function() {
if (this.co2CurrentLevel >= this.co2Threshold){
AirQualityAccessory.prototype.setCo2Detected = function () {
if (this.co2CurrentLevel >= this.co2Threshold) {
this.co2Detected = 1;
}
else{
else {
this.co2Detected = 0;
}
this.service
.setCharacteristic(Characteristic.CarbonDioxideDetected, this.co2Detected);
.setCharacteristic(Characteristic.CarbonDioxideDetected, this.co2Detected);
}

AirQualityAccessory.prototype.getCo2Detected = function(callback) {
this.log('Current Power =', this.co2CurrentLevel, 'W');
callback(null, this.co2Detected);
AirQualityAccessory.prototype.getCo2Detected = function (callback) {
this.log('Current Power =', this.co2CurrentLevel, 'W');
callback(null, this.co2Detected);
}

AirQualityAccessory.prototype.getServices = function() {
AirQualityAccessory.prototype.getServices = function () {
return [this.service, this.informationService];
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-enlighten-power",
"version": "1.1.2",
"version": "1.1.3",
"main": "index.js",
"description": "This plugin simulates a CO2 air quality accessory that you can switch to the \"detected\" state when the power produced by your Envoy solar system reaches a threshold.",
"author": {
Expand Down

0 comments on commit 2dde4f2

Please sign in to comment.