-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-Live-Stream-TV.js
299 lines (281 loc) · 10.4 KB
/
MMM-Live-Stream-TV.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
Module.register("MMM-Live-Stream-TV", {
logPrefix: "[MMM-Live-Stream-TV]:: ",
defaults: {
portname: "/dev/ttyACM0", //COMX for windows, /dev/ttyACM0* for raspberry pi
style: "tv",
animationSpeed: 1000,
slideshowInterval: 15 * 60 * 1000, //15 mins
autoplaying: false,
developerMode: false,
frameWidth: "600",
maxWidth: "100%",
currentChannel: 0,
sensors: [
{
name: "Potentiometer",
}
],
streams: [
{
streamUrl: "https://videos3.earthcam.com/fecnetwork/20489.flv/playlist.m3u8",
url: "https://www.earthcam.com/usa/newyork/empirestatebuilding/?cam=esb37",
name: "Empire State Building",
channelNumber: 0,
},
{
streamUrl: "https://videos3.earthcam.com/fecnetwork/3983.flv/playlist.m3u8",
url: "https://www.earthcam.com/usa/newyork/brooklynbridge/?cam=gzcchd",
name: "Brooklyn Bridge",
channelNumber: 1,
},
{
streamUrl: "https://videos3.earthcam.com/fecnetwork/10874.flv/playlist.m3u8",
url: "https://www.earthcam.com/usa/newyork/worldtradecenter/?cam=skyline_g",
name: "World Trade Center",
channelNumber: 2,
},
{
streamUrl: "https://videos3.earthcam.com/fecnetwork/chargingbull.flv/playlist.m3u8",
url: "https://www.earthcam.com/usa/newyork/wallstreet/chargingbull/?cam=chargingbull_hd",
name: "Wall Street Bull",
channelNumber: 3,
},
{
streamUrl: "https://videos3.earthcam.com/fecnetwork/hdtimes10.flv/playlist.m3u8",
url: "https://www.earthcam.com/usa/newyork/timessquare/?cam=tsrobo1",
name: "Times Square",
channelNumber: 4,
},
{
streamUrl: "https://videos3.earthcam.com/fecnetwork/4017timessquare.flv/playlist.m3u8",
url: "https://www.earthcam.com/cams/newyork/timessquare/?cam=tstwo_hd",
name: "Times Square View (South)",
channelNumber: 5,
},
{
streamUrl: "http://videos3.earthcam.com/fecnetwork/hdtimes10.flv/playlist.m3u8",
url: "https://www.earthcam.com/cams/newyork/timessquare/?cam=tsnorth_hd",
name: "Times Square View (North)",
channelNumber: 6,
},
{
streamUrl: "https://videos3.earthcam.com/fecnetwork/9974.flv/playlist.m3u8",
url: "https://www.earthcam.com/cams/newyork/timessquare/?cam=tsstreet",
name: "Times Square Street Cam",
channelNumber: 7,
},
{
streamUrl: "https://videos3.earthcam.com/fecnetwork/15559.flv/playlist.m3u8",
url: "https://www.earthcam.com/cams/newyork/timessquare/?cam=tsrobo3",
name: "Times Square Crossroads",
channelNumber: 8,
},
{
streamUrl: "https://videos3.earthcam.com/fecnetwork/13903.flv/playlist.m3u8",
url: "https://www.earthcam.com/usa/newyork/midtown/skyline/?cam=midtown4k",
name: "Midtown Skyline",
channelNumber: 9,
},
{
streamUrl: "https://videos-3.earthcam.com/fecnetwork/4089.flv/playlist.m3u8",
url: "https://www.earthcam.com/usa/newyork/midtown/skyline/?cam=midtown4k",
name: "Statue of Liberty",
channelNumber: 10,
}
]
},
start: function () {
Log.info(`Starting module: ${this.name}`);
const self = this;
// mapping through streams to create most recent live stream url
this.config.streams.map((stream) => {
axios.get(stream.url, config)
.then(response => {
const startJSON = response.data.indexOf("{\"cam\"")
const endJSON = response.data.indexOf("\"related_cams\":") - 2
const slicedJSON = response.data.slice(startJSON, endJSON).concat("}");
const parsedJSON = JSON.parse(slicedJSON)
const camName = Object.keys(parsedJSON.cam)[0]
const liveStreamURL = parsedJSON.cam[camName].html5_streamingdomain + parsedJSON.cam[camName].html5_streampath
stream.streamURL = liveStreamURL;
})
})
console.log(this.config.streams);
// initializing hls variable to be referenced when creating/destroying streams
this.hls = "";
// setting a blank interval
this.interval = null;
switch (this.config.style) {
case "tv":
// initializing status booleans
this.loading = true;
this.isArduinoStarting = false;
this.isArduinoStarted = false;
this.isArduinoConnected = false;
// sends the configuration details to node_helper
console.log(this.config);
self.log(this.config);
this.sendSocketNotification("CONFIG", this.config);
// sends a notification to node_helper to intialize the python shell
this.sendSocketNotification("INITIALIZE", null);
self.log(("[MMM-Live-Stream-TV::START()]: data: " + JSON.stringify(self.data, null, 4)), "dev");
self.log(("[MMM-Live-Stream-TV::START()]: config: " + JSON.stringify(self.config, null, 4)), "dev");
break;
case "slideshow":
this.sendSocketNotification("CONFIG", this.config);
console.log("in slideshow");
this.sendSocketNotification("SLIDESHOW", null);
case "static":
this.sendSocketNotification("STATIC", null);
}
},
getTranslations: function () {
return {
en: "translations/en.json"
};
},
getStyles: function () {
return [
"MMM-Live-Stream-TV.css"
];
},
// Define required scripts.
getScripts: function () {
// this loads the hls js file, and axios
return ["https://cdn.jsdelivr.net/npm/hls.js", "https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.js"];
},
socketNotificationReceived: function (notification, payload) {
const self = this;
self.log(notification);
self.log(JSON.stringify(payload, null, 4));
switch (notification) {
case "status":
if (payload.name == "setup") {
if (payload.data == "starting") {
self.log("[socketNotificationReceived::status]: starting");
this.isArduinoConnected = true;
this.isArduinoStarting = true;
} else if (payload.data == "started") {
self.log("[socketNotificationReceived::status]: started");
this.isArduinoStarting = false;
this.isArduinoStarted = true;
} else if (payload.data == "failed") {
self.log("[socketNotificationReceived::status]: failed");
this.isArduinoStarting = false;
this.isArduinoStarted = false;
}
}
if (payload.name == "connect") {
if (payload.data == "connected") {
self.log("[socketNotificationReceived::status]: connected");
this.isArduinoConnected = true;
this.sendNotification("SHOW_ALERT", { type: "notification", imageFA: "fa-microchip", title: self.translate("ARDUINO_CONNECTED") });
} else if (payload.data == "disconnected") {
self.log("[socketNotificationReceived::status]: disconnected");
this.isArduinoConnected = false;
this.sendNotification("SHOW_ALERT", { type: "notification", imageFA: "fa-microchip", title: self.translate("ARDUINO_DISCONNECTED") });
}
}
if (payload.name == "initialized") {
this.loading = false;
}
if (payload.name == "nextFeed") {
}
// self.updateDom(self.config.animationSpeed);
break;
case "sensor":
// if (this.isArduinoConnected && this.isArduinoStarted) {
if (this.isArduinoConnected) {
self.log("[socketNotificationReceived::sensor]: ");
self.log(JSON.stringify(payload, null, 4));
this.message = null;
this.config.sensors = payload;
console.log(`${this.name} received a module notification: ${notification}`)
console.log(payload);
var newChannel = parseInt(payload[0].value);
Log.log(newChannel + "NEW CHANNEL");
console.log(newChannel);
// sets current channel to the value that the Arduino Serial is sending out
this.config.currentChannel = newChannel;
this.updateDom(self.config.animationSpeed);
}
break;
case "slideshow":
console.log(`${this.name}: Slideshow Update: ${notification}`)
console.log(payload);
var newChannel = parseInt(payload);
Log.log(newChannel + " NEW CHANNEL");
// sets current channel to the value that the Arduino Serial is sending out
this.config.currentChannel = newChannel;
this.updateDom(self.config.animationSpeed);
case "error":
console.log("[socketNotificationReceived::error]:");
if (payload == "pyshell-throw") {
this.message = "Error : PyShell down!";
this.isArduinoConnected = false;
this.isArduinoStarted = false;
}
self.updateDom(self.config.animationSpeed);
break;
}
},
getDom: function () {
if (this.hls) {
this.hls.destroy();
};
var wrapper = document.createElement("div");
var video = document.createElement("video");
wrapper.appendChild(video);
// creating info divs below the video
var videoInfo = document.createElement("div");
var channelNumber = document.createElement("span");
channelNumber.className = ("channel-div");
channelNumber.innerHTML = `Channel ${this.config.currentChannel}`;
videoInfo.appendChild(channelNumber);
var videoName = document.createElement("span");
videoName.innerHTML = ` - ${this.config.streams[this.config.currentChannel].name}`
videoInfo.appendChild(videoName);
wrapper.appendChild(videoInfo);
video.width = this.config.frameWidth;
if (Hls.isSupported()) {
const currentChannel = this.config.streams[this.config.currentChannel].streamUrl
const hls = new Hls(config);
this.hls = hls;
//bind video element to HLS object
hls.attachMedia(video);
// MEDIA_ATTACHED event is fired by hls object once MediaSource is ready
hls.on(Hls.Events.MEDIA_ATTACHED, function () {
console.log("video and hls.js are now bound together!");
const proxy_url = 'http://127.0.0.1:8181';
const video_url = currentChannel;
const referer_url = 'https://www.earthcam.com/';
const file_extension = '.m3u8';
const hls_proxy_url = `${proxy_url}/${btoa(`${video_url}|${referer_url}`)}${file_extension}`
hls.loadSource(hls_proxy_url);
hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {
console.log(`manifest loaded, found ${data.levels.length} quality level`)
video.play();
});
})
}
else {
console.log("HLS is not supported, failed to attach media");
}
return wrapper;
},
log: function (message, type) {
var self = this;
if (self.config.developerMode) {
var date = new Date();
var time = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
message = self.name + ": (" + self.data.index + ")(" + time + ") " + message;
} else { message = self.name + ": " + message; }
switch (type) {
case "error": Log.error(this.logPrefix + message); break;
case "warn": Log.warn(this.logPrefix + message); break;
case "info": Log.info(this.logPrefix + message); break;
case "dev": if (self.config.developerMode) { Log.log(this.logPrefix + message); } break;
default: Log.log(this.logPrefix + message);
}
}
});