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 pathMMM-Ring.js
executable file
·154 lines (133 loc) · 4.16 KB
/
MMM-Ring.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
/* Magic Mirror
* Module: 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
*/
Module.register("MMM-Ring", {
DisplayTypes: {
NONE: 1,
ERROR: 2,
VIDEO: 3
},
defaults: {
ringEmail: undefined,
ringPwd: undefined,
ringStreamMotion: false,
ring2faRefreshToken: undefined,
ringMinutesToStreamVideo: 1.5,
ringVideoWidth: "600",
muted: false
},
start: function() {
this.errorMessage = "";
this.displayType = this.DisplayTypes.NONE;
this.hls = "";
if (
this.config.ringEmail !== undefined ||
this.config.ringPwd !== undefined
) {
this.displayType = this.DisplayTypes.ERROR;
this.errorMessage =
"ringEmail and ringPwd are no longer valid configuration properties. Ring now requires a 2 factor authentication (2fa) refresh token. Must use ring2faRefreshToken property in config.";
return;
}
if (this.config.ring2faRefreshToken === undefined) {
this.displayType = this.DisplayTypes.ERROR;
this.errorMessage =
"Must provide ring2faRefreshToken within the MMM-Ring configuration within the Magic Mirror config file.";
return;
}
if (this.config.ringMinutesToStreamVideo > 5) {
this.displayType = this.DisplayTypes.ERROR;
this.errorMessage =
"ringMinutesToStreamVideo configuration property can not be larger than 5";
return;
}
this.sendSocketNotification("BEGIN_RING_MONITORING", this.config);
},
getScripts: function() {
return ["https://cdn.jsdelivr.net/npm/hls.js"];
},
getStyles: function() {
return ["MMM-Ring.css"];
},
requiresVersion: "2.1.0", // Required version of MagicMirror
getDom: function() {
if (this.hls) {
this.hls.destroy();
this.hls = null;
}
var wrapper = document.createElement("div");
switch (this.displayType) {
case this.DisplayTypes.NONE:
return wrapper;
case this.DisplayTypes.ERROR:
wrapper.innerHTML = this.errorMessage;
return wrapper;
case this.DisplayTypes.VIDEO:
var streamPath = window.location.href + "MMM-Ring/stream.m3u8";
var video = document.createElement("video");
video.className = "video";
video.width = this.config.ringVideoWidth;
if (this.config.muted) {
video.muted = true;
}
wrapper.appendChild(video);
if (Hls.isSupported()) {
const config = {
liveDurationInfinity: true
};
var hls = new Hls(config);
this.hls = hls;
hls.on(Hls.Events.ERROR, function(event, data) {
var errorType = data.type;
var errorDetails = data.details;
var errorFatal = data.fatal;
Log.error(
`***************** MMM-Ring ERROR! Type: ${errorType}, Details: ${errorDetails}, Fatal: ${errorFatal}`
);
});
hls.attachMedia(video);
hls.on(Hls.Events.MEDIA_ATTACHED, function() {
hls.loadSource(streamPath);
hls.on(Hls.Events.MANIFEST_PARSED, function() {
video.play();
});
});
} else if (video.canPlayType("application/vnd.apple.mpegurl")) {
video.src = streamPath;
video.addEventListener("loadedmetadata", function() {
video.play();
});
}
return wrapper;
}
},
socketNotificationReceived: function(notification, payload) {
switch (notification) {
case "DISPLAY_ERROR":
this.displayType = this.DisplayTypes.ERROR;
this.errorMessage = payload;
this.updateDom();
break;
case "VIDEO_STREAM_ENDED":
this.displayType = this.DisplayTypes.NONE;
this.updateDom();
break;
case "VIDEO_STREAM_AVAILABLE":
// if we are already on video stream then bail
if (this.displayType === this.DisplayTypes.VIDEO) {
break;
}
this.displayType = this.DisplayTypes.VIDEO;
this.updateDom();
break;
}
}
});