1
- const { app, BrowserWindow, ipcMain } = require ( 'electron' ) ;
1
+ // node requirements
2
+ const {
3
+ dialog, app, BrowserWindow, ipcMain,
4
+ } = require ( 'electron' ) ;
2
5
const fs = require ( 'fs' ) ;
3
6
const path = require ( 'path' ) ;
4
7
const connectSQL = require ( './model/sql-connect' ) ;
5
8
const connectMongoose = require ( './model/mongoose-connect' ) ;
6
9
const CommunicationSchema = require ( './model/mongoose-communicatonSchema' ) ;
7
10
const HealthInfoSchema = require ( './model/mongoose-healthInfoSchema' ) ;
8
11
12
+ // declare a variable pool for SQL connection
13
+ let pool ;
14
+
15
+ // declare win variable ---> Ousman
9
16
let win ;
17
+
18
+ // declaring a createWindow function ---> Ousman
10
19
function createWindow ( ) {
20
+ // assign win to an instance of a new browser window.
11
21
win = new BrowserWindow ( {
22
+ // giving our window its width
12
23
width : 900 ,
24
+ // giving our window its hieght
13
25
height : 800 ,
26
+ // specify the path of the icon -- Which icon is this?.. note too tsure --> Ousman
14
27
icon : path . join ( __dirname , 'app/assets/icons/icon.png' ) ,
28
+ // enable node inegreation --> node intgeration, default is usally false --> Ousman
15
29
webPreferences : {
16
30
nodeIntegration : true ,
17
31
} ,
18
32
} ) ;
19
33
20
34
// Development
35
+ // loads our application window to localHost 8080, application will not render without this loadUrl --> Ousman
21
36
win . loadURL ( 'http://localhost:8080/' ) ;
22
37
23
38
// Production
24
39
// win.loadURL(`file://${path.join(__dirname, './dist/index.html')}`);
25
40
41
+ // assign window to null on close and set splash property in settings.json back to true so splash page renders on restart
26
42
win . on ( 'closed' , ( ) => {
27
- win = null ;
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 ;
28
52
} ) ;
29
53
}
54
+
55
+ // invoke createWindow function on Electron application load --> Ousman
30
56
app . on ( 'ready' , createWindow ) ;
31
57
58
+ // quits the application when all windows are closed --> Ousman
32
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
33
71
if ( process . platform !== 'darwin' ) {
72
+ // quits application
34
73
app . quit ( ) ;
35
74
}
36
75
} ) ;
37
76
77
+ // event 'activate' emmitted upon application starting
38
78
app . on ( 'activate' , ( ) => {
79
+ // if there is no window present invoke the create window function --> Ousman
39
80
if ( win === null ) {
40
81
createWindow ( ) ;
41
82
}
42
83
} ) ;
43
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
+
44
116
// Load settings JSON and returns current setup status back to the render process.
117
+ // ipc 'setup' route --> Ousman
45
118
ipcMain . on ( 'setup' , ( message ) => {
119
+ //console.log('setup message received');
120
+ // assigns state to the returned the object returned from settings.json --> Ousman
46
121
const state = JSON . parse (
122
+ // read json from settings.json
47
123
fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
48
124
encoding : 'UTF-8' ,
49
125
} ) ,
50
126
) ;
127
+ // destructure setupRequired from state constant ---> Ousman
51
128
const { setupRequired } = state ;
129
+ // assigning message object a property of return value and assigning it the setupRequired from state destructuring --> Ousman
52
130
message . returnValue = setupRequired ;
53
131
} ) ;
54
132
55
133
// Loads existing settings JSON and update settings to include new services entered by the user.
134
+ // on ipc 'submit' request --> Ousman
56
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
57
137
const state = JSON . parse (
58
138
fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
59
139
encoding : 'UTF-8' ,
60
140
} ) ,
61
141
) ;
62
- // if statement is used to remove hard coded data.
63
- if ( state . michelleWasHere ) {
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
64
146
state . setupRequired = false ;
65
- state . michelleWasHere = false ;
66
147
state . services = [ JSON . parse ( newService ) ] ;
67
- fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) ) ;
68
148
} else {
69
- state . setupRequired = false ;
149
+ // Else the newService is pushed into the services array
70
150
state . services . push ( JSON . parse ( newService ) ) ;
71
- fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) ) ;
72
- }
151
+ }
152
+
153
+ // Rewrites user/settings.json to show state
154
+ fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) ) ;
73
155
} ) ;
74
156
75
157
// Load settings JSON and returns updated state back to the render process.
158
+ // on ipc 'dashboard' request --> Ousman
76
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
77
161
const state = JSON . parse (
78
162
fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
79
163
encoding : 'UTF-8' ,
80
164
} ) ,
81
165
) ;
166
+ // destructure services from state... what is services? --> Ousman
82
167
const { services } = state ;
83
168
const dashboardList = services . reduce ( ( acc , curVal ) => {
84
169
acc . push ( curVal [ 0 ] ) ;
@@ -87,69 +172,115 @@ ipcMain.on('dashboard', (message) => {
87
172
message . returnValue = dashboardList ;
88
173
} ) ;
89
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
+
90
203
// Queries the database for communications information and returns it back to the render process.
91
204
ipcMain . on ( 'overviewRequest' , ( message , index ) => {
92
- const databaseType = JSON . parse (
205
+ console . log ( 'hello from overview request' ) ;
206
+ const { services } = JSON . parse (
93
207
fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , { encoding : 'UTF-8' } ) ,
94
- ) . services [ index ] [ 1 ] ;
208
+ ) ;
209
+
210
+ const databaseType = services [ index ] [ 1 ] ;
211
+ const URI = services [ index ] [ 2 ] ;
95
212
96
213
if ( databaseType === 'MongoDB' ) {
97
- connectMongoose ( index ) ;
214
+ connectMongoose ( index , URI ) ;
98
215
CommunicationSchema . find ( { } , ( err , data ) => {
99
216
if ( err ) {
100
217
console . log ( `An error occured while querying the database: ${ err } ` ) ;
101
218
message . sender . send ( 'overviewResponse' , JSON . stringify ( err ) ) ;
102
219
}
220
+
103
221
const queryResults = JSON . stringify ( data ) ;
104
222
// Asynchronous event emitter used to transmit query results back to the render process.
105
223
message . sender . send ( 'overviewResponse' , queryResults ) ;
106
224
} ) ;
107
225
}
108
226
109
227
if ( databaseType === 'SQL' ) {
110
- const pool = connectSQL ( index ) ;
228
+ pool = connectSQL ( index , URI ) ;
111
229
const getCommunications = 'SELECT * FROM communications' ;
112
230
pool . query ( getCommunications , ( err , result ) => {
113
231
if ( err ) {
114
- console . log ( 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
+
115
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 ) ;
116
250
}
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
251
} ) ;
121
252
}
122
253
} ) ;
123
254
124
255
// Queries the database for computer health information and returns it back to the render process.
125
256
ipcMain . on ( 'detailsRequest' , ( message , index ) => {
257
+ console . log ( 'detailsRequest message received' ) ;
126
258
const databaseType = JSON . parse (
127
259
fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , { encoding : 'UTF-8' } ) ,
128
260
) . services [ index ] [ 1 ] ;
129
261
130
262
if ( databaseType === 'MongoDB' ) {
131
- connectMongoose ( index ) ;
132
263
HealthInfoSchema . find ( { } , ( err , data ) => {
133
264
if ( err ) {
134
265
message . sender . send ( 'detailsResponse' , JSON . stringify ( err ) ) ;
135
266
}
136
267
const queryResults = JSON . stringify ( data ) ;
137
- console . log ( 'QUERY RESULTS =>' , queryResults ) ;
138
268
// Asynchronous event emitter used to transmit query results back to the render process.
139
269
message . sender . send ( 'detailsResponse' , queryResults ) ;
140
270
} ) ;
141
271
}
142
272
143
273
if ( databaseType === 'SQL' ) {
144
- const pool = connectSQL ( index ) ;
145
274
const getHealth = 'SELECT * FROM healthInfo' ;
146
275
pool . query ( getHealth , ( err , result ) => {
147
276
if ( err ) {
148
277
message . sender . send ( 'detailsResponse' , JSON . stringify ( 'Database info could not be retreived.' ) ) ;
149
278
}
150
279
const queryResults = JSON . stringify ( result . rows ) ;
151
280
// Asynchronous event emitter used to transmit query results back to the render process.
281
+ // console.log('healthInfo data about to comeback');
152
282
message . sender . send ( 'detailsResponse' , queryResults ) ;
153
283
} ) ;
154
284
}
155
285
} ) ;
286
+
0 commit comments