WIP dynamic platform

This commit is contained in:
Arne Blumentritt 2021-05-01 20:48:12 +02:00
parent 0530ba6c39
commit e0bd97ee5d
3 changed files with 254 additions and 171 deletions

View File

@ -61,5 +61,9 @@
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"ts-node": "^9.1.1", "ts-node": "^9.1.1",
"typescript": "^4.2.2" "typescript": "^4.2.2"
},
"funding": {
"type" : "buymeacoffee",
"url" : "https://buymeacoffee.com/naofireblade"
} }
} }

View File

@ -10,11 +10,14 @@ const debug = require('debug')('my-app:my-module');
*/ */
export class NeatoVacuumRobotAccessory export class NeatoVacuumRobotAccessory
{ {
private robot: any; private robot: any;
private log: Logger; private log: Logger;
private readonly refresh: any; private readonly refresh: any;
private spotClean: boolean;
private options: any;
private batteryService: Service;
private cleanService: Service; private cleanService: Service;
private findMeService: Service; private findMeService: Service;
private goToDockService: Service; private goToDockService: Service;
@ -38,7 +41,28 @@ export class NeatoVacuumRobotAccessory
{ {
this.robot = accessory.context.robot; this.robot = accessory.context.robot;
this.log = platform.log; this.log = platform.log;
this.refresh = (this.config)['refresh'];
if ('refresh' in this.config && this.config['refresh'] !== 'auto')
{
// parse config parameter
this.refresh = parseInt(this.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
if (this.refresh > 0 && this.refresh < 60)
{
this.log.warn("Minimum refresh time is 60 seconds to not overload the neato servers");
this.refresh = (this.refresh > 0 && this.refresh < 60) ? 60 : this.refresh;
}
}
else
{
this.refresh = 'auto';
}
this.log.debug(this.robot.name + " ## Refresh set to: " + this.refresh);
this.spotClean = false;
this.options = {};
// set accessory information // set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)! this.accessory.getService(this.platform.Service.AccessoryInformation)!
@ -48,25 +72,17 @@ export class NeatoVacuumRobotAccessory
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.robot.meta.firmware) .setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.robot.meta.firmware)
.setCharacteristic(this.platform.Characteristic.Name, this.robot.name); .setCharacteristic(this.platform.Characteristic.Name, this.robot.name);
let cleanServiceName = this.robot.name + " Clean"; this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)
let findMeServiceName = this.robot.name + " Find Me";
let goToDockServiceName = this.robot.name + " Go to Dock";
let dockStateServiceName = this.robot.name + " Docked";
let ecoServiceName = this.robot.name + " Eco Mode";
let noGoLinesServiceName = this.robot.name + " NoGo Lines";
let extraCareServiceName = this.robot.name + " Extra Care";
let scheduleServiceName = this.robot.name + " Schedule";
let spotCleanServiceName = this.robot.name + " Clean Spot";
this.cleanService = this.getSwitchService(cleanServiceName); this.cleanService = this.getSwitchService(this.robot.name + " Clean");
this.findMeService = this.getSwitchService(findMeServiceName); this.findMeService = this.getSwitchService(this.robot.name + " Find Me");
this.goToDockService = this.getSwitchService(goToDockServiceName); this.goToDockService = this.getSwitchService(this.robot.name + " Go to Dock");
this.dockStateService = this.accessory.getService(dockStateServiceName) || this.accessory.addService(this.platform.Service.Switch, dockStateServiceName, dockStateServiceName); this.dockStateService = this.getOccupancyService(this.robot.name + " Docked")
this.ecoService = this.getSwitchService(ecoServiceName); this.ecoService = this.getSwitchService(this.robot.name + " Eco Mode");
this.noGoLinesService = this.getSwitchService(noGoLinesServiceName); this.noGoLinesService = this.getSwitchService(this.robot.name + " NoGo Lines");
this.extraCareService = this.getSwitchService(extraCareServiceName); this.extraCareService = this.getSwitchService(this.robot.name + " Extra Care");
this.scheduleService = this.getSwitchService(scheduleServiceName); this.scheduleService = this.getSwitchService(this.robot.name + " Schedule");
this.spotCleanService = this.getSwitchService(spotCleanServiceName); this.spotCleanService = this.getSwitchService(this.robot.name + " Clean Spot");
this.cleanService.getCharacteristic(this.platform.Characteristic.On) this.cleanService.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setClean.bind(this)) .onSet(this.setClean.bind(this))
@ -75,13 +91,13 @@ export class NeatoVacuumRobotAccessory
.onSet(this.setFindMe.bind(this)) .onSet(this.setFindMe.bind(this))
.onGet(this.getFindMe.bind(this)); .onGet(this.getFindMe.bind(this));
this.goToDockService.getCharacteristic(this.platform.Characteristic.On) this.goToDockService.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setFindMe.bind(this)) .onSet(this.setGoToDock.bind(this))
.onGet(this.getFindMe.bind(this)); .onGet(this.getGoToDock.bind(this));
this.dockStateService.getCharacteristic(this.platform.Characteristic.On) this.dockStateService.getCharacteristic(this.platform.Characteristic.On)
.onGet(this.getFindMe.bind(this)); .onGet(this.getFindMe.bind(this));
this.ecoService.getCharacteristic(this.platform.Characteristic.On) this.ecoService.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setFindMe.bind(this)) .onSet(this.setEco.bind(this))
.onGet(this.getFindMe.bind(this)); .onGet(this.getEco.bind(this));
this.noGoLinesService.getCharacteristic(this.platform.Characteristic.On) this.noGoLinesService.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setFindMe.bind(this)) .onSet(this.setFindMe.bind(this))
.onGet(this.getFindMe.bind(this)); .onGet(this.getFindMe.bind(this));
@ -92,8 +108,10 @@ export class NeatoVacuumRobotAccessory
.onSet(this.setFindMe.bind(this)) .onSet(this.setFindMe.bind(this))
.onGet(this.getFindMe.bind(this)); .onGet(this.getFindMe.bind(this));
this.spotCleanService.getCharacteristic(this.platform.Characteristic.On) this.spotCleanService.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setFindMe.bind(this)) .onSet(this.setSpotClean.bind(this))
.onGet(this.getFindMe.bind(this)); .onGet(this.getSpotClean.bind(this));
this.updateRobotPeriodically().then(r => this.log.debug(this.robot.name + " ## Periodic update started with interval: " + this.refresh));
} }
getSwitchService(servicename: string) getSwitchService(servicename: string)
@ -101,6 +119,11 @@ export class NeatoVacuumRobotAccessory
return this.accessory.getService(servicename) || this.accessory.addService(this.platform.Service.Switch, servicename, servicename) return this.accessory.getService(servicename) || this.accessory.addService(this.platform.Service.Switch, servicename, servicename)
} }
getOccupancyService(servicename: string)
{
return this.accessory.getService(servicename) || this.accessory.addService(this.platform.Service.OccupancySensor, servicename, servicename)
}
async setClean(on: CharacteristicValue) async setClean(on: CharacteristicValue)
{ {
// TODO debug(this.robot.name + ": " + (on ? "Enabled ".brightGreen : "Disabled".red) + " Clean " + (this.boundary ? JSON.stringify(this.boundary) : '')); // TODO debug(this.robot.name + ": " + (on ? "Enabled ".brightGreen : "Disabled".red) + " Clean " + (this.boundary ? JSON.stringify(this.boundary) : ''));
@ -110,70 +133,43 @@ export class NeatoVacuumRobotAccessory
// Start // Start
if (on) if (on)
{
// No room given or same room
if (this.robot.boundary == null || this.robot.cleaningBoundaryId === this.robot.boundary.id)
{ {
// Resume cleaning // Resume cleaning
if (this.robot.canResume) if (this.robot.canResume)
{ {
debug(this.robot.name + ": ## Resume cleaning"); debug(this.robot.name + " => Resume cleaning");
await this.robot.resumeCleaning(); await this.robot.resumeCleaning();
return; return;
} }
// Start cleaning // Start cleaning
else if (this.robot.canStart) else if (this.robot.canStart)
{ {
// TODO this.clean(callback); await this.clean(CleanType.ALL)
} }
// Cannot start // Cannot start
else else
{ {
// TODO debug(this.name + ": Cannot start, maybe already cleaning (expected)"); this.log.debug(this.robot.name + ": Cannot start, maybe already cleaning (expected)");
return; return;
} }
} }
// Different room given
else
{
// 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();
// });
}
// Start new cleaning of new room
else
{
// debug(this.name + ": ## Start cleaning of new room");
// this.clean(callback);
}
}
}
// Stop // Stop
else else
{ {
if (this.robot.canPause) if (this.robot.canPause)
{ {
// debug(this.name + ": ## Pause cleaning"); this.log.debug(this.robot.name + " => Pause cleaning");
// this.robot.pauseCleaning((error) => { await this.robot.pauseCleaning();
// callback(error);
// });
} }
else else
{ {
// debug(this.name + ": Already paused"); this.log.debug(this.robot.name + ": Already paused");
// callback();
} }
} }
} }
catch (error) catch (error)
{ {
this.log.warn("Cannot start cleaning: " + error); this.log.error("Error setting cleaning to: " + on + ". " + error);
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE); throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
} }
} }
@ -183,19 +179,9 @@ export class NeatoVacuumRobotAccessory
try try
{ {
await this.updateRobot(); await this.updateRobot();
let on = this.robot.canPause && !this.spotClean;
let cleaning; this.log.debug(this.robot.name + " ## Clean is: " + on);
if (this.robot.boundary == null) return on;
{
cleaning = this.robot.canPause;
}
else
{
cleaning = this.robot.canPause && (this.robot.cleaningBoundaryId === this.robot.boundary.id)
}
// TODO debug(this.robot.name + ": Cleaning is " + (cleaning ? 'ON'.brightGreen : 'OFF'.red));
return cleaning;
} }
catch (error) catch (error)
{ {
@ -204,6 +190,74 @@ export class NeatoVacuumRobotAccessory
} }
} }
async getSpotClean(): Promise<CharacteristicValue>
{
await this.updateRobot();
let on = this.robot.canPause && this.spotClean;
this.log.debug(this.robot.name + " ## Spot Clean is: " + on);
return on;
}
async setSpotClean(on: CharacteristicValue)
{
await this.clean(CleanType.SPOT)
}
getGoToDock()
{
return false;
}
async setGoToDock(on: CharacteristicValue)
{
if (on)
{
await this.updateRobot();
setTimeout(() => {
this.goToDockService.updateCharacteristic(this.platform.Characteristic.On, false);
}, 1000);
try
{
if (this.robot.canPause)
{
this.log.debug(this.robot.name + " => Pause cleaning to go to dock");
await this.robot.pauseCleaning();
setTimeout(async () => {
await this.robot.sendToBase();
}, 1000);
}
else if (this.robot.canGoToBase)
{
this.log.debug(this.robot.name + " => Go to dock");
await this.robot.sendToBase();
}
else
{
this.log.warn(this.robot.name + "=| Can't go to dock at the moment");
}
}
catch (error)
{
this.log.warn("Error setting go to dock to: " + on + ". " + error);
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
}
getEco()
{
let on = this.options.eco;
this.log.debug(this.robot.name + " ## Eco is: " + on);
return on;
}
setEco(on: CharacteristicValue)
{
this.options.eco = on;
this.log.debug(this.robot.name + " ## Option eco set to: " + on);
}
getFindMe() getFindMe()
{ {
@ -214,7 +268,7 @@ export class NeatoVacuumRobotAccessory
{ {
if (on) if (on)
{ {
// TODO debug(this.name + ": ## Find me"); this.log.debug(this.robot.name + " => Find me")
setTimeout(() => { setTimeout(() => {
this.findMeService.updateCharacteristic(this.platform.Characteristic.On, false); this.findMeService.updateCharacteristic(this.platform.Characteristic.On, false);
}, 1000); }, 1000);
@ -225,36 +279,13 @@ export class NeatoVacuumRobotAccessory
} }
catch (error) catch (error)
{ {
this.log.warn("Cannot start find me: " + error); this.log.warn(this.robot.name + " ## Cannot start find me. " + error);
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE); throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
} }
} }
} }
async updateRobot() async clean(cleanType: CleanType)
{
// 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;
}
}
}
async clean(boundary, spot)
{ {
// Start automatic update while cleaning // Start automatic update while cleaning
if (this.refresh === 'auto') if (this.refresh === 'auto')
@ -264,47 +295,52 @@ export class NeatoVacuumRobotAccessory
}, 60 * 1000); }, 60 * 1000);
} }
let eco = this.ecoService.getCharacteristic(this.platform.Characteristic.On).value; this.log.info("Start cleaning with options type: " + cleanType + ", eco: " + this.options.eco + ", noGoLines: " + this.options.noGoLines + ", extraCare: " + this.options.extraCare);
let extraCare = this.extraCareService.getCharacteristic(this.platform.Characteristic.On).value;
let nogoLines = this.noGoLinesService.getCharacteristic(this.platform.Characteristic.On).value;
let room = (boundary == null) ? '' : boundary.name;
// debug(this.robot.name + ": ## Start cleaning (" + (room !== '' ? room + " " : '') + "eco: " + eco + ", extraCare: " + extraCare + ", nogoLines: " + nogoLines + ", spot: " + JSON.stringify(spot) + ")");
debug(this.robot.name + ": ## Start cleaning eco: " + eco + ", extraCare: " + extraCare + ", nogoLines: " + nogoLines + ", spot: " + JSON.stringify(spot) + ")");
// Normal cleaning
if (boundary == null && (typeof spot === 'undefined'))
{
try try
{ {
await this.robot.startCleaning(eco, extraCare ? 2 : 1, nogoLines); switch (cleanType)
{
case CleanType.ALL:
await this.robot.startCleaning(this.options.eco, this.options.extraCare ? 2 : 1, this.options.noGoLines);
this.spotClean = false;
break;
case CleanType.SPOT:
await this.robot.startSpotCleaning(this.options.eco, this.options.spot.width, this.options.spot.height, this.options.spot.repeat, this.options.extraCare ? 2 : 1);
this.spotClean = true;
break;
}
} }
catch (error) catch (error)
{ {
this.log.error("Cannot start cleaning. " + error); this.log.error("Cannot start cleaning. " + error);
} }
} }
// Room cleaning
else if (room !== '') async updateRobot()
{ {
// Data is outdated
if (typeof (this.robot.lastUpdate) === 'undefined' || new Date().getTime() - this.robot.lastUpdate > 2000)
{
debug(this.robot.name + ": ++ Updating robot state");
this.robot.lastUpdate = new Date().getTime();
try try
{ {
await this.robot.startCleaningBoundary(eco, extraCare, boundary.id); await this.robot.getState();
// Set options initially by api
if (typeof this.options.eco == 'undefined')
{
this.options.eco = this.robot.eco;
this.options.noGoLines = this.robot.noGoLines;
this.options.extraCare = this.robot.navigationMode == 2;
this.log.debug("Options initially set to eco: " + this.options.eco + ", noGoLines: " + this.options.noGoLines + ", extraCare: " + this.options.extraCare);
}
} }
catch (error) catch (error)
{ {
this.log.error("Cannot start room cleaning. " + error); this.log.error("Cannot update robot " + this.robot.name + ". Check if robot is online. " + error);
} return false;
}
// Spot cleaning
else
{
try
{
await this.robot.startSpotCleaning(eco, spot.width, spot.height, spot.repeat, extraCare ? 2 : 1);
}
catch (error)
{
this.log.error("Cannot start spot cleaning. " + error);
} }
} }
} }
@ -312,59 +348,65 @@ export class NeatoVacuumRobotAccessory
async updateRobotPeriodically() async updateRobotPeriodically()
{ {
await this.updateRobot() await this.updateRobot()
await this.updateCharacteristics();
// Clear any other overlapping timers for this robot // Clear any other overlapping timers for this robot
clearTimeout(this.robot.timer); clearTimeout(this.robot.timer);
// Tell all accessories of this robot (mainAccessory and roomAccessories) that updated robot data is available // Tell all accessories of this robot (mainAccessory and roomAccessories) that updated robot data is available
this.robot.mainAccessory.updated(); // this.robot.mainAccessory.updated();
this.robot.roomAccessories.forEach(accessory => { // this.robot.roomAccessories.forEach(accessory => {
accessory.updated(); // accessory.updated();
}); // });
// Periodic refresh interval set in config // Periodic refresh interval set in config
if (this.refresh !== 'auto' && this.refresh !== 0) if (this.refresh !== 'auto' && this.refresh !== 0)
{ {
this.log.debug(this.robot.device.name + ": ++ Next background update in " + this.refresh + " seconds"); this.log.debug(this.robot.name + " ## Next background update in " + this.refresh + " seconds");
this.robot.timer = setTimeout(this.updateRobotPeriodically.bind(this), this.refresh * 1000); this.robot.timer = setTimeout(this.updateRobotPeriodically.bind(this), this.refresh * 1000);
} }
// Auto refresh set in config // Auto refresh set in config (cleaning)
else if (this.refresh === 'auto' && this.robot.device.canPause) else if (this.refresh === 'auto' && !this.robot.canPause)
{ {
this.log.debug(this.robot.device.name + ": ++ Next background update in 60 seconds while cleaning (auto mode)"); this.log.debug(this.robot.name + " ## Next background update in 30 minutes (auto mode)");
this.robot.timer = setTimeout(this.updateRobotPeriodically.bind(this), 30 * 60 * 1000);
}
// Auto refresh set in config (cleaning)
else if (this.refresh === 'auto' && this.robot.canPause)
{
this.log.debug(this.robot.name + " ## Next background update in 60 seconds while cleaning (auto mode)");
this.robot.timer = setTimeout(this.updateRobotPeriodically.bind(this), 60 * 1000); this.robot.timer = setTimeout(this.updateRobotPeriodically.bind(this), 60 * 1000);
} }
// No refresh // No refresh
else else
{ {
debug(this.robot.device.name + ": ++ Stopped background updates"); debug(this.robot.name + " ## Stopped background updates");
} }
} }
async updateCharacteristics()
{
// Update Switches
// Clean
this.cleanService.updateCharacteristic(this.platform.Characteristic.On, await this.getClean());
// /** // Spot Clean
// * Handle the "GET" requests from HomeKit this.spotCleanService.updateCharacteristic(this.platform.Characteristic.On, await this.getSpotClean());
// * These are sent when HomeKit wants to know the current state of the accessory, for example, checking if a Light bulb is on.
// * // Go To Dock
// * GET requests should return as fast as possbile. A long delay here will result in this.goToDockService.updateCharacteristic(this.platform.Characteristic.On, await this.getGoToDock());
// * HomeKit being unresponsive and a bad user experience in general.
// * // // Schedule
// * If your device takes time to respond you should update the status of your device // this.scheduleService.updateCharacteristic(this.platform.Characteristic.On, await this.getSchedule());
// * asynchronously instead using the `updateCharacteristic` method instead.
// // Battery
// * @example this.batteryService.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.robot.charge);
// * this.service.updateCharacteristic(this.platform.Characteristic.On, true) this.batteryService.updateCharacteristic(this.platform.Characteristic.ChargingState, this.robot.isCharging);
// */ }
// async getOn(): Promise<CharacteristicValue> }
// {
// // implement your own code to check if the device is on enum CleanType
// const isOn = this.exampleStates.On; {
// ALL,
// this.platform.log.debug('Get Characteristic On ->', isOn); SPOT
//
// // 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);
//
// return isOn;
// }
} }

37
src/accessories/room.ts Normal file
View File

@ -0,0 +1,37 @@
// import {CharacteristicValue, Logger, PlatformAccessory, PlatformConfig, Service} from 'homebridge';
// import {HomebridgeNeatoPlatform} from '../homebridgeNeatoPlatform';
//
// const debug = require('debug')('my-app:my-module');
//
// /**
// * Platform Accessory
// * An instance of this class is created for each accessory your platform registers
// * Each accessory may expose multiple services of different service types.
// */
// export class Room
// {
//
// private robot: any;
// private log: Logger;
// private readonly refresh: any;
//
//
// /**
// * These are just used to create a working example
// * You should implement your own code to track the state of your accessory
// */
//
// constructor(
// private readonly platform: HomebridgeNeatoPlatform,
// private readonly accessory: PlatformAccessory,
// private readonly isNew: Boolean,
// private readonly config: PlatformConfig)
// {
//
// }
//
// async setCleanRoom()
// {
//
// }
// }