Skip to content

Commit

Permalink
fixed usage of tag id instead of pet id for name of assigned pet for …
Browse files Browse the repository at this point in the history
…pet flap
  • Loading branch information
Sickbart committed Sep 1, 2024
1 parent 158dd27 commit 77b2a30
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1078,17 +1078,21 @@ class Sureflap extends utils.Adapter {
setPetLocation(pet, value) {
return /** @type {Promise<void>} */(new Promise((resolve, reject) => {
const pet_id = this.getPetId(pet);
const postData = JSON.stringify({
'where': (value ? '1' : '2'),
'since': this.getCurrentDateFormattedForSurepetApi()
});
const options = this.buildOptions('/api/pet/' + pet_id + '/position', 'POST', this.sureFlapState['token']);
if (pet_id !== undefined) {
const postData = JSON.stringify({
'where': (value ? '1' : '2'),
'since': this.getCurrentDateFormattedForSurepetApi()
});
const options = this.buildOptions('/api/pet/' + pet_id + '/position', 'POST', this.sureFlapState['token']);

this.httpRequest('set_pet_location', options, postData).then(() => {
return resolve();
}).catch(error => {
return reject(error);
});
this.httpRequest('set_pet_location', options, postData).then(() => {
return resolve();
}).catch(error => {
return reject(error);
});
} else {
return reject(`could not get pet id for pet '${pet}'.`);
}
}));
}

Expand Down Expand Up @@ -1231,7 +1235,7 @@ class Sureflap extends utils.Adapter {
this.log.error(`could not set pet type to adapter (${error})`);
}
} else {
this.log.warn(`could not get pet name for pet id (${this.sureFlapState.devices[deviceIndex].tags[t].id})`);
this.log.warn(`could not get pet name for pet tag id (${this.sureFlapState.devices[deviceIndex].tags[t].id})`);
this.log.debug(`cat flap '${this.sureFlapState.devices[deviceIndex].name}' has ${this.sureFlapState.devices[deviceIndex].tags.length} pets assigned and household has ${this.sureFlapState.pets.length} pets assigned.`);
}
}
Expand Down Expand Up @@ -2251,7 +2255,7 @@ class Sureflap extends utils.Adapter {
if (name !== undefined) {
deletePromiseArray.push(this.removeAssignedPetsFromPetFlap(obj_name + '.assigned_pets.' + name));
} else {
this.log.warn(`could not get pet name for pet id (${this.sureFlapState.devices[d].tags[t].id})`);
this.log.warn(`could not get pet name for pet tag id (${this.sureFlapState.devices[d].tags[t].id})`);
this.log.debug(`pet flap '${obj_name}' has ${this.sureFlapState.devices[d].tags.length} pets assigned and household has ${this.sureFlapState.pets.length} pets assigned.`);
}
}
Expand Down Expand Up @@ -2570,9 +2574,9 @@ class Sureflap extends utils.Adapter {
} else {
const name = this.getPetNameForTagId(this.sureFlapState.devices[deviceIndex].tags[t].id);
if (name !== undefined) {
promiseArray.push(this.setObjectNotExistsPromise(obj_name + '.assigned_pets.' + name, this.buildStateObject('Pet \'' + name + '\' (\'' + this.sureFlapState.devices[deviceIndex].tags[t].id + '\')', 'text', 'string')));
promiseArray.push(this.setObjectNotExistsPromise(obj_name + '.assigned_pets.' + name, this.buildStateObject('Pet \'' + name + '\' (\'' + this.getPetId(name) + '\')', 'text', 'string')));
} else {
this.log.warn(`could not get pet name for pet id (${this.sureFlapState.devices[deviceIndex].tags[t].id})`);
this.log.warn(`could not get pet name for pet tag id (${this.sureFlapState.devices[deviceIndex].tags[t].id})`);
this.log.debug(`pet flap '${this.sureFlapState.devices[deviceIndex].name}' has ${this.sureFlapState.devices[deviceIndex].tags.length} pets assigned and household has ${this.sureFlapState.pets.length} pets assigned.`);
}
}
Expand Down Expand Up @@ -2617,7 +2621,7 @@ class Sureflap extends utils.Adapter {
if (name !== undefined) {
promiseArray.push(this.setObjectNotExistsPromise(obj_name + '.assigned_pets.' + name, this.buildStateObject('Pet \'' + name + '\' (' + this.getPetId(name) + ')', 'text', 'string')));
} else {
this.log.warn(`could not get pet name for pet id (${this.sureFlapState.devices[deviceIndex].tags[t].id})`);
this.log.warn(`could not get pet name for pet tag id (${this.sureFlapState.devices[deviceIndex].tags[t].id})`);
this.log.debug(`feeder '${this.sureFlapState.devices[deviceIndex].name}' has ${this.sureFlapState.devices[deviceIndex].tags.length} pets assigned and household has ${this.sureFlapState.pets.length} pets assigned.`);
}
}
Expand Down Expand Up @@ -2699,7 +2703,7 @@ class Sureflap extends utils.Adapter {
if (name !== undefined) {
promiseArray.push(this.setObjectNotExistsPromise(obj_name + '.assigned_pets.' + name, this.buildStateObject('Pet \'' + name + '\' (' + this.getPetId(name) + ')', 'text', 'string')));
} else {
this.log.warn(`could not get pet name for pet id (${this.sureFlapState.devices[deviceIndex].tags[t].id})`);
this.log.warn(`could not get pet name for pet tag id (${this.sureFlapState.devices[deviceIndex].tags[t].id})`);
this.log.debug(`water dispenser '${this.sureFlapState.devices[deviceIndex].name}' has ${this.sureFlapState.devices[deviceIndex].tags.length} pets assigned and household has ${this.sureFlapState.pets.length} pets assigned.`);
}
}
Expand Down Expand Up @@ -2746,7 +2750,7 @@ class Sureflap extends utils.Adapter {
});
});
} else {
this.log.warn(`could not get pet name for pet id (${this.sureFlapState.devices[deviceIndex].tags[tag].id})`);
this.log.warn(`could not get pet name for pet tag id (${this.sureFlapState.devices[deviceIndex].tags[tag].id})`);
this.log.debug(`cat flap '${this.sureFlapState.devices[deviceIndex].name}' has ${this.sureFlapState.devices[deviceIndex].tags.length} pets assigned and household has ${this.sureFlapState.pets.length} pets assigned.`);
return reject();
}
Expand Down Expand Up @@ -3314,15 +3318,15 @@ class Sureflap extends utils.Adapter {
* returns the pet id of the pet
*
* @param {string} name
* @return {string} pet id
* @return {string|undefined} pet id
*/
getPetId(name) {
for (let i = 0; i < this.sureFlapState.pets.length; i++) {
if (this.sureFlapState.pets[i].name === name) {
return this.sureFlapState.pets[i].id;
}
}
return '';
return undefined;
}

/**
Expand Down

0 comments on commit 77b2a30

Please sign in to comment.