|
| 1 | +// node requirements |
| 2 | +const { |
| 3 | + dialog, app, BrowserWindow, ipcMain, |
| 4 | +} = require('electron'); |
| 5 | +const fs = require('fs'); |
| 6 | +const path = require('path'); |
| 7 | +const connectSQL = require('./model/sql-connect'); |
| 8 | +const connectMongoose = require('./model/mongoose-connect'); |
| 9 | +const CommunicationSchema = require('./model/mongoose-communicatonSchema'); |
| 10 | +const HealthInfoSchema = require('./model/mongoose-healthInfoSchema'); |
| 11 | + |
| 12 | +// declare a variable pool for SQL connection |
| 13 | +let pool; |
| 14 | + |
| 15 | +// declare win variable ---> Ousman |
| 16 | +let win; |
| 17 | + |
| 18 | +// declaring a createWindow function ---> Ousman |
| 19 | +function createWindow() { |
| 20 | + // assign win to an instance of a new browser window. |
| 21 | + win = new BrowserWindow({ |
| 22 | + // giving our window its width |
| 23 | + width: 900, |
| 24 | + // giving our window its hieght |
| 25 | + height: 800, |
| 26 | + // specify the path of the icon -- Which icon is this?.. note too tsure --> Ousman |
| 27 | + icon: path.join(__dirname, 'app/assets/icons/icon.png'), |
| 28 | + // enable node inegreation --> node intgeration, default is usally false --> Ousman |
| 29 | + webPreferences: { |
| 30 | + nodeIntegration: true, |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + // Development |
| 35 | + // loads our application window to localHost 8080, application will not render without this loadUrl --> Ousman |
| 36 | + win.loadURL('http://localhost:8080/'); |
| 37 | + |
| 38 | + // Production |
| 39 | + // win.loadURL(`file://${path.join(__dirname, './dist/index.html')}`); |
| 40 | + |
| 41 | + // assign window to null on close and set splash property in settings.json back to true so splash page renders on restart |
| 42 | + win.on('closed', () => { |
| 43 | + const state = JSON.parse( |
| 44 | + // read json from settings.json |
| 45 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { |
| 46 | + encoding: 'UTF-8', |
| 47 | + }), |
| 48 | + ); |
| 49 | + // reassign state.splash |
| 50 | + state.splash = true; |
| 51 | + fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state), { encoding: 'UTF-8' }); win = null; |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +// invoke createWindow function on Electron application load --> Ousman |
| 56 | +app.on('ready', createWindow); |
| 57 | + |
| 58 | +// quits the application when all windows are closed --> Ousman |
| 59 | +app.on('window-all-closed', () => { |
| 60 | + console.log('window-all-closed message received'); |
| 61 | + const state = JSON.parse( |
| 62 | + // read json from settings.json |
| 63 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { |
| 64 | + encoding: 'UTF-8', |
| 65 | + }), |
| 66 | + ); |
| 67 | + // reassign state.splash |
| 68 | + state.splash = true; |
| 69 | + fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state), { encoding: 'UTF-8' }); |
| 70 | + // process platform is a property that return a string identifying the OS platform on which NodeJs process is running --> Ousman |
| 71 | + if (process.platform !== 'darwin') { |
| 72 | + // quits application |
| 73 | + app.quit(); |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +// event 'activate' emmitted upon application starting |
| 78 | +app.on('activate', () => { |
| 79 | + // if there is no window present invoke the create window function --> Ousman |
| 80 | + if (win === null) { |
| 81 | + createWindow(); |
| 82 | + } |
| 83 | +}); |
| 84 | + |
| 85 | +// Fired by the useEffect hook inside of the Splash.jsx component, this message route will toggle |
| 86 | +// splash property inside of settings.json to false once the Splash page renders itself just once |
| 87 | +ipcMain.on('toggleSplash', (message) => { |
| 88 | + //console.log('toggleSplash message received'); |
| 89 | + const state = JSON.parse( |
| 90 | + // read json from settings.json |
| 91 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { |
| 92 | + encoding: 'UTF-8', |
| 93 | + }), |
| 94 | + ); |
| 95 | + // reassign state.splash to false |
| 96 | + state.splash = false; |
| 97 | + |
| 98 | + // overwrite settings.json with false splash property |
| 99 | + fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state), { encoding: 'UTF-8' }); |
| 100 | + |
| 101 | + message.returnValue = state.splash; |
| 102 | +}); |
| 103 | + |
| 104 | +ipcMain.on('checkSplash', (message) => { |
| 105 | + //sconsole.log('checkSplash message received'); |
| 106 | + const state = JSON.parse( |
| 107 | + // read json from settings.json |
| 108 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { |
| 109 | + encoding: 'UTF-8', |
| 110 | + }), |
| 111 | + ); |
| 112 | + |
| 113 | + message.returnValue = state.splash; |
| 114 | +}); |
| 115 | + |
| 116 | +// Load settings JSON and returns current setup status back to the render process. |
| 117 | +// ipc 'setup' route --> Ousman |
| 118 | +ipcMain.on('setup', (message) => { |
| 119 | + //console.log('setup message received'); |
| 120 | + // assigns state to the returned the object returned from settings.json --> Ousman |
| 121 | + const state = JSON.parse( |
| 122 | + // read json from settings.json |
| 123 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { |
| 124 | + encoding: 'UTF-8', |
| 125 | + }), |
| 126 | + ); |
| 127 | + // destructure setupRequired from state constant ---> Ousman |
| 128 | + const { setupRequired } = state; |
| 129 | + // assigning message object a property of return value and assigning it the setupRequired from state destructuring --> Ousman |
| 130 | + message.returnValue = setupRequired; |
| 131 | +}); |
| 132 | + |
| 133 | +// Loads existing settings JSON and update settings to include new services entered by the user. |
| 134 | +// on ipc 'submit' request --> Ousman |
| 135 | +ipcMain.on('submit', (message, newService) => { |
| 136 | + // Declares a variable state and initialize it to the returned parsed json object from the user/settings.json file |
| 137 | + const state = JSON.parse( |
| 138 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { |
| 139 | + encoding: 'UTF-8', |
| 140 | + }), |
| 141 | + ); |
| 142 | + |
| 143 | + // Checks if setup is required by checking if the value for the state key 'setupRequired' is true |
| 144 | + if (state.setupRequired) { |
| 145 | + // If setup is required, the value for key 'setupRequired' is reassign to false and the value for key 'services' is reassign to an array with newService as its only element |
| 146 | + state.setupRequired = false; |
| 147 | + state.services = [JSON.parse(newService)]; |
| 148 | + } else { |
| 149 | + // Else the newService is pushed into the services array |
| 150 | + state.services.push(JSON.parse(newService)); |
| 151 | + } |
| 152 | + |
| 153 | + // Rewrites user/settings.json to show state |
| 154 | + fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state)); |
| 155 | +}); |
| 156 | + |
| 157 | +// Load settings JSON and returns updated state back to the render process. |
| 158 | +// on ipc 'dashboard' request --> Ousman |
| 159 | +ipcMain.on('dashboard', (message) => { |
| 160 | + // Declares a variable state and initialize it to the returned parse json object from the user/settings.json file |
| 161 | + const state = JSON.parse( |
| 162 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { |
| 163 | + encoding: 'UTF-8', |
| 164 | + }), |
| 165 | + ); |
| 166 | + // destructure services from state... what is services? --> Ousman |
| 167 | + const { services } = state; |
| 168 | + const dashboardList = services.reduce((acc, curVal) => { |
| 169 | + acc.push(curVal[0]); |
| 170 | + return acc; |
| 171 | + }, []); |
| 172 | + message.returnValue = dashboardList; |
| 173 | +}); |
| 174 | + |
| 175 | +// Deletes the service at position 'index' from the services array within the user/setting.json file, |
| 176 | +// resets the user/setting.json file to what it was originally if all of the services are deleted, |
| 177 | +// and sends the remaining services back to onDelete function within DeleteService as a response |
| 178 | +ipcMain.on('deleteService', (message, index) => { |
| 179 | + // Declares a variable state and initialize it to the returned parse json object from the user/settings.json file |
| 180 | + let state = JSON.parse( |
| 181 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { |
| 182 | + encoding: 'UTF-8', |
| 183 | + }), |
| 184 | + ); |
| 185 | + |
| 186 | + // Send a response back with the updated services |
| 187 | + const { splash } = state; |
| 188 | +// Checks if there is more than one services in the services array |
| 189 | + if (state.services.length > 1) { |
| 190 | + // If true, removes the service at position 'index' |
| 191 | + state.services.splice(index, 1); |
| 192 | + } else { |
| 193 | + // Else reassign state to what the user/setting.json file was originally before any database was save |
| 194 | + state = { setupRequired: true, services: ['hard', 'coded', 'in'], splash }; |
| 195 | + } |
| 196 | + |
| 197 | + // Rewrites json from settings.json |
| 198 | + fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state), { encoding: 'UTF-8' }); |
| 199 | + message.sender.send('deleteResponse', state.services); |
| 200 | +}); |
| 201 | + |
| 202 | + |
| 203 | +// Queries the database for communications information and returns it back to the render process. |
| 204 | +ipcMain.on('overviewRequest', (message, index) => { |
| 205 | + console.log('hello from overview request'); |
| 206 | + const { services } = JSON.parse( |
| 207 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { encoding: 'UTF-8' }), |
| 208 | + ); |
| 209 | + |
| 210 | + const databaseType = services[index][1]; |
| 211 | + const URI = services[index][2]; |
| 212 | + |
| 213 | + if (databaseType === 'MongoDB') { |
| 214 | + connectMongoose(index, URI); |
| 215 | + CommunicationSchema.find({}, (err, data) => { |
| 216 | + if (err) { |
| 217 | + console.log(`An error occured while querying the database: ${err}`); |
| 218 | + message.sender.send('overviewResponse', JSON.stringify(err)); |
| 219 | + } |
| 220 | + |
| 221 | + const queryResults = JSON.stringify(data); |
| 222 | + // Asynchronous event emitter used to transmit query results back to the render process. |
| 223 | + message.sender.send('overviewResponse', queryResults); |
| 224 | + }); |
| 225 | + } |
| 226 | + |
| 227 | + if (databaseType === 'SQL') { |
| 228 | + pool = connectSQL(index, URI); |
| 229 | + const getCommunications = 'SELECT * FROM communications'; |
| 230 | + pool.query(getCommunications, (err, result) => { |
| 231 | + if (err) { |
| 232 | + // error object to log to Electron GUI ---> Ousman |
| 233 | + const errorAlert = { |
| 234 | + type: 'error', |
| 235 | + title: 'Error in Main process', |
| 236 | + message: 'Database information could not be retreived. Check that table exists.', |
| 237 | + }; |
| 238 | + |
| 239 | + // after requiring dialog in the topmost section of main. We invoke the method showMessagebox passing the error object we created --> Ousman |
| 240 | + dialog.showMessageBox(errorAlert); |
| 241 | + |
| 242 | + |
| 243 | + message.sender.send(JSON.stringify('Database info could not be retreived.')); |
| 244 | + } else { |
| 245 | + console.log('Connected to SQL Database'); |
| 246 | + const queryResults = JSON.stringify(result.rows); |
| 247 | + // Asynchronous event emitter used to transmit query results back to the render process. |
| 248 | + console.log('ipcMain about to send overviewResponse message'); |
| 249 | + message.sender.send('overviewResponse', queryResults); |
| 250 | + } |
| 251 | + }); |
| 252 | + } |
| 253 | +}); |
| 254 | + |
| 255 | +// Queries the database for computer health information and returns it back to the render process. |
| 256 | +ipcMain.on('detailsRequest', (message, index) => { |
| 257 | + console.log('detailsRequest message received'); |
| 258 | + const databaseType = JSON.parse( |
| 259 | + fs.readFileSync(path.resolve(__dirname, './user/settings.json'), { encoding: 'UTF-8' }), |
| 260 | + ).services[index][1]; |
| 261 | + |
| 262 | + if (databaseType === 'MongoDB') { |
| 263 | + HealthInfoSchema.find({}, (err, data) => { |
| 264 | + if (err) { |
| 265 | + message.sender.send('detailsResponse', JSON.stringify(err)); |
| 266 | + } |
| 267 | + const queryResults = JSON.stringify(data); |
| 268 | + // Asynchronous event emitter used to transmit query results back to the render process. |
| 269 | + message.sender.send('detailsResponse', queryResults); |
| 270 | + }); |
| 271 | + } |
| 272 | + |
| 273 | + if (databaseType === 'SQL') { |
| 274 | + const getHealth = 'SELECT * FROM healthInfo'; |
| 275 | + pool.query(getHealth, (err, result) => { |
| 276 | + if (err) { |
| 277 | + message.sender.send('detailsResponse', JSON.stringify('Database info could not be retreived.')); |
| 278 | + } |
| 279 | + const queryResults = JSON.stringify(result.rows); |
| 280 | + // Asynchronous event emitter used to transmit query results back to the render process. |
| 281 | + // console.log('healthInfo data about to comeback'); |
| 282 | + message.sender.send('detailsResponse', queryResults); |
| 283 | + }); |
| 284 | + } |
| 285 | +}); |
| 286 | + |
0 commit comments