-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
143 lines (103 loc) · 3.25 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
136
137
138
139
140
141
142
143
/*
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 server = require("./Server/Server");
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, webPreferences:{
nodeIntegration: true
}
});
mainWindow.loadURL("file://" + __dirname + "/renderer/index.html");
mainWindow.focus();
//dev tools
// mainWindow.webContents.openDevTools();
ipcMain.on('startDevice', (evt, type, id) =>{
console.log('type', type, 'device id', id);
let newDevice = new (require('./Device/Devices/' + type))(id, type);
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 from " + d.type);
evt.sender.send('image', {device: d.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;
console.log('opening port', portNum);
currentServer = new server.Server(portNum, () =>{
console.log('server started on', portNum);
evt.sender.send('startedServer', portNum);
})
});
ipcMain.on('startDepth', (evt, type, id) =>{
devices[id].getDepth();
});
ipcMain.on('stopDepth', (evt, type, id) =>{
devices[id].depthFeed = false;
});
ipcMain.on('startColor', (evt, type, id) =>{
devices[id].getColor();
});
ipcMain.on('stopColor', (evt, type, id) =>{
devices[id].colorFeed = false;
});
ipcMain.on("setSize", (evt, type, id, w, h) => {
devices[id].setSize(w, h)
});
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(){
for(let i = 0; i < devices.length; i++){
devices[i].stop();
}
app.removeAllListeners();
app.quit();
console.log('quit renderer');
});