-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
126 lines (115 loc) · 3.82 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
"use strict";
const path = require("path");
const fs = require("fs");
var NodeHelper = require("node_helper");
const pm2 = require("pm2");
var log = (...args) => { /* do nothing */ };
module.exports = NodeHelper.create({
start () {
this.pm2 = pm2;
this.VLCPath = null;
this.ServerStarted = false;
},
stop () {
console.log("[VLC] Try to close VLCServer!");
this.pm2.stop("VLCServer", (e,p) => {
if (e) {
console.error("[VLC] Error: VLCServer can't stop !");
console.error("[VLC] Detail:", e);
}
});
},
socketNotificationReceived (noti, payload) {
switch (noti) {
case "INIT":
this.config= payload;
console.log("[VLC] EXT-VLCServer Version:", require("./package.json").version, "rev:", require("./package.json").rev);
this.initialize();
break;
case "RESTART":
this.VLCRestart();
break;
}
},
initialize () {
if (this.config.debug) log = (...args) => { console.log("[VLC]", ...args); };
console.log("[VLC] Launch VLC Http Server...");
this.VLC();
},
/** launch vlc with pm2 **/
VLC () {
this.VLCPath = path.resolve(this.config.vlcPath, "vlc");
if (!fs.existsSync(this.VLCPath)) {
console.error("[VLC] VLC is not installed or not found!");
this.sendSocketNotification("WARNING" , { message: "VLC_NotInstalled" });
return;
}
log("Found VLC in", this.VLCPath);
this.pm2.connect((err) => {
if (err) return console.error("[VLC]", err);
this.VLCStart();
this.pm2.launchBus((err, pm2_bus) => {
if (err) return console.error("[VLC] Bus connect error", err);
log("Bus Listener connected");
pm2_bus.on("process:event", (packet) => {
if (packet.process.name === "VLCServer") {
//console.log(`[VLC] Event: ${packet.event} Status: ${packet.process.status}`)
if (packet.process.status === "online") {
if (!this.ServerStarted) {
console.log(`[VLC] VLC Http Server Started! (id:${packet.process.pm_id})`);
this.sendSocketNotification("STARTED");
}
this.ServerStarted = true;
} else {
if (this.ServerStarted) {
console.error(`[VLC] VLC Http Server Closed! (id:${packet.process.pm_id})`);
this.sendSocketNotification("ERROR" , { message: "VLC_Close" });
this.sendSocketNotification("CLOSED");
}
this.ServerStarted = false;
}
}
});
pm2_bus.on("log:err", (packet) => {
if (packet.process.name === "VLCServer") {
if (packet.data.includes("main interface error:")) {
console.error("[VLC]", packet.data);
this.sendSocketNotification("ERROR" , { message: "VLC_ErrorPacket" });
} else {
log("[PACKET DATA]", packet.data);
}
}
});
});
});
},
VLCStart () {
this.pm2.start({
script: this.VLCPath,
name: "VLCServer",
out_file: "/dev/null",
/* eslint-disable @stylistic/array-element-newline */
args: [
"-I http",
"--extraintf", "http",
"--http-port", 8082,
"--http-host", "127.0.0.1",
"--http-password", "EXT-VLCServer"
]
/* eslint-enable @stylistic/array-element-newline */
}, (err, proc) => {
if (err) {
this.sendSocketNotification("WARNING" , { message: "VLCError", values: err.message });
console.error(`[VLC] ${err}`);
} else {
console.log("[VLC] Start listening on port 8082");
}
});
},
VLCRestart () {
this.pm2.restart("VLCServer", (err, proc) => {
if (err) return console.error(`[VLC] Error: ${err}`);
console.log("[VLC] VLC Http Server Restarted!");
});
}
});