/
sparkCodec.js
179 lines (165 loc) · 4.64 KB
/
sparkCodec.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
const { connect } = require('jsxapi');
const request = require('superagent');
const { mockConnect } = require('./xapi');
/**
* Class wrapper for Cisco TP Codec
* Convenience Methods for using xAPI
* @class Codec
*/
class Codec {
/**
* Creates an instance of Codec.
* @param {object} codec
* @param {string} codec.name
* @param {string} codec.username
* @param {string} codec.macAddress
* @param {string} codec.password
* @param {string} codec.ip
* @param {object} [codec.io]
* @param {string} [codec.feedbackUrl]
* @memberof Codec
*/
constructor(codec) {
const { name, username, macAddress, password, ip, io, feedbackUrl } = codec;
this.name = name;
this.username = username;
this.password = password;
this.ip = ip;
this.macAddress = macAddress;
io && (this.io = io);
feedbackUrl && (this.feedbackUrl = feedbackUrl);
if (macAddress === 'DEMO') {
this.xapi = mockConnect();
}
else {
this.xapi = connect(this.ip, {
username: this.username,
password: this.password,
});
}
}
/**
* Initializes Codec and sets PeopleCount and Presence Detector to ON
* Also Registers feedback
* @memberof Codec
*/
async init() {
// Set a configuration
console.log('Setting Presence / PeopleCount to ON...');
this.xapi.config.set('RoomAnalytics PeoplePresenceDetector', 'On');
this.xapi.config.set('RoomAnalytics PeopleCountOutOfCall', 'On');
this.xapi.on('error', (err) => {
switch (err) {
case "client-socket":
console.error(`Could not connect to ${this.name}: invalid URL.`);
case "client-authentication":
console.error(`Could not connect to ${this.name}: invalid credentials`);
case "client-timeout":
console.error(`Could not connect to ${this.name}: timeout`);
default:
console.error(`Could not connect to ${this.name}: ${err}`);
}
});
this.registerFeedback(this.io);
try {
const status = await this.getStatus();
this.lastPresence = status[1];
this.lastCount = status[0];
}
catch (e) {
console.error('Could not fetch initial status');
}
}
/**
*
*
* @param {object} io // Socket.io instance
* @memberof Codec
*/
registerFeedback(io) {
this.xapi.status.on('RoomAnalytics', async (event) => {
const payload = {
codec: this.name,
ip: this.ip,
macAddress: this.macAddress
};
if (event.hasOwnProperty('PeoplePresence')) {
payload.type = 'PeoplePresence';
payload.data = event.PeoplePresence;
}
else if (event.hasOwnProperty('PeopleCount')) {
payload.type = 'PeopleCount'
payload.data = event.PeopleCount.Current;
}
console.log(`Emitting Event: ${JSON.stringify(payload)}`);
io.sockets.emit(payload.type, payload);
if (this.feedbackUrl) {
try {
console.log(`Sending POST Feedback to URL: ${this.feedbackUrl}`);
await request.post(this.feedbackUrl)
.send(payload)
}
catch (e) {
console.log(`Error sending feedback ${e}`);
}
}
});
// Disable for DEMO Codecs until mock is complete
if (this.macAddress !== 'DEMO') {
this.pairingFeedback = this.xapi.feedback.on('Status/spark/paireddevice/userid', async (personId) => {
try {
// NOTE: not sure how you want to set the url in your constructor, so I'm going to let you
// set this yourself. Needs to go to https://cl-proxbot.herokuapp.com/sendto
console.log(`Person paired: ${personId}`);
// Posting directly for CLEU
await request.post('https://cl-proxbot.herokuapp.com/sendto')
.send({ personId })
}
catch (e) {
console.log(`Error sending feedback ${e}`);
}
});
}
}
/**
*
*
* @returns
* @memberof Codec
*/
deregisterFeedback() {
if (this.macAddress !== 'DEMO') {
return this.presenceFeedback();
}
else {
this.xapi.status.removeAllListeners('RoomAnalytics');
this.xapi.status.stop();
return true;
}
}
/**
*
*
* @returns {Promise} //
* @memberof Codec
*/
async getStatus() {
let count = this.xapi.status
.get('RoomAnalytics PeopleCount Current')
let presence = this.xapi.status
.get('RoomAnalytics PeoplePresence')
return Promise.all([count, presence]);
}
toJSON(key) {
return {
name: this.name,
ip: this.ip,
macAddress: this.macAddress,
lastPresence: this.lastPresence,
lastCount: this.lastCount
}
}
}
module.exports = {
Codec
};