-
Notifications
You must be signed in to change notification settings - Fork 0
/
fulfillment.js
210 lines (190 loc) · 5.38 KB
/
fulfillment.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Owlet app login credentials
const OWLET_EMAIL = "";
const OWLET_PASSWORD = "";
const functions = require("firebase-functions");
const { dialogflow } = require("actions-on-google");
const { connect } = require("@williamshupe/owlet");
const app = dialogflow();
const owletPromise = connect(OWLET_EMAIL, OWLET_PASSWORD);
const deviceIdPromise = owletPromise
.then((owlet) => owlet.getDevices())
.then((devices) => devices[0].id);
const getProperties = () =>
owletPromise.then((owlet) => {
return deviceIdPromise.then((deviceId) => {
return owlet.getProperties(deviceId);
});
});
const baseStationOffCheck = (conv, props) => {
if (!props.isBaseStationOn) {
conv.ask("I'm not sure, the base station is not turned on");
return true;
}
};
const sockDisconnectedCheck = (conv, props) => {
if (!props.isSockConnected) {
conv.ask("I'm not sure, the sock is not connected to the base station");
return true;
}
};
const sockOffCheck = (conv, props) => {
if (props.isSockOff) {
conv.ask(`I'm not sure, ${props.babyName} is not wearing the sock.`);
return true;
}
};
app.intent("Get status", (conv) => {
const command = conv.parameters.status_property;
return getProperties().then((props) => {
if (command === "on") {
if (props.isBaseStationOn) {
conv.ask("Yes, the base station is turned on.");
} else {
conv.ask("No, the base station is turned off");
}
return;
}
if (command === "off") {
if (props.isBaseStationOn) {
conv.ask("No, the base station is turned on.");
} else {
conv.ask("Yes, the base station is turned off");
}
return;
}
if (command === "charging") {
if (props.isCharging) {
conv.ask("Yes, the sock is plugged in and charging.");
} else {
conv.ask("No, the sock is not currently charging.");
}
return;
}
if (command === "charged") {
if (props.batteryLevel === 100) {
conv.ask("Yes, the sock is fully charged.");
} else {
conv.ask("No, the sock is not fully charged");
}
return;
}
if (command === "connected") {
if (props.isSockConnected) {
conv.ask("Yes, the sock is connected to the base station.");
} else {
conv.ask("No, the sock is not connected");
}
}
});
});
app.intent("Get charge", (conv) => {
return getProperties().then((props) => {
if (sockDisconnectedCheck(conv, props)) {
return;
}
conv.ask(`The sock has ${props.batteryLevel} percent of battery left.`);
});
});
app.intent("Get readings", (conv) => {
return getProperties().then((props) => {
if (baseStationOffCheck(conv, props)) {
return;
}
if (sockDisconnectedCheck(conv, props)) {
return;
}
if (props.isWiggling) {
conv.ask(`${props.babyName} is wiggling`);
return;
}
conv.ask(
`${props.babyName}'s heart rate is ${props.heartRate} beats per minute and their oxygen level is ${props.oxygenLevel} percent`
);
});
});
app.intent("Get wiggling", (conv) => {
return getProperties().then((props) => {
if (baseStationOffCheck(conv, props)) {
return;
}
if (sockDisconnectedCheck(conv, props)) {
return;
}
if (sockOffCheck(conv, props)) {
return;
}
if (props.isWiggling) {
conv.ask(`Yes, ${props.babyName} is wiggling`);
} else {
conv.ask(`No, ${props.babyName} is not wiggling`);
}
});
});
app.intent("Get reading", (conv) => {
const command = conv.parameters.reading_property;
return getProperties().then((props) => {
if (baseStationOffCheck(conv, props)) {
return;
}
if (sockDisconnectedCheck(conv, props)) {
return;
}
if (command === "heart rate") {
conv.ask(
`${props.babyName}'s heart rate is ${props.heartRate} beats per minute`
);
return;
}
if (command === "oxygen level") {
conv.ask(
`${props.babyName}'s oxygen level is ${props.oxygenLevel} percent`
);
}
});
});
app.intent("Set state", (conv) => {
const command = conv.parameters.set_state;
return owletPromise.then((owlet) => {
return deviceIdPromise.then((deviceId) => {
if (command === "on") {
return owlet.turnBaseStationOn(deviceId).then(() => {
conv.ask("Sure, turning the base station on");
});
}
if (command === "off") {
return owlet.turnBaseStationOff(deviceId).then(() => {
conv.ask("Sure, turning off the base station");
});
}
});
});
});
app.intent("Get wearing sock", (conv) => {
return getProperties().then((props) => {
if (sockDisconnectedCheck(conv, props)) {
return true;
}
if (props.isSockOff) {
if (props.isBaseStationOn) {
conv.ask(
`${props.babyName} is not wearing the sock, but the base station is turned on.`
);
} else {
conv.ask(
`${props.babyName} is not wearing the sock and the base station is turned off.`
);
}
} else {
if (props.isBaseStationOn) {
conv.ask(
`${props.babyName} is wearing the sock and the base station is turned on.`
);
} else {
conv.ask(
`${props.babyName} is wearing the sock, but the base station is turned off.`
);
}
}
});
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);