forked from evancohen/smart-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
111 lines (95 loc) · 3.55 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
/* global __dirname */
/* global process */
const electron = require('electron')
// Child Process for keyword spotter
const {spawn} = require('child_process')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
// Prevent the monitor from going to sleep.
const powerSaveBlocker = electron.powerSaveBlocker
powerSaveBlocker.start('prevent-display-sleep')
// Load the smart mirror config
var config;
try{
config = require(__dirname + "/config.js");
} catch (e) {
var error = "Unknown Error"
if (typeof e.code != 'undefined' && e.code == 'MODULE_NOT_FOUND') {
error = "'config.js' not found. \nPlease ensure that you have created 'config.js' " +
"in the root of your smart-mirror directory."
} else if (typeof e.message != 'undefined') {
error = "Syntax Error. \nLooks like there's an error in your config file: " + e.message
}
console.log("Config Error: ", error)
app.quit()
}
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Get the displays and render the mirror on a secondary screen if it exists
var atomScreen = electron.screen;
var displays = atomScreen.getAllDisplays();
var externalDisplay = null;
for (var i in displays) {
if (displays[i].bounds.x > 0 || displays[i].bounds.y > 0) {
externalDisplay = displays[i];
break;
}
}
var browserWindowOptions = {width: 800, height: 600, icon: 'favicon.ico' , kiosk:true, autoHideMenuBar:true, darkTheme:true};
if (externalDisplay) {
browserWindowOptions.x = externalDisplay.bounds.x + 50
browserWindowOptions.y = externalDisplay.bounds.y + 50
}
// Create the browser window.
mainWindow = new BrowserWindow(browserWindowOptions)
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/index.html')
// Open the DevTools if run with "npm start dev"
if(process.argv[2] == "dev"){
mainWindow.webContents.openDevTools();
}
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// Get keyword spotting config
if(typeof config.speech == 'undefined'){
config.speech = {}
}
var modelFile = config.speech.model || "smart_mirror.pmdl"
var kwsSensitivity = config.speech.sensitivity || 0.5
// Initilize the keyword spotter
var kwsProcess = spawn('python', ['./speech/kws.py', modelFile, kwsSensitivity], {detached: false})
// Handel messages from python script
kwsProcess.stderr.on('data', function (data) {
var message = data.toString()
if(message.startsWith('INFO')){
// When a keyword is spotted, ping the speech service
mainWindow.webContents.send('keyword-spotted', true)
}else{
console.error(message)
}
})
kwsProcess.stdout.on('data', function (data) {
console.log(data.toString())
})
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
app.quit()
})
// No matter how the app is quit, we should clean up after ourselvs
app.on('will-quit', function () {
kwsProcess.kill()
})