-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-WyzeBridge.js
executable file
·224 lines (203 loc) · 5.78 KB
/
MMM-WyzeBridge.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
/* global Module */
/* Magic Mirror
* Module: MMM-WyzeBridge
*
* By Andrés Vanegas <[email protected]>
* MIT Licensed.
*/
Module.register("MMM-WyzeBridge", {
/**
* @member {Object} defaults - Defines the default config values.
* @property {int} updateInterval Default time to show next camera (in milliseconds). Defaults to 30000.
* @property {int} retryDelay Time to ask to wyze-bridge server for cameras updates (in milliseconds). Defaults to 5000.
* @property {boolean} controls If video player should show its controls. Defaults to false.
* @property {int} height video player height. Defaults to 350.
* @property {int} width video player width. Defaults to 700.
* @property {string} targetHost wyze-bridge host to connect (e.g http://localhost). Defaults to null.
* @property {int} targetPort wyze-bridge port to connect (e.g 5000). Defaults to null.
* @property {int} animationSpeed Animation speed to update DOM. Defaults to 400.
* @property {str[]|RegExp[]} filter camera filters (can be a string representing name, pattern or a regex). Defaults to [] (empty array).
*/
defaults: {
updateInterval: 30000,
retryDelay: 5000,
controls: false,
height: 350,
width: 700,
targetHost: null,
targetPort: null,
animationSpeed: 400,
filter: [],
},
// Required version of MagicMirror
requiresVersion: "2.1.0",
// Placeholders
wrapper: null,
message: null,
messageWrapper: null,
playerWrapper: null,
player: null,
cameras: 0,
// Overrides start method
start: function () {
this.config = {
...this.defaults,
...this.config,
};
this.message = this.translate("LOADING");
this.updateDom(this.config.animationSpeed);
this.sendNotification("SET_CONFIG", {
...this.config,
__protocol: window.location.protocol,
__port: window.location.port,
});
},
/**
* Notification send helper method
* @param {string} notification notification type
* @param {any} payload notification payload
*/
sendNotification(notification, payload) {
this.sendSocketNotification(this.name + "-" + notification, payload);
},
// Override socket notification received method
socketNotificationReceived: function (notification, payload) {
switch (notification.replace(this.name + "-", "")) {
case "CAMERAS_UPDATED":
this.cameras = payload;
break;
case "SET_MESSAGE":
this.message = this.translate(payload)
this.updateDom(this.config.animationSpeed);
break;
case "SET_CAMERA":
this.message = null;
this.updateDom(this.config.animationSpeed);
this.swapSource(payload);
break;
}
},
/**
* Change current camera and activate player
* @param {object} camera camera data to be show
*/
swapSource: function (camera) {
Log.log("Showing camera " + camera.nickname);
if (this.player === null) {
this.showPlayer();
}
// this.player.poster("/" + this.name + camera.image_url);
this.player.src({
src: "/" + this.name + camera.video_url,
type: 'application/x-mpegURL',
});
this.player.play();
},
/**
* Clears an attribute from this instance
* @param {string} nodename attribute to be clear
*/
clearNode(nodename) {
try {
switch (nodename) {
case 'player':
if (this.player !== null) {
this.player.dispose();
}
break;
default:
if (this[nodename] !== null) {
this[nodename].parentNode.removeChild(this[nodename])
}
}
} catch (e) { }
if (this.hasOwnProperty(nodename)) {
this[nodename] = null;
}
},
/**
* Show message in wrapper and hide the player
*/
showMessage() {
this.clearNode('player');
this.clearNode('playerWrapper');
if (this.messageWrapper === null) {
this.messageWrapper = document.createElement("div");
this.messageWrapper.classList.add("message-container");
this.messageWrapper.style.width = this.config.width + "px";
this.messageWrapper.style.height = this.config.height + "px";
this.wrapper.appendChild(this.messageWrapper);
}
this.messageWrapper.innerHTML = this.message;
},
/**
* Show player and hide the message
*/
showPlayer() {
this.clearNode('messageWrapper');
if (this.playerWrapper === null) {
this.playerWrapper = document.createElement("video-js");
this.playerWrapper.classList.add("player_" + this.name);
this.playerWrapper.setAttribute("id", "player_" + this.identifier);
this.wrapper.appendChild(this.playerWrapper);
}
if (this.player === null && this.playerWrapper.offsetParent !== null) {
try {
var player = videojs(this.playerWrapper, {
autoplay: false,
controls: this.config.controls,
muted: "muted",
preload: "none",
width: this.config.width,
height: this.config.height,
fluid: true,
liveui: true,
loadingSpinner: false,
});
this.player = player;
} catch (e) {
this.clearNode('player');
Log.error(e);
}
}
},
// Override function to retrieve DOM elements
getDom: function () {
if (this.wrapper === null) {
this.wrapper = document.createElement("div");
this.wrapper.classList.add("wrapper_" + this.name);
this.wrapper.style.width = this.config.width + "px";
this.wrapper.style.height = this.config.height + "px";
}
if (this.message !== null) {
this.showMessage();
} else {
this.showPlayer();
}
return this.wrapper;
},
// Load scripts
getScripts: function () {
const __lang = this.config.lang || this.language || "en";
return [
this.file("js/video.min.js"),
this.file("js/lang/" + (__lang) + ".js"),
this.file("js/videojs-http-streaming.min.js"),
];
},
// Load stylesheets
getStyles: function () {
return [
this.file("css/video-js.css"),
this.name + ".css",
];
},
// Load translations files
getTranslations: function () {
//FIXME: This can be load a one file javascript definition
return {
en: "translations/en.json",
es: "translations/es.json"
};
},
});