forked from electron/electron-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
229 lines (207 loc) · 5.67 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
'use strict';
const unity = require('./unity.js');
const spawn = require('child_process').spawn;
const xml2js = require('xml2js');
const electron = require('electron');
// Module to control application life.
const app = electron.app;
const globalShortcut = electron.globalShortcut;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
const Menu = electron.Menu;
const Tray = electron.Tray;
let mainWindow, appIcon;
function createWindow () {
mainWindow = new BrowserWindow({ width: 800, height: 100, frame: false });
mainWindow.loadURL('file://' + __dirname + '/index.html');
mainWindow.on('closed', function() {
mainWindow = null;
});
mainWindow.on('focus', function(e) {
console.log('focused!');
e.sender.webContents.send('focused', 'true');
});
mainWindow.on('blur', function(e) {
e.sender.hide();
});
}
let _patientID;
function setPatient(patient) {
console.log('Patient', patient);
mainWindow.webContents.send('patientUpdated', patient.ID ? patient: null);
_patientID = patient.ID[0];
}
function setEncounter(encounter) {
console.log('Encounter', encounter);
mainWindow.webContents.send('encounterUpdated', encounter.ID ? encounter: null);
}
function setHIE(hie) {
console.log('HIE', hie);
mainWindow.webContents.send('hieUpdated', hie.userID ? hie: null);
}
function uaiData(data) {
data = data.toString(); // Buffer -> string //
// FIXME: split on |, really want more robust XML parsing //
data.split('|').forEach(s => {
s = s.trim();
if(s.length == 0) return;
if(s.indexOf('<') == 0) {
xml2js.parseString(s, (err, result) => {
if(result.PatientContext) {
setPatient(result.PatientContext);
} else if(result.EncounterContext) {
setEncounter(result.EncounterContext);
} else if(result.HIEContext) {
setHIE(result.HIEContext);
} else {
console.error('invalid XML object', result);
}
});
} else {
// not an XML message //
console.log('UAI log: ' + s);
}
});
}
function uaiError(data) {
console.error('UAI error: ' + data);
}
let uai;
function startUAIListener() {
uai = spawn(__dirname + '/AllscriptsUAIUtility/AllscriptsUAIUtility.exe');
uai.stdout.on('data', uaiData);
uai.stderr.on('data', uaiError);
// connect to UAI server //
uai.stdin.write('connect\n');
}
function startUnity() {
unity.getToken((err, token) => {
if(err) {
console.error('Could not get unity token: ' + err);
return;
} else {
console.log('Unity token: ' + token);
}
/*unity.getUserDetails((err, body) => {
if(err) console.error('error: ' + err);
console.log('user details: ' + body);
});*/
unity.authEHR((err) => {
console.log('Unity Ready');
unity.getServerInfo((err, body) => {
console.log('unity server: ' + body);
});
});
});
}
app.on('ready', function() {
createWindow();
//mainWindow.webContents.openDevTools();
startUnity();
var ret = globalShortcut.register('ctrl+shift+h', function() {
console.log('ctrl+shift+h is pressed');
if(mainWindow) {
mainWindow.show();
}
});
if(!ret) {
console.log('registration failed');
}
appIcon = new Tray(__dirname + '/dr_weird.jpg');
appIcon.setToolTip('NeauxClicks');
appIcon.on('click', function(e) {
mainWindow.show();
});
appIcon.on('double-click', function(e) {
mainWindow.show();
});
var contextMenu = Menu.buildFromTemplate([
{ label: 'Quit', type: 'normal', click: function() {
mainWindow.close();
}
}
]);
appIcon.setContextMenu(contextMenu);
startUAIListener();
});
app.on('window-all-closed', function () {
console.log('all windows closed');
app.quit();
});
app.on('will-quit', function(event) {
// Unregister all shortcuts.
globalShortcut.unregisterAll();
// shut down UAI to make sure we disconnect //
// then continue quitting the application //
uai.on('close', (code) => {
console.log('quitting');
app.exit(0);
});
console.log('shutting down UAI');
uai.stdin.write('quit\n');
event.preventDefault();
});
electron.ipcMain.on('boxCommand', function(event, cmd) {
console.log('command: ' + (cmd ? cmd: '<blank>'));
switch(cmd.toLowerCase()) {
case 'quit':
case 'exit':
mainWindow.close();
return;
case '':
mainWindow.hide();
return;
}
if(cmd.indexOf('!') == 0) {
uai.stdin.write(cmd.substring(1) + '\n');
return;
}
cmd = cmd.toLowerCase();
const bpRexp = /(bp) (\d+)[\/\\](\d+)/;
let res;
if((res = bpRexp.exec(cmd)) != null) {
console.dir(res);
let data = { type: res[1], value: res[2], value2: res[3] };
unity.saveVital(_patientID, data, (err, body) => {
if(err) {
console.error('error', err, body);
return;
}
console.log(body);
});
return;
}
const wtRexp = /(wt|weight) (\d+(?:.\d+)?) ?(?:(lb|kg|pound|kilogram)s?)?/;
if((res = wtRexp.exec(cmd)) != null) {
console.dir(res);
let data = { type: 'wt', value: res[2] };
if(res[3]) {
switch(res[3]) {
case 'lb':
case 'pound':
data.units = 'us';
}
}
//console.log(data);
unity.saveVital(_patientID, data, (err, body) => {
if(err) {
console.error('error', err, body);
return;
}
console.log(body);
});
return;
}
const vitRexp = /(o2|pulse|resp|pain) (\d+)/;
if((res = vitRexp.exec(cmd)) != null) {
let data = { type: res[1], value: res[2] };
unity.saveVital(_patientID, data, (err, body) => {
if(err) {
console.error('error', err, body);
return;
}
console.log(body);
});
return;
}
});