This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
236 lines (209 loc) · 5.28 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
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
234
235
236
'use strict'
// Global
const { app, BrowserWindow, ipcMain, Menu, Tray } = require('electron');
const path = require('path');
const { Client } = require('discord-rpc');
const julian = require('julian');
const { autoUpdater } = require('electron-updater');
let connected = false;
let minimized = false;
let electronWindow;
let tray;
let location = 'Jezero Crater';
// App
function createWindow() {
electronWindow = new BrowserWindow({
backgroundColor: '#36393f',
closable: false,
fullscreenable: false,
icon: 'percy.png',
width: 340,
height: 380,
resizable: false,
titleBarStyle: 'hidden',
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
zoomFactor: 1.0,
},
});
electronWindow.loadFile('index.html');
electronWindow.on('minimize', event => {
event.preventDefault();
electronWindow.hide();
minimized = true;
});
electronWindow.on('restore', () => {
electronWindow.show();
minimized = false;
if (connected) {
electronWindow.webContents.executeJavaScript('setConnected();');
} else {
electronWindow.webContents.executeJavaScript('setDisconnected();');
}
});
electronWindow.webContents.executeJavaScript(`startup('${app.getVersion()}', '${location}');`);
}
function createTray() {
const icon = path.join(__dirname, 'percy.png');
const appIcon = new Tray(icon);
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show',
click: () => {
electronWindow.show();
minimized = false;
if (connected) {
electronWindow.webContents.executeJavaScript('setConnected();');
} else {
electronWindow.webContents.executeJavaScript('setDisconnected();');
}
}
},
{
label: 'Quit',
click: () => {
electronWindow.destroy();
}
},
]);
appIcon.on('double-click', () => {
electronWindow.show();
});
appIcon.setToolTip('Perseverance RPC');
appIcon.setContextMenu(contextMenu);
tray = appIcon;
}
Menu.setApplicationMenu(Menu.buildFromTemplate([
{
label: 'Options',
submenu: [
{
role: 'toggleDevTools',
},
{
label: 'Quit',
click: () => {
electronWindow.destroy();
}
},
]
},
]));
ipcMain.on('updateLocation', (event, arg) => {
location = arg;
update();
event.returnValue = true;
});
ipcMain.on('reconnect', event => {
forceReconnect();
event.returnValue = true;
});
app.on('ready', () => {
createWindow();
createTray();
autoUpdater.checkForUpdatesAndNotify();
});
app.on('window-all-closed', () => {
app.quit();
})
app.on('activate', () => {
if (electronWindow === null) {
createWindow();
}
});
app.on('quit', () => console.log("Quitting"));
// RPC
const clientId = '811145575134658561';
const day = 24 * 60 * 60 * 1000;
const marsDay = 88775244;
const landingTimestamp = 1613681692000;
let forced = false;
function getMSD(earthTimestamp) {
return (julian(earthTimestamp) - 2451549.5) / 1.0274912517 + 44796;
}
function getMission() {
const date = Date.now();
const diff = getMSD(date) - getMSD(landingTimestamp);
const sol = Math.floor(diff);
const percentage = (diff * 100 - sol * 100).toFixed(2);
const totalSol = sol * marsDay;
const solTimestamp = landingTimestamp + totalSol;
let countDown = false;
let timestamp = solTimestamp;
if ((timestamp + day) < date) {
countDown = true;
timestamp += marsDay;
}
return {
sol,
percentage,
countDown,
timestamp,
}
}
const rpcClient = new Client({ transport: 'ipc' });
let updateInterval;
rpcClient.on('ready', () => {
update();
updateInterval = setInterval(update, 10 * 1000);
connected = true;
if (!minimized && electronWindow) {
electronWindow.webContents.executeJavaScript('setConnected();');
}
});
rpcClient.on('disconnected', () => {
connected = false;
if (!minimized && electronWindow) {
electronWindow.webContents.executeJavaScript('setDisconnected();');
}
clearInterval(updateInterval);
updateInterval = null;
console.log('Disconnected, attempting reconnect every 10 seconds');
if (forced) {
forced = false;
console.log('Forced disconnect, fast tracking reconnect');
setTimeout(connect, 1000);
} else {
setTimeout(connect, 10000);
}
})
function update() {
const missionInfo = getMission();
const state = `Perseverance - Sol ${missionInfo.sol} (${missionInfo.percentage}%)`;
const details = `Location: ${location}`;
const buttons = [
{
label: 'Where is Perseverance!',
url: 'https://mars.nasa.gov/mars2020/mission/where-is-the-rover/',
},
{
label: 'Read about Sol (Mars day)!',
url: 'https://en.wikipedia.org/wiki/Sol_(day_on_Mars)',
}
];
rpcClient.setActivity({
state,
details,
startTimestamp: missionInfo.timestamp,
endTimestamp: missionInfo.countDown ? missionInfo.timestamp : undefined,
largeImageKey: 'ps',
largeImageText: 'Nasa is running this mission!',
buttons,
}).catch(console.error);
}
function connect() {
rpcClient._connectPromise = undefined;
rpcClient.login({ clientId })
.then(() => console.log('RPC Connected.'))
.catch(console.error);
}
function forceReconnect() {
forced = true;
try {
rpcClient.destroy();
} catch {
connect();
}
}
connect();