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

Commit

Permalink
Showing 1 changed file with 43 additions and 40 deletions.
83 changes: 43 additions & 40 deletions src/nest/session.ts
Original file line number Diff line number Diff line change
@@ -110,7 +110,6 @@ export class NestSession {
const appLaunch = await this.getAppLaunch(session);
if (appLaunch) {
const currDate = new Date();
currDate.setMinutes(currDate.getMinutes() - 1);
const epoch = Math.round(currDate.getTime() / 1000);

const data = {
@@ -119,51 +118,55 @@ export class NestSession {
sessionID: `${session?.userid}.${String(Math.random()).substr(2, 5)}.${epoch}`,
};

while (true) {
try {
const req: AxiosRequestConfig = {
method: 'POST',
timeout: SUBSCRIBE_TIMEOUT * 1e3,
url: `${appLaunch.service_urls.urls.transport_url}/v5/subscribe`,
headers: {
Authorization: 'Basic ' + this.config.access_token,
'User-Agent': NestEndpoints.USER_AGENT_STRING,
Referer: this.endpoints.NEST_API_HOSTNAME,
},
data: data,
};
const response = (await axios(req)).data;
this.subscribeFailures = 0;
const objects = response.objects;
try {
const req: AxiosRequestConfig = {
method: 'POST',
timeout: SUBSCRIBE_TIMEOUT * 1e3,
url: `${appLaunch.service_urls.urls.transport_url}/v5/subscribe`,
headers: {
Authorization: 'Basic ' + this.config.access_token,
'User-Agent': NestEndpoints.USER_AGENT_STRING,
Referer: this.endpoints.NEST_API_HOSTNAME,
},
data: data,
};
const response = (await axios(req)).data;
this.subscribeFailures = 0;
const objects = response.objects;
if (objects && Array.isArray(objects)) {
for (const object of objects) {
const uuid = object.object_key.split('.')[1];
const camera = cameras.find((x) => x.info.uuid === uuid);
if (camera) {
this.log.debug(`Updating info for ${camera.info.name}`);
const curr_streaming = camera.info.properties['streaming.enabled'];
const curr_chime = camera.info.properties['doorbell.indoor_chime.enabled'];
const curr_audio = camera.info.properties['audio.enabled'];
if (object.object_key) {
const uuid = object.object_key.split('.')[1];
const camera = cameras.find((x) => x.info.uuid === uuid);
if (camera) {
this.log.debug(`Updating info for ${camera.info.name}`);
const curr_streaming = camera.info.properties['streaming.enabled'];
const curr_chime = camera.info.properties['doorbell.indoor_chime.enabled'];
const curr_audio = camera.info.properties['audio.enabled'];

const newProps = (await camera.updateData()).properties;
if (curr_streaming !== newProps['streaming.enabled']) {
camera.emit(NestCamEvents.CAMERA_STATE_CHANGED, newProps['streaming.enabled']);
}
if (curr_chime !== newProps['doorbell.indoor_chime.enabled']) {
camera.emit(NestCamEvents.CHIME_STATE_CHANGED, newProps['doorbell.indoor_chime.enabled']);
}
if (curr_audio !== newProps['audio.enabled']) {
camera.emit(NestCamEvents.AUDIO_STATE_CHANGED, newProps['audio.enabled']);
const newProps = (await camera.updateData()).properties;
if (curr_streaming !== newProps['streaming.enabled']) {
camera.emit(NestCamEvents.CAMERA_STATE_CHANGED, newProps['streaming.enabled']);
}
if (curr_chime !== newProps['doorbell.indoor_chime.enabled']) {
camera.emit(NestCamEvents.CHIME_STATE_CHANGED, newProps['doorbell.indoor_chime.enabled']);
}
if (curr_audio !== newProps['audio.enabled']) {
camera.emit(NestCamEvents.AUDIO_STATE_CHANGED, newProps['audio.enabled']);
}
}
}
}
} catch (error) {
handleError(this.log, error, 'Error subscribing');
if (this.subscribeFailures < 10) {
this.subscribeFailures++;
}

await delay(RETRY_INTERVAL * Math.pow(this.subscribeFailures, 2));
}
} catch (error) {
handleError(this.log, error, 'Error subscribing');
if (this.subscribeFailures < 10) {
this.subscribeFailures++;
}

await delay(RETRY_INTERVAL * Math.pow(this.subscribeFailures, 2));
} finally {
await this.subscribe(cameras);
}
}
}

0 comments on commit 247e75a

Please sign in to comment.