-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_scene.js
More file actions
89 lines (77 loc) · 2.08 KB
/
create_scene.js
File metadata and controls
89 lines (77 loc) · 2.08 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
80
81
82
83
84
85
86
87
88
89
var request = require('request');
var config = require('./config.json');
// Example scene for 2 devices.
// When activated it will call the designated action shown below.
// In this example, each device will call action":"setOff"
var scene_on = {
"name": "Scene living room ON",
"description": "scene to turn ON all lights in living room",
"actions": [
{
"ddid": config.device1_id,
"action": "setOn",
"parameters": {}
},
{
"ddid": config.device2_id,
"action": "setOn",
"parameters": {}
},
]
};
// Example scene for 2 devices.
// When activated it will call the designated action shown below.
// In this example, each device will call action":"setOn"
var scene_off = {
"name": "Scene living room OFF",
"description": "scene to turn OFF all lights in living room",
"actions": [
{
"ddid": config.device1_id,
"action": "setOff",
"parameters": {}
},
{
"ddid": config.device2_id,
"action": "setOff",
"parameters": {}
}
]
};
/**
* API call to `create` scene
* @params config.user_token - user token which has authenticated oauth2 application
* @params scene_data - new scene payload for making the API request
*/
function create_scene(scene_data) {
var options = {
method:"POST",
url:"https://api.artik.cloud/v1.1/scenes",
headers:{
'Authorization': 'Bearer ' + config.user_token,
'Content-Type': 'application/json'
},
body: scene_data,
json:true
};
request(options, function (error, response, body) {
handle_response(error, response, body)
});
}
/**
* helper function to handle the api response
*/
function handle_response(error, response, body) {
if(error) throw new Error(error);
try {
console.log("\n", JSON.stringify(body));
} catch(e) {
console.log("\n> ", e);
}
}
console.log("\nVisit https://my.artik.cloud/scenes after scenes are created")
console.log("\n1. Creating Scene 1: ", scene_on.name)
console.log("\n2. Creating Scene 2: ", scene_off.name)
console.log("\nResponse data of API calls are shown below ... ")
create_scene(scene_on);
create_scene(scene_off);