-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOscClient.js
260 lines (220 loc) · 10.3 KB
/
OscClient.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2015 Bubl Technology Inc.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed
// except according to those terms.
'use strict';
var request;
var Poll;
var Q;
(function(isNode, isAngular) {
var OscClient = function(domainArg, portArg) {
var domain = domainArg || 'localhost';
var port = portArg || 8000;
var serverAddress = 'http://' + domain + ':' + port;
// HTTP CONTENT TYPES
var applicationJsonType = 'application/json; charset=utf-8';
var applicationOctetType = 'application/octet-stream';
// HTTP REQUEST
var makeHttpRequest = function(method, url, contentType, body) {
var deferred = Q.defer();
request(httpRequestBody(method, url, contentType, body), function(err, res, body) {
if (res.headers['content-type'] === 'application/json; charset=utf-8') {
body = JSON.parse(body);
}
deferred.resolve({'error': err, 'body': body, 'response': res});
});
return deferred.promise;
};
// HTTP REQUEST BODY
var httpRequestBody = function(method, url, contentType, body) {
if (contentType !== 'application/octet-stream') {
body = JSON.stringify(body);
}
return {
method: method,
url: url,
body: body,
headers: {
'Content-Type': contentType,
'X-XSRF-Protected': '1'
},
withCredentials: false,
responseType: 'arraybuffer'
};
};
// OSC INFO
var infoUrl = serverAddress + '/osc/info';
this.getInfo = function() {
return makeHttpRequest('GET', infoUrl, applicationJsonType);
};
// OSC STATE
var stateUrl = serverAddress + '/osc/state';
this.getState = function() {
return makeHttpRequest('POST', stateUrl, applicationJsonType);
};
// OSC CHECK FOR UPDATES
var checkForUpdatesUrl = serverAddress + '/osc/checkForUpdates';
this.checkForUpdates = function(stateFingerprint, waitTimeout) {
return makeHttpRequest('POST', checkForUpdatesUrl, applicationJsonType, {'stateFingerprint': stateFingerprint, 'waitTimeout': waitTimeout});
};
// OSC COMMANDS EXECUTE
var commandsExecuteUrl = serverAddress + '/osc/commands/execute';
var commandsRequest = function(name, params, callback) {
request(httpRequestBody('POST', commandsExecuteUrl, 'application/json; charset=utf-8', {'name': name, 'parameters': params}), callback);
};
var commandsExecute = function(name, params, statusCallback) {
var deferred = Q.defer();
commandsRequest(name, params, function(err, res, body) {
var timeStamp = Date.now();
if (res.headers['content-type'] === 'application/json; charset=utf-8') {
var results = JSON.parse(body);
if (results.state !== 'inProgress') {
deferred.resolve({'error': err, 'response': res, 'body': results});
} else {
var commandId = results.id;
if(isNode) { // TODO: remove the condition once poll.js is set up for sharing
Poll.commandStatus(this, commandId, deferred, timeStamp, statusCallback);
} else {
deferred.resolve({'error': err, 'response': res, 'body': results});
}
}
} else {
deferred.resolve({'error': err, 'response': res, 'body': body}); // only for getImage command
}
}.bind(this));
return deferred.promise;
}.bind(this);
// OSC COMMANDS START SESSION
this.startSession = function(timeout) {
return commandsExecute('camera.startSession', {'timeout': timeout});
};
// OSC COMMANDS UPDATE SESSION
this.updateSession = function(sessionId, timeout) {
return commandsExecute('camera.updateSession', {'sessionId': sessionId, 'timeout': timeout});
};
// OSC COMMANDS CLOSE SESSION
this.closeSession = function(sessionId) {
return commandsExecute('camera.closeSession', {'sessionId': sessionId});
};
// OSC COMMANDS TAKE PICTURE
this.takePicture = function(sessionId, statusCallback) {
return commandsExecute('camera.takePicture', {'sessionId':sessionId}, statusCallback);
};
// OSC COMMANDS LIST IMAGES
this.listImages = function(entryCount, includeThumb, maxSize, continuationToken) {
return commandsExecute('camera.listImages', {'entryCount': entryCount, 'includeThumb': includeThumb, 'maxSize': maxSize, 'continuationToken': continuationToken});
};
// OSC COMMANDS DELETE
this.delete = function(fileUri) {
return commandsExecute('camera.delete', {'fileUri': fileUri});
};
// OSC COMMANDS GET IMAGE
this.getImage = function(fileUri, maxSize) {
return commandsExecute('camera.getImage', {'fileUri': fileUri, 'maxSize': maxSize});
};
// OSC COMMANDS GET METADATA
this.getMetadata = function(fileUri) {
return commandsExecute('camera.getMetadata', {'fileUri': fileUri});
};
// OSC COMMANDS SET OPTIONS
this.setOptions = function(sessionId, options) {
return commandsExecute('camera.setOptions', {'sessionId': sessionId, 'options': options});
};
// OSC COMMANDS GET OPTIONS
this.getOptions = function(sessionId, optionNames) {
return commandsExecute('camera.getOptions', {'sessionId': sessionId, 'optionNames': optionNames});
};
// OSC COMMANDS STATUS
var commandsStatusUrl = serverAddress + '/osc/commands/status';
this.commandsStatus = function(commandId) {
return makeHttpRequest('POST', commandsStatusUrl, applicationJsonType, {'id': commandId});
};
/*********************************************************/
/* Bubl's Vendor Specific Endpoints and Commands */
/*********************************************************/
// OSC BUBL UPDATE
var bublUpdateUrl = serverAddress + '/osc/_bublUpdate';
this.bublUpdate = function(updateFileBin) {
return makeHttpRequest('POST', bublUpdateUrl, applicationOctetType, updateFileBin);
};
// OSC BUBL GET IMAGE
var bublGetImageUrl = serverAddress + '/osc/_bublGetImage/';
this.bublGetImage = function(fileUri) {
return makeHttpRequest('GET', bublGetImageUrl + encodeURIComponent(fileUri), applicationJsonType);
};
// BUBL STOP
var bublStopUrl = serverAddress + '/osc/commands/_bublStop';
this.bublStop = function(commandId) {
return makeHttpRequest('POST', bublStopUrl, applicationJsonType, {'id': commandId});
};
// BUBL POLL
var bublPollUrl = serverAddress + '/osc/commands/_bublPoll';
this.bublPoll = function(commandId, fingerprint, waitTimeout) {
return makeHttpRequest('POST', bublPollUrl, applicationJsonType, {'id': commandId, 'fingerprint': fingerprint, 'waitTimeout': waitTimeout});
};
// BUBL COMMANDS CAPTURE VIDEO
this.bublCaptureVideo = function(sessionId, statusCallback) {
return commandsExecute('camera._bublCaptureVideo', {'sessionId':sessionId}, statusCallback);
};
// OSC COMMANDS BUBL TIMELAPSE
this.bublTimelapse = function(sessionId, statusCallback) {
return commandsExecute('camera._bublTimelapse', {'sessionId':sessionId}, statusCallback);
};
// OSC COMMANDS BUBL STREAM
this.bublStream = function(sessionId, statusCallback) {
return commandsExecute('camera._bublStream', {'sessionId': sessionId}, statusCallback);
};
// OSC COMMANDS BUBL SHUTDOWN
this.bublShutdown = function(sessionId, shutdownDelay) {
return commandsExecute('camera._bublShutdown', {'sessionId':sessionId, 'shutdownDelay':shutdownDelay});
};
};
if(isAngular) {
angular.module('clientUi')
.factory('OscClient', function() { return OscClient; });
var $http = angular.injector(['ng']).get('$http');
var _convert2String = function(data) {
if(data === null) { return 'null'; }
var dataU8 = new Uint8Array(data);
var dataStr = '';
for(var i = 0; i < dataU8.byteLength; i++) {
dataStr += String.fromCharCode(dataU8[i]);
}
return dataStr;
};
request = function(reqObject, callOnComplete) {
reqObject.data = reqObject.body;
delete reqObject.body;
$http(reqObject)
.success(function(data, status, headers) {
callOnComplete(
undefined,
{
'headers':{'content-type':headers('content-type')},
'statusCode':status
},
(headers('content-type') === 'application/json; charset=utf-8')?_convert2String(data):data
);
})
.error(function(err, status, headers) {
callOnComplete(
undefined,
{
'headers':{'content-type':headers('content-type')},
'statusCode':status
},
_convert2String(err)
);
});
};
Q = window.Q;
} else if(isNode) {
request = require('./server_tests/node_modules/request');
Poll = require('./server_tests/lib/poll.js');
Q = require('./server_tests/node_modules/q');
module.exports = OscClient;
}
})( typeof module !== 'undefined' && module.exports,
typeof angular !== 'undefined');