forked from mmende/samsung-remote-models-2014-and-newer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
102 lines (88 loc) · 1.76 KB
/
server.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
/* eslint-disable */
const Hapi = require('hapi')
const Path = require('path')
const Inert = require('inert')
const SamsungTv = require('samsung-remote')
const DEBUG = true
console.debug = (...args) => {
if (DEBUG) {
console.log.apply(this, args)
}
}
const deviceConfig = {
ip: null,
appId: '721b6fce-4ee6-48ba-8045-955a539edadb',
userId: '654321',
}
const server = new Hapi.Server({
port: 3000,
host: 'localhost',
routes: {
files: {
relativeTo: Path.join(__dirname, 'public'),
},
},
})
let samsungTv
server.route({
method: 'POST',
path: '/api/ping',
handler: (request, h) => {
deviceConfig.ip = request.payload.ip
samsungTv = new SamsungTv(deviceConfig)
return samsungTv.init()
},
})
server.route({
method: 'POST',
path: '/api/connect',
handler: (request, h) => {
return samsungTv.requestPin().then(() => {
return {}
})
},
})
server.route({
method: 'POST',
path: '/api/pair',
handler: (request, h) => {
const pin = request.payload.pin
return samsungTv.confirmPin(pin).then((_identity) => {
return samsungTv.connect().then(() => _identity)
})
},
})
server.route({
method: 'POST',
path: '/api/key',
handler: (request, h) => {
const keyCode = request.payload.keyCode
samsungTv.sendKey(keyCode)
return {}
},
})
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return h.file('index.html')
},
})
server.route({
method: 'GET',
path: '/app.js',
handler: (request, h) => {
return h.file('app.js')
},
})
const provision = async () => {
try {
await server.register(Inert)
await server.start()
} catch (err) {
console.log(err)
process.exit(1)
}
console.log('Server running at:', server.info.uri)
}
provision()