-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathMain.js
312 lines (280 loc) · 11 KB
/
Main.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// node requirements
const { dialog, app, BrowserWindow, ipcMain } = require('electron');
const fs = require('fs');
const path = require('path');
const connectSQL = require('./model/sql-connect');
const connectMongoose = require('./model/mongoose-connect');
const CommunicationSchema = require('./model/mongoose-communicatonSchema');
const HealthInfoSchema = require('./model/mongoose-healthInfoSchema');
// declare a variable pool for SQL connection
let pool;
// declare win variable ---> Ousman
let win;
// declaring a createWindow function ---> Ousman
function createWindow() {
// assign win to an instance of a new browser window.
win = new BrowserWindow({
// giving our window its width
width: 1920,
// giving our window its hieght
height: 1080,
// specify the path of the icon -- Which icon is this?.. note too tsure --> Ousman
icon: path.join(__dirname, 'app/assets/icons/icon.png'),
// enable node inegreation --> node intgeration, default is usally false --> Ousman
webPreferences: {
nodeIntegration: true,
},
});
// Development
// loads our application window to localHost 8080, application will not render without this loadUrl --> Ousman
win.loadURL('http://localhost:8080/');
// Production
// win.loadURL(`file://${path.join(__dirname, './dist/index.html')}`);
// assign window to null on close and set splash property in settings.json back to true so splash page renders on restart
win.on('closed', () => {
const state = JSON.parse(
// read json from settings.json
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
encoding: 'UTF-8',
})
);
// reassign state.splash
state.splash = true;
fs.writeFileSync(
path.resolve(__dirname, './user/settings.json'),
JSON.stringify(state),
{ encoding: 'UTF-8' }
);
win = null;
});
}
// invoke createWindow function on Electron application load --> Ousman
app.on('ready', createWindow);
// quits the application when all windows are closed --> Ousman
app.on('window-all-closed', () => {
console.log('window-all-closed message received');
const state = JSON.parse(
// read json from settings.json
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
encoding: 'UTF-8',
})
);
// reassign state.splash
state.splash = true;
fs.writeFileSync(
path.resolve(__dirname, './user/settings.json'),
JSON.stringify(state),
{ encoding: 'UTF-8' }
);
// process platform is a property that return a string identifying the OS platform on which NodeJs process is running --> Ousman
if (process.platform !== 'darwin') {
// quits application
app.quit();
}
});
// event 'activate' emmitted upon application starting
app.on('activate', () => {
// if there is no window present invoke the create window function --> Ousman
if (win === null) {
createWindow();
}
});
// Fired by the useEffect hook inside of the Splash.jsx component, this message route will toggle
// splash property inside of settings.json to false once the Splash page renders itself just once
ipcMain.on('toggleSplash', (message) => {
// console.log('toggleSplash message received');
const state = JSON.parse(
// read json from settings.json
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
encoding: 'UTF-8',
})
);
// reassign state.splash to false
state.splash = false;
// overwrite settings.json with false splash property
fs.writeFileSync(
path.resolve(__dirname, './user/settings.json'),
JSON.stringify(state),
{ encoding: 'UTF-8' }
);
message.returnValue = state.splash;
});
ipcMain.on('checkSplash', (message) => {
// sconsole.log('checkSplash message received');
const state = JSON.parse(
// read json from settings.json
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
encoding: 'UTF-8',
})
);
message.returnValue = state.splash;
});
// Load settings JSON and returns current setup status back to the render process.
// ipc 'setup' route --> Ousman
ipcMain.on('setup', (message) => {
// console.log('setup message received');
// assigns state to the returned the object returned from settings.json --> Ousman
const state = JSON.parse(
// read json from settings.json
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
encoding: 'UTF-8',
})
);
// destructure setupRequired from state constant ---> Ousman
const { setupRequired } = state;
// assigning message object a property of return value and assigning it the setupRequired from state destructuring --> Ousman
message.returnValue = setupRequired;
});
// Loads existing settings JSON and update settings to include new services entered by the user.
// on ipc 'submit' request --> Ousman
ipcMain.on('submit', (message, newService) => {
// Declares a variable state and initialize it to the returned parsed json object from the user/settings.json file
const state = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
encoding: 'UTF-8',
})
);
// Checks if setup is required by checking if the value for the state key 'setupRequired' is true
if (state.setupRequired) {
// 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
state.setupRequired = false;
state.services = [JSON.parse(newService)];
} else {
// Else the newService is pushed into the services array
state.services.push(JSON.parse(newService));
}
// Rewrites user/settings.json to show state
fs.writeFileSync(
path.resolve(__dirname, './user/settings.json'),
JSON.stringify(state)
);
});
// Load settings JSON and returns updated state back to the render process.
// on ipc 'dashboard' request --> Ousman
ipcMain.on('dashboard', (message) => {
// Declares a variable state and initialize it to the returned parse json object from the user/settings.json file
const state = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
encoding: 'UTF-8',
})
);
// destructure services from state... what is services? --> Ousman
const { services } = state;
const dashboardList = services.reduce((acc, curVal) => {
acc.push(curVal[0]);
return acc;
}, []);
message.returnValue = dashboardList;
});
// Deletes the service at position 'index' from the services array within the user/setting.json file,
// resets the user/setting.json file to what it was originally if all of the services are deleted,
// and sends the remaining services back to onDelete function within DeleteService as a response
ipcMain.on('deleteService', (message, index) => {
// Declares a variable state and initialize it to the returned parse json object from the user/settings.json file
let state = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
encoding: 'UTF-8',
})
);
// Send a response back with the updated services
const { splash } = state;
// Checks if there is more than one services in the services array
if (state.services.length > 1) {
// If true, removes the service at position 'index'
state.services.splice(index, 1);
} else {
// Else reassign state to what the user/setting.json file was originally before any database was save
state = { setupRequired: true, services: ['hard', 'coded', 'in'], splash };
}
// Rewrites json from settings.json
fs.writeFileSync(
path.resolve(__dirname, './user/settings.json'),
JSON.stringify(state),
{ encoding: 'UTF-8' }
);
message.sender.send('deleteResponse', state.services);
});
// Queries the database for communications information and returns it back to the render process.
ipcMain.on('overviewRequest', (message, index) => {
console.log('hello from overview request');
const { services } = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
encoding: 'UTF-8',
})
);
const databaseType = services[index][1];
const URI = services[index][2];
if (databaseType === 'MongoDB') {
connectMongoose(index, URI);
CommunicationSchema.find({}, (err, data) => {
if (err) {
console.log(`An error occured while querying the database: ${err}`);
message.sender.send('overviewResponse', JSON.stringify(err));
}
const queryResults = JSON.stringify(data);
// Asynchronous event emitter used to transmit query results back to the render process.
message.sender.send('overviewResponse', queryResults);
});
}
if (databaseType === 'SQL') {
pool = connectSQL(index, URI);
const getCommunications = 'SELECT * FROM communications';
pool.query(getCommunications, (err, result) => {
if (err) {
// error object to log to Electron GUI ---> Ousman
const errorAlert = {
type: 'error',
title: 'Error in Main process',
message:
'Database information could not be retreived. Check that table exists.',
};
// after requiring dialog in the topmost section of main. We invoke the method showMessagebox passing the error object we created --> Ousman
dialog.showMessageBox(errorAlert);
message.sender.send(
JSON.stringify('Database info could not be retreived.')
);
} else {
console.log('Connected to SQL Database');
const queryResults = JSON.stringify(result.rows);
// Asynchronous event emitter used to transmit query results back to the render process.
console.log('ipcMain about to send overviewResponse message');
message.sender.send('overviewResponse', queryResults);
}
});
}
});
// Queries the database for computer health information and returns it back to the render process.
ipcMain.on('detailsRequest', (message, index) => {
console.log('detailsRequest message received');
const databaseType = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
encoding: 'UTF-8',
})
).services[index][1];
if (databaseType === 'MongoDB') {
HealthInfoSchema.find({}, (err, data) => {
if (err) {
message.sender.send('detailsResponse', JSON.stringify(err));
}
const queryResults = JSON.stringify(data);
// Asynchronous event emitter used to transmit query results back to the render process.
message.sender.send('detailsResponse', queryResults);
console.log('Message Sent');
});
}
if (databaseType === 'SQL') {
const getHealth = 'SELECT * FROM healthInfo';
pool.query(getHealth, (err, result) => {
if (err) {
message.sender.send(
'detailsResponse',
JSON.stringify('Database info could not be retreived.')
);
}
const queryResults = JSON.stringify(result.rows);
// Asynchronous event emitter used to transmit query results back to the render process.
// console.log('healthInfo data about to comeback');
message.sender.send('detailsResponse', queryResults);
});
}
});