|
1 | 1 | const electron = require('electron'); |
2 | 2 | const path = require('path'); |
3 | 3 | const url = require('url'); |
| 4 | +const fs = require('fs'); |
4 | 5 | const { ipcMain } = require('electron'); |
5 | 6 | const loadBalancer = require('electron-load-balancer'); |
6 | 7 |
|
7 | 8 | const { app } = electron; |
8 | | -const { BrowserWindow } = electron; |
| 9 | +const { BrowserWindow, dialog } = electron; |
9 | 10 | const nativeImage = electron.nativeImage; |
10 | 11 |
|
| 12 | +const { extractFileName } = require('../src/utils/fileNameProcessor'); |
| 13 | + |
11 | 14 | if (process.env.DEV) { |
12 | 15 | const { |
13 | 16 | default: installExtension, |
@@ -41,7 +44,7 @@ function createWindow() { |
41 | 44 | icon, |
42 | 45 | webPreferences: { |
43 | 46 | nodeIntegration: true, |
44 | | - enableRemoteModule: true, |
| 47 | + enableRemoteModule: false, |
45 | 48 | }, |
46 | 49 | minWidth: 500, |
47 | 50 | minHeight: 300, |
@@ -149,3 +152,96 @@ ipcMain.on('FETCH_LA', (event, args) => { |
149 | 152 | ipcMain.on('SENSORS_SCAN', (event, args) => { |
150 | 153 | mainWindow.webContents.send('SENSORS_SCAN', args); |
151 | 154 | }); |
| 155 | + |
| 156 | +ipcMain.handle('OPEN_IMPORT_WINDOW', async (event, dataPath) => { |
| 157 | + return dialog |
| 158 | + .showOpenDialog(null, { |
| 159 | + title: 'Select file(s) to import', |
| 160 | + filters: [{ name: 'Data File', extensions: ['csv'] }], |
| 161 | + properties: ['openFile', 'multiSelections'], |
| 162 | + }) |
| 163 | + .then(result => { |
| 164 | + if (result.filePaths) { |
| 165 | + var message = 'Import successful'; |
| 166 | + result.filePaths.forEach(filePath => { |
| 167 | + const fileName = extractFileName(filePath); |
| 168 | + fs.copyFile(filePath, `${dataPath}/${fileName}`, err => { |
| 169 | + if (err) { |
| 170 | + console.log(err); |
| 171 | + message = 'Import failed'; |
| 172 | + } |
| 173 | + }); |
| 174 | + }); |
| 175 | + } |
| 176 | + return message; |
| 177 | + }) |
| 178 | + .catch(err => { |
| 179 | + console.log(err); |
| 180 | + }); |
| 181 | +}); |
| 182 | + |
| 183 | +ipcMain.handle('OPEN_EXPORT_WINDOW', async (event, filePath) => { |
| 184 | + return dialog |
| 185 | + .showOpenDialog(null, { |
| 186 | + title: 'Select export location', |
| 187 | + properties: ['openDirectory'], |
| 188 | + }) |
| 189 | + .then(result => { |
| 190 | + const dirPath = result.filePaths[0]; |
| 191 | + if (dirPath) { |
| 192 | + var message = 'Export successful'; |
| 193 | + const fileName = extractFileName(filePath); |
| 194 | + fs.copyFile(filePath, `${dirPath}/${fileName}`, err => { |
| 195 | + if (err) { |
| 196 | + console.log(err); |
| 197 | + message = 'Export failed'; |
| 198 | + } |
| 199 | + }); |
| 200 | + return message; |
| 201 | + } |
| 202 | + }) |
| 203 | + .catch(err => { |
| 204 | + console.log(err); |
| 205 | + }); |
| 206 | +}); |
| 207 | + |
| 208 | +ipcMain.handle('MAKE_DIRECTORY', (event, dirPath) => { |
| 209 | + if (!fs.existsSync(dirPath)) { |
| 210 | + fs.mkdirSync(dirPath, { recursive: true }); |
| 211 | + } |
| 212 | +}); |
| 213 | + |
| 214 | +ipcMain.handle('DELETE_FILE', (event, path) => { |
| 215 | + fs.unlink(path, err => { |
| 216 | + if (err) { |
| 217 | + console.log(err); |
| 218 | + } |
| 219 | + }); |
| 220 | +}); |
| 221 | + |
| 222 | +ipcMain.handle('LIST_FILES', (event, dirPath, extension) => { |
| 223 | + const processedFiles = fs |
| 224 | + .readdirSync(dirPath) |
| 225 | + .filter(files => { |
| 226 | + return path.extname(files).toLowerCase() === extension; |
| 227 | + }) |
| 228 | + .map(file => { |
| 229 | + const filepath = path.join(dirPath, file); |
| 230 | + return { |
| 231 | + name: file, |
| 232 | + filepath, |
| 233 | + metaData: getMetaData(filepath), |
| 234 | + }; |
| 235 | + }); |
| 236 | + return processedFiles; |
| 237 | +}); |
| 238 | + |
| 239 | +getMetaData = path => { |
| 240 | + const content = fs.readFileSync(path, 'utf8'); |
| 241 | + const data = content.split(/\r?\n/)[0].split(','); |
| 242 | + return { |
| 243 | + device: data[0], |
| 244 | + date: data[1], |
| 245 | + time: data[2], |
| 246 | + }; |
| 247 | +}; |
0 commit comments