-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamHandler.js
More file actions
172 lines (144 loc) · 5.48 KB
/
streamHandler.js
File metadata and controls
172 lines (144 loc) · 5.48 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
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
const https = require('https');
const { Notification } = require('electron');
const path = require('path');
// Keep track of the active stream request
let activeRequest = null;
let reconnectTimeout = null;
let isStreamingStopped = false;
async function startStreaming(mainWindow) {
const { default: Store } = await import('electron-store');
const store = new Store();
const username = store.get("username");
const password = store.get("password");
if (!username || !password) {
console.error("Username or password is missing from store!");
return;
}
// Reset the stopped flag when starting
isStreamingStopped = false;
console.log("Streaming started");
const options = {
hostname: 'pbx.sipcentric.com',
path: '/api/v1/stream',
method: 'GET',
headers: {
'Authorization': 'Basic ' + Buffer.from(`${username}:${password}`).toString('base64'),
'Accept': 'application/json',
},
};
function connectStream() {
// Don't reconnect if we've explicitly stopped streaming
if (isStreamingStopped) {
return;
}
activeRequest = https.request(options, (res) => {
res.on('data', (chunk) => {
try {
const message = chunk.toString().trim();
// Ignore Atmosphere HTML comments and empty responses
if (message.startsWith('<!--') || message === '' || message === 'EOD') {
return;
}
const event = JSON.parse(message);
handleEvent(event, mainWindow, store);
} catch (error) {
console.error('Error parsing stream data:', error);
console.log('Stream content: ', chunk.toString());
}
});
res.on('end', () => {
console.log('Stream ended. Reconnecting...');
// Only reconnect if we haven't stopped the stream
if (!isStreamingStopped) {
reconnectTimeout = setTimeout(connectStream, 5000); // Auto-reconnect after 5 seconds
}
});
});
activeRequest.on('error', (err) => {
console.error('Stream error:', err);
// Only reconnect if we haven't stopped the stream
if (!isStreamingStopped) {
reconnectTimeout = setTimeout(connectStream, 5000); // Retry on error
}
});
activeRequest.end();
}
connectStream();
}
function stopStreaming() {
if (isStreamingStopped)
{ console.log("Streaming was already stopped");
return true;
}
// Set flag to prevent reconnection
isStreamingStopped = true;
// Clear any pending reconnect timeout
if (reconnectTimeout) {
clearTimeout(reconnectTimeout);
reconnectTimeout = null;
}
// Abort the active request if it exists
if (activeRequest) {
activeRequest.destroy();
activeRequest = null;
console.log('Stream connection closed');
}
return true; // Indicate successful disconnect
}
function handleEvent(event, mainWindow, store) {
if (event.event === 'heartbeat') {
return; // Ignore heartbeat events
}
switch (event.event) {
case 'incomingcall':
const extension = store.get('extension', '');
const customer = store.get('customer', '');
const endpoint = `/customers/${customer}/endpoints/${extension}`;
const showNotifications = store.get('showNotifications', false);
if (event.values.endpoint == endpoint) {
console.log('Incoming call to my extension:', event.values);
if (showNotifications) showIncomingCallNotification(event.values);
} else {
console.log('Incoming call to other extension:', event.values);
}
break;
case 'smsreceived':
console.log('Incoming SMS:', event.values);
break;
case 'smsdelivered':
console.log('SMS delivered:', event.values);
break;
case 'callended':
console.log('Call Ended:', event.values);
break;
default:
console.log('Unknown event:', event);
}
// Send event data to renderer process
if (mainWindow) {
mainWindow.webContents.send('stream-event', event);
}
}
function showIncomingCallNotification(callerInfo) {
const callerName = callerInfo.callerIdName || 'Unknown';
const callerNumber = callerInfo.callerIdNumber || 'Unknown Number';
const callerDid = callerInfo.did || 'Unpsecified';
const notification = new Notification({
title: 'Incoming Call',
body: `${callerName} (${callerNumber})\nOn Number: ${callerDid}`,
silent: true, // Set to true if you don't want a sound
urgency: 'critical', // Makes notification more prominent
timeoutType: 'default',
icon: path.join(__dirname, 'assets/icon128.png')
});
notification.show();
// Optional: You can handle click events on the notification
notification.on('click', () => {
// Focus the main window or open a specific call handling UI
// For example:
// mainWindow.focus();
// mainWindow.webContents.send('show-call-ui', callerInfo);
});
return notification;
}
module.exports = { startStreaming, stopStreaming };