-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathexample.ts
More file actions
96 lines (79 loc) · 2.91 KB
/
example.ts
File metadata and controls
96 lines (79 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { PushNotificationAction, RingApi } from "ring-client-api";
import { readFile, writeFile } from "fs";
import { promisify } from "util";
async function example() {
const { env } = process,
ringApi = new RingApi({
// This value comes from the .env file
refreshToken: env.RING_REFRESH_TOKEN!,
debug: true,
}),
locations = await ringApi.getLocations(),
allCameras = await ringApi.getCameras();
console.log(
`Found ${locations.length} location(s) with ${allCameras.length} camera(s).`
);
ringApi.onRefreshTokenUpdated.subscribe(
async ({ newRefreshToken, oldRefreshToken }) => {
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 = await promisify(readFile)(".env"),
updatedConfig = currentConfig
.toString()
.replace(oldRefreshToken, newRefreshToken);
await promisify(writeFile)(".env", updatedConfig);
}
);
for (const location of locations) {
let haveConnected = false;
location.onConnected.subscribe((connected) => {
if (!haveConnected && !connected) {
return;
} else if (connected) {
haveConnected = true;
}
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 = await 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.onNewNotification.subscribe((notification) => {
const action = notification.android_config.category,
event =
action === PushNotificationAction.Motion
? "Motion detected"
: action === PushNotificationAction.Ding
? "Doorbell pressed"
: `Video started (${action})`;
console.log(
`${event} on ${camera.name} camera. Ding id ${
notification.data.event.ding.id
}. Received at ${new Date()}`
);
});
});
console.log("Listening for motion and doorbell presses on your cameras.");
}
}
example();