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 <[email protected]>
Change-Id: I9da636fcf3e472b00fe76f3e9a12b63e054e9eca
  • Loading branch information
Dave Smith committed Jul 28, 2020
1 parent 08a5fb7 commit afde5b2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 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 @@ -311,7 +311,7 @@ app.onExecute(async (body) => {
Object.assign(result.states, data);
})
.catch((error) => {
console.error(`Unable to update ${device.id}.`, error);
functions.logger.error('EXECUTE', device.id, error);
result.ids.push(device.id);
if (error instanceof SmartHomeError) {
result.status = 'ERROR';
Expand All @@ -338,7 +338,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 @@ -347,17 +347,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 @@ -368,12 +368,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 @@ -397,6 +397,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
34 changes: 17 additions & 17 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,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 @@ -245,7 +245,7 @@ app.onExecute(async (body) => {
})
// TODO: Add error response handling
.catch(() => {
console.error(`Unable to update ${device.id}`);
functions.logger.error(`Unable to update ${device.id}`);
}),
);
}
Expand All @@ -262,7 +262,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 @@ -271,17 +271,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 @@ -292,12 +292,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 @@ -317,6 +317,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-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 afde5b2

Please sign in to comment.