-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
233 lines (195 loc) · 6.81 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var tcpp = require('tcp-ping');
const electron = require('electron');
const wpilib_NT = require('wpilib-nt-client');
const client = new wpilib_NT.Client();
const process = require('process');
if (require('electron-squirrel-startup')) return;
// The client will try to reconnect after 1 second
client.setReconnectDelay(1000);
/** Module to control application life. */
const app = electron.app;
/** Module to create native browser window.*/
const BrowserWindow = electron.BrowserWindow;
/** Module for receiving messages from the BrowserWindow */
const ipc = electron.ipcMain;
const globalShortcut = electron.globalShortcut;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
/**
* The Main Window of the Program
* @type {Electron.BrowserWindow}
* */
let mainWindow;
let connectedFunc, ready = false;
// Foward NT values to BrowserWindow
let clientDataListener = (key, val, valType, mesgType, id, flags) => {
// handle boolean value
if (val === 'true' || val === 'false') {
val = val === 'true';
}
// send data to BrowserWindow
mainWindow.webContents.send(mesgType, {
key,
val,
valType,
id,
flags
});
};
// Start up the BrowserWindow
function createWindow() {
// When the script starts running in the window set the ready variable
ipc.on('ready', (ev, mesg) => {
console.log('NetworkTables is ready');
ready = mainWindow != null;
// Remove old Listener
client.removeListener(clientDataListener);
// Add new listener with immediate callback
client.addListener(clientDataListener, true);
// Send connection message to the window if if the message is ready
if (connectedFunc) connectedFunc();
});
ipc.on('add', (ev, mesg) => {
client.Assign(mesg.val, mesg.key, (mesg.flags & 1) === 1);
}); // Handle add from BrowserWindow
ipc.on('update', (ev, mesg) => {
client.Update(mesg.id, mesg.val);
}); // Handle update from BrowserWindow
ipc.on('windowError', (ev, error) => {
console.log(error);
}); // Handle error from BrowserWindow
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1450,
height: 570,
// The width and height of BrowserWindows
show: false,
icon: __dirname + '/../images/icon.png',
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
});
// Move window to top (left) of screen.
mainWindow.setPosition(0, 0);
// Load window.
mainWindow.loadURL(`file://${__dirname}/index.html`);
// Once the python server is ready, load window contents.
mainWindow.once('ready-to-show', () => {
console.log('main window is ready to be shown');
mainWindow.show();
});
// Remove menu
//mainWindow.setMenu(null);
// Emitted when the window is closed.
mainWindow.on('closed', () => {
console.log('main window closed');
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
ready = false;
connectedFunc = null;
client.removeListener(clientDataListener);
process.exit();
});
mainWindow.on('unresponsive', () => {
console.log('Main Window is unresponsive');
});
mainWindow.webContents.on('did-fail-load', () => {
console.log('window failed load');
});
globalShortcut.register('f5', function () {
console.log('f5 is pressed')
mainWindow.reload();
})
globalShortcut.register('CommandOrControl+R', function () {
console.log('CommandOrControl+R is pressed')
mainWindow.reload();
})
// Start NT connection processs
startNTconnect();
}
let targetHost = "";
const teamNumber = process.env.teamNumber || "6083";
function startNTconnect() {
let roboRioIP = "10.";
if (teamNumber.length <= 2) {
roboRioIP += "0." + teamNumber;
} else {
roboRioIP += teamNumber.substr(0, teamNumber.length - 2) + "." + teamNumber.substr(teamNumber.length - 2, 2);
}
roboRioIP += ".2";
let NtAddress = ["172.22.11.2", roboRioIP, "roborio-" + teamNumber + "-frc.local", "127.0.0.1"]
NtAddress.forEach(function (host) {
if (targetHost === "") {
tcpp.probe(host, 1735, function (_, available) {
if (available) {
console.log("Set target to: " + host);
targetHost = host;
}
});
}
});
// Scan ip to find roborio.
setTimeout(function () {
if (targetHost == "") {
startNTconnect();
}
else {
connectToNT(targetHost);
setTimeout(function () {
client.Update(client.getKeyID("/SmartDashboard/NT/ip"), targetHost);
readPing();
}, 1000);
}
}, 5000);
// Wait 5 sec before connect
}
function readPing() {
tcpp.ping({ address: targetHost, port: 1735 }, function (_, data) {
client.Update(client.getKeyID("/SmartDashboard/NT/ping"), Math.round(parseFloat(data.avg)));
setTimeout(function () {
readPing();
}, 3000);
});
}
// Connect to NT with given address and port.
function connectToNT(address, port) {
console.log(`Trying to connect to ${address}` + (port ? ':' + port : ''));
let callback = (connected) => {
console.log('Sending status');
mainWindow.webContents.send('connected', connected);
};
if (port) {
client.start(callback, address, port);
} else {
client.start(callback, address);
}
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', () => {
console.log('app is ready');
createWindow();
});
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q.
// Not like we're creating a consumer application though.
// Let's just kill it anyway.
// If you want to restore the standard behavior, uncomment the next line.
// if (process.platform !== 'darwin')
app.quit();
});
app.on('quit', function () {
console.log('Application quit.');
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow == null) createWindow();
});