-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
97 lines (83 loc) · 2.31 KB
/
main.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
const { app, BrowserWindow, ipcMain } = require('electron')
const ipc = ipcMain;
let win;
let screenWidth = 1280;
let screenHeight = 486;
function createWindow () {
win = new BrowserWindow({
width: screenWidth,
minWidth: screenWidth,
maxWidth: screenWidth,
height: screenHeight,
minHeight: screenHeight,
maxHeight: screenHeight,
x: 0,
y: 0,
backgroundColor: "#FFDF94",
center: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
})
win.loadFile('src/index.html')
}
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// wpilib stuff
const ntClient = require('wpilib-nt-client');
const client = new ntClient.Client();
client.setReconnectDelay(1000);
let clientDataListener = (key, val, valType, mesgType, id, flags) => {
if (val === 'true' || val === 'false') {
val = val === 'true';
}
win.webContents.send(mesgType, {
key,
val,
valType,
id,
flags
});
};
ipc.on('ready', (ev, mesg) => {
console.log('NetworkTables is ready');
// Remove old Listener
client.removeListener(clientDataListener);
// Add new listener with immediate callback
client.addListener(clientDataListener, true);
});
// When the user chooses the address of the bot than try to connect
ipc.on('connect', (ev, address, port) => {
let callback = (connected, err) => {
try{
win.webContents.send('connected', connected); //throws error ere
} catch(e){ /* errors but works */ }
};
if (port) {
client.start(callback, address, port);
} else {
client.start(callback, address);
console.log("connecting to " + address)
}
});
ipc.on('stop-connect', () => {
client.stop()
});
ipc.on('add', (ev, mesg) => {
client.Assign(mesg.val, mesg.key, (mesg.flags & 1) === 1);
});
ipc.on('update', (ev, mesg) => {
client.Update(mesg.id, mesg.val);
});
ipc.on('delete', (ev, mesg) => {
client.Delete(mesg.id)
});
client.addListener(clientDataListener, true);