WIP dynamic platform

This commit is contained in:
Arne Blumentritt 2021-04-29 19:46:31 +02:00
parent 4a97891dfd
commit 0ed30314df
6 changed files with 3085 additions and 149 deletions

9
homebridge-neato.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

2841
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -48,7 +48,7 @@
"dependencies": {
"colors": "^1.4.0",
"debug": "^4.1.1",
"node-botvac": "^0.4.0",
"node-botvac": "^0.4.1",
"uuid": "^3.3.2"
},
"devDependencies": {

View File

@ -1,4 +1,4 @@
import {CharacteristicValue, PlatformAccessory, Service} from 'homebridge';
import {CharacteristicValue, Logger, PlatformAccessory, Service} from 'homebridge';
import {HomebridgeNeatoPlatform} from '../homebridgeNeatoPlatform';
const debug = require('debug')('my-app:my-module');
@ -10,14 +10,15 @@ const debug = require('debug')('my-app:my-module');
export class NeatoVacuumRobotAccessory
{
private cleanService: Service;
private findMeService: Service;
private robot: any;
private log: Logger;
// private goToDockService: Service;
// private dockStateService: Service;
// private ecoService: Service;
// private noGoLinesService: Service;
// private extraCareService: Service;
// private scheduleService: Service;
// private findMeService: Service;
// private spotCleanService: Service;
/**
@ -31,6 +32,7 @@ export class NeatoVacuumRobotAccessory
private readonly isNew: Boolean)
{
this.robot = accessory.context.robot;
this.log = platform.log;
// set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)!
@ -40,13 +42,17 @@ export class NeatoVacuumRobotAccessory
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.robot.meta.firmware)
.setCharacteristic(this.platform.Characteristic.Name, this.robot.name);
let cleanServiceName = robot.name + " Clean";
let cleanServiceName = this.robot.name + " Clean";
this.cleanService = this.accessory.getService(cleanServiceName) || this.accessory.addService(this.platform.Service.Switch, cleanServiceName, "CLEAN");
let findMeServiceName = this.robot.name + " Find Me";
this.findMeService = this.accessory.getService(findMeServiceName) || this.accessory.addService(this.platform.Service.Switch, findMeServiceName, "FIND_ME");
this.cleanService.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setClean.bind(this))
.onGet(this.getClean.bind(this));
this.findMeService.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setFindMe.bind(this))
.onGet(this.getFindMe.bind(this));
// /**
// * Updating characteristics values asynchronously.
@ -75,33 +81,33 @@ export class NeatoVacuumRobotAccessory
async setClean(on: CharacteristicValue)
{
// TODO debug(this.robot.name + ": " + (on ? "Enabled ".brightGreen : "Disabled".red) + " Clean " + (this.boundary ? JSON.stringify(this.boundary) : ''));
this.platform.updateRobot(this.robot._serial, (error, result) =>
try
{
await this.updateRobot();
// Start
if (on)
{
// No room given or same room
if (this.boundary == null || this.robot.cleaningBoundaryId === this.boundary.id)
if (this.robot.boundary == null || this.robot.cleaningBoundaryId === this.robot.boundary.id)
{
// Resume cleaning
if (this.robot.canResume)
{
debug(this.name + ": ## Resume cleaning");
this.robot.resumeCleaning((error) =>
{
callback(error);
});
debug(this.robot.name + ": ## Resume cleaning");
await this.robot.resumeCleaning();
return;
}
// Start cleaning
else if (this.robot.canStart)
{
this.clean(callback);
// TODO this.clean(callback);
}
// Cannot start
else
{
debug(this.name + ": Cannot start, maybe already cleaning (expected)");
callback();
// TODO debug(this.name + ": Cannot start, maybe already cleaning (expected)");
return;
}
}
// Different room given
@ -110,18 +116,18 @@ export class NeatoVacuumRobotAccessory
// Return to dock
if (this.robot.canPause || this.robot.canResume)
{
debug(this.name + ": ## Returning to dock to start cleaning of new room");
this.setGoToDock(true, (error, result) =>
{
this.nextRoom = this.boundary.id;
callback();
});
// debug(this.name + ": ## Returning to dock to start cleaning of new room");
// this.setGoToDock(true, (error, result) =>
// {
// this.nextRoom = this.boundary.id;
// callback();
// });
}
// Start new cleaning of new room
else
{
debug(this.name + ": ## Start cleaning of new room");
this.clean(callback);
// debug(this.name + ": ## Start cleaning of new room");
// this.clean(callback);
}
}
}
@ -130,32 +136,99 @@ export class NeatoVacuumRobotAccessory
{
if (this.robot.canPause)
{
debug(this.name + ": ## Pause cleaning");
this.robot.pauseCleaning((error) =>
{
callback(error);
});
// debug(this.name + ": ## Pause cleaning");
// this.robot.pauseCleaning((error) => {
// callback(error);
// });
}
else
{
debug(this.name + ": Already paused");
callback();
// debug(this.name + ": Already paused");
// callback();
}
}
});
}
catch (error)
{
this.log.warn("Cannot start cleaning: " + error);
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
async getClean(): Promise<CharacteristicValue>
{
// implement your own code to check if the device is on
const isOn = this.exampleStates.On;
try
{
await this.updateRobot();
this.platform.log.debug('Get Characteristic On ->', isOn);
let cleaning;
if (this.robot.boundary == null)
{
cleaning = this.robot.canPause;
}
else
{
cleaning = this.robot.canPause && (this.robot.cleaningBoundaryId === this.robot.boundary.id)
}
// if you need to return an error to show the device as "Not Responding" in the Home app:
// throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
// TODO debug(this.robot.name + ": Cleaning is " + (cleaning ? 'ON'.brightGreen : 'OFF'.red));
return cleaning;
}
catch (error)
{
this.log.warn("Cannot get cleaning status: " + error);
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
return isOn;
getFindMe()
{
return false;
}
async setFindMe(on: CharacteristicValue)
{
if (on)
{
// TODO debug(this.name + ": ## Find me");
setTimeout(() => {
this.findMeService.updateCharacteristic(this.platform.Characteristic.On, false);
}, 1000);
try
{
await this.robot.findMe();
}
catch (error)
{
this.log.warn("Cannot start find me: " + error);
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
}
async updateRobot()
{
// Data is up to date
if (typeof (this.robot.lastUpdate) !== 'undefined' && new Date().getTime() - this.robot.lastUpdate < 2000)
{
return;
}
else
{
debug(this.robot.name + ": ++ Updating robot state");
this.robot.lastUpdate = new Date().getTime();
try
{
await this.robot.getState();
}
catch (error)
{
this.log.error("Cannot update robot " + this.robot.name + ". Check if robot is online. " + error);
return false;
}
}
}

View File

@ -49,135 +49,148 @@ export class HomebridgeNeatoPlatform implements DynamicPlatformPlugin
debug("Discovering new robots");
let client = new NeatoApi.Client();
// Login
client.authorize((this.config)['email'], (this.config)['password'], false, (error) => {
if (error)
{
this.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: " + error);
return;
}
else
{
// Get all robots
try
{
// Login
client.authorize((this.config)['email'], (this.config)['password'], false, (error) => {
if (error)
{
throw new this.api.hap.HapStatusError(this.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
// Get all robots from account
client.getRobots((error, robots) => {
if (error)
{
this.log.error("Successful login but can't connect to your neato robot: " + error);
return;
throw new this.api.hap.HapStatusError(this.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
else if (robots.length === 0)
{
this.log.error("Successful login but no robots associated with your account.");
return;
throw new this.api.hap.HapStatusError(this.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
else
{
debug("Found " + robots.length + " robots");
let loadedRobots = 0;
robots.forEach((robot) => {
// Get additional information for the robot
robot.getState((error, state) => {
if (error)
debug("Found " + robots.length + " robots");
let loadedRobots = 0;
for (let robot of robots)
{
// Get additional information for the robot
robot.getState((error, state) => {
this.log.debug("Got state for robot: " + robot.name);
robot.meta = state.meta;
if (error)
{
this.log.error("Error getting robot meta information: " + error + ": " + state);
throw new this.api.hap.HapStatusError(this.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
try
{
const uuid = this.api.hap.uuid.generate(robot._serial);
const existingAccessory = this.robotAccessories.find(accessory => accessory.UUID === uuid);
if (existingAccessory)
{
this.log.error("Error getting robot meta information: " + error + ": " + state);
return;
// the accessory already exists
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
existingAccessory.context.robot = robot;
// TODO update maps
// if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
// existingAccessory.context.device = device;
// this.api.updatePlatformAccessories([existingAccessory]);
// create the accessory handler for the restored accessory
// this is imported from `platformAccessory.ts`
new NeatoVacuumRobotAccessory(this, existingAccessory, false);
// it is possible to remove platform accessories at any time using `api.unregisterPlatformAccessories`, eg.:
// remove platform accessories when no longer present
// this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory]);
// this.log.info('Removing existing accessory from cache:', existingAccessory.displayName);
}
else
{
const uuid = this.api.hap.uuid.generate(robot._serial);
const existingAccessory = this.robotAccessories.find(accessory => accessory.UUID === uuid);
this.log.info('Adding new accessory: ', robot.name);
const accessory = new this.api.platformAccessory(robot.name, uuid);
if (existingAccessory)
{
// the accessory already exists
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
accessory.context.robot = robot;
new NeatoVacuumRobotAccessory(this, accessory, true);
// TODO update maps
// if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
// existingAccessory.context.device = device;
// this.api.updatePlatformAccessories([existingAccessory]);
// create the accessory handler for the restored accessory
// this is imported from `platformAccessory.ts`
new NeatoVacuumRobotAccessory(this, existingAccessory, false);
// it is possible to remove platform accessories at any time using `api.unregisterPlatformAccessories`, eg.:
// remove platform accessories when no longer present
// this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory]);
// this.log.info('Removing existing accessory from cache:', existingAccessory.displayName);
}
else
{
this.log.info('Adding new accessory: ', robot.name);
const accessory = new this.api.platformAccessory(robot.name, uuid);
accessory.context.robot = robot;
new NeatoVacuumRobotAccessory(this, accessory, true);
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
// TODO get maps
}
// // Get all maps for each robot
// robot.getPersistentMaps((error, maps) => {
// if (error)
// {
// this.log.error("Error updating persistent maps: " + error + ": " + maps);
// callback();
// }
// // Robot has no maps
// else if (maps.length === 0)
// {
// robot.maps = [];
// this.robotAccessories.push({device: robot, meta: state.meta, availableServices: state.availableServices});
// loadedRobots++;
// if (loadedRobots === robots.length)
// {
// callback();
// }
// }
// // Robot has maps
// else
// {
// robot.maps = maps;
// let loadedMaps = 0;
// robot.maps.forEach((map) => {
// // Save zones in each map
// robot.getMapBoundaries(map.id, (error, result) => {
// if (error)
// {
// this.log.error("Error getting boundaries: " + error + ": " + result)
// }
// else
// {
// map.boundaries = result.boundaries;
// }
// loadedMaps++;
//
// // Robot is completely requested if zones for all maps are loaded
// if (loadedMaps === robot.maps.length)
// {
// this.robotAccessories.push({device: robot, meta: state.meta, availableServices: state.availableServices});
// loadedRobots++;
// if (loadedRobots === robots.length)
// {
// callback();
// }
// }
// })
// });
// }
// });
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
// TODO get maps
}
});
}
catch (error)
{
this.log.error("Error creating robot accessory: " + robot.name);
this.log.error(error);
throw new this.api.hap.HapStatusError(this.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
// // Get all maps for each robot
// robot.getPersistentMaps((error, maps) => {
// if (error)
// {
// this.log.error("Error updating persistent maps: " + error + ": " + maps);
// callback();
// }
// // Robot has no maps
// else if (maps.length === 0)
// {
// robot.maps = [];
// this.robotAccessories.push({device: robot, meta: state.meta, availableServices: state.availableServices});
// loadedRobots++;
// if (loadedRobots === robots.length)
// {
// callback();
// }
// }
// // Robot has maps
// else
// {
// robot.maps = maps;
// let loadedMaps = 0;
// robot.maps.forEach((map) => {
// // Save zones in each map
// robot.getMapBoundaries(map.id, (error, result) => {
// if (error)
// {
// this.log.error("Error getting boundaries: " + error + ": " + result)
// }
// else
// {
// map.boundaries = result.boundaries;
// }
// loadedMaps++;
//
// // Robot is completely requested if zones for all maps are loaded
// if (loadedMaps === robot.maps.length)
// {
// this.robotAccessories.push({device: robot, meta: state.meta, availableServices: state.availableServices});
// loadedRobots++;
// if (loadedRobots === robots.length)
// {
// callback();
// }
// }
// })
// });
// }
// });
});
}
});
}
});
});
}
catch (error)
{
this.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: " + error);
}
}
}

View File

@ -1,7 +1,7 @@
import { API } from 'homebridge';
import { PLATFORM_NAME } from './settings';
import { HomebridgeNeatoPlatform } from './platform';
import { HomebridgeNeatoPlatform } from './homebridgeNeatoPlatform';
/**
* This method registers the platform with Homebridge