Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
taurgis committed Jul 14, 2024
1 parent c4be090 commit c938fb1
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,29 +274,32 @@ export class iRobotPlatformAccessory {
user_pmapv_id: never,
}) {
if (this.accessory.context.maps !== undefined) {
let index = -1;
let currentMap: {
regions: any,

Check warning on line 278 in src/platformAccessory.ts

View workflow job for this annotation

GitHub Actions / lint (16.x)

Unexpected any. Specify a different type

Check warning on line 278 in src/platformAccessory.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Unexpected any. Specify a different type
user_pmapv_id: string,
} | null = null;

for (const map of this.accessory.context.maps) {
if (map.pmap_id === lastCommand.pmap_id) {
index = this.accessory.context.maps.indexOf(map);
currentMap = map;
}
}

if (index !== -1) {
if (currentMap) {
//update the user_pmapv_id if necessary
this.platform.log.debug('Comparing user_pmapv_id:', lastCommand.user_pmapv_id, 'with', this.accessory.context.maps[index].user_pmapv_id);
if (lastCommand.user_pmapv_id !== this.accessory.context.maps[index].user_pmapv_id) {
this.platform.log.debug('Comparing user_pmapv_id:', lastCommand.user_pmapv_id, 'with', currentMap.user_pmapv_id);
if (lastCommand.user_pmapv_id !== currentMap.user_pmapv_id) {
this.platform.log.info('Updating user_pmapv_id for roomba since the map was updated since last time:', this.device.name, '(', lastCommand.user_pmapv_id, ')');

this.accessory.context.maps[index].user_pmapv_id = lastCommand.user_pmapv_id;
currentMap.user_pmapv_id = lastCommand.user_pmapv_id;
}

for (const region of lastCommand.regions) {
let exists = false;

for (const region_ of this.accessory.context.maps[index].regions) {
for (const region_ of currentMap.regions) {
if (region_.region_id === region.region_id) {
const regionIndex = this.accessory.context.maps[index].regions.indexOf(region_);
const regionIndex = currentMap.regions.indexOf(region_);

this.platform.log.info('Updating existing region for roomba.', this.device.name, '(', region.region_id, ')');

Expand All @@ -307,15 +310,15 @@ export class iRobotPlatformAccessory {
this.platform.log.debug('Keeping old parameters for region', region.region_id);
}

this.accessory.context.maps[index].regions[regionIndex] = region;
currentMap.regions[regionIndex] = region;

exists = true;
}
}

if (!exists) {
this.platform.log.info('Adding new region for roomba:', this.device.name, '\n', region);
this.accessory.context.maps[index].regions.push(region);
currentMap.regions.push(region);
}
}

Expand Down Expand Up @@ -344,8 +347,10 @@ export class iRobotPlatformAccessory {
'regions': lastCommand.regions,
'user_pmapv_id': lastCommand.user_pmapv_id,
}];
this.platform.log.debug(this.device.name + '\'s map update:',

this.platform.log.debug(this.device.name + '\'s map create:',
'\n map:', JSON.stringify(this.accessory.context.maps));

this.platform.log.info('Updating Homekit Rooms for Roomba:', this.device.name);
this.updateRooms();
}
Expand Down Expand Up @@ -530,7 +535,6 @@ export class iRobotPlatformAccessory {
}
}


/**
* Handle "SET" requests from HomeKit
*/
Expand Down

0 comments on commit c938fb1

Please sign in to comment.