-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
99 lines (71 loc) · 2.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
process.env.DEBUG = 'actions-on-google:*';
const Assistant = require('actions-on-google').ApiAiAssistant;
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const bonsaiStatus = admin.database().ref('bonsai_status');
// API.AI Intent names
const WATER_INTENT = 'water-duration';
const STOP_INTENT = 'stop-watering';
// Contexts
const WATER_CONTEXT = 'water_duration';
const STOP_CONTEXT = 'stop';
// Context Parameters
const DURATION_PARAM = 'duration';
const FULFILLMENT_PARAM = 'fulfillment';
var userRequest;
exports.assistantcodelab = functions.https.onRequest((request, response) => {
const assistant = new Assistant({request: request, response: response});
userRequest = request;
let actionMap = new Map();
actionMap.set(WATER_INTENT, water);
actionMap.set(STOP_INTENT, stop)
assistant.handleRequest(actionMap);
/**
*Code to start the watering process
**/
function water(assistant) {
var d = new Date();
var n = d.getTime();
const duration = assistant.getContextArgument(WATER_CONTEXT, DURATION_PARAM).value;
const numberTime = duration.amount;
const unitTime = duration.unit;
var timeInSeconds = 120;//default
switch(unitTime){
case "h":
timeInSeconds = numberTime *60*60;
break;
case "s":
timeInSeconds = numberTime;
break;
default:
timeInSeconds = numberTime * 60;//default to unit being minutes
break;
}
var currentStatus = {
'status_date': 0,
'watering': true,
'watering_duration': 120
};
currentStatus.status_date = n;
currentStatus.watering_duration = timeInSeconds;
bonsaiStatus.push().set(currentStatus);
assistant.tell(request.body.result.fulfillment.speech);
}
/**
*Code to stop the watering process
**/
function stop(assistant) {
var d = new Date();
var n = d.getTime();
var currentStatus = {
'status_date': 0,
'watering': false,
'watering_duration': 0
};
currentStatus.status_date = n;
bonsaiStatus.push().set(currentStatus);
assistant.tell(request.body.result.fulfillment.speech);
}
});