|
| 1 | +const { app, BrowserWindow, ipcMain } = require('electron'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const connectSQL = require('./model/sql-connect'); |
| 5 | +const connectMongoose = require('./model/mongoose-connect'); |
| 6 | +const CommunicationSchema = require('./model/mongoose-communicatonSchema'); |
| 7 | +const HealthInfoSchema = require('./model/mongoose-healthInfoSchema'); |
| 8 | + |
| 9 | +let win; |
| 10 | +function createWindow() { |
| 11 | + win = new BrowserWindow({ |
| 12 | + width: 900, |
| 13 | + height: 800, |
| 14 | + icon: path.join(__dirname, 'app/assets/icons/icon.png'), |
| 15 | + webPreferences: { |
| 16 | + nodeIntegration: true, |
| 17 | + }, |
| 18 | + }); |
| 19 | + |
| 20 | + // Development |
| 21 | + win.loadURL('http://localhost:8080/'); |
| 22 | + |
| 23 | + // Production |
| 24 | + // win.loadURL(`file://${path.join(__dirname, './dist/index.html')}`); |
| 25 | + |
| 26 | + win.on('closed', () => { |
| 27 | + win = null; |
| 28 | + }); |
| 29 | +} |
| 30 | +app.on('ready', createWindow); |
| 31 | + |
| 32 | +app.on('window-all-closed', () => { |
| 33 | + if (process.platform !== 'darwin') { |
| 34 | + app.quit(); |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +app.on('activate', () => { |
| 39 | + if (win === null) { |
| 40 | + createWindow(); |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +// Load settings JSON and returns current setup status back to the render process. |
| 45 | +ipcMain.on('setup', (message) => { |
| 46 | + const state = JSON.parse( |
| 47 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { |
| 48 | + encoding: 'UTF-8', |
| 49 | + }), |
| 50 | + ); |
| 51 | + const { setupRequired } = state; |
| 52 | + message.returnValue = setupRequired; |
| 53 | +}); |
| 54 | + |
| 55 | +// Loads existing settings JSON and update settings to include new services entered by the user. |
| 56 | +ipcMain.on('submit', (message, newService) => { |
| 57 | + const state = JSON.parse( |
| 58 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { |
| 59 | + encoding: 'UTF-8', |
| 60 | + }), |
| 61 | + ); |
| 62 | + // if statement is used to remove hard coded data. |
| 63 | + if (state.michelleWasHere) { |
| 64 | + state.setupRequired = false; |
| 65 | + state.michelleWasHere = false; |
| 66 | + state.services = [JSON.parse(newService)]; |
| 67 | + fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state)); |
| 68 | + } else { |
| 69 | + state.setupRequired = false; |
| 70 | + state.services.push(JSON.parse(newService)); |
| 71 | + fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state)); |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +// Load settings JSON and returns updated state back to the render process. |
| 76 | +ipcMain.on('dashboard', (message) => { |
| 77 | + const state = JSON.parse( |
| 78 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { |
| 79 | + encoding: 'UTF-8', |
| 80 | + }), |
| 81 | + ); |
| 82 | + const { services } = state; |
| 83 | + const dashboardList = services.reduce((acc, curVal) => { |
| 84 | + acc.push(curVal[0]); |
| 85 | + return acc; |
| 86 | + }, []); |
| 87 | + message.returnValue = dashboardList; |
| 88 | +}); |
| 89 | + |
| 90 | +// Queries the database for communications information and returns it back to the render process. |
| 91 | +ipcMain.on('overviewRequest', (message, index) => { |
| 92 | + const databaseType = JSON.parse( |
| 93 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { encoding: 'UTF-8' }), |
| 94 | + ).services[index][1]; |
| 95 | + |
| 96 | + if (databaseType === 'MongoDB') { |
| 97 | + connectMongoose(index); |
| 98 | + CommunicationSchema.find({}, (err, data) => { |
| 99 | + if (err) { |
| 100 | + console.log(`An error occured while querying the database: ${err}`); |
| 101 | + message.sender.send('overviewResponse', JSON.stringify(err)); |
| 102 | + } |
| 103 | + const queryResults = JSON.stringify(data); |
| 104 | + // Asynchronous event emitter used to transmit query results back to the render process. |
| 105 | + message.sender.send('overviewResponse', queryResults); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + if (databaseType === 'SQL') { |
| 110 | + const pool = connectSQL(index); |
| 111 | + const getCommunications = 'SELECT * FROM communications'; |
| 112 | + pool.query(getCommunications, (err, result) => { |
| 113 | + if (err) { |
| 114 | + console.log(err); |
| 115 | + message.sender.send(JSON.stringify('Database info could not be retreived.')); |
| 116 | + } |
| 117 | + const queryResults = JSON.stringify(result.rows); |
| 118 | + // Asynchronous event emitter used to transmit query results back to the render process. |
| 119 | + message.sender.send('overviewResponse', queryResults); |
| 120 | + }); |
| 121 | + } |
| 122 | +}); |
| 123 | + |
| 124 | +// Queries the database for computer health information and returns it back to the render process. |
| 125 | +ipcMain.on('detailsRequest', (message, index) => { |
| 126 | + const databaseType = JSON.parse( |
| 127 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { encoding: 'UTF-8' }), |
| 128 | + ).services[index][1]; |
| 129 | + |
| 130 | + if (databaseType === 'MongoDB') { |
| 131 | + connectMongoose(index); |
| 132 | + HealthInfoSchema.find({}, (err, data) => { |
| 133 | + if (err) { |
| 134 | + message.sender.send('detailsResponse', JSON.stringify(err)); |
| 135 | + } |
| 136 | + const queryResults = JSON.stringify(data); |
| 137 | + console.log('QUERY RESULTS =>', queryResults); |
| 138 | + // Asynchronous event emitter used to transmit query results back to the render process. |
| 139 | + message.sender.send('detailsResponse', queryResults); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + if (databaseType === 'SQL') { |
| 144 | + const pool = connectSQL(index); |
| 145 | + const getHealth = 'SELECT * FROM healthInfo'; |
| 146 | + pool.query(getHealth, (err, result) => { |
| 147 | + if (err) { |
| 148 | + message.sender.send('detailsResponse', JSON.stringify('Database info could not be retreived.')); |
| 149 | + } |
| 150 | + const queryResults = JSON.stringify(result.rows); |
| 151 | + // Asynchronous event emitter used to transmit query results back to the render process. |
| 152 | + message.sender.send('detailsResponse', queryResults); |
| 153 | + }); |
| 154 | + } |
| 155 | +}); |
0 commit comments