forked from shoaibuddin/smart-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
114 lines (97 loc) · 3.66 KB
/
Copy pathmain.js
File metadata and controls
114 lines (97 loc) · 3.66 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
/* 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')
// 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') {
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: 1900, height: 1000, icon: 'favicon.ico' , kiosk:true, autoHideMenuBar:true, darkTheme:true,"web-preferences": {
"web-security": false
}};
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.log("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))
}
})
// 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()
})