Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
153 lines (133 loc) · 5.03 KB
/
Copy pathmain.js
File metadata and controls
153 lines (133 loc) · 5.03 KB
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
/* global __dirname */
/* global process */
const electron = require('electron')
// Child Process for keyword spotter
const {spawn, exec} = 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')
// Launching the mirror in dev mode
const DevelopmentMode = process.argv[2] == "dev";
// 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') {
console.log(e)
error = "Syntax Error. \nLooks like there's an error in your config file: " + e.message + '\n' +
'Protip: You might want to paste your config file into a JavaScript validator like http://jshint.com/'
}
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 (DevelopmentMode) {
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
})
}
// Initilize the keyword spotter
var kwsProcess = spawn('node', ['./sonus.js'], {detached: false})
// Handel messages from node
kwsProcess.stderr.on('data', function (data) {
var message = data.toString()
console.error("ERROR", message.substring(4))
})
kwsProcess.stdout.on('data', function (data) {
var message = data.toString()
if (message.startsWith('!h:')) {
mainWindow.webContents.send('hotword', true)
} else if (message.startsWith('!p:')) {
mainWindow.webContents.send('partial-results', message.substring(4))
} else if (message.startsWith('!f:')) {
mainWindow.webContents.send('final-results', message.substring(4))
} else {
console.error(message.substring(3))
}
})
// Motion detection
if(config.motion && config.motion.enabled){
var mtnProcess = spawn('npm', ['run','motion'], {detached: false})
// Handel messages from node
mtnProcess.stderr.on('data', function (data) {
var message = data.toString()
console.error("ERROR", message.substring(4))
})
mtnProcess.stdout.on('data', function (data) {
var message = data.toString()
if (message.startsWith('!s:')) {
console.log(message.substring(3))
mainWindow.webContents.send('motionstart', true)
} else if (message.startsWith('!e:')) {
console.log(message.substring(3))
mainWindow.webContents.send('motionend', true)
} else if (message.startsWith('!c:')) {
console.log(message.substring(3))
mainWindow.webContents.send('calibrated', true)
} else if (message.startsWith('!E:')) {
console.log(message.substring(3))
mainWindow.webContents.send('Error', message.substring(3))
mtnProcess.kill();
} else {
console.error(message)
}
})
}
// 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()
// While cleaning up we should turn the screen back on in the event
// the program exits before the screen is woken up
if(mtnProcess){
mtnProcess.kill()
}
if(config.autoTimer && config.autoTimer.wake_cmd){
exec(config.autoTimer.wake_cmd).kill()
}
})