-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
84 lines (68 loc) · 2.15 KB
/
index.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
const fs = require('fs')
const os = require('os')
const https = require('https')
const express = require('express')
const { Server } = require('socket.io')
const { lookup } = require('dns')
const open = require('open')
const hostname = os.hostname()
const app = express()
const server = https.createServer(
{
key: fs.readFileSync(`${__dirname}/localhost+2-key.pem`),
cert: fs.readFileSync(`${__dirname}/localhost+2.pem`),
requestCert: false,
rejectUnauthorized: false,
},
app
)
const io = new Server(server)
app.use(express.static(`${__dirname}/client/dist`))
io.on('connection', (socket) => {
console.log(`${getDate()} New device is connected`)
io.emit('connection message', io.of('/').sockets.size)
socket.on('disconnect', (reason) => {
console.log(`${getDate()} Device is disconnected`)
io.emit('connection message', io.of('/').sockets.size)
})
socket.on('motion message', (motionData) => {
io.emit('motion message', motionData)
})
socket.on('settings message', (settingsData) => {
io.emit('settings message', settingsData)
})
// Ping-pong events are needed to calculate the signal latency
socket.on('ping', (dateFromDesktop) => {
io.emit('ping', dateFromDesktop)
})
socket.on('pong', (dateFromDesktop) => {
io.emit('pong', dateFromDesktop)
})
})
function getDate() {
let date = new Date()
return `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}:${date.getMilliseconds()}`
}
let address = null
app.get('/hostname', (request, responce) => {
responce.status(200).type('text/html')
responce.send(address)
})
const options = {
family: 4,
all: true,
}
server.listen(443, '0.0.0.0', function () {
console.log(`${getDate()} Audio-motion interface is up and running`)
lookup(hostname, options, function (err, ips, fam) {
const serverIP = ips.find(ip => ip.address.indexOf('192.168') === 0)
if (serverIP) {
address = serverIP.address
console.log(`${getDate()} Opening https://${address} in default browser`)
open(`https://${address}`)
console.log(`${getDate()} Close terminal for exit from AMI`)
} else {
address = 'ami.stranno.su'
}
})
})