homebridge-kobold-Homaassis.../index.js

164 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
2019-09-19 12:10:17 +02:00
let inherits = require('util').inherits,
2019-09-19 09:37:16 +02:00
debug = require('debug')('homebridge-neato'),
botvac = require('node-botvac'),
2019-09-19 09:37:16 +02:00
Service,
Characteristic;
2019-09-19 09:37:16 +02:00
module.exports = function (homebridge)
{
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-neato", "NeatoVacuumRobot", NeatoVacuumRobotPlatform);
2019-07-20 22:27:16 +02:00
};
2019-09-19 09:37:16 +02:00
function NeatoVacuumRobotPlatform(log, config)
{
this.log = log;
this.serial = "1-3-3-7";
this.email = config['email'];
this.password = config['password'];
this.hiddenServices = ('disabled' in config ? config['disabled'] : '');
if ('refresh' in config && config['refresh'] !== 'auto')
{
// parse config parameter
this.refresh = parseInt(config['refresh']);
// must be integer and positive
this.refresh = (typeof this.refresh !== 'number' || (this.refresh % 1) !== 0 || this.refresh < 0) ? 60 : this.refresh;
// minimum 60s to save some load on the neato servers
this.refresh = (this.refresh > 0 && this.refresh < 60) ? 60 : this.refresh;
}
// default auto
else
{
this.refresh = 'auto';
}
debug("Refresh is set to: " + this.refresh);
}
NeatoVacuumRobotPlatform.prototype = {
2019-09-19 09:37:16 +02:00
accessories: function (callback)
{
let accessories = [];
2019-09-19 11:25:36 +02:00
let platform = this;
platform.boundaryNames = [];
2019-09-19 09:37:16 +02:00
this.getRobots(function ()
{
2019-09-19 11:25:36 +02:00
if (platform.robots)
2019-09-19 09:37:16 +02:00
{
2019-09-19 11:25:36 +02:00
platform.robots.forEach((robot, i) =>
2019-09-19 09:37:16 +02:00
{
2019-09-19 11:25:36 +02:00
platform.log("Found robot #" + (i + 1) + " named \"" + robot.name + "\" with serial \"" + robot._serial + "\"");
let NeatoVacuumRobotAccessory = require('./accessories/neatVacuumRobot')(Service, Characteristic);
let robotAccessory = new NeatoVacuumRobotAccessory(robot, platform);
2019-09-19 09:37:16 +02:00
accessories.push(robotAccessory);
2019-09-19 11:25:36 +02:00
2019-09-19 09:37:16 +02:00
if (robot.maps)
{
robot.maps.forEach((map) =>
{
if (map.boundaries)
{
map.boundaries.forEach((boundary) =>
{
if (boundary.type === "polygon")
{
2019-09-19 11:25:36 +02:00
accessories.push(new NeatoVacuumRobotAccessory(robot, platform, boundary))
2019-09-19 09:37:16 +02:00
}
})
}
})
}
})
}
callback(accessories);
});
},
getRobots: function (callback)
{
debug("Loading your robots");
let client = new botvac.Client();
let that = this;
client.authorize(this.email, this.password, false, (error) =>
{
if (error)
{
that.log(error);
that.log.error("Can't log on to neato cloud. Please check your internet connection and your credentials. Try again later if the neato servers have issues.");
callback();
}
else
{
client.getRobots((error, robots) =>
{
if (error)
{
that.log(error);
that.log.error("Successful login but can't connect to your neato robot.");
callback();
}
else
{
if (robots.length === 0)
{
that.log.error("Successful login but no robots associated with your account.");
that.robots = [];
callback();
}
else
{
debug("Found " + robots.length + " robots");
let updatedRobotCount = 0;
that.robots = robots;
that.robots.forEach((robot) =>
{
robot.getPersistentMaps((error, result) =>
{
if (error)
{
that.log("Error updating persistent maps: " + error + ": " + result);
callback();
return;
}
robot.maps = result;
let processedMapCount = 0;
if (robot.maps.length === 0)
{
callback();
}
robot.maps.forEach((map) =>
{
robot.getMapBoundaries(map.id, (error, result) =>
{
if (error)
{
this.log("error getting boundaries: " + error + ": " + result)
}
else
{
map.boundaries = result.boundaries;
}
processedMapCount++;
2019-09-19 12:10:17 +02:00
if (processedMapCount === robot.maps.length)
2019-09-19 09:37:16 +02:00
{
updatedRobotCount++;
if (updatedRobotCount === that.robots.length)
{
callback();
}
}
})
})
})
})
}
}
});
}
});
}
2019-09-19 11:25:36 +02:00
};