forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome-automation-state.js
More file actions
79 lines (73 loc) · 2.86 KB
/
home-automation-state.js
File metadata and controls
79 lines (73 loc) · 2.86 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const HOME_AUTOMATION_STATE_PROPERTY = 'homeAutomation.state';
class DeviceState {
/**
*
* @param {String} Device Name
* @param {String} Room the device is in
* @param {String} Device properties - e.g. temperature=72'F, brightnessLevel=20%
* @param {String} State of the device - on/ off/ standby/ pause etc.
*/
constructor(device, room, deviceProperty, deviceState) {
this.deviceName = device || '';
this.room = room || '';
this.deviceProperty = deviceProperty || '';
this.deviceState = deviceState || '';
}
};
class HomeAutomationState {
/**
*
* @param {ConversationState} convoState Conversation state
* @param {UserState} userState User state
*/
constructor(conversationState, userState) {
if (!conversationState) throw new Error('Invalid conversation state provided.');
if (!userState) throw new Error('Invalid user state provided.');
// Device property accessor for home automation scenario.
this.deviceProperty = conversationState.createProperty(HOME_AUTOMATION_STATE_PROPERTY);
}
/**
*
* @param {String} device
* @param {String} room
* @param {String} deviceState
* @param {String} deviceProperty
* @param {TurnContext} context object
*/
async setDevice(device, room, deviceState, deviceProperty, context) {
// Get devices from state.
let operations = await this.deviceProperty.get(context);
if (operations === undefined) {
operations = new Array(new DeviceState(device, room, deviceProperty, deviceState));
} else {
// add this operation
operations.push(new DeviceState(device, room, deviceProperty, deviceState));
}
return this.deviceProperty.set(context, operations);
}
/**
*
* @param {TurnContext} context object
* @returns {String} text readout of state operations
*/
async getDevices(context) {
let returnText = 'No operations found';
// read out of current devices from state
const operations = await this.deviceProperty.get(context);
if (operations === undefined) {
return returnText;
}
returnText = '';
operations.forEach((device, idx) => {
returnText += '\n[' + idx + ']. ' +
(device.deviceName ? device.deviceName : 'Unknown device') +
(device.room ? ' in room = ' + device.room : '') +
(device.deviceState ? ' is ' + device.deviceState : '') +
(device.deviceProperty ? ' device property = ' + device.deviceProperty : '');
});
return returnText;
}
}
module.exports.HomeAutomationState = HomeAutomationState;