From c1010d40be3e7b614af83b0805c635c831d4706f Mon Sep 17 00:00:00 2001 From: Bart Dorsey Date: Fri, 22 May 2020 10:26:02 -0500 Subject: [PATCH 1/4] Adding code to log responses if you pass a debug flag into the options. --- lib/blink.js | 718 ++++++++++++++++++++++++----------------- lib/blink_exception.js | 5 +- 2 files changed, 416 insertions(+), 307 deletions(-) diff --git a/lib/blink.js b/lib/blink.js index 50624f6..762854f 100644 --- a/lib/blink.js +++ b/lib/blink.js @@ -1,18 +1,18 @@ /** * Created by madshall on 3/17/17. */ -const readline = require('readline'); -require('./constants'); -const BlinkCamera = require('./blink_camera'); -const BlinkException = require('./blink_exception'); -const BlinkAuthenticationException = require('./blink_auth_exception'); -const BlinkURLHandler = require('./blink_url_handler'); -const { guid } = require('./util'); - -const request = require('request'); - -function _statusCodeIsError(response){ - return response.statusCode < 200 || response.statusCode > 299 +const readline = require("readline"); +require("./constants"); +const BlinkCamera = require("./blink_camera"); +const BlinkException = require("./blink_exception"); +const BlinkAuthenticationException = require("./blink_auth_exception"); +const BlinkURLHandler = require("./blink_url_handler"); +const { guid } = require("./util"); + +const request = require("request"); + +function _statusCodeIsError(response) { + return response.statusCode < 200 || response.statusCode > 299; } module.exports = class Blink { constructor(username, password, device_id, options) { @@ -69,20 +69,19 @@ module.exports = class Blink { } } - return Promise - .all(promises) + return Promise.all(promises) .then(() => this.getSummary()) - .then(summaries => { + .then((summaries) => { for (var id in this._cameras) { if (this._cameras.hasOwnProperty(id)) { let camera = this._cameras[id]; - Object.keys(summaries).forEach(networkId => { - summaries[networkId].devices.forEach(device => { + Object.keys(summaries).forEach((networkId) => { + summaries[networkId].devices.forEach((device) => { if (device.device_id === camera.id) { camera.update(device); } }); - }) + }); } } }); @@ -90,220 +89,279 @@ module.exports = class Blink { getSummary() { const promises = []; - const networks = this.networks.map(_ => _.id); + const networks = this.networks.map((_) => _.id); + + networks.forEach((networkId) => { + promises.push( + new Promise((resolve, reject) => { + if (!this._auth_header) { + return reject( + new BlinkException("Authentication token must be set") + ); + } - networks.forEach(networkId => { - promises.push(new Promise((resolve, reject) => { - if (!this._auth_header) { - return reject(new BlinkException("Authentication token must be set")); - } + request( + { + url: this.urls.network_url + networkId + "/homescreen", + headers: this._auth_header, + json: true, + }, + (err, response, body) => { + if (err || _statusCodeIsError(response)) { + return reject( + new BlinkException( + `Can't retrieve system summary`, + this.debug ? response : null + ) + ); + } + return resolve(body); + } + ); + }) + ); + }); - request({ - url: this.urls.network_url + networkId + "/homescreen", - headers: this._auth_header, - json: true - }, (err, response, body) => { - if (err || _statusCodeIsError(response)) { - return reject(new BlinkException(`Can't retrieve system summary`)); - } - return resolve(body); + return Promise.all(promises).then((results) => { + return results.reduce((acc, result, index) => { + acc[networks[index]] = result; + this._networks.forEach((network) => { + if (network.id !== networks[index]) return; + network = { + ...network, + ...result, + }; }); - })); + return acc; + }, {}); }); - - return Promise.all(promises) - .then(results => { - return results.reduce((acc, result, index) => { - acc[networks[index]] = result; - this._networks.forEach(network => { - if (network.id !== networks[index]) return; - network = { - ...network, - ...result, - }; - }); - return acc; - }, {}); - }); } getCameraThumbs() { - return this.refresh() - .then(() => { - var result = {}; - for (var id in this._cameras) { - if (this._cameras.hasOwnProperty(id)) { - result[id] = this._cameras.thumbnail; - } + return this.refresh().then(() => { + var result = {}; + for (var id in this._cameras) { + if (this._cameras.hasOwnProperty(id)) { + result[id] = this._cameras.thumbnail; } - return result; - }); + } + return result; + }); } getEvents(networkIds = []) { const promises = []; - const networks = networkIds.length ? networkIds : this.networks.map(_ => _.id); - - networks.forEach(networkId => { - promises.push(new Promise((resolve, reject) => { - request({ - url: this.urls.event_url + networkId, - headers: this._auth_header, - json: true - }, (err, response, body) => { - if (err || _statusCodeIsError(response)) { - return reject(new BlinkException(`Can't retrieve system events`)); - } - this._events = body.event; - return resolve(this._events); - }); - })); + const networks = networkIds.length + ? networkIds + : this.networks.map((_) => _.id); + + networks.forEach((networkId) => { + promises.push( + new Promise((resolve, reject) => { + request( + { + url: this.urls.event_url + networkId, + headers: this._auth_header, + json: true, + }, + (err, response, body) => { + if (err || _statusCodeIsError(response)) { + return reject( + new BlinkException( + `Can't retrieve system events`, + this.debug ? response : null + ) + ); + } + this._events = body.event; + return resolve(this._events); + } + ); + }) + ); }); - return Promise.all(promises) - .then(results => { - return results.reduce((acc, result, index) => { - acc[networks[index]] = result; - return acc; - }, {}); - }); + return Promise.all(promises).then((results) => { + return results.reduce((acc, result, index) => { + acc[networks[index]] = result; + return acc; + }, {}); + }); } isOnline(networkIds = []) { const promises = []; - const networks = networkIds.length ? networkIds : this.networks.map(_ => _.id); - - networks.forEach(networkId => { - promises.push(new Promise((resolve, reject) => { - request({ - url: this.urls.network_url + networkId + '/syncmodules', - headers: this._auth_header, - json: true - }, (err, response, body) => { - if (err || _statusCodeIsError(response)) { - return reject(new BlinkException(`Can't retrieve system status`)); - } - return resolve(body.syncmodule.status === 'online'); - }); - })); + const networks = networkIds.length + ? networkIds + : this.networks.map((_) => _.id); + + networks.forEach((networkId) => { + promises.push( + new Promise((resolve, reject) => { + request( + { + url: this.urls.network_url + networkId + "/syncmodules", + headers: this._auth_header, + json: true, + }, + (err, response, body) => { + if (err || _statusCodeIsError(response)) { + return reject( + new BlinkException( + `Can't retrieve system status`, + this.debug ? response : null + ) + ); + } + return resolve(body.syncmodule.status === "online"); + } + ); + }) + ); }); - return Promise.all(promises) - .then(results => { - return results.reduce((acc, result, index) => { - acc[networks[index]] = result; - return acc; - }, {}); - }); + return Promise.all(promises).then((results) => { + return results.reduce((acc, result, index) => { + acc[networks[index]] = result; + return acc; + }, {}); + }); } getClients() { return new Promise((resolve, reject) => { - request({ - url: this.urls.base_url + '/account/clients', - headers: this._auth_header, - json: true - }, (err, response, body) => { - if (err || _statusCodeIsError(response)) { - return reject(new BlinkException(`Can't retrieve connected clients`)); + request( + { + url: this.urls.base_url + "/account/clients", + headers: this._auth_header, + json: true, + }, + (err, response, body) => { + if (err || _statusCodeIsError(response)) { + return reject( + new BlinkException( + `Can't retrieve connected clients`, + this.debug ? response : null + ) + ); + } + return resolve(body); } - return resolve(body); - }); + ); }); } getLastMotions() { - return this.getEvents() - .then(events => { - var result = {}; - events.forEach(event => { - let camera_id = event.camera_id; - let camera = this._cameras[camera_id]; - - if (event.type === 'motion') { - let url = this.urls.base_url + event.video_url; - result[camera_id] = camera.motion = { - 'video': url, - 'image': url.replace(/\.[^.]+$]/, '.jpg'), - 'time': event.created_at - }; - } - }); - return result; + return this.getEvents().then((events) => { + var result = {}; + events.forEach((event) => { + let camera_id = event.camera_id; + let camera = this._cameras[camera_id]; + + if (event.type === "motion") { + let url = this.urls.base_url + event.video_url; + result[camera_id] = camera.motion = { + video: url, + image: url.replace(/\.[^.]+$]/, ".jpg"), + time: event.created_at, + }; + } }); + return result; + }); } isArmed() { - return this.getSummary() - .then(summaries => { - return Object.keys(summaries).reduce((acc, key) => { - acc[key] = summaries[key].network.armed; - return acc; - }, {}); - }); + return this.getSummary().then((summaries) => { + return Object.keys(summaries).reduce((acc, key) => { + acc[key] = summaries[key].network.armed; + return acc; + }, {}); + }); } setArmed(value = true, networkIds = []) { const promises = []; - const networksToArm = networkIds.length ? networkIds : this.networks.map(_ => _.id); - - networksToArm.forEach(networkId => { - promises.push(new Promise((resolve, reject) => { - let state = value ? 'arm' : 'disarm'; - request.post({ - url: this.urls.network_url + networkId + '/' + state, - json: true, - headers: this._auth_header, - body: {} - }, (err, response, body) => { - if (err || _statusCodeIsError(response)) { - return reject(new BlinkException(`Can't ${state} the network: ${networkId}`)); - } - return resolve(body); - }); - })); + const networksToArm = networkIds.length + ? networkIds + : this.networks.map((_) => _.id); + + networksToArm.forEach((networkId) => { + promises.push( + new Promise((resolve, reject) => { + let state = value ? "arm" : "disarm"; + request.post( + { + url: this.urls.network_url + networkId + "/" + state, + json: true, + headers: this._auth_header, + body: {}, + }, + (err, response, body) => { + if (err || _statusCodeIsError(response)) { + return reject( + new BlinkException( + `Can't ${state} the network: ${networkId}`, + this.debug ? response : null + ) + ); + } + return resolve(body); + } + ); + }) + ); }); - return Promise.all(promises) - .then(results => { - return results.reduce((acc, result, index) => { - acc[networksToArm[index]] = result; - return acc; - }, {}); - }); + return Promise.all(promises).then((results) => { + return results.reduce((acc, result, index) => { + acc[networksToArm[index]] = result; + return acc; + }, {}); + }); } - getVideos(page = 0, since = new Date(2008)) { // Blink was founded in 2009 + getVideos(page = 0, since = new Date(2008)) { + // Blink was founded in 2009 return new Promise((resolve, reject) => { - request({ - url: `${this.urls.video_url}?since=${since.toISOString()}&page=${page}`, - headers: this._auth_header, - json: true - }, (err, response, body) => { - if (err || _statusCodeIsError(response)) { - return reject(new BlinkException(`Can't fetch videos`)); - } - return resolve(body); - }); + request( + { + url: `${ + this.urls.video_url + }?since=${since.toISOString()}&page=${page}`, + headers: this._auth_header, + json: true, + }, + (err, response, body) => { + if (err || _statusCodeIsError(response)) { + return reject( + new BlinkException( + `Can't fetch videos`, + this.debug ? response : null + ) + ); + } + return resolve(body); + } + ); }); } getCameras() { - return this.getSummary() - .then(summaries => { - Object.keys(summaries).forEach(networkId => { - summaries[networkId].devices.forEach(device => { - if (device.device_type === 'camera') { - device.region_id = this._region_id; - device.network_id = networkId; - let newDevice = new BlinkCamera(device, this.urls); - this._cameras[newDevice.id] = newDevice; - this._idlookup[newDevice.id] = newDevice.name; - } - }); + return this.getSummary().then((summaries) => { + Object.keys(summaries).forEach((networkId) => { + summaries[networkId].devices.forEach((device) => { + if (device.device_type === "camera") { + device.region_id = this._region_id; + device.network_id = networkId; + let newDevice = new BlinkCamera(device, this.urls); + this._cameras[newDevice.id] = newDevice; + this._idlookup[newDevice.id] = newDevice.name; + } }); - return this._cameras; }); + return this._cameras; + }); } getLinks() { @@ -311,8 +369,8 @@ module.exports = class Blink { if (this._cameras.hasOwnProperty(id)) { let camera = this._cameras[id]; let network_id_url = this.urls.network_url + camera.network_id; - let image_url = network_id_url + '/camera/' + camera.id + '/thumbnail'; - let arm_url = network_id_url + '/camera/' + camera.id + '/'; + let image_url = network_id_url + "/camera/" + camera.id + "/thumbnail"; + let arm_url = network_id_url + "/camera/" + camera.id + "/"; camera.image_link = image_url; camera.arm_link = arm_url; camera.header = this._auth_header; @@ -321,130 +379,166 @@ module.exports = class Blink { } setupSystem(name_or_id) { - if ( !((this._username && this._password) || (this._token && this._region_id ))) { - throw new BlinkAuthenticationException("(_username, _password) or (_token, _region_id) are required for system setup"); + if ( + !((this._username && this._password) || (this._token && this._region_id)) + ) { + throw new BlinkAuthenticationException( + "(_username, _password) or (_token, _region_id) are required for system setup" + ); } - if(this._token){ - this._setupWithToken() + if (this._token) { + this._setupWithToken(); return this.getIDs(name_or_id) .then(this.getCameras.bind(this)) .then(this.getLinks.bind(this)); - } - else{ + } else { return this._getAuthToken() .then(this.getIDs.bind(this, name_or_id)) .then(this.getCameras.bind(this)) .then(this.getLinks.bind(this)); } } - _setupWithToken(){ - this._host = this._region_id + '.' + BLINK_URL; - this._auth_header = { - 'Host': this._host, - 'TOKEN_AUTH': this._token - }; - this.urls = new BlinkURLHandler(this._account_id, this._region_id); + _setupWithToken() { + this._host = this._region_id + "." + BLINK_URL; + this._auth_header = { + Host: this._host, + TOKEN_AUTH: this._token, + }; + this.urls = new BlinkURLHandler(this._account_id, this._region_id); } _getAuthToken(repeats = 0) { return new Promise((resolve, reject) => { - if (typeof this._username != 'string') { - return reject(new BlinkAuthenticationException("Username must be a string")); + if (typeof this._username != "string") { + return reject( + new BlinkAuthenticationException("Username must be a string") + ); } - if (typeof this._password != 'string') { - return reject(new BlinkAuthenticationException("Password must be a string")); + if (typeof this._password != "string") { + return reject( + new BlinkAuthenticationException("Password must be a string") + ); } - if (typeof this._device_id != 'string') { - return reject(new BlinkAuthenticationException("Device ID must be a string")); + if (typeof this._device_id != "string") { + return reject( + new BlinkAuthenticationException("Device ID must be a string") + ); } let headers = { - 'Host': DEFAULT_URL, - 'Content-Type': 'application/json' + Host: DEFAULT_URL, + "Content-Type": "application/json", }; const notification_key = guid(SIZE_NOTIFICATION_KEY); let data = { - "email": this._username, - "password": this._password, - "notification_key": notification_key, - "unique_id": this._device_id, - "app_version": "6.0.7 (520300) #afb0be72a", - "client_name": this.device_name, - "client_type": "android", - "device_identifier": this._device_id, - "device_name": this.device_name, - "os_version": "5.1.1", - "reauth": "true", + email: this._username, + password: this._password, + notification_key: notification_key, + unique_id: this._device_id, + app_version: "6.0.7 (520300) #afb0be72a", + client_name: this.device_name, + client_type: "android", + device_identifier: this._device_id, + device_name: this.device_name, + os_version: "5.1.1", + reauth: "true", }; let authenticate = (response) => { - this._host = this._region_id + '.' + BLINK_URL; + this._host = this._region_id + "." + BLINK_URL; this._token = response.authtoken.authtoken; this._auth_header = { - 'Host': this._host, - 'TOKEN_AUTH': this._token + Host: this._host, + TOKEN_AUTH: this._token, }; this.urls = new BlinkURLHandler(this._account_id, this._region_id); resolve(true); }; - request.post({ - url: this.auth_2FA ? LOGIN_URL_2FA : LOGIN_URL, - json: true, - headers: headers, - body: data - }, (err, response, body) => { - if (err || _statusCodeIsError(response)) { - return reject(new BlinkAuthenticationException(`Authentication problem: ${body.message}`)); - } else { - if ((body.client || {}).verification_required) { - if (!this.auth_2FA) { - if (repeats === 1) return reject(new BlinkAuthenticationException(`Authentication problem: verification timeout`)); - return new Promise((resolve, reject) => { - setTimeout(() => { - this._getAuthToken(repeats + 1).then(resolve, reject); - }, this.verification_timeout); - }).then(resolve, reject); - } - const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout - }); - return rl.question(`Enter the verification code sent to ${this._username}: `, pin => { - request.post({ - url: `${BASE_URL}/api/v4/account/${body.account.id}/client/${body.client.id}/pin/verify`, - json: true, - headers: headers, - body: { - pin: `${pin}`, - } - }, (err, response, body) => { - if (err || _statusCodeIsError(response)) { - return reject(new BlinkAuthenticationException(`Authentication problem: ${body.message}`)); - } - if (!body.region) { - return reject(new BlinkAuthenticationException(`Authentication problem: ${body.message}`)); - } - for (var key in body.region) { - this._region_id = key; - this._region = body.region[key]; - } - authenticate(body); + request.post( + { + url: this.auth_2FA ? LOGIN_URL_2FA : LOGIN_URL, + json: true, + headers: headers, + body: data, + }, + (err, response, body) => { + if (err || _statusCodeIsError(response)) { + return reject( + new BlinkAuthenticationException( + `Authentication problem: ${body.message}` + ) + ); + } else { + if ((body.client || {}).verification_required) { + if (!this.auth_2FA) { + if (repeats === 1) + return reject( + new BlinkAuthenticationException( + `Authentication problem: verification timeout` + ) + ); + return new Promise((resolve, reject) => { + setTimeout(() => { + this._getAuthToken(repeats + 1).then(resolve, reject); + }, this.verification_timeout); + }).then(resolve, reject); + } + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, }); - rl.close(); - }); - } - if (!body.region) { - return reject(new BlinkAuthenticationException(body.message)); - } - for (var key in body.region) { - this._region_id = key; - this._region = body.region[key]; + return rl.question( + `Enter the verification code sent to ${this._username}: `, + (pin) => { + request.post( + { + url: `${BASE_URL}/api/v4/account/${body.account.id}/client/${body.client.id}/pin/verify`, + json: true, + headers: headers, + body: { + pin: `${pin}`, + }, + }, + (err, response, body) => { + if (err || _statusCodeIsError(response)) { + return reject( + new BlinkAuthenticationException( + `Authentication problem: ${body.message}`, + this.debug ? response : null + ) + ); + } + if (!body.region) { + return reject( + new BlinkAuthenticationException( + `Authentication problem: ${body.message}` + ) + ); + } + for (var key in body.region) { + this._region_id = key; + this._region = body.region[key]; + } + authenticate(body); + } + ); + rl.close(); + } + ); + } + if (!body.region) { + return reject(new BlinkAuthenticationException(body.message)); + } + for (var key in body.region) { + this._region_id = key; + this._region = body.region[key]; + } + authenticate(body); } - authenticate(body); } - }); + ); }); } @@ -452,42 +546,56 @@ module.exports = class Blink { var that = this; return new Promise((resolve, reject) => { if (!this._auth_header) { - return reject(new BlinkException("You have to be authenticated before calling this method")); + return reject( + new BlinkException( + "You have to be authenticated before calling this method" + ) + ); } - request({ - url: this.urls.networks_url, - headers: this._auth_header, - json: true - }, (err, response, body) => { - if (err || _statusCodeIsError(response)) { - return reject(new BlinkException(`Can't retrieve system status`)); - } else { - var network = false; - if (typeof name_or_id != 'undefined') { - body.networks.forEach(function(n){ - if (n.id == name_or_id || n.name == name_or_id) { - network = n; - that._networks.push(network); - } - }); - - if (!network) { - return reject(new BlinkException("No network found for " + name_or_id)); - } + request( + { + url: this.urls.networks_url, + headers: this._auth_header, + json: true, + }, + (err, response, body) => { + if (err || _statusCodeIsError(response)) { + return reject( + new BlinkException( + `Can't retrieve system status`, + this.debug ? response : null + ) + ); } else { - if (!body.networks.length) { - return reject(new BlinkException("No networks found")); + var network = false; + if (typeof name_or_id != "undefined") { + body.networks.forEach(function (n) { + if (n.id == name_or_id || n.name == name_or_id) { + network = n; + that._networks.push(network); + } + }); + + if (!network) { + return reject( + new BlinkException("No network found for " + name_or_id) + ); + } + } else { + if (!body.networks.length) { + return reject(new BlinkException("No networks found")); + } + body.networks.forEach((network) => { + that._networks.push(network); + }); } - body.networks.forEach(network => { - that._networks.push(network); - }); - } - that._account_id = that._networks[0].account_id; - that.urls = new BlinkURLHandler(that.accountId, that.regionId); - return resolve(that); + that._account_id = that._networks[0].account_id; + that.urls = new BlinkURLHandler(that.accountId, that.regionId); + return resolve(that); + } } - }); + ); }); } }; diff --git a/lib/blink_exception.js b/lib/blink_exception.js index 63d39b0..f90243d 100644 --- a/lib/blink_exception.js +++ b/lib/blink_exception.js @@ -3,7 +3,8 @@ */ module.exports = class BlinkException extends Error { - constructor(message) { + constructor(message, response = {}) { + message = `${message}: ${response.body}`; super(message); } -}; \ No newline at end of file +}; From 6794a8d8831489fa220b6db399781f5bc9580d08 Mon Sep 17 00:00:00 2001 From: Bart Dorsey Date: Fri, 22 May 2020 10:29:31 -0500 Subject: [PATCH 2/4] Changing quotes back to single quotes to match madshall's style --- lib/blink.js | 78 ++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/blink.js b/lib/blink.js index 762854f..3231bcd 100644 --- a/lib/blink.js +++ b/lib/blink.js @@ -1,15 +1,15 @@ /** * Created by madshall on 3/17/17. */ -const readline = require("readline"); -require("./constants"); -const BlinkCamera = require("./blink_camera"); -const BlinkException = require("./blink_exception"); -const BlinkAuthenticationException = require("./blink_auth_exception"); -const BlinkURLHandler = require("./blink_url_handler"); -const { guid } = require("./util"); +const readline = require('readline'); +require('./constants'); +const BlinkCamera = require('./blink_camera'); +const BlinkException = require('./blink_exception'); +const BlinkAuthenticationException = require('./blink_auth_exception'); +const BlinkURLHandler = require('./blink_url_handler'); +const { guid } = require('./util'); -const request = require("request"); +const request = require('request'); function _statusCodeIsError(response) { return response.statusCode < 200 || response.statusCode > 299; @@ -31,7 +31,7 @@ module.exports = class Blink { this._device_id = device_id; this.auth_2FA = false; this.verification_timeout = 1e3 * 60; - this.device_name = "node-blink-security"; + this.device_name = 'node-blink-security'; this.urls = null; Object.assign(this, options); } @@ -96,13 +96,13 @@ module.exports = class Blink { new Promise((resolve, reject) => { if (!this._auth_header) { return reject( - new BlinkException("Authentication token must be set") + new BlinkException('Authentication token must be set') ); } request( { - url: this.urls.network_url + networkId + "/homescreen", + url: this.urls.network_url + networkId + '/homescreen', headers: this._auth_header, json: true, }, @@ -200,7 +200,7 @@ module.exports = class Blink { new Promise((resolve, reject) => { request( { - url: this.urls.network_url + networkId + "/syncmodules", + url: this.urls.network_url + networkId + '/syncmodules', headers: this._auth_header, json: true, }, @@ -213,7 +213,7 @@ module.exports = class Blink { ) ); } - return resolve(body.syncmodule.status === "online"); + return resolve(body.syncmodule.status === 'online'); } ); }) @@ -232,7 +232,7 @@ module.exports = class Blink { return new Promise((resolve, reject) => { request( { - url: this.urls.base_url + "/account/clients", + url: this.urls.base_url + '/account/clients', headers: this._auth_header, json: true, }, @@ -258,11 +258,11 @@ module.exports = class Blink { let camera_id = event.camera_id; let camera = this._cameras[camera_id]; - if (event.type === "motion") { + if (event.type === 'motion') { let url = this.urls.base_url + event.video_url; result[camera_id] = camera.motion = { video: url, - image: url.replace(/\.[^.]+$]/, ".jpg"), + image: url.replace(/\.[^.]+$]/, '.jpg'), time: event.created_at, }; } @@ -289,10 +289,10 @@ module.exports = class Blink { networksToArm.forEach((networkId) => { promises.push( new Promise((resolve, reject) => { - let state = value ? "arm" : "disarm"; + let state = value ? 'arm' : 'disarm'; request.post( { - url: this.urls.network_url + networkId + "/" + state, + url: this.urls.network_url + networkId + '/' + state, json: true, headers: this._auth_header, body: {}, @@ -351,7 +351,7 @@ module.exports = class Blink { return this.getSummary().then((summaries) => { Object.keys(summaries).forEach((networkId) => { summaries[networkId].devices.forEach((device) => { - if (device.device_type === "camera") { + if (device.device_type === 'camera') { device.region_id = this._region_id; device.network_id = networkId; let newDevice = new BlinkCamera(device, this.urls); @@ -369,8 +369,8 @@ module.exports = class Blink { if (this._cameras.hasOwnProperty(id)) { let camera = this._cameras[id]; let network_id_url = this.urls.network_url + camera.network_id; - let image_url = network_id_url + "/camera/" + camera.id + "/thumbnail"; - let arm_url = network_id_url + "/camera/" + camera.id + "/"; + let image_url = network_id_url + '/camera/' + camera.id + '/thumbnail'; + let arm_url = network_id_url + '/camera/' + camera.id + '/'; camera.image_link = image_url; camera.arm_link = arm_url; camera.header = this._auth_header; @@ -383,7 +383,7 @@ module.exports = class Blink { !((this._username && this._password) || (this._token && this._region_id)) ) { throw new BlinkAuthenticationException( - "(_username, _password) or (_token, _region_id) are required for system setup" + '(_username, _password) or (_token, _region_id) are required for system setup' ); } if (this._token) { @@ -399,7 +399,7 @@ module.exports = class Blink { } } _setupWithToken() { - this._host = this._region_id + "." + BLINK_URL; + this._host = this._region_id + '.' + BLINK_URL; this._auth_header = { Host: this._host, TOKEN_AUTH: this._token, @@ -408,25 +408,25 @@ module.exports = class Blink { } _getAuthToken(repeats = 0) { return new Promise((resolve, reject) => { - if (typeof this._username != "string") { + if (typeof this._username != 'string') { return reject( - new BlinkAuthenticationException("Username must be a string") + new BlinkAuthenticationException('Username must be a string') ); } - if (typeof this._password != "string") { + if (typeof this._password != 'string') { return reject( - new BlinkAuthenticationException("Password must be a string") + new BlinkAuthenticationException('Password must be a string') ); } - if (typeof this._device_id != "string") { + if (typeof this._device_id != 'string') { return reject( - new BlinkAuthenticationException("Device ID must be a string") + new BlinkAuthenticationException('Device ID must be a string') ); } let headers = { Host: DEFAULT_URL, - "Content-Type": "application/json", + 'Content-Type': 'application/json', }; const notification_key = guid(SIZE_NOTIFICATION_KEY); let data = { @@ -434,17 +434,17 @@ module.exports = class Blink { password: this._password, notification_key: notification_key, unique_id: this._device_id, - app_version: "6.0.7 (520300) #afb0be72a", + app_version: '6.0.7 (520300) #afb0be72a', client_name: this.device_name, - client_type: "android", + client_type: 'android', device_identifier: this._device_id, device_name: this.device_name, - os_version: "5.1.1", - reauth: "true", + os_version: '5.1.1', + reauth: 'true', }; let authenticate = (response) => { - this._host = this._region_id + "." + BLINK_URL; + this._host = this._region_id + '.' + BLINK_URL; this._token = response.authtoken.authtoken; this._auth_header = { @@ -548,7 +548,7 @@ module.exports = class Blink { if (!this._auth_header) { return reject( new BlinkException( - "You have to be authenticated before calling this method" + 'You have to be authenticated before calling this method' ) ); } @@ -568,7 +568,7 @@ module.exports = class Blink { ); } else { var network = false; - if (typeof name_or_id != "undefined") { + if (typeof name_or_id != 'undefined') { body.networks.forEach(function (n) { if (n.id == name_or_id || n.name == name_or_id) { network = n; @@ -578,12 +578,12 @@ module.exports = class Blink { if (!network) { return reject( - new BlinkException("No network found for " + name_or_id) + new BlinkException('No network found for ' + name_or_id) ); } } else { if (!body.networks.length) { - return reject(new BlinkException("No networks found")); + return reject(new BlinkException('No networks found')); } body.networks.forEach((network) => { that._networks.push(network); From 37a183faaf3458f81c13e167136c3b0993538aea Mon Sep 17 00:00:00 2001 From: Bart Dorsey Date: Fri, 22 May 2020 10:31:59 -0500 Subject: [PATCH 3/4] Changing some more prettier rules to match madshall's coding style --- lib/blink.js | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/blink.js b/lib/blink.js index 3231bcd..6f6a381 100644 --- a/lib/blink.js +++ b/lib/blink.js @@ -71,12 +71,12 @@ module.exports = class Blink { return Promise.all(promises) .then(() => this.getSummary()) - .then((summaries) => { + .then(summaries => { for (var id in this._cameras) { if (this._cameras.hasOwnProperty(id)) { let camera = this._cameras[id]; - Object.keys(summaries).forEach((networkId) => { - summaries[networkId].devices.forEach((device) => { + Object.keys(summaries).forEach(networkId => { + summaries[networkId].devices.forEach(device => { if (device.device_id === camera.id) { camera.update(device); } @@ -89,9 +89,9 @@ module.exports = class Blink { getSummary() { const promises = []; - const networks = this.networks.map((_) => _.id); + const networks = this.networks.map(_ => _.id); - networks.forEach((networkId) => { + networks.forEach(networkId => { promises.push( new Promise((resolve, reject) => { if (!this._auth_header) { @@ -122,10 +122,10 @@ module.exports = class Blink { ); }); - return Promise.all(promises).then((results) => { + return Promise.all(promises).then(results => { return results.reduce((acc, result, index) => { acc[networks[index]] = result; - this._networks.forEach((network) => { + this._networks.forEach(network => { if (network.id !== networks[index]) return; network = { ...network, @@ -153,9 +153,9 @@ module.exports = class Blink { const promises = []; const networks = networkIds.length ? networkIds - : this.networks.map((_) => _.id); + : this.networks.map(_ => _.id); - networks.forEach((networkId) => { + networks.forEach(networkId => { promises.push( new Promise((resolve, reject) => { request( @@ -181,7 +181,7 @@ module.exports = class Blink { ); }); - return Promise.all(promises).then((results) => { + return Promise.all(promises).then(results => { return results.reduce((acc, result, index) => { acc[networks[index]] = result; return acc; @@ -193,9 +193,9 @@ module.exports = class Blink { const promises = []; const networks = networkIds.length ? networkIds - : this.networks.map((_) => _.id); + : this.networks.map(_ => _.id); - networks.forEach((networkId) => { + networks.forEach(networkId => { promises.push( new Promise((resolve, reject) => { request( @@ -220,7 +220,7 @@ module.exports = class Blink { ); }); - return Promise.all(promises).then((results) => { + return Promise.all(promises).then(results => { return results.reduce((acc, result, index) => { acc[networks[index]] = result; return acc; @@ -252,9 +252,9 @@ module.exports = class Blink { } getLastMotions() { - return this.getEvents().then((events) => { + return this.getEvents().then(events => { var result = {}; - events.forEach((event) => { + events.forEach(event => { let camera_id = event.camera_id; let camera = this._cameras[camera_id]; @@ -272,7 +272,7 @@ module.exports = class Blink { } isArmed() { - return this.getSummary().then((summaries) => { + return this.getSummary().then(summaries => { return Object.keys(summaries).reduce((acc, key) => { acc[key] = summaries[key].network.armed; return acc; @@ -284,9 +284,9 @@ module.exports = class Blink { const promises = []; const networksToArm = networkIds.length ? networkIds - : this.networks.map((_) => _.id); + : this.networks.map(_ => _.id); - networksToArm.forEach((networkId) => { + networksToArm.forEach(networkId => { promises.push( new Promise((resolve, reject) => { let state = value ? 'arm' : 'disarm'; @@ -313,7 +313,7 @@ module.exports = class Blink { ); }); - return Promise.all(promises).then((results) => { + return Promise.all(promises).then(results => { return results.reduce((acc, result, index) => { acc[networksToArm[index]] = result; return acc; @@ -348,9 +348,9 @@ module.exports = class Blink { } getCameras() { - return this.getSummary().then((summaries) => { - Object.keys(summaries).forEach((networkId) => { - summaries[networkId].devices.forEach((device) => { + return this.getSummary().then(summaries => { + Object.keys(summaries).forEach(networkId => { + summaries[networkId].devices.forEach(device => { if (device.device_type === 'camera') { device.region_id = this._region_id; device.network_id = networkId; @@ -443,7 +443,7 @@ module.exports = class Blink { reauth: 'true', }; - let authenticate = (response) => { + let authenticate = response => { this._host = this._region_id + '.' + BLINK_URL; this._token = response.authtoken.authtoken; @@ -491,7 +491,7 @@ module.exports = class Blink { }); return rl.question( `Enter the verification code sent to ${this._username}: `, - (pin) => { + pin => { request.post( { url: `${BASE_URL}/api/v4/account/${body.account.id}/client/${body.client.id}/pin/verify`, @@ -585,7 +585,7 @@ module.exports = class Blink { if (!body.networks.length) { return reject(new BlinkException('No networks found')); } - body.networks.forEach((network) => { + body.networks.forEach(network => { that._networks.push(network); }); } From 9396b20ea68e293bcd1cbde5ae1d26c065a0f649 Mon Sep 17 00:00:00 2001 From: Bart Dorsey Date: Fri, 22 May 2020 10:33:11 -0500 Subject: [PATCH 4/4] Adding prettier config --- .prettierrc | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..4984e01 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "tabWidth": 2, + "semi": true, + "singleQuote": true, + "arrowParens": "avoid" +}