-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
430 lines (371 loc) · 13.4 KB
/
index.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
'use strict';
var fs = require('fs');
var url = require('url');
var http = require('http');
var semver = require('semver');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var minVersionRequired = '2.1.4';
(function () {
var SonyCamera = function (url, port, path) {
this.url = url || '192.168.122.1';
this.port = port || 8080;
this.path = path || '/sony/camera';
this.method = "old";
this.rpcReq = {
id: 1,
version: '1.0'
};
this.params = {};
this.status = "UNKNOWN";
this.connected = false;
this.ready = false;
this.availableApiList = [];
};
util.inherits(SonyCamera, EventEmitter);
SonyCamera.prototype.show = function () {
console.log(this.url + ':' + this.port + this.path);
};
SonyCamera.prototype.call = function (method, params, callback) {
var self = this;
this.rpcReq.method = method;
this.rpcReq.params = params || [];
var postData = JSON.stringify(this.rpcReq);
var timeoutHandle = null;
var req = http.request({
method: 'POST',
hostname: this.url,
port: this.port,
path: this.path,
timeout: 2000,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
}, function (res) {
//console.log(res);
res.setEncoding('utf8');
var rawData = '';
res.on('data', function(chunk) {
rawData += chunk;
});
var parsedData = null;
res.on('end', function() {
clearTimeout(timeoutHandle);
try {
parsedData = JSON.parse(rawData);
var result = parsedData ? parsedData.result : null;
var error = parsedData ? parsedData.error : null;
//console.log(result);
if(error) {
if(error.length > 0 && error[0] == 1 && method == 'getEvent') {
setTimeout(function() {
self.call(method, params, callback);
});
return;
}
console.log("SonyWifi: error during request", method, error);
}
//console.log("completed", error, result);
callback && callback(error, result);
} catch (e) {
console.log(e.message);
callback && callback(e);
}
});
});
timeoutHandle = setTimeout(function() {
req.abort();
console.log("SonyWifi: network appears to be disconnected (timeout for " + method + ")");
self.emit('disconnected');
}, 60000);
req.write(postData);
req.end();
req.on('error', function(err){
clearTimeout(timeoutHandle);
if(err && err.code) {
console.log("SonyWifi: network appears to be disconnected (error for " + method + ": " + err + ", err.code:", err.code, ")");
if(method == "getApplicationInfo" && err.code == 'ECONNREFUSED' && self.method == 'old') {
console.log("SonyWifi: ECONNREFUSED when connecting to port " + this.port + ", trying new method...");
self.method = 'new';
self.port = 10000;
setTimeout(function() {
self.call(method, params, callback);
});
return;
}
self.emit('disconnected');
}
callback && callback(err);
});
};
SonyCamera.prototype._processEvents = function (waitForChange, callback) {
var self = this;
this.eventPending = true;
this.call('getEvent', [waitForChange || false], function (err, results) {
self.eventPending = false;
// console.log(err);
if (!err) {
for(var i = 0; i < results.length; i++) {
var item = results[i];
if(item instanceof Array) {
if(item.length > 0) {
item = {
type: item[0].type,
items: item
}
} else {
continue;
}
}
if(!item) {
continue;
} else if(item.type && item.type == 'cameraStatus') {
self.status = item.cameraStatus;
if(self.status == "NotReady") {
self.connected = false;
console.log("SonyWifi: disconnected, trying to reconnect");
setTimeout(function(){self.connect(); }, 2500);
}
if(self.status == "IDLE") self.ready = true; else self.ready = false;
if(self.status != item.cameraStatus) {
self.emit('status', item.cameraStatus);
console.log("SonyWifi: status", self.status);
}
} else if(item.type && item.type == 'storageInformation') {
for(var j = 0; j < item.items.length; j++) {
if(item.items[j].recordTarget) {
self.photosRemaining = item.items[j].numberOfRecordableImages || 0;
}
}
} else if(item.type && item.type == 'availableApiList') {
self.availableApiList = item.names || [];
} else if(item.type && item[item.type + 'Candidates']) {
var oldVal = self.params[item.type] ? self.params[item.type].current : null;
self.params[item.type] = {
current: item['current' + item.type.charAt(0).toUpperCase() + item.type.slice(1)],
available: item[item.type + 'Candidates'],
}
if(oldVal !== self.params[item.type].current) {
console.log("SonyWifi: " + item.type + " = " + self.params[item.type].current + "(+" + (self.params[item.type].available ? self.params[item.type].available.length : "NULL") + " available)");
self.emit("update", item.type, self.params[item.type]);
}
}
}
}
if (callback) {
callback(err);
}
});
};
SonyCamera.prototype.connect = function (callback) {
var self = this;
if(this.connecting) return callback && callback('Already trying to connect');
this.connecting = true;
console.log("SonyWifi: connecting...");
this.getAppVersion(function(err, version) {
if(!err && version) {
console.log("SonyWifi: app version", version);
if(semver.gte(version, minVersionRequired)) {
var connected = function() {
self.connected = true;
var _checkEvents = function(err) {
if(!err) {
if(self.connected) self._processEvents(true, _checkEvents); else console.log("SonyWifi: disconnected, stopping event poll");
} else {
setTimeout(_checkEvents, 5000);
}
};
self._processEvents(false, function(){
self.connecting = false;
callback && callback(err);
_checkEvents();
});
}
if(self.method == "old") {
self.call('startRecMode', null, function(err) {
if(!err && !self.connected) {
connected();
} else {
self.connecting = false;
callback && callback(err);
}
});
} else {
connected();
}
} else {
callback(
{
err: 'APPVERSION',
message:'Could not connect to camera -- remote control application must be updated (currently installed: ' + version + ', should be ' + minVersionRequired + ' or newer)'
}
);
}
} else {
self.connecting = false;
callback && callback(err);
}
});
};
SonyCamera.prototype.disconnect = function (callback) {
this.call('stopRecMode', null, function(err) {
if(!err) {
this.connected = false;
}
callback && callback(err);
});
};
SonyCamera.prototype.startViewfinder = function (req, res) {
var self = this;
this.call('startLiveview', null, function (err, output) {
var liveviewUrl = url.parse(output[0]);
//console.log(liveviewUrl);
var COMMON_HEADER_SIZE = 8;
var PAYLOAD_HEADER_SIZE = 128;
var JPEG_SIZE_POSITION = 4;
var PADDING_SIZE_POSITION = 7;
var jpegSize = 0;
var paddingSize = 0;
var bufferIndex = 0;
var liveviewReq = http.request(liveviewUrl, function (liveviewRes) {
var imageBuffer;
var buffer = Buffer.alloc ? Buffer.alloc(0) : new Buffer(0);
liveviewRes.on('data', function (chunk) {
if (jpegSize === 0) {
buffer = Buffer.concat([buffer, chunk]);
if (buffer.length >= (COMMON_HEADER_SIZE + PAYLOAD_HEADER_SIZE)) {
jpegSize =
buffer.readUInt8(COMMON_HEADER_SIZE + JPEG_SIZE_POSITION) * 65536 +
buffer.readUInt16BE(COMMON_HEADER_SIZE + JPEG_SIZE_POSITION + 1);
imageBuffer = Buffer.alloc ? Buffer.alloc(jpegSize) : new Buffer(jpegSize);
paddingSize = buffer.readUInt8(COMMON_HEADER_SIZE + PADDING_SIZE_POSITION);
buffer = buffer.slice(8 + 128);
if (buffer.length > 0) {
buffer.copy(imageBuffer, bufferIndex, 0, buffer.length);
bufferIndex += buffer.length;
}
}
} else {
chunk.copy(imageBuffer, bufferIndex, 0, chunk.length);
bufferIndex += chunk.length;
if (chunk.length < jpegSize) {
jpegSize -= chunk.length;
} else {
self.emit('liveviewJpeg', imageBuffer);
buffer = chunk.slice(jpegSize + paddingSize);
jpegSize = 0;
bufferIndex = 0;
}
}
});
liveviewRes.on('end', function () {
console.log('End');
});
liveviewRes.on('close', function () {
console.log('Close');
});
});
liveviewReq.on('error', function(e) {
console.error('Error: ', e);
});
liveviewReq.end();
});
};
SonyCamera.prototype.stopViewfinder = function (callback) {
this.call('stopLiveview', null, callback);
};
SonyCamera.prototype.capture = function (enableDoubleCallback, callback) {
var self = this;
if(!callback && typeof enableDoubleCallback == "function") {
callback = enableDoubleCallback;
enableDoubleCallback = false;
}
if(this.status != "IDLE") {
console.log("SonyWifi: camera busy, capture not available. Status:", this.status);
return callback && callback('camera not ready');
}
this.ready = false;
var processCaptureResult = function(err, output) {
if (err) {
if(err.length > 0 && err[0] == 40403) { // capture still in progress
self.call('awaitTakePicture', null, processCaptureResult);
} else {
callback && callback(err);
}
return;
}
var url = output[0][0];
var parts = url.split('?')[0].split('/');
var photoName = parts[parts.length - 1];
console.log("SonyWifi: Capture complete:", photoName);
if(enableDoubleCallback) callback && callback(err, photoName);
http.get(url, function(res) {
//res.setEncoding('binary');
var statusCode = res.statusCode;
var contentType = res.headers['content-type'];
var error;
if (statusCode !== 200) {
error = new Error('Request Failed. Status Code:', statusCode);
}
if (error) {
//console.log(error.message);
// consume response data to free up memory
res.resume();
callback && callback(err);
return;
}
var rawData = [];
res.on('data', function(chunk) {
//console.log("got data", chunk.length);
rawData.push(chunk);
});
res.on('end', function() {
console.log("SonyWifi: Retrieved preview image:", photoName);
callback && callback(null, photoName, Buffer.concat(rawData));
});
}).on('error', function(e) {
callback && callback(e);
});
}
self.call('actTakePicture', null, processCaptureResult);
};
SonyCamera.prototype.startBulbShooting = function (callback) {
console.log('startBulbShooting');
this.call('startBulbShooting', null, callback);
};
SonyCamera.prototype.stopBulbShooting = function (callback) {
console.log('stopBulbShooting');
this.call('stopBulbShooting', null, callback);
};
SonyCamera.prototype.zoomIn = function (callback) {
this.call('actZoom', ['in', 'start'], callback);
};
SonyCamera.prototype.zoomOut = function (callback) {
this.call('actZoom', ['out', 'start'], callback);
};
SonyCamera.prototype.getAppVersion = function (callback) {
this.call('getApplicationInfo', null, function(err, res) {
var version = null;
if(!err && res && res.length > 1) {
version = res[1];
}
callback && callback(err, version);
});
};
SonyCamera.prototype.set = function (param, value, callback) {
if(this.status != "IDLE") return callback && callback('camera not ready');
var action = 'set' + param.charAt(0).toUpperCase() + param.slice(1);
if(this.availableApiList.indexOf(action) === -1 || !this.params[param]) {
return callback && callback("param not available");
}
if(this.params[param].available.indexOf(value) === -1) {
return callback && callback("value not available");
}
this.call(action, [value], callback);
};
// Client-side export
if (typeof window !== 'undefined' && window.SonyCamera) { window.SonyCamera = SonyCamera; }
// Server-side export
if (typeof module !== 'undefined') { module.exports = SonyCamera; }
}());