├── _config.yml ├── test ├── test.properties └── test.js ├── node-arlo-api.js ├── package.json ├── README.md ├── node-arlo-cli.js ├── LICENSE └── arlo-api └── index.js /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-minimal -------------------------------------------------------------------------------- /test/test.properties: -------------------------------------------------------------------------------- 1 | username = 2 | password = -------------------------------------------------------------------------------- /node-arlo-api.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var Arlo = require("./arlo-api"); 4 | 5 | module.exports = { 6 | ArloApi: Arlo 7 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-arlo-api", 3 | "version": "1.0.2", 4 | "description": "NodeJS wrapper around Arlo event-based API.", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/nevertheless75/node-arlo-api.git" 12 | }, 13 | "keywords": [ 14 | "JavaScript", 15 | "NodeJS", 16 | "Arlo" 17 | ], 18 | "author": "Nevertheless", 19 | "license": "SEE LICENSE IN LICENSE", 20 | "bugs": { 21 | "url": "https://github.com/nevertheless75/node-arlo-api/issues" 22 | }, 23 | "homepage": "https://github.com/nevertheless75/node-arlo-api#readme", 24 | "dependencies": { 25 | "JSON": "^1.0.0", 26 | "clui": "^0.3.1", 27 | "dateformat": "^2.0.0", 28 | "debug": "^2.6.0", 29 | "eventsource": "^0.2.1", 30 | "fluent-ffmpeg": "^2.1.0", 31 | "inquirer": "^2.0.0", 32 | "minimist": "^1.2.0", 33 | "node-rest-client": "^2.0.1", 34 | "node-uuid": "^1.4.7", 35 | "properties-reader": "0.0.15", 36 | "q": "^1.4.1" 37 | }, 38 | "devDependencies": { 39 | "mocha": "^3.2.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Node Arlo API 2 | 3 | An API library for Node.js that interacts with Netgear's Arlo camera system. 4 | 5 | The API is not complete yet and at the moment mainly used for personal 6 | purposes only. I will continue completing the API and also adding more 7 | documentation and examples. 8 | 9 | Some example usage is already shown in node-arlo-cli which can be started 10 | as follows: 11 | 12 | ``` 13 | node node-arlo-cli -u 'ARLO_USERNAME' -p 'ARLO_PASSWORD' 14 | ``` 15 | 16 | You still will be prompted providing username and password whereas the defaults 17 | are those passed. 18 | 19 | The example shows how to derive device information, arm or disarm the system. 20 | This should provide a basic understand of using the API in general. 21 | 22 | # Example based on arlo-api.js 23 | 24 | If you just want to copy the contents of arlo-api.js and use that, here's how it 25 | might look within your app: 26 | 27 | ``` 28 | import ArloApi from 'arlo'; 29 | 30 | const arlo = new ArloApi('username', 'password'); 31 | const auth = {username: 'username', password: 'password'}; 32 | const arlo = new ArloApi(auth.username, auth.password); 33 | 34 | const devices = arlo.getDevices(); 35 | const cameras = arlo.getDevices().then(deviceArray => arlo.getCameras(deviceArray[0].deviceId, deviceArray[0].xCloudId))); 36 | ``` 37 | -------------------------------------------------------------------------------- /node-arlo-cli.js: -------------------------------------------------------------------------------- 1 | var ArloApi = require('./node-arlo-api').ArloApi; 2 | 3 | var inquirer = require('inquirer'); 4 | var clui = require('clui'); 5 | var argv = require('minimist')(process.argv.slice(2)); 6 | 7 | var uParam = ""; 8 | if (argv.u && argv.u !== ""){ 9 | uParam = argv.u; 10 | } 11 | 12 | var pParam = ""; 13 | if (argv.p && argv.p !== ""){ 14 | pParam = argv.p; 15 | } 16 | 17 | inquirer.prompt([ 18 | { 19 | type: 'input', 20 | message: 'Enter your Arlo username:', 21 | name: 'username', 22 | default: uParam 23 | }, 24 | { 25 | type: 'password', 26 | message: 'Enter your Arlo password:', 27 | name: 'password', 28 | default: pParam 29 | }]).then(function (answers) { 30 | 31 | var username, password; 32 | 33 | if (answers.username && answers.username !== ""){ 34 | username = answers.username; 35 | } 36 | 37 | if (answers.password && answers.password !== ""){ 38 | password = answers.password; 39 | } 40 | 41 | var arlo = new ArloApi(username, password); 42 | 43 | var Spinner = clui.Spinner; 44 | var ls = new Spinner("Creating Arlo session ..."); 45 | ls.start(); 46 | 47 | var self = this; 48 | self.ls = ls; 49 | self.arlo = arlo; 50 | initSession.bind(self); 51 | 52 | arlo.getDevices() 53 | .then(initSession) 54 | .fail(function(error){ 55 | ls.stop(); 56 | console.error("Failed creating session: " + error.message); 57 | process.exit(0); 58 | }); 59 | }); 60 | 61 | var operations = [ 62 | { 63 | type: 'list', 64 | name: 'operation', 65 | message: 'What do you want to do?', 66 | choices: ['Arm', 'Disarm', 'Cameras', 'Basestation', 'Modes'] 67 | }]; 68 | 69 | var anotherOperation = [ 70 | { 71 | type: 'confirm', 72 | name: 'promptAgain', 73 | message: 'Do you want to do something else?', 74 | default: true 75 | }]; 76 | 77 | function promptAgain() { 78 | inquirer.prompt(anotherOperation).then(function (answers) { 79 | if (answers.promptAgain) { 80 | prompt(); 81 | }else{ 82 | process.exit(0); 83 | } 84 | }); 85 | } 86 | 87 | function prompt() { 88 | inquirer.prompt(operations).then(function (answers) { 89 | if (answers.operation == "Arm"){ 90 | this.arlo.arm(this.deviceId, this.xCloudId).then(promptAgain); 91 | } 92 | if (answers.operation == "Disarm"){ 93 | this.arlo.disarm(this.deviceId, this.xCloudId).then(promptAgain); 94 | } 95 | if (answers.operation == "Modes"){ 96 | this.arlo.getModes(this.deviceId, this.xCloudId).then(print).then(promptAgain); 97 | } 98 | if (answers.operation == "Basestation"){ 99 | this.arlo.getBaseStation(this.deviceId, this.xCloudId).then(print).then(promptAgain); 100 | }s 101 | if (answers.operation == "Cameras"){ 102 | this.arlo.getCameras(this.deviceId, this.xCloudId).then(print).then(promptAgain); 103 | } 104 | }); 105 | } 106 | 107 | function initSession(devices){ 108 | ls.stop(); 109 | for (var i = 0; i < devices.length; i++){ 110 | if (devices[i].deviceType == "basestation"){ 111 | var deviceId = devices[i].deviceId; 112 | var xCloudId = devices[i].xCloudId; 113 | 114 | console.log("Basestation found with device ID: " + deviceId); 115 | console.log("X-Cloud ID: " + xCloudId); 116 | 117 | var self = this; 118 | self.arlo = arlo; 119 | self.deviceId = deviceId; 120 | self.xCloudId = xCloudId; 121 | prompt.bind(self); 122 | prompt(); 123 | } 124 | } 125 | } 126 | 127 | function print(data){ 128 | console.log(data); 129 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var ArloApi = require('../node-arlo-api').ArloApi; 2 | var PropertiesReader = require('properties-reader'); 3 | var assert = require('assert'); 4 | var q = require('q'); 5 | 6 | var properties = PropertiesReader(__dirname + '/test.properties'); 7 | 8 | var u = properties.get("username"); 9 | var p = properties.get("password"); 10 | 11 | var arlo = new ArloApi(u, p); 12 | 13 | describe('ArloApi', function() { 14 | describe('#login()', function() { 15 | it('should return a valid token', function(done) { 16 | arlo.login().then(function (data){ 17 | if (data.success && data.data.token){ 18 | done(); 19 | }else{ 20 | done(new Error("no valid token returned")); 21 | } 22 | }).fail( function(error){ 23 | done(error); 24 | }); 25 | }); 26 | }); 27 | }); 28 | 29 | describe('ArloApi', function() { 30 | 31 | describe('#getBaseStation()', function() { 32 | it('should return information about the base station', function(done) { 33 | setup().then(function (arloSystem){ 34 | arlo.getBaseStation(arloSystem.deviceId, arloSystem.xCloudId).then(function (data){ 35 | if (data && data.properties.state == "idle"){ 36 | done(); 37 | }else{ 38 | done(new Error("no correct information returned")); 39 | } 40 | }).fail( function(error){ 41 | done(error); 42 | }); 43 | }); 44 | }); 45 | }); 46 | }); 47 | 48 | describe('ArloApi', function() { 49 | 50 | describe('#getCameras()', function() { 51 | it('should return information about registered cameras', function(done) { 52 | setup().then(function (arloSystem){ 53 | arlo.getCameras(arloSystem.deviceId, arloSystem.xCloudId).then(function (data){ 54 | for (var i=0; i, 245 | * "lastName":, 246 | * "devices": 247 | * { 248 | * :, 249 | * : 250 | * }, 251 | * "lastModified":, 252 | * "adminUser":true, 253 | * "email":"", 254 | * "id":"" 255 | * } 256 | */ 257 | ArloApi.prototype.updateFriends = function (body) { 258 | return this._request(this._config.ARLO_BASE_URL + "users/friends", client.put, body); 259 | }; 260 | 261 | ArloApi.prototype.updateDeviceName = function (parentId, deviceId, name) { 262 | var body = {"deviceId" : deviceId, "deviceName" : name, "parentId" : parentId}; 263 | return this._request(this._config.ARLO_BASE_URL + "users/devices/renameDevice", client.put, body); 264 | }; 265 | 266 | /** 267 | * Example structure of body parameter: 268 | * 269 | * { 270 | * "devices": 271 | * { 272 | * :1, 273 | * :2 274 | * }, 275 | * } 276 | */ 277 | ArloApi.prototype.updateDisplayOrder = function (body) { 278 | return this._request(this._config.ARLO_BASE_URL + "users/devices/displayOrder", client.post, body); 279 | }; 280 | 281 | ArloApi.prototype._getConfig = function () { 282 | return this._config; 283 | }; 284 | 285 | ArloApi.prototype.deleteRecording = function (createDate, utcCreatedDate, deviceId) { 286 | var body = {"data" : [ { "createdDate" : createDate, "utcCreatedDate" : utcCreatedDate, "deviceId" : deviceId} ] }; 287 | return this._request(this._config.ARLO_BASE_URL + "users/library/recycle", client.post, body); 288 | }; 289 | 290 | ArloApi.prototype._request = function (url, httpMethod, body) { 291 | 292 | var deferred = q.defer(); 293 | 294 | var self = {}; 295 | self.deferred = deferred; 296 | self.url = url; 297 | self.httpMethod = httpMethod; 298 | self.config = this._config; 299 | self.body = body; 300 | 301 | this._login.bind(self); 302 | 303 | this._login() 304 | .then(this._prepare.bind(self)) 305 | .then(this._send.bind(self)) 306 | .fail( function(error){ 307 | deferred.reject(error); 308 | }); 309 | 310 | return deferred.promise; 311 | } 312 | 313 | ArloApi.prototype._requestAndResponse = function (deviceId, xCloudId, body) { 314 | 315 | var deferred = q.defer(); 316 | 317 | var self = {}; 318 | self.config = this._config; 319 | 320 | var transId = uuid.v1(); 321 | 322 | self.transId = transId; 323 | self.deviceId = deviceId; 324 | self.xCloudId = xCloudId; 325 | self.body = body; 326 | 327 | self.rnrDeferred = deferred; 328 | 329 | self.notify = this._notify; 330 | 331 | this._login.bind(self); 332 | 333 | this._login() 334 | .then(this._subscribe.bind(self)) 335 | .then(this._register.bind(self)) 336 | .then(this._invoke.bind(self)) 337 | .then(this._confirm.bind(self)) 338 | .fail( function(error){ 339 | deferred.reject(error); 340 | }); 341 | 342 | return deferred.promise; 343 | } 344 | 345 | ArloApi.prototype._login = function () { 346 | 347 | var deferred = q.defer(); 348 | 349 | if (session){ 350 | deferred.resolve(); 351 | return deferred.promise; 352 | } 353 | 354 | var args = 355 | { 356 | data: { "email" : this._config.username, "password" : this._config.password }, 357 | headers: { "Content-Type": "application/json", "User-Agent": "" } 358 | }; 359 | 360 | client.post(this._config.LOGIN_URL, args, function (data, resp){ 361 | if (data.success){ 362 | session = {}; 363 | session.userId = data.data.userId; 364 | session.token = data.data.token; 365 | session.setcookie = resp.headers["set-cookie"]; 366 | session.transactions = []; 367 | deferred.resolve(data); 368 | }else{ 369 | var error = new Error(); 370 | error.message = data.data.reason; 371 | deferred.reject(error); 372 | } 373 | }); 374 | 375 | return deferred.promise; 376 | }; 377 | 378 | ArloApi.prototype._prepare = function () { 379 | this.body.transId = uuid.v1(); 380 | if (this.xCloudId) 381 | this.body.xCloudId = this.xCloudId; 382 | 383 | this.body.from = session.userId + "_web"; 384 | if (this.deviceId) 385 | this.body.to = this.deviceId; 386 | } 387 | 388 | ArloApi.prototype._send = function () { 389 | 390 | var self = this; 391 | var args = {}; 392 | 393 | args.headers = 394 | { 395 | "Content-Type" : "application/json", 396 | "User-Agent" : "web", 397 | "Authorization" : session.token, 398 | "Cookie" : session.setcookie.join( "; " ) 399 | }; 400 | 401 | if (this.xCloudId) 402 | args.headers.xCloudId = this.xCloudId; 403 | 404 | if (this.body) 405 | args.data = this.body; 406 | 407 | this.httpMethod(this.url, args, function (data, resp){ 408 | if (data.success){ 409 | this.deferred.resolve(data.data); 410 | }else{ 411 | var error = new Error(); 412 | error.message = data.data.reason; 413 | this.deferred.reject(error); 414 | } 415 | }.bind(self)); 416 | } 417 | 418 | ArloApi.prototype._stream = function () { 419 | 420 | var deferred = q.defer(); 421 | 422 | var args = {}; 423 | 424 | args.headers = 425 | { 426 | "Content-Type" : "application/json", 427 | "User-Agent" : "web", 428 | "Authorization" : session.token, 429 | "xCloudId" : this.xCloudId, 430 | "Cookie" : session.setcookie.join( "; " ) 431 | }; 432 | 433 | args.data = this.body; 434 | 435 | client.post(this.config.STREAM_URL, args, function (data, resp){ 436 | 437 | debug("_stream callback: " + data); 438 | 439 | if (data.success){ 440 | var command = ffmpeg(data.data.url); 441 | } 442 | }) 443 | 444 | return deferred.promise; 445 | } 446 | 447 | ArloApi.prototype._subscribe = function () { 448 | 449 | var deferred = q.defer(); 450 | 451 | if (session.subscribed){ 452 | deferred.resolve(); 453 | return deferred.promise; 454 | } 455 | 456 | var args = 457 | { 458 | headers : { "Content-Type" : "text/event-stream", "Cookie" : session.setcookie.join( "; " ), "Last-Event-ID" : this.transId } 459 | }; 460 | 461 | var eventSource = new EventSource(this.config.SUBSCRIBE_URL + session.token, args); 462 | 463 | eventStream = {}; 464 | eventStream.userId = session.userId; 465 | eventStream.eventSource = eventSource; 466 | 467 | eventSource.onopen = function (event) { 468 | if (event) { 469 | debug("Event stream opened."); 470 | } 471 | }; 472 | 473 | eventSource.onmessage = function (event) { 474 | if (event) { 475 | 476 | var data = JSON.parse(event.data); 477 | 478 | if (event.type === "message" && data.action === "logout"){ 479 | session = {}; 480 | // TODO : unsubscribe and logout 481 | debug(data.reason); 482 | } else if (event.type === "message" && data.status === "connected"){ 483 | eventStream.connected = true; 484 | debug("Event stream connected (" + eventStream.userId + ")."); 485 | session.subscribed = true; 486 | deferred.resolve(data); 487 | } else if (event.type === "message" && data.action === "is"){ 488 | 489 | if (session){ 490 | for (var i=0; i