Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
fix for thumbnails and camera state
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandawg93 committed Feb 21, 2020
1 parent 0e3ec7b commit 079957f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 12 deletions.
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ let Accessory, Service, Characteristic, hap, UUIDGen;
const Nest = require('./lib/nest.js').NestAPI;
const NestConnection = require('./lib/nest-connection.js');

const UPDATE_INTERVAL = 10000;

Promise.delay = function(time_ms) {
return new Promise(resolve => setTimeout(resolve, time_ms));
}
Expand Down Expand Up @@ -101,7 +103,7 @@ class NestCamPlatform {
accessory.addService(motion);
setInterval(async function() {
camera.checkMotion(accessory);
}, 10000);
}, UPDATE_INTERVAL);
}
//Add enabled/disabled service
accessory.addService(Service.Switch, 'Streaming')
Expand All @@ -112,6 +114,13 @@ class NestCamPlatform {
self.log.info("Setting %s to %s", accessory.displayName, (value ? 'on' : 'off'));
callback();
});
//Check enabled/disabled state
setInterval(async function() {
await camera.updateInfo();
let service = accessory.getService(Service.Switch);
service.updateCharacteristic(Characteristic.On, camera.enabled);
}, UPDATE_INTERVAL);

accessory.configureCameraSource(camera);
configuredAccessories.push(accessory);
});
Expand Down
50 changes: 50 additions & 0 deletions lib/nest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const https = require('https');
const axios = require('axios');
const EventEmitter = require('events');
const NestCam = require('./nestcam').NestCam;
Expand Down Expand Up @@ -64,6 +65,55 @@ class NestAPI extends EventEmitter {
self.log.debug(req.method + ' request sent to ' + req.url);
return (await axios(req)).data;
}

/**
* Send a generic api request using promises
* @param hostname The base uri to send the request
* @param endpoint The endpoint to send the request
* @param method Usually "GET" or "POST"
* @param body The body of the request or null if a "GET"
*/
sendPromiseRequest(hostname, endpoint, method, body) {
let self = this;

return new Promise((resolve, reject) => {
let headers = {
'User-Agent': 'iPhone iPhone OS 11.0 Dropcam/5.14.0 com.nestlabs.jasper.release Darwin',
'Referer': 'https://home.nest.com/'
};

if (method === 'POST') {
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
}

if (self.accessToken !== undefined) {
headers['Cookie'] = 'user_token=' + self.accessToken;
}

let options = {
hostname: hostname,
path: endpoint,
method: method,
headers: headers
};
let req = https.request(options, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
let error = new Error('Unexpected API Error - ' + res.statusCode);
error.code = res.statusCode;
reject(error);
}

const resBody = [];
res.on('data', (chunk) => resBody.push(chunk));
res.on('end', () => resolve(Buffer.concat(resBody)));
});
req.on('error', (err) => reject(err));
if (body !== undefined) {
req.write(body);
}
req.end();
});
}
}

module.exports = {
Expand Down
43 changes: 33 additions & 10 deletions lib/nestcam.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ class NestCam {
self.motionDetected = false;
}

async updateInfo() {
let self = this;
let query = querystring.stringify({
'uuid': self.uuid
});
try {
self.log.debug("Updating info for %s", self.name);
let response = await self.api.sendRequest(NestAPIHostname, '/api/cameras.get_with_properties?' + query, 'GET');
response.items.forEach((info) => {
self.name = info.name;
self.uuid = info.uuid;
self.enabled = info.is_streaming_enabled;
self.serialNumber = info.serial_number;
self.softwareVersion = info.combined_software_version;
self.detectors = info.detectors;
self.type = info.type;
self.nexusTalkHost = info.direct_nexustalk_host;
self.apiHost = info.nexus_api_http_server;
});
} catch(error) {
error.status = error.response && error.response.status;
self.log.error('Error fetching cameras - ' + error.status);
}
}

async toggleActive(enabled) {
let self = this;
let query = querystring.stringify({
Expand Down Expand Up @@ -151,21 +176,19 @@ class NestCam {

// Camera Source

async handleSnapshotRequest(request, callback) {
handleSnapshotRequest(request, callback) {
let self = this;
let query = querystring.stringify({
uuid: self.uuid,
width: request.width
});
try {
let response = await self.api.sendRequest(self.apiHost, '/get_image?' + query, 'GET');
callback(undefined, response);
} catch(error) {
error.status = error.response && error.response.status;
if (error.status != 404) {
self.log.error('Error getting image - ' + error.status);
}
}
self.api.sendPromiseRequest(self.apiHost, '/get_image?' + query, 'GET')
.then((response) => {
callback(undefined, response);
})
.catch((err) => {
callback(err);
});
}

handleCloseConnection(connectionID) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-nest-cam2",
"version": "1.1.0",
"version": "1.1.1",
"description": "Nest cam plugin for homebridge: https://homebridge.io/",
"license": "ISC",
"keywords": [
Expand Down

0 comments on commit 079957f

Please sign in to comment.