-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
174 lines (137 loc) · 4.25 KB
/
main.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
var response;
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require("ejs");
const path = require('path');
const app = express();
const routes = require("./routes/index.js")
app.use("/", routes);
app.use(bodyParser.urlencoded({ extended: false })); // for parsing application/x-www-form-urlencoded
app.set('views', './views');
app.use(express.static('views'));
function middleware(req, res, next) {
console.log(req.url + JSON.stringify(req.body));
next();
}
app.use(middleware);
app.set("view engine", "ejs");
//var serveStatic = require( "serve-static" );
//app.use('/media', serveStatic('z:\\'));
//console.log (" from main.js" + routes.connected.toString());
const port = 8080;
const http = require("http").Server(app);
const cluster = require("cluster");
const os = require("os");
// if (cluster.isMaster) {
// for (let i = 0; i < os.cpus().length; i++) {
// cluster.fork()
// }
// }
// else {
// http.listen(port, () => {
// console.log(process.pid);
// console.log(`Server is liestemnig on ${port}`);
// })
// }
http.listen(port, () => {
console.log(process.pid);
console.log(`Server is liestemnig on ${port}`);
})
const io = require("socket.io")(http);
global.io = io;
global.app = app;
io.on('connection', (socket) => {
console.log("Socket Server connected");
io.emit('casparstatus', { data1: connected.toString() });
});
var connected = false;
var net = require('net');
var client = net.Socket();
async function connecttocasparcg(host1, port1, res) {
if (connected == false) {
console.log("Connection Request from client");
client = new net.Socket();
var aa = await client.connect(port1, host1);
//console.log(aa);
connected = true;
}
else {
console.log("Disconnection Request from client");
client.end();
//client.distroy();
connected = false;
}
io.emit('casparstatus', { data1: connected.toString() });
res.end();
}
client.on('error', function (err) {
console.log(err);
connected = false;
io.emit('casparstatus', { data1: connected.toString() });
})
client.on('close', function () {
console.log('Casparcg Connection closed');
connected = false;
io.emit('casparstatus', { data1: connected.toString() });
});
client.on('end', function () {
console.log('Casparcg Connection closed');
connected = false;
io.emit('casparstatus', { data1: connected.toString() });
});
client.on('connect', function () {
console.log('Casparcg connected');
connected = true;
io.emit('casparstatus', { data1: connected.toString() });
});
client.on('disconnect', function () {
console.log('Casparcg disconnected');
connected = false;
io.emit('casparstatus', { data1: connected.toString() });
});
client.on('uncaughtException', function (err) {
console.log("client uncaughtException error");
connected = false;
io.emit('casparstatus', { data1: connected.toString() });
});
process.on('uncaughtException', function (err) {
console.log("process uncaughtException error");
// connected = false;
// io.emit('casparstatus', { data1: connected.toString() });
});
app.post('/endpoint', (req, res) => {
if (connected == true) {
var aa = (req.body.cmd).split("\n");
for (i = 0; i < aa.length; i++) {
client.removeAllListeners();
client.write(aa[i] + `\r\n`);
client.on('data', function (data) {
res.end(data);
});
}
}
});
app.post('/connect', (req, res) => {
connecttocasparcg(req.body.host, req.body.port, res);
//res.end();
});
//main index file
app.get('/', (req, res) => {
res.render("index", {});
res.end();
});
const dirTree = require("directory-tree");
app.post('/getfilesforanywhere', (req, res) => {
var product = [];
const tree = dirTree(req.body.cmd, {}, (item, PATH, stats) => {
product.push([item.path])
})
res.send(product);
});
const nms1 = require("./nms.js");
var osc1 = require("./osc.js");
const ffmpeg1 = require("./ffmpeg.js");
const cpuuses1 = require("./cpuuses.js");
const vtrcontrol1 = require("./vtrcontrol.js");
const mysql1 = require("./sql.js");
const xdcamcontrol1 = require("./xdcamcontrol.js");