-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
291 lines (253 loc) · 6.61 KB
/
app.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
//require Statements
var request = require('request-promise');
const url = require('url');
const path = require('path');
const {spawn} = require('child_process');
const { app, BrowserWindow, Menu, ipcMain, Notification} = require(
'electron');
var lastAngry = 0
let MainWindow;
let SettingsWindow;
var recording = false;
// Listen for the app to ready:
app.on('ready', function () {
MainWindow = new BrowserWindow({
webPreferences: {nodeIntegration: true, contextIsolation: false, enableRemoteModule: true} });
MainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'HTML Templates/Home.html'),
protocol: 'file:',
slashes: true
}));
MainWindow.removeMenu()
MainWindow.on('closed', function(){
app.quit();
})
MainWindow.on('close', function(){
killServer()
})
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
Menu.setApplicationMenu(mainMenu);
const childPython = spawn('python3', ['backend/server.py']);
childPython.stdout.on('data', (data) => {
console.log(`stdout: ${data}`)
})
childPython.stderr.on('error', (error) => {
console.log(`${error}`)
} )
childPython.on("close", (code) => {
console.log(`exited on code: ${code}`)
})
console.log("hi"
)
startLoop()
console.log("hi")
});
//Start Loop For Recording
function startLoop(){
setTimeout(() => {
if(recording){
const Data = record();
console.log("recording")
isAngry()
}
startLoop();
}, 500)
}
//Send the Angry Notification
function sendAngryNotification(){
lastAngry = 15
new Notification({title: "Emonitor", body:'Woah! Calm down and try to smile!'}).show()
}
// Create Main Menu Template
const mainMenuTemplate = [
{
label: 'File',
submenu: [
{
label: 'Quit',
accelerator: process.platform == 'darwin' ? 'Command+Q' : 'Ctrl+Q',
click() {
app.quit();
}
}
]
}
];
//check how angry user is
async function isAngry() {
var data = {
}
var options = {
method: 'POST',
timeout: 180000,
uri: 'http://127.0.0.1:5000/isangry',
json: true
};
var sendrequest = await request(options)
.then(function (parsedBody) {
let result;
result = parsedBody['isangry'];
if (result && lastAngry <= 0) {
console.log("angry")
sendAngryNotification()
}
else{
lastAngry = lastAngry - 1
}
return result;
})
.catch(function (err) {
console.log(err);
return 0;
});
}
//record an image
async function record() {
// console.log('recording');
var data = {
"photoSize": 1
}
var options = {
method: 'POST',
timeout: 180000,
uri: 'http://127.0.0.1:5000/takeimage',
body: data,
json: true
};
var sendrequest = await request(options)
.then(function (parsedBody) {
let result;
console.log(parsedBody);
result = parsedBody['emotions'];
console.log(result);
MainWindow.webContents.send("data:update", result[5], result[2], result[7], result[6], result[4], result[3]);
console.log(result)
return parsedBody['emotions'];
})
.catch(function (err) {
console.log(err);
return 0;
});
}
//get summary data
async function summarize(emotion1, emotion2, emotion3, comparing){
var data = {
"emotion1": emotion1,
"emotion2": emotion2,
"emotion3": emotion3,
"comparing": comparing
}
var options = {
method: 'POST',
uri: 'http://127.0.0.1:5000/summarize',
body: data,
json: true
};
var sendrequest = await request(options)
.then(function (parsedBody) {
let result;
appEmo = parsedBody['result'];
console.log(appEmo)
var keys = Object.keys(appEmo);
var values = Object.values(appEmo);
var topSixKeys = keys.slice(Math.max(-5, -keys.length))
var topSixValues = values.slice(Math.max(-5, -keys.length))
MainWindow.webContents.send("emotion:summary", topSixKeys, topSixValues)
return result;
})
.catch(function (err) {
console.log(err);
return 0;
});
}
//kill Server
async function killServer() {
var options = {
method: 'POST',
uri: 'http://127.0.0.1:5000/kill',
json: true
};
var sendrequest = await request(options)
.then(function (parsedBody) {
let result;
})
.catch(function (err) {
console.log(err);
});
}
//fucntion to end session
async function endSession(){
var options = {
method: 'POST',
uri: 'http://127.0.0.1:5000/endsession',
json: true
};
var sendrequest = await request(options)
.then(function (parsedBody) {
let result;
})
.catch(function (err) {
console.log(err);
});
}
//function to create settings window
function createSettingsWindow(){
SettingsWindow = new BrowserWindow({
width: 1200,
height: 720,
title: 'Settings',
resizable: false,
fullscreen: false,
webPreferences: {nodeIntegration: true, contextIsolation:false}
});
SettingsWindow.loadURL(url.format({
pathname: path.join(__dirname, 'HTML Templates/Settings.html'),
protocol: 'file:',
slashes: true
}));
SettingsWindow.on('close', function(){
SettingsWindow=null
});
}
//listener to open settings
ipcMain.on("open:settings", function(e){
if(SettingsWindow==null){
createSettingsWindow();
}
else{
SettingsWindow.show();
}
})
//listener to change recording status
ipcMain.on("recorder:change", function(e){
recording=!recording;
if (!recording){
endSession();
}
});
//listener to start summary maker
ipcMain.on("summary:emotions", function(e, emotion1, emotion2, emotion3, comparing){
var appEmo = summarize(emotion1, emotion2, emotion3, comparing)
})
function createHelpWindow(){
SettingsWindow = new BrowserWindow({
width: 1200,
height: 820,
title: 'Find a Therapist near you!',
resizable: false,
fullscreen: false,
webPreferences: {nodeIntegration: true, contextIsolation:false}
});
SettingsWindow.loadURL("https://www.google.com/maps/search/Therapists+Near+Me/@37.6,-95.7528906");
SettingsWindow.on('close', function(){
SettingsWindow=null
});
}
ipcMain.on("open:TPHELP", function(e){
if(SettingsWindow==null){
createHelpWindow();
}
else{
SettingsWindow.show();
}
})