Skip to content

Commit

Permalink
Added fix for #228
Browse files Browse the repository at this point in the history
  • Loading branch information
robertraaijmakers committed Feb 23, 2025
1 parent e6a7e2e commit 74033d5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 26 deletions.
6 changes: 3 additions & 3 deletions .homeychangelog.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@
"en": "Fixed a couple of trash APIs using the GeneralAfvalwijzer implementation",
"nl": "Een foutmelding opgelost voor diverse APIs die gebruik maken van GeneralAfvalwijzer."
},
"5.1.3": {
"en": "Added Woerden (#227), added 5 week manual interval (#226), added widget highlight when trash is collected (can be toggled on/off through widget settings).",
"nl": "Regio Woerden wordt nu ondersteund (#227), tevens 5-weekse handmatige interval toegevoegd (#226), widget highlight toegevoegd als afval wordt opgehaald (kan aan/uitgezet worden via de widget instellingen)."
"5.1.4": {
"en": "Added Woerden (#227), added 5 week manual interval (#226), fixed (#228), added widget highlight when trash is collected (can be toggled on/off through widget settings).",
"nl": "Regio Woerden wordt nu ondersteund (#227), tevens 5-weekse handmatige interval toegevoegd (#226), fixed (#228), widget highlight toegevoegd als afval wordt opgehaald (kan aan/uitgezet worden via de widget instellingen)."
}
}
2 changes: 1 addition & 1 deletion .homeycompose/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"nl": "Afval Herinnering 2.0",
"sv": "Avfallspåminnelse 2.0"
},
"version": "5.1.3",
"version": "5.1.4",
"platforms": [
"local"
],
Expand Down
2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"nl": "Afval Herinnering 2.0",
"sv": "Avfallspåminnelse 2.0"
},
"version": "5.1.3",
"version": "5.1.4",
"platforms": [
"local"
],
Expand Down
57 changes: 40 additions & 17 deletions lib/trashapis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,53 @@ export class TrashApis {
if (apiSettings?.apiId && apiSettings?.apiId !== '') {
try {
const collectionDays = await this.ExecuteApi(apiSettings);

if (Object.keys(collectionDays).length === 0) {
throw new Error(`No trash data found.`);
}

apiFindResult.id = apiSettings.apiId;
apiFindResult.days = collectionDays;
} catch (error) {
this.#log(`Executing API: ${apiSettings.apiId}.`);
this.#log(error);
}

return apiFindResult;
}

for (const apiDefinition of this.#apiList) {
try {
const collectionDays = await apiDefinition.execute(apiSettings);
apiFindResult = {
days: collectionDays,
id: apiDefinition.id,
name: apiDefinition.name,
};

break;
} catch (error) {
this.#log(`Executing API: ${apiDefinition.id} - ${apiDefinition.name}.`);
this.#log(error);
}
}
apiFindResult = (await this.findFirstSuccessfulApi(apiSettings)) as ApiFindResult;

return apiFindResult;
}

async findFirstSuccessfulApi(apiSettings: ApiSettings) {
return new Promise(async (resolve, reject) => {
let resolved = false;

for (const apiDefinition of this.#apiList) {
apiDefinition
.execute(apiSettings)
.then((collectionDays) => {
if (!resolved) {
resolved = true;
let apiFindResult: ApiFindResult = {
id: apiDefinition.id,
name: apiDefinition.name,
days: collectionDays,
};
resolve(apiFindResult);
}
})
.catch((error) => this.#log(`API failed: ${apiDefinition.id} - ${error}`));
}

setTimeout(() => {
if (!resolved) reject(new Error('All API calls failed.'));
}, 10000); // Optional timeout
});
}

async #mijnAfvalWijzer(apiSettings: ApiSettings) {
return this.#generalMijnAfvalwijzerApiImplementation(apiSettings, 'www.mijnafvalwijzer.nl');
}
Expand Down Expand Up @@ -388,6 +407,7 @@ export class TrashApis {
searchResultIndex = nextResult > 0 ? searchResultIndex + 4 + nextResult : -1;
}

this.#log('Anddddd exit');
return fDates;
}

Expand Down Expand Up @@ -792,12 +812,11 @@ export class TrashApis {
async #rovaWasteCalendar(apiSettings: ApiSettings, hostname: string, startPath: string) {
this.#log('Checking afvalkalender Rova');

let fDates: ActivityDates[] = [];

await validateCountry(apiSettings, 'NL');
await validateZipcode(apiSettings);
await validateHousenumber(apiSettings);

let fDates: ActivityDates[] = [];
const houseNumberMatch = `${apiSettings.housenumber}`.match(/\d+/g);
const numberAdditionMatch = `${apiSettings.housenumber}`.match(/[a-zA-Z]+/g);

Expand Down Expand Up @@ -827,6 +846,10 @@ export class TrashApis {
});

const result = <any>getRecycleData.body;
if (result.length === 0) {
throw new Error('No data found for this address.');
}

for (var et in result) {
const entry = result[et];
const trashDate = new Date(entry.date.substring(0, 4) + '-' + entry.date.substring(5, 7) + '-' + entry.date.substring(8, 10));
Expand Down
2 changes: 2 additions & 0 deletions settings/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class SettingScript {
return console.log(trashErr);
}

console.log('Incoming results!', trashResult);

if (trashResult.id === '') {
resp.innerHTML = this.homey.__('settings.fail');
resp.style.color = 'red';
Expand Down
24 changes: 20 additions & 4 deletions test/trashApiTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ describe('TrashApiSvr', function () {
assert.equal(isValid, true);
});
});
*/
describe('TrashApiRov', function () {
it('API - Afvalwijzer - Rova', async function () {
/*it('API - Afvalwijzer - Rova', async function () {
const apiSettings: ApiSettings = {
zipcode: '3824GL',
housenumber: '11',
Expand All @@ -236,9 +236,25 @@ describe('TrashApiRov', function () {
const result = await testAPI(apiSettings);
const isValid = validateApiResults(apiSettings, result);
assert.equal(isValid, true);
});*/

it('API - Afvalwijzer - Rova - Niet bestaand adres', async function () {
const apiSettings: ApiSettings = {
zipcode: '7461JC',
housenumber: '12',
country: 'NL',
apiId: 'rov',
cleanApiId: '',
streetname: '',
};

const result = await testAPI(apiSettings);
console.log(result);
const isValid = validateApiResults(apiSettings, result);
assert.equal(isValid, false);
});
});
/*
describe('TrashApiRad', function () {
it('API - RAD 2', async function () {
const apiSettings: ApiSettings = {
Expand Down Expand Up @@ -502,7 +518,7 @@ describe('TrashApiAfw', function () {
const isValid = validateApiResults(apiSettings, result);
assert.equal(isValid, true);
});
});*/
});
describe('Woerden Ximmio', function () {
it('API - Ximmio - Woerden', async function () {
Expand Down

0 comments on commit 74033d5

Please sign in to comment.