This repository has been archived by the owner on Aug 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
187 lines (164 loc) · 6.15 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
if (require('electron-squirrel-startup')) return;
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const electron = require('electron');
const wpilib_NT = require('wpilib-nt-client');
const fs = require('fs');
const client = new wpilib_NT.Client();
// Debug client
// client.debug = (type, msg) => console.log(`${type}: ${msg}`);
/** 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 robotIp = '10.14.18.2';
const simIp = '127.0.0.1';
// 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;
let clientDataListener = (key, val, valType, mesgType, id, flags) => {
if (val === 'true' || val === 'false') {
val = val === 'true';
}
mainWindow.webContents.send(mesgType, {
key,
val,
valType,
id,
flags
});
};
function createWindow() {
// Attempt to connect to the localhost
if (process.platform !== "win32")
{
client.start((con, err) => {
let connectFunc = () => {
console.log('Sending status to localhost');
mainWindow.webContents.send('connected', con);
};
// If the Window is ready than send the connection status to it
if (ready) connectFunc();
if (con) connectedFunc = connectFunc;
}, simIp);
}
// 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();
} else {
ipc.emit('connect', null, robotIp);
}
});
// When the user chooses the address of the bot than try to connect
ipc.on('connect', (ev, address, port) => {
client.setReconnectDelay(1000);
console.log(`Trying to connect to ${address}` + (port ? ':' + port : ''));
let callback = (connected, err) => {
console.log('Sent status to ' + address);
client.connected = true; // This line allows the server to continue trying reconnects
mainWindow.webContents.send('connected', connected);
};
if (port) {
client.start(callback, address, port);
} else {
client.start(callback, address);
}
});
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('windowError', (ev, error) => {
console.log(error);
});
// Create the browser window.
const windowSize = JSON.parse(fs.readFileSync(__dirname + '/settings/window.json'));
mainWindow = new BrowserWindow({
width: windowSize.width,
height: windowSize.height,
// 1366x570 is a good standard height, but you may want to change this to fit your DriverStation's screen better.
// It's best if the dashboard takes up as much space as possible without covering the DriverStation application.
// The window is closed until the python server is ready
show: false,
icon: __dirname + '/../images/icon.png',
// Security additions and electron 5.0+ compatibility
webPreferences: {
nodeIntegration: true
}
});
// 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();
});
mainWindow.on('resize', () => {
windowSize.width = mainWindow.getSize()[0];
windowSize.height = mainWindow.getSize()[1];
});
// 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);
fs.writeFileSync(__dirname + '/settings/window.json', JSON.stringify(windowSize));
});
mainWindow.on('unresponsive', () => {
console.log('Main Window is unresponsive');
});
mainWindow.webContents.on('did-fail-load', () => {
console.log('window failed load');
});
}
// 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();
});