WIP dynamic platform

This commit is contained in:
Arne Blumentritt 2021-04-30 19:51:04 +02:00
parent 0ed30314df
commit eda198ed09
3 changed files with 74 additions and 14 deletions

View File

@ -207,7 +207,6 @@ NeatoVacuumRobotPlatform.prototype = {
map.boundaries = result.boundaries;
}
loadedMaps++;
// Robot is completely requested if zones for all maps are loaded
if (loadedMaps === robot.maps.length)
{

View File

@ -1,7 +1,8 @@
import {CharacteristicValue, Logger, PlatformAccessory, Service} from 'homebridge';
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
@ -13,6 +14,7 @@ export class NeatoVacuumRobotAccessory
private findMeService: Service;
private robot: any;
private log: Logger;
private readonly refresh: any;
// private goToDockService: Service;
// private dockStateService: Service;
// private ecoService: Service;
@ -29,10 +31,12 @@ export class NeatoVacuumRobotAccessory
constructor(
private readonly platform: HomebridgeNeatoPlatform,
private readonly accessory: PlatformAccessory,
private readonly isNew: Boolean)
private readonly isNew: Boolean,
private readonly config: PlatformConfig)
{
this.robot = accessory.context.robot;
this.log = platform.log;
this.refresh = (this.config)['refresh'];
// set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)!
@ -231,6 +235,58 @@ export class NeatoVacuumRobotAccessory
}
}
async clean(boundary, spot)
{
// Start automatic update while cleaning
if (this.refresh === 'auto')
{
setTimeout(() => {
this.platform.updateRobotTimer(this.robot._serial);
}, 60 * 1000);
}
let eco = this.robotObject.mainAccessory.ecoService.getCharacteristic(Characteristic.On).value;
let extraCare = this.robotObject.mainAccessory.extraCareService.getCharacteristic(Characteristic.On).value;
let nogoLines = this.robotObject.mainAccessory.noGoLinesService.getCharacteristic(Characteristic.On).value;
let room = (this.boundary == null) ? '' : this.boundary.name;
debug(this.name + ": ## Start cleaning (" + (room !== '' ? room + " " : '') + "eco: " + eco + ", extraCare: " + extraCare + ", nogoLines: " + nogoLines + ", spot: " + JSON.stringify(spot)
+ ")");
// Normal cleaning
if (this.boundary == null && (typeof spot === 'undefined'))
{
this.robot.startCleaning(eco, extraCare ? 2 : 1, nogoLines, (error, result) => {
if (error)
{
this.log.error("Cannot start cleaning. " + error + ": " + JSON.stringify(result));
}
callback(error);
});
}
// Room cleaning
else if (room !== '')
{
this.robot.startCleaningBoundary(eco, extraCare, this.boundary.id, (error, result) => {
if (error)
{
this.log.error("Cannot start room cleaning. " + error + ": " + JSON.stringify(result));
}
callback(error);
});
}
// Spot cleaning
else
{
this.robot.startSpotCleaning(eco, spot.width, spot.height, spot.repeat, extraCare ? 2 : 1, (error, result) => {
if (error)
{
this.log.error("Cannot start spot cleaning. " + error + ": " + JSON.stringify(result));
}
callback(error);
});
}
}
// /**
// * Handle the "GET" requests from HomeKit

View File

@ -24,7 +24,7 @@ export class HomebridgeNeatoPlatform implements DynamicPlatformPlugin
public readonly config: PlatformConfig,
public readonly api: API)
{
this.log.debug('Finished initializing platform:', this.config.name);
this.log.debug('Finished initializing platform:', this.config.platform);
this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback');
@ -55,7 +55,10 @@ export class HomebridgeNeatoPlatform implements DynamicPlatformPlugin
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);
this.log.warn("Cannot connect to neato server. No new robots will be found and existing robots will be unresponsive.");
this.log.warn(error);
// TODO retry after x min
return;
}
// Get all robots from account
@ -63,12 +66,14 @@ export class HomebridgeNeatoPlatform implements DynamicPlatformPlugin
if (error)
{
this.log.error("Successful login but can't connect to your neato robot: " + error);
throw new this.api.hap.HapStatusError(this.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
// TODO retry after x min
return;
}
else if (robots.length === 0)
{
this.log.error("Successful login but no robots associated with your account.");
throw new this.api.hap.HapStatusError(this.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
// TODO retry after x min
return;
}
debug("Found " + robots.length + " robots");
@ -84,7 +89,7 @@ export class HomebridgeNeatoPlatform implements DynamicPlatformPlugin
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);
return;
}
try
@ -105,7 +110,7 @@ export class HomebridgeNeatoPlatform implements DynamicPlatformPlugin
// create the accessory handler for the restored accessory
// this is imported from `platformAccessory.ts`
new NeatoVacuumRobotAccessory(this, existingAccessory, false);
new NeatoVacuumRobotAccessory(this, existingAccessory, false, this.config);
// it is possible to remove platform accessories at any time using `api.unregisterPlatformAccessories`, eg.:
// remove platform accessories when no longer present
@ -118,7 +123,7 @@ export class HomebridgeNeatoPlatform implements DynamicPlatformPlugin
const accessory = new this.api.platformAccessory(robot.name, uuid);
accessory.context.robot = robot;
new NeatoVacuumRobotAccessory(this, accessory, true);
new NeatoVacuumRobotAccessory(this, accessory, true, this.config);
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);