Skip to content

Commit

Permalink
Update to Node 10 and Firebase logger.
Browse files Browse the repository at this point in the history
Bug: 161992646
Signed-off-by: Dave Smith <smithdave@google.com>
Change-Id: I732eeb0d1d3bf5ffb188dd7d25a83c78079866e3
  • Loading branch information
Dave Smith committed Jul 28, 2020
1 parent a016f4e commit 3ca572c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
34 changes: 17 additions & 17 deletions washer-done/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ const homegraph = google.homegraph({
version: 'v1',
auth: auth,
});
// Hardcoded user ID
const USER_ID = '123';

exports.login = functions.https.onRequest((request, response) => {
if (request.method === 'GET') {
console.log('Requesting login page');
functions.logger.log('Requesting login page');
response.send(`
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand All @@ -54,7 +56,7 @@ exports.login = functions.https.onRequest((request, response) => {
// Here, you should validate the user account.
// In this sample, we do not do that.
const responseurl = decodeURIComponent(request.body.responseurl);
console.log(`Redirect to ${responseurl}`);
functions.logger.log(`Redirect to ${responseurl}`);
return response.redirect(responseurl);
} else {
// Unsupported method
Expand All @@ -66,7 +68,7 @@ exports.fakeauth = functions.https.onRequest((request, response) => {
const responseurl = util.format('%s?code=%s&state=%s',
decodeURIComponent(request.query.redirect_uri), 'xxxxxx',
request.query.state);
console.log(`Set redirect as ${responseurl}`);
functions.logger.log(`Set redirect as ${responseurl}`);
return response.redirect(
`/login?responseurl=${encodeURIComponent(responseurl)}`);
});
Expand All @@ -76,7 +78,7 @@ exports.faketoken = functions.https.onRequest((request, response) => {
request.query.grant_type : request.body.grant_type;
const secondsInDay = 86400; // 60 * 60 * 24
const HTTP_STATUS_OK = 200;
console.log(`Grant type ${grantType}`);
functions.logger.log(`Grant type ${grantType}`);

let obj;
if (grantType === 'authorization_code') {
Expand All @@ -97,15 +99,13 @@ exports.faketoken = functions.https.onRequest((request, response) => {
.json(obj);
});

const app = smarthome({
debug: true,
});
const app = smarthome();

app.onSync((body) => {
return {
requestId: body.requestId,
payload: {
agentUserId: '123',
agentUserId: USER_ID,
devices: [{
id: 'washer',
type: 'action.devices.types.WASHER',
Expand Down Expand Up @@ -227,7 +227,7 @@ app.onExecute(async (body) => {
result.ids.push(device.id);
Object.assign(result.states, data);
})
.catch(() => console.error(`Unable to update ${device.id}`)) );
.catch(() => functions.logger.error('EXECUTE', device.id)));
}
}
}
Expand All @@ -242,7 +242,7 @@ app.onExecute(async (body) => {
});

app.onDisconnect((body, headers) => {
console.log('User account unlinked from Google Assistant');
functions.logger.log('User account unlinked from Google Assistant');
// Return empty response
return {};
});
Expand All @@ -251,17 +251,17 @@ exports.smarthome = functions.https.onRequest(app);

exports.requestsync = functions.https.onRequest(async (request, response) => {
response.set('Access-Control-Allow-Origin', '*');
console.info('Request SYNC for user 123');
functions.logger.info(`Request SYNC for user ${USER_ID}`);
try {
const res = await homegraph.devices.requestSync({
requestBody: {
agentUserId: '123',
agentUserId: USER_ID,
},
});
console.info('Request sync response:', res.status, res.data);
functions.logger.info('Request sync response:', res.status, res.data);
response.json(res.data);
} catch (err) {
console.error(err);
functions.logger.error(err);
response.status(500).send(`Error requesting sync: ${err}`);
}
});
Expand All @@ -272,12 +272,12 @@ exports.requestsync = functions.https.onRequest(async (request, response) => {
*/
exports.reportstate = functions.database.ref('{deviceId}').onWrite(
async (change, context) => {
console.info('Firebase write event triggered this cloud function');
functions.logger.info('Firebase write event triggered Report State');
const snapshot = change.after.val();

const requestBody = {
requestId: 'ff36a3cc', /* Any unique ID */
agentUserId: '123', /* Hardcoded user ID */
agentUserId: USER_ID,
payload: {
devices: {
states: {
Expand All @@ -295,6 +295,6 @@ exports.reportstate = functions.database.ref('{deviceId}').onWrite(
const res = await homegraph.devices.reportStateAndNotification({
requestBody,
});
console.info('Report state response:', res.status, res.data);
functions.logger.info('Report state response:', res.status, res.data);
});

4 changes: 2 additions & 2 deletions washer-done/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "smarthome-washer",
"description": "Codelab for controlling devices through the Google Assistant",
"engines": {
"node": "8"
"node": "10"
},
"scripts": {
"lint": "eslint ."
Expand All @@ -11,7 +11,7 @@
"actions-on-google": "^2.12.0",
"cors": "^2.8.5",
"firebase-admin": "^8.0.0",
"firebase-functions": "^3.2.0",
"firebase-functions": "^3.8.0",
"googleapis": "^43.0.0"
},
"private": true,
Expand Down
20 changes: 10 additions & 10 deletions washer-start/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ const homegraph = google.homegraph({
version: 'v1',
auth: auth,
});
// Hardcoded user ID
const USER_ID = '123';

exports.login = functions.https.onRequest((request, response) => {
if (request.method === 'GET') {
console.log('Requesting login page');
functions.logger.log('Requesting login page');
response.send(`
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand All @@ -54,7 +56,7 @@ exports.login = functions.https.onRequest((request, response) => {
// Here, you should validate the user account.
// In this sample, we do not do that.
const responseurl = decodeURIComponent(request.body.responseurl);
console.log(`Redirect to ${responseurl}`);
functions.logger.log(`Redirect to ${responseurl}`);
return response.redirect(responseurl);
} else {
// Unsupported method
Expand All @@ -66,7 +68,7 @@ exports.fakeauth = functions.https.onRequest((request, response) => {
const responseurl = util.format('%s?code=%s&state=%s',
decodeURIComponent(request.query.redirect_uri), 'xxxxxx',
request.query.state);
console.log(`Set redirect as ${responseurl}`);
functions.logger.log(`Set redirect as ${responseurl}`);
return response.redirect(
`/login?responseurl=${encodeURIComponent(responseurl)}`);
});
Expand All @@ -76,7 +78,7 @@ exports.faketoken = functions.https.onRequest((request, response) => {
request.query.grant_type : request.body.grant_type;
const secondsInDay = 86400; // 60 * 60 * 24
const HTTP_STATUS_OK = 200;
console.log(`Grant type ${grantType}`);
functions.logger.log(`Grant type ${grantType}`);

let obj;
if (grantType === 'authorization_code') {
Expand All @@ -97,9 +99,7 @@ exports.faketoken = functions.https.onRequest((request, response) => {
.json(obj);
});

const app = smarthome({
debug: true,
});
const app = smarthome();

app.onSync((body) => {
// TODO: Implement full SYNC response
Expand Down Expand Up @@ -135,7 +135,7 @@ app.onExecute((body) => {
});

app.onDisconnect((body, headers) => {
console.log('User account unlinked from Google Assistant');
functions.logger.log('User account unlinked from Google Assistant');
// Return empty response
return {};
});
Expand All @@ -144,7 +144,7 @@ exports.smarthome = functions.https.onRequest(app);

exports.requestsync = functions.https.onRequest(async (request, response) => {
response.set('Access-Control-Allow-Origin', '*');
console.info('Request SYNC for user 123');
functions.logger.info(`Request SYNC for user ${USER_ID}`);

// TODO: Call HomeGraph API for user '123'
response.status(500).send(`Request SYNC not implemented`);
Expand All @@ -156,7 +156,7 @@ exports.requestsync = functions.https.onRequest(async (request, response) => {
*/
exports.reportstate = functions.database.ref('{deviceId}').onWrite(
async (change, context) => {
console.info('Firebase write event triggered this cloud function');
functions.logger.info('Firebase write event triggered Report State');

// TODO: Get latest state and call HomeGraph API
});
Expand Down
4 changes: 2 additions & 2 deletions washer-start/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "smarthome-washer",
"description": "Codelab for controlling devices through the Google Assistant",
"engines": {
"node": "8"
"node": "10"
},
"scripts": {
"lint": "eslint ."
Expand All @@ -11,7 +11,7 @@
"actions-on-google": "^2.12.0",
"cors": "^2.8.5",
"firebase-admin": "^8.0.0",
"firebase-functions": "^3.2.0",
"firebase-functions": "^3.8.0",
"googleapis": "^43.0.0"
},
"private": true,
Expand Down

0 comments on commit 3ca572c

Please sign in to comment.