-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelectron.js
More file actions
195 lines (175 loc) · 6.05 KB
/
Copy pathelectron.js
File metadata and controls
195 lines (175 loc) · 6.05 KB
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
const {app, BrowserWindow, ipcMain} = require('electron')
if (require('electron-squirrel-startup')) return app.quit();
const path = require("path");
let send_fake_data, fs, SerialPort, Readline, rearPort, frontPort;
fs = require('fs');
if (process.env.COMPUTERNAME === "FORRESTS-LAPTOP" && process.env.fakeData) {
console.log("Sending Fake Data")
send_fake_data = true;
} else {
SerialPort = require('serialport').SerialPort;
Readline = require('@serialport/parser-readline').ReadlineParser;
}
async function createPort(portID) {
let port;
if (portID.startsWith('FAKE')) {
console.log('creating rearPort with id ' + portID + ' note: is fake');
win.webContents.send('console', 'creating rearPort with id ' + portID + ' note: is fake');
setInterval(() => {
const string = fs.readFileSync(`fake/${portID}.json`);
parseAndSend(string)
}, portID === 'FAKE_FRONT' ? 200 : 1520)
return "Its a fake port bozo";
}
console.log('creating port @ ' + portID);
port = await new Promise((resolve, reject) => {
const port = new SerialPort({path: portID, baudRate: 115200}, (err) => {
if (err) {
console.log('Unable to open rearPort: ', err.message);
win.webContents.send('console', 'Unable to open rearPort: ' + err.message)
resolve(undefined);
} else {
console.log('Successfully opened port @ ' + portID);
win.webContents.send('console', 'Successfully opened port @ ' + portID)
resolve(port)
}
});
});
if (!port) return;
const parser = port.pipe(new Readline({delimiter: '\n'}));
parser.on('data', data => {
parseAndSend(data);
});
return port;
}
ipcMain.on('ready', async () => {
if (rearPort) return;
console.log('The program is ready for data');
if (send_fake_data) {
frontPort = createPort('FAKE_FRONT')
rearPort = createPort('FAKE_REAR')
return
}
if (process.env.COMPUTERNAME === "FORRESTS-LAPTOP") {
frontPort = await createPort('COM4');
// rearPort = createPort('FAKE_REAR')
rearPort = await createPort('COM20');
} else {
frontPort = await createPort('COM6');
rearPort = await createPort('COM5');
}
});
ipcMain.on('reset', async () => {
if(rearPort.isOpen) {
await new Promise(((resolve, reject) => {
rearPort.close((err) => {
if (err) reject(err);
else resolve();
})
}))
}
if(frontPort.isOpen) {
await new Promise(((resolve, reject) => {
frontPort.close((err) => {
if (err) reject(err);
else resolve();
})
}))
}
if (send_fake_data) {
frontPort = createPort('FAKE_FRONT')
rearPort = createPort('FAKE_REAR')
return
}
if (process.env.COMPUTERNAME === "FORRESTS-LAPTOP") {
frontPort = await createPort('COM4');
rearPort = createPort('FAKE_REAR')
} else {
frontPort = await createPort('COM6');
rearPort = await createPort('COM5');
}
});
ipcMain.on('inverter', (event, on) => {
if (frontPort && !send_fake_data)
frontPort.write(Buffer.from(`i${on ? 'y' : 'n'}\n`, 'utf8'));
else console.log('No frontPort')
console.log('Inverter port set to ' + on)
});
ipcMain.on('led_select', (event, led) => {
if (rearPort && !send_fake_data) {
rearPort.write(Buffer.from('l', 'utf8'));
rearPort.write(Buffer.from([led]))
}
else console.log('No rearPort')
console.log('The user has selected LED ' + led)
});
ipcMain.on('charger_current', (event, current) => {
if (rearPort && !send_fake_data) {
rearPort.write(Buffer.from('c', 'utf8'));
rearPort.write(Buffer.from([current]))
}
else console.log('No frontPort')
console.log('The user has selected charger current ' + current)
});
let win;
function createWindow() {
win = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
backgroundThrottling: false,
},
width: 1280,
height: 800,
fullscreen: process.env.COMPUTERNAME !== "FORRESTS-LAPTOP",
})
win.loadFile('public/index.html')
}
function parseAndSend(string) {
let jsonData;
try {
jsonData = JSON.parse(string);
if (send_fake_data) {
const excluded = ['ch']
for (const [key, value] of Object.entries(jsonData)) {
if (!excluded.includes(key)) {
if (typeof value === 'number')
jsonData[key] = (Math.random() - 0.5) * value * 0.001 + value;
if (typeof value === 'object')
for (const [k, v] of Object.entries(value)) {
if (typeof v === 'number')
value[k] = (Math.random() - 0.5) * v * 0.001 + v;
}
}
}
jsonData['fake'] = true
}
win.webContents.send('data', jsonData)
} catch (error) {
console.log(string)
win.webContents.send('console', 'Error parsing data: ' + error.message)
}
}
app.whenReady().then(createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
console.log("Closing program.")
app.quit()
}
})
let currentFile = undefined;
ipcMain.on('file_open', (event) => {
// If /logs doesn't exist, create the folder
if (!fs.existsSync('./logs')) {
fs.mkdirSync('./logs');
}
//Set current file to log, then the current month, day, hour, minute, second considering the timezone
currentFile = `./logs/log${new Date().toLocaleString('en-US', {timeZone: 'America/New_York'})
.replace(/[/, :]/g, '-')
.replaceAll('--', '-')}.txt`;
fs.writeFileSync(currentFile, '');
});
ipcMain.on('file_write', (event, data) => {
if (currentFile) {
fs.appendFileSync(currentFile, data);
}
});