Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refine promise usages in smart coffee machine scripts #337

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions examples/scripts/smart-coffee-machine-oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,38 +251,37 @@ Assumes one medium americano if not specified, but time and mode are mandatory f
// Override a write handler for availableResourceLevel property,
// utilizing the uriVariables properly
thing.setPropertyWriteHandler('availableResourceLevel', (val, options) => {
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resources[id] = val;
return thing.writeProperty('allAvailableResources', resources);
});
}
}
return new Promise((resolve, reject) => {
resolve('Please specify id variable as uriVariables.');
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resources[id] = val;
thing.writeProperty('allAvailableResources', resources);
resolve();
});
}
}
reject('Please specify id variable as uriVariables.');
});
});
// Override a read handler for availableResourceLevel property,
// utilizing the uriVariables properly
thing.setPropertyReadHandler('availableResourceLevel', (options) => {
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resolve(resources[id]);
});
});
}
}
}
return new Promise((resolve, reject) => {
resolve('Please specify id variable as uriVariables.');
reject('Please specify id variable as uriVariables.');
});
});
// Set up a handler for makeDrink action
Expand Down
45 changes: 22 additions & 23 deletions examples/scripts/smart-coffee-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,38 +237,37 @@ Assumes one medium americano if not specified, but time and mode are mandatory f
// Override a write handler for availableResourceLevel property,
// utilizing the uriVariables properly
thing.setPropertyWriteHandler('availableResourceLevel', (val, options) => {
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resources[id] = val;
return thing.writeProperty('allAvailableResources', resources);
});
}
}
return new Promise((resolve, reject) => {
resolve('Please specify id variable as uriVariables.');
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resources[id] = val;
thing.writeProperty('allAvailableResources', resources);
resolve();
});
}
}
reject('Please specify id variable as uriVariables.');
});
});
// Override a read handler for availableResourceLevel property,
// utilizing the uriVariables properly
thing.setPropertyReadHandler('availableResourceLevel', (options) => {
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resolve(resources[id]);
});
});
}
}
}
return new Promise((resolve, reject) => {
resolve('Please specify id variable as uriVariables.');
reject('Please specify id variable as uriVariables.');
});
});
// Set up a handler for makeDrink action
Expand Down
47 changes: 22 additions & 25 deletions packages/examples/src/scripts/smart-coffee-machine-oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,41 +263,38 @@ Assumes one medium americano if not specified, but time and mode are mandatory f
// Override a write handler for availableResourceLevel property,
// utilizing the uriVariables properly
thing.setPropertyWriteHandler('availableResourceLevel', (val, options) => {

// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables: any = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resources[id] = val;
return thing.writeProperty('allAvailableResources', resources);
});
}
}
return new Promise((resolve, reject) => {
resolve('Please specify id variable as uriVariables.');
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables: any = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resources[id] = val;
thing.writeProperty('allAvailableResources', resources);
resolve();
});
}
}
reject('Please specify id variable as uriVariables.');
});
});

// Override a read handler for availableResourceLevel property,
// utilizing the uriVariables properly
thing.setPropertyReadHandler('availableResourceLevel', (options) => {

// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables: any = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables: any = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resolve(resources[id]);
});
});
}
}
}
return new Promise((resolve, reject) => {
resolve('Please specify id variable as uriVariables.');
reject('Please specify id variable as uriVariables.');
});
});

Expand Down
47 changes: 22 additions & 25 deletions packages/examples/src/scripts/smart-coffee-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,41 +249,38 @@ Assumes one medium americano if not specified, but time and mode are mandatory f
// Override a write handler for availableResourceLevel property,
// utilizing the uriVariables properly
thing.setPropertyWriteHandler('availableResourceLevel', (val, options) => {

// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables: any = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resources[id] = val;
return thing.writeProperty('allAvailableResources', resources);
});
}
}
return new Promise((resolve, reject) => {
resolve('Please specify id variable as uriVariables.');
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables: any = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resources[id] = val;
thing.writeProperty('allAvailableResources', resources);
resolve();
});
}
}
reject('Please specify id variable as uriVariables.');
});
});

// Override a read handler for availableResourceLevel property,
// utilizing the uriVariables properly
thing.setPropertyReadHandler('availableResourceLevel', (options) => {

// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables: any = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
// Check if uriVariables are provided
if (options && typeof options === 'object' && 'uriVariables' in options) {
const uriVariables: any = options['uriVariables'];
if ('id' in uriVariables) {
return thing.readProperty('allAvailableResources').then((resources) => {
const id = uriVariables['id'];
resolve(resources[id]);
});
});
}
}
}
return new Promise((resolve, reject) => {
resolve('Please specify id variable as uriVariables.');
reject('Please specify id variable as uriVariables.');
});
});

Expand Down