UNPKG

@koush/ring-client-api

Version:

Unofficial API for Ring doorbells, cameras, security alarm system and smart lighting

74 lines (73 loc) 4.03 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); require("dotenv/config"); const api_1 = require("../api"); const operators_1 = require("rxjs/operators"); const fs_1 = require("fs"); const util_1 = require("util"); function example() { return __awaiter(this, void 0, void 0, function* () { const { env } = process, ringApi = new api_1.RingApi({ // Replace with your refresh token refreshToken: env.RING_REFRESH_TOKEN, // Listen for dings and motion events cameraDingsPollingSeconds: 2, debug: true, }), locations = yield ringApi.getLocations(), allCameras = yield ringApi.getCameras(); console.log(`Found ${locations.length} location(s) with ${allCameras.length} camera(s).`); ringApi.onRefreshTokenUpdated.subscribe(({ newRefreshToken, oldRefreshToken }) => __awaiter(this, void 0, void 0, function* () { console.log('Refresh Token Updated: ', newRefreshToken); // If you are implementing a project that use `ring-client-api`, you should subscribe to onRefreshTokenUpdated and update your config each time it fires an event // Here is an example using a .env file for configuration if (!oldRefreshToken) { return; } const currentConfig = yield (0, util_1.promisify)(fs_1.readFile)('.env'), updatedConfig = currentConfig .toString() .replace(oldRefreshToken, newRefreshToken); yield (0, util_1.promisify)(fs_1.writeFile)('.env', updatedConfig); })); for (const location of locations) { location.onConnected.pipe((0, operators_1.skip)(1)).subscribe((connected) => { const status = connected ? 'Connected to' : 'Disconnected from'; console.log(`**** ${status} location ${location.name} - ${location.id}`); }); } for (const location of locations) { const cameras = location.cameras, devices = yield location.getDevices(); console.log(`\nLocation ${location.name} (${location.id}) has the following ${cameras.length} camera(s):`); for (const camera of cameras) { console.log(`- ${camera.id}: ${camera.name} (${camera.deviceType})`); } console.log(`\nLocation ${location.name} (${location.id}) has the following ${devices.length} device(s):`); for (const device of devices) { console.log(`- ${device.zid}: ${device.name} (${device.deviceType})`); } } if (allCameras.length) { allCameras.forEach((camera) => { camera.onNewDing.subscribe((ding) => { const event = ding.kind === 'motion' ? 'Motion detected' : ding.kind === 'ding' ? 'Doorbell pressed' : `Video started (${ding.kind})`; console.log(`${event} on ${camera.name} camera. Ding id ${ding.id_str}. Received at ${new Date()}`); }); }); console.log('Listening for motion and doorbell presses on your cameras.'); } }); } example().catch((e) => { console.error('Example threw an error:', e); });