This repository has been archived by the owner on Aug 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnode_helper.js
257 lines (216 loc) · 7.57 KB
/
node_helper.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* Magic Mirror
* Node Helper: MMM-Ring
*
* By Dustin Bryant
* MIT Licensed.
*
* Huge thanks to dgreif on GitHub for the RingAPI and
* examples which resulted in ability to create this
* Magic Mirror module.
* https://github.com/dgreif/ring
*/
const NodeHelper = require("node_helper");
const pathApi = require("path");
const util = require("util");
const fs = require("fs");
const mainRingApi = require("ring-client-api");
const operators = require("rxjs/operators");
const fileWatcher = require("chokidar");
module.exports = NodeHelper.create({
requiresVersion: "2.11.0",
start: function () {
this.toLog("Starting module: MMM-Ring");
this.videoOutputDirectory = pathApi.join(this.path, "public");
this.envFile = pathApi.join(this.path, ".env");
this.ringApi = null;
this.config = null;
this.watcher = null;
this.audioPlaylistFile = "stream.m3u8";
this.sipSession = null;
this.sessionRunning = false;
},
stop: function () {
this.toLog("Stopping module helper: MMM-Ring");
this.stopWatchingFile();
if (this.sipSession) {
this.sipSession.stop();
this.sipSession = null;
}
this.cleanUpVideoStreamDirectory();
},
socketNotificationReceived: async function (notification, payload) {
if (notification === "BEGIN_RING_MONITORING") {
this.config = payload;
if (!(await util.promisify(fs.exists)(this.envFile))) {
await util.promisify(fs.writeFile)(
this.envFile,
`RING_2FA_REFRESH_TOKEN=${this.config.ring2faRefreshToken}`
);
}
require("dotenv").config({ path: this.envFile });
this.monitorRingActivity();
}
},
cleanUpVideoStreamDirectory: async function () {
if (!(await util.promisify(fs.exists)(this.videoOutputDirectory))) {
await util.promisify(fs.mkdir)(this.videoOutputDirectory);
}
const files = await util.promisify(fs.readdir)(this.videoOutputDirectory);
const unlinkPromises = files.map((filename) =>
util.promisify(fs.unlink)(`${this.videoOutputDirectory}/${filename}`)
);
return await Promise.all(unlinkPromises);
},
monitorRingActivity: async function () {
this.ringApi = new mainRingApi.RingApi({
refreshToken: process.env.RING_2FA_REFRESH_TOKEN,
debug: true,
cameraDingsPollingSeconds: 2
});
this.ringApi.onRefreshTokenUpdated.subscribe(
async ({ newRefreshToken, oldRefreshToken }) => {
this.toLog("Refresh Token Updated");
if (!oldRefreshToken) {
return;
}
const currentConfig = await util.promisify(fs.readFile)(this.envFile);
const updateConfig = currentConfig
.toString()
.replace(oldRefreshToken, newRefreshToken);
await util.promisify(fs.writeFile)(this.envFile, updateConfig);
}
);
const locations = await this.ringApi.getLocations();
const allCameras = await this.ringApi.getCameras();
this.toLog(
`Found ${locations.length} location(s) with ${allCameras.length} camera(s).`
);
for (const location of locations) {
location.onConnected.pipe(operators.skip(1)).subscribe((connected) => {
const status = connected ? "Connected to" : "Disconnected from";
this.toLog(
`**** ${status} location ${location.locationDetails.name} - ${location.locationId}`
);
});
}
for (const location of locations) {
const cameras = location.cameras,
devices = await location.getDevices();
this.toLog(
`Location ${location.locationDetails.name} has the following ${cameras.length} camera(s):`
);
for (const camera of cameras) {
this.toLog(`- ${camera.id}: ${camera.name} (${camera.deviceType})`);
}
this.toLog(
`Location ${location.locationDetails.name} has the following ${devices.length} device(s):`
);
for (const device of devices) {
this.toLog(`- ${device.zid}: ${device.name} (${device.deviceType})`);
}
}
if (allCameras === undefined || allCameras.length == 0) {
this.toLog(
`no cameras were found! Ensure you have a camera on your account or that you provided accurate login information in the config. You'll need to restart MagicMirror.`
);
this.sendSocketNotification(
"DISPLAY_ERROR",
"No cameras were found! Check console for more info."
);
return;
}
// Start listening for doorbell presses on each camera
allCameras.forEach((camera) => {
camera.onDoorbellPressed.subscribe(async () => {
if (!this.sipSession) {
await this.startSession(camera, "ring");
}
});
this.toLog(`Actively listening for doorbell presses`);
//Check config value if node app should stream motion
if(this.config.ringStreamMotion){
camera.onMotionDetected.subscribe(async (newMotion) => {
//NewMotion is a true false value indicating whether the motion is new based on the dings made in the last 65 seconds
// This prevents the stream from being triggered on startup because it would not be an active motion event.
if (!this.sipSession && newMotion) {
await this.startSession(camera, "motion");
}
});
this.toLog(`Actively listening for Motion events`);
}
});
},
startSession: async function (camera, type) {
if (this.sipSession || this.sessionRunning === true) {
return;
}
this.sessionRunning = true;
if(type === "ring"){
this.toLog(`${camera.name} had its doorbell rung! Preparing video stream.`);
}else if(type === "motion"){
this.toLog(`${camera.name} has sensed motion Preparing video stream.`);
}else{
this.toLog(`${camera.name} been summoned by something other than a ring or motion. (spooky) Preparing video stream.`);
}
await this.cleanUpVideoStreamDirectory();
this.watchForStreamStarted();
const streamTimeOut = this.config.ringMinutesToStreamVideo * 60 * 1000;
this.sipSession = await camera.streamVideo({
output: [
"-preset",
"veryfast",
"-g",
"25",
"-sc_threshold",
"0",
"-f",
"hls",
"-hls_time",
"2",
"-hls_list_size",
"6",
"-hls_flags",
"delete_segments",
pathApi.join(this.videoOutputDirectory, this.audioPlaylistFile)
]
});
this.sipSession.onCallEnded.subscribe(() => {
this.toLog(`${camera.name} video stream has ended`);
this.sendSocketNotification("VIDEO_STREAM_ENDED", null);
this.stopWatchingFile();
this.sipSession = null;
this.sessionRunning = false;
});
setTimeout(() => {
this.toLog(`timeout hit`);
if (this.sipSession) {
this.sipSession.stop();
}
}, streamTimeOut);
},
stopWatchingFile: function () {
if (this.watcher) {
this.watcher.close();
this.watcher = null;
}
},
toLog: function (message) {
console.log(`MMM-Ring at (${new Date().toLocaleString()}): ${message}`);
},
watchForStreamStarted: function () {
this.stopWatchingFile();
// only watch for file for 15 seconds
setTimeout(() => this.stopWatchingFile(), 15 * 1000);
this.watcher = fileWatcher.watch(this.videoOutputDirectory, {
ignored: /(^|[\/\\])\../,
persistent: true
});
this.watcher.on("add", (filePath) => {
var fileName = filePath.split("\\").pop().split("/").pop();
if (fileName === this.audioPlaylistFile) {
this.stopWatchingFile();
this.sendSocketNotification("VIDEO_STREAM_AVAILABLE", null);
}
});
}
});