forked from jakkra/SmartMirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
146 lines (116 loc) · 4.34 KB
/
server.js
File metadata and controls
146 lines (116 loc) · 4.34 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
require('dotenv').config({silent : true})
const exec = require('child_process').exec;
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);
const mirrorSocket = require('./util/mirror_socket')(expressWs);
const commandHandler = require('./speech/command_handler')(mirrorSocket);
mirrorConfigFiles();
const config = require('./config');
let speech, hotword, commands, hue = null;
if (config.modules.googleCloudSpeech === true) {
speech = require('./speech/stream.js');
hotword = require('./speech/hot_word.js');
commands = require('./speech/command_classify')
}
const messages = require('./util/messages.js');
const requestHelper = require('./util/request_helper');
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.set('port', (process.env.PORT || 3001))
// Express only serves static assets in production
if (process.env.NODE_ENV === 'production') {
console.log('In production');
app.use(express.static('client/build'))
}
require('./routes')(app, mirrorSocket);
app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
if(process.env.target ==='PI'){ // Send current temperature when a client connects
const tempLogger = require('./util/temp_logger');
const temperature = tempLogger.getTemperature();
sendTemperatureToClient(temperature);
}
});
});
app.listen(app.get('port'), () => {
console.log(`Find the server at: http://localhost:${app.get('port')}/`); // eslint-disable-line no-console
});
app.get('/api/parse/:command', (req, res) => {
if(!req.params.command) {
res.json({
success: false,
message: 'Missing param: command',
});
}
const command = commands.classifyCommand(req.params.command.toLowerCase());
console.log(command);
commandHandler.handle(command);
res.redirect("/app");
});
if (config.modules.googleCloudSpeech === true) {
hotword.initCallback(hotwordDetectedCallback);
hotword.listenForHotword();
}
function done(){
hotword.listenForHotword();
mirrorSocket.sendToClient('recording', {isRecording: false});
}
function hotwordDetectedCallback(){
mirrorSocket.sendToClient('recording', {isRecording: true});
speech.listen((param) => {
console.log("_______" + new Date() + "_______");
console.log(param);
if(param && param.results && param.results[0] && param.results[0].alternatives && param.results[0].alternatives[0]) {
const result = param.results[0].alternatives[0];
console.log(result.transcript);
const command = commands.classifyCommand(result.transcript.toLowerCase());
console.log(command);
commandHandler.handle(command);
}
}, done)
}
if (process.env.target ==='PI' && config.modules.tempPirSensor === true){
const tempLogger = require('./util/temp_logger');
const motionDetector = require('./util/motion');
const buttonListener = require('./util/button');
tempLogger.start();
tempLogger.pollTemperature(1000 * 60, sendTemperatureToClient);
motionDetector.start(() => {
requestHelper.reportMotion();
mirrorSocket.sendToClient('motion', {message: messages.getMessage()});
});
buttonListener.start(onShortButtonClicked, onLongButtonPressed, onLongLongButtonPressed, onDoubleClick, 3000);
function onShortButtonClicked(){
hotword.stopRecord();
hotwordDetectedCallback();
}
function onLongButtonPressed(){
console.log('Long press!');
exec("sudo tvservice -o");
}
function onLongLongButtonPressed(){
console.log('Long Long press!');
exec("sudo reboot");
}
function onDoubleClick() {
console.log('Double click');
exec("sudo tvservice -p; sudo chvt 6; sudo chvt 7;");
}
}
function sendTemperatureToClient(readTemperature){
mirrorSocket.sendToClient('temperature', { temperature: readTemperature });
}
function mirrorConfigFiles(){
const fs = require('fs');
let serverConfig = __dirname + '/config.js';
let clientConfig = __dirname + '/client/src/config.js';
// If the client config already exists, either it has been
// created manually or already symlinked, so skip trying
if(!fs.existsSync(clientConfig)){
// Symbolically link the client and server config files to avoid
// having to maintain two copies
fs.symlinkSync(serverConfig, clientConfig);
}
}