-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (88 loc) · 2.08 KB
/
index.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
// Require the framework and instantiate it
const package = require('./package.json')
const path = require('path')
const execFile = require('child_process').execFile;
const cmdTable = {
'move': {
file: 'xdotool',
options: ['mousemove_relative', '--']
},
'scrollUp': {
file: 'xdotool',
options: ['click', '5']
},
'scrollDown': {
file: 'xdotool',
options: ['click', '4']
},
'volumeUp': {
file: 'xdotool',
options: ['key', 'XF86AudioRaiseVolume']
},
'volumeDown': {
file: 'xdotool',
options: ['key', 'XF86AudioLowerVolume']
},
}
const msgTable = {
'move': {
ok: 'move ok',
fail: 'move fail!'
},
'scrollUp': {
ok: 'scrollUp ok',
fail: 'scrollUp fail!'
},
'scrollDown': {
ok: 'scrollDown ok',
fail: 'scrollDown fail!'
},
'volumeUp': {
ok: 'volumeUp ok',
fail: 'volumeUp fail!'
},
'volumeDown': {
ok: 'volumeDown ok',
fail: 'volumeDown fail!'
}
}
const fastify = require('fastify')({
logger: process.env.LOCAL
})
fastify.get('/version', (request, reply) => {
return { version: package.version }
})
fastify.post('/:cmd', async (request, reply) => {
const cmdName = request.params.cmd
const command = cmdTable[cmdName]
if (!cmdName || !command) return { error: 'invalid command'}
let options = []
if (request.body && request.body.x && request.body.y) {
const { x, y } = request.body
options.push(command.options[0])
options.push(command.options[1])
options.push(x)
options.push(y)
console.log(x, y, options)
} else {
options = command.options
}
execFile(command.file, options, function callback(error, stdout, stderr) {
if (error) {
reply.send({ command: msgTable[cmdName].fail })
} else {
reply.send({ command: msgTable[cmdName].ok })
}
});
})
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
})
// Run the server!
fastify.listen(3000, '0.0.0.0', function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
fastify.log.info(`server listening on ${address}`)
})