-
Notifications
You must be signed in to change notification settings - Fork 1
/
crownstone-localize-user.js
157 lines (134 loc) · 6.36 KB
/
crownstone-localize-user.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
module.exports = function (RED) {
function CrownstoneLocalizeUser(config) {
RED.nodes.createNode(this, config);
var node = this;
// Input field values
var sphereId = config.sphereId;
var userId = config.userId;
// Retrieve the cloud object from global context
var globalContext = node.context().global;
var cloud;
// Wait one tick of the event loop in case the authenticate node runs later and did not yet store the cloud in global context
setImmediate(() => {
cloud = globalContext.get("crownstoneCloud");
if (cloud === undefined) { // Cloud object is not stored in global context. The authentication node is not used.
node.error("The cloud object is not stored in global context. Use the Crownstone authenticate node.");
return;
}
});
// Input event. This code executes when the node gets triggered. 'msg' is the object that is received from the previous node.
node.on('input', function (msg, send, done) {
(async () => {
// Overwrite default node properties with incoming values
if (msg.userId !== undefined) {
userId = msg.userId;
if (msg.sphereId !== undefined) {
sphereId = msg.sphereId;
}
}
// Get the sphere
let sphere = cloud.sphere(sphereId);
// Request present people in the sphere
let presentPeople = await sphere.presentPeople();
if (presentPeople.length === 0) { // No present people
send(msg);
return;
}
// Get location of selected user.
let user = presentPeople.find(person => person.userId === userId);
if (user === undefined) { // Choosen user not present
send(msg);
return;
}
if (user.locations.length === 0) { // User not in a location
send(msg);
return;
}
// Get location id
let userLocationId = user.locations[0];
//msg.locationId = userLocationId;
// Get location name
let locations = await cloud.locations();
let userLocationName = locations.find(location => location.id === userLocationId).name;
//msg.locationName = userLocationName;
msg.payload = { "locationName": userLocationName, "locationId": userLocationId };
send(msg);
})().catch((e) => {
if (e.statusCode === 401) {
msg.payload = e;
node.error("Authorization Required", msg);
return;
}
else {
msg.payload = e;
node.error("There was a problem localizing the user", msg);
return;
}
});
});
}
RED.nodes.registerType("crownstone localize user", CrownstoneLocalizeUser);
// This section is for the oneditprepare event in the browser to get a list of spheres.
RED.httpAdmin.get("/spheres/:nodeId", function (req, res) {
var node = RED.nodes.getNode(req.params.nodeId); // This is a reference to the currently deployed node in runtime. This does not work if the user just dragged the node on the workspace.
if (node === null) { // Node with the given id does not exist
res.statusCode = 400;
res.json({ "error": "Node with the given id does not exist" });
return;
}
var globalContext = node.context().global;
var cloud = globalContext.get("crownstoneCloud");
if (cloud === undefined) { // Cloud object is not stored in global context
res.statusCode = 401;
res.json({ "error": "Cloud object is not available. You are unauthorized to make this request." });
return;
}
(async () => {
// Request spheres
let spheres = await cloud.spheres();
// Map the list of spheres to a more compact format
let spheresMapped = spheres.map(sphere => ({ "id": sphere.id, "name": sphere.name }));
// res.setHeader('Cache-Control', 'max-age=120, public');
res.statusCode = 200;
res.json(spheresMapped);
return;
})();
});
// This section is for the oneditprepare event in the browser to get a list of users.
RED.httpAdmin.get("/users/:nodeId/:sphereId", function (req, res) {
var node = RED.nodes.getNode(req.params.nodeId); // This is a reference to the currently deployed node in runtime. This does not work if the user just dragged the node on the workspace.
if (node === null) { // Node with the given id does not exist
res.statusCode = 400;
res.json({ "error": "Node with the given id does not exist" });
return;
}
var globalContext = node.context().global;
var cloud = globalContext.get("crownstoneCloud");
if (cloud === undefined) { // Cloud object is not stored in global context
res.statusCode = 401;
res.json({ "error": "Cloud object is not available. You are unauthorized to make this request." });
return;
}
let sphere = cloud.sphere(req.params.sphereId);
if (sphere === undefined) { // Choosen sphere not present or accessable by this user
res.statusCode = 403;
res.json({ "error": "Choosen sphere not present or accessable by this user" });
return;
}
(async () => {
// Request users from the sphere
let users = await sphere.users();
// Map the list of users to a more compact format
let usersMapped = [];
for (let level in users) {
for (let user of users[level]) {
usersMapped.push({ "id": user.id, "firstName": user.firstName, "lastName": user.lastName });
}
}
// res.setHeader('Cache-Control', 'max-age=120, public');
res.statusCode = 200;
res.json(usersMapped);
return;
})();
});
}