-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (95 loc) · 3.12 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
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
/*
To rebuild: $(npm bin)/electron-rebuild
https://github.com/electron/electron-rebuild
To run: npm start
*/
const electron = require('electron');
const {app, BrowserWindow, ipcMain} = electron;
const socketServer = require("./socketServer/SocketServer");
const device = require("./Device/Device");
let devices = [];
let mainWindow = null;
let currentServer = null;
function createWindow(){
const {width, height} = electron.screen.getPrimaryDisplay().workAreaSize;
mainWindow = new BrowserWindow({width, height});
mainWindow.loadURL("file://" + __dirname + "/renderer/index.html");
mainWindow.focus();
mainWindow.webContents.openDevTools();
ipcMain.on('startDevice', (evt, type, id) =>{
console.log('type', type, 'device id', id);
let newDevice = new (require('./Device/Devices/' + type))(id);
newDevice.start();
newDevice.onError = err => {
console.log(err);
evt.sender.send('startError', err);
};
devices.push(newDevice);
devices.forEach(d => {
d.onImage = (img, frameType) => {
let id = d.id;
//console.log('sending image of length', img.length);
evt.sender.send('image', {device: type, id, img, frameType});
if(currentServer){
currentServer.sendImage({device: type, id, img, frameType});
}
};
});
evt.sender.send('startedDevice', type, id);
console.log(`${devices.length} total Devices connected`);
});
ipcMain.on('startServer', (evt, port) => {
let portNum = port;
if (port == "") {
portNum = 8080;
}
console.log('opening port', portNum);
currentServer = new socketServer.SocketServer(portNum, () =>{
console.log('socket started on', portNum);
evt.sender.send('startedServer', portNum);
})
});
ipcMain.on('startDepth', (evt, type, id) =>{
devices[id].getDepth();
});
ipcMain.on('startColor', (evt, type, id) =>{
devices[id].getColor();
});
ipcMain.on('close', (evt) => {
//currentServer.stop();
devices.forEach(d => {
d.stop();
});
evt.sender.send('closed');
devices = [];
});
ipcMain.on('closeServer', (evt) => {
currentServer.stop();
evt.sender.send('closedServer');
console.log('server closed');
});
ipcMain.on('closeDevice', (evt, id) => {
devices[id].stop();
console.log('closed device of id ' + id);
evt.sender.send('closedDevice', id);
devices.splice(id, 1);
console.log(devices.length);
});
mainWindow.on('closed', function(){
mainWindow = null;
});
}
app.on('ready', createWindow);
app.on('activate', function(){
if(mainWindow == null){
createWindow();
}
});
app.on('window-all-closed', function(){
console.log('window all closed currentServer', currentServer);
if (currentServer) {
currentServer.stop();
currentServer = null;
}
console.log('quit renderer');
});