-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
119 lines (101 loc) · 2.95 KB
/
index.js
File metadata and controls
119 lines (101 loc) · 2.95 KB
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
import 'dotenv/config'
import { sip } from './sip.js'
import { ring } from './ring.js'
import { tones } from './tones.js'
import { startHealthServer } from './health.js';
const { NOTIFY_URL } = process.env;
const health = startHealthServer();
// Initialize
Promise.all([
sip.initialize(),
ring.initialize()
]).then(() => {
tones.initialize(sip, ring)
console.log('INDEX - Initialized.')
// Setup event listeners for SIP
sip.on('ringing', () => {
console.error('INDEX - SIP ringing')
// for Ring button initiated all, the ring speaker should not play the ringback tone unless sip is actually ringing
tones.sipRinging()
})
sip.on('callEstablished', (rtpInfo) => {
console.log('INDEX - SIP call established')
tones.sipReady()
ring.pipeAudio(sip)
})
sip.on('callFailed', (err) => {
console.error('INDEX - SIP call failed:', err)
fullCleanup()
})
sip.on('callEnded', () => {
console.log('INDEX - SIP call ended. Cleaning up everything.')
fullCleanup()
})
sip.on('inboundCall', () => {
console.log('INDEX - Inbound SIP call. Initiating RING call.')
ring.initiateCall()
})
// Setup event listeners for Ring
ring.on('callEstablished', () => {
console.log('INDEX - Ring call established')
sip.pipeAudio(ring)
})
ring.on('receivingAudio', () => {
console.log('INDEX - Receiving audio from Ring')
tones.ringReady()
})
ring.on('callEnded', () => {
console.log('INDEX - Ring call ended. Cleaning up everything.')
fullCleanup()
})
ring.on('buttonPressed', (camera) => {
console.log(`INDEX - Button pressed for ${camera.name}`)
notify()
doConnect()
})
sip.register()
ring.listen()
//doConnect() // for testing purposes
})
process.on('SIGINT', () => {
console.log('\nINDEX - Caught Ctrl+C. Cleaning up and exiting...')
fullCleanup()
})
// Functions
async function fullCleanup() {
sip.cleanup();
ring.cleanup();
tones.cleanup();
try {
await health.close();
console.log('INDEX - Health server closed.');
} catch (err) {
console.error('INDEX - Error closing health server:', err);
}
setTimeout(() => {
process.exit(0);
}, 200);
}
function doConnect(camera) {
Promise.all([
sip.initiateCall(),
ring.initiateCall()
])
.then(() => {
console.log('INDEX - Both calls initiated in parallel.')
})
.catch((err) => {
console.error('INDEX - Error initiating calls:', err)
fullCleanup()
})
}
function notify() {
if (NOTIFY_URL && NOTIFY_URL.startsWith('http')) {
fetch(NOTIFY_URL)
.then(response => response.text())
.then(data => console.log('INDEX - Notification sent successfully:', data))
.catch(error => console.error('INDEX - Error sending notification:', error));
} else {
console.log('INDEX - NOTIFY_URL is not set or invalid.');
}
}