()
| 5 | import { promisify } from 'util' |
| 6 | |
| 7 | async function example() { |
| 8 | const { env } = process, |
| 9 | ringApi = new RingApi({ |
| 10 | // Replace with your refresh token |
| 11 | refreshToken: env.RING_REFRESH_TOKEN!, |
| 12 | debug: true, |
| 13 | }), |
| 14 | locations = await ringApi.getLocations(), |
| 15 | allCameras = await ringApi.getCameras() |
| 16 | |
| 17 | console.log( |
| 18 | `Found ${locations.length} location(s) with ${allCameras.length} camera(s).`, |
| 19 | ) |
| 20 | |
| 21 | ringApi.onRefreshTokenUpdated.subscribe( |
| 22 | async ({ newRefreshToken, oldRefreshToken }) => { |
| 23 | // 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 |
| 24 | // Here is an example using a .env file for configuration |
| 25 | if (!oldRefreshToken) { |
| 26 | return |
| 27 | } |
| 28 | |
| 29 | const currentConfig = await promisify(readFile)('.env'), |
| 30 | updatedConfig = currentConfig |
| 31 | .toString() |
| 32 | .replace(oldRefreshToken, newRefreshToken) |
| 33 | |
| 34 | await promisify(writeFile)('.env', updatedConfig) |
| 35 | }, |
| 36 | ) |
| 37 | |
| 38 | for (const location of locations) { |
| 39 | location.onConnected.pipe(skip(1)).subscribe((connected) => { |
| 40 | const status = connected ? 'Connected to' : 'Disconnected from' |
| 41 | console.log(`**** ${status} location ${location.name} - ${location.id}`) |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | for (const location of locations) { |
| 46 | const cameras = location.cameras, |
| 47 | devices = await location.getDevices() |
| 48 | |
| 49 | console.log( |
| 50 | `\nLocation ${location.name} (${location.id}) has the following ${cameras.length} camera(s):`, |
| 51 | ) |
| 52 | |
| 53 | for (const camera of cameras) { |
| 54 | console.log(`- ${camera.id}: ${camera.name} (${camera.deviceType})`) |
| 55 | } |
| 56 | |
| 57 | console.log( |
| 58 | `\nLocation ${location.name} (${location.id}) has the following ${devices.length} device(s):`, |
| 59 | ) |
| 60 | |
| 61 | for (const device of devices) { |
| 62 | console.log(`- ${device.zid}: ${device.name} (${device.deviceType})`) |
| 63 | } |
| 64 | } |
no test coverage detected