-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
153 lines (133 loc) · 3.5 KB
/
routes.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
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
'use strict'
const async = require('async')
module.exports = {
/**
* Setups all routes for the md-dm-api app.
* @param {express} app
* @param {winston} logger
* @param {MdDmConnection} connection
*/
setup: function (app, logger, connection) {
// GET /
// Displays the connection status and basic device information (parsed
// output from `version`).
app.get('/', (req, res) => res.json(connection.device))
// GET /hardware
// Displays the hardware info, including a list of cards (parsed
// output from `showhw`).
app.get('/hardware', (req, res) => connection.getHardware(function (err, hardware) {
if (err)
{
logger.warn(`Failed to get hardware: ${err}`)
return res.status(500).json({ message: err })
}
res.json(hardware)
}))
// GET /outputs
// Displays the current routing from the outputs (parsed output from
// dumpdmroutei).
app.get('/outputs', (req, res) => connection.getOutputs(function (err, outputs) {
if (err)
{
logger.warn(`Failed to get hardware: ${err}`)
return res.status(500).json({ message: err })
}
res.json(outputs)
}))
// GET /output/:slot
// Same as /outputs, but returns routing from a single slot.
app.get('/output/:slot', (req, res) => connection.getOutputs(function (err, outputs) {
if (err)
{
logger.warn(`Failed to get hardware: ${err}`)
return res.status(500).json({ message: err })
}
var output = outputs.filter(function (entry) {
return entry.slot == req.params.slot
})
if (output.length)
{
res.json(output[0])
}
else
{
res.status(400).json({
slot: req.params.slot,
message: `No output device found on slot ${req.params.slot}`
})
}
}))
// POST /output/:slot
// Changes the input routing, either video, audio or USB, of a specific
// output card, using its slot. Only one parameter is required.
// --
// {
// "video": 1,
// "audio": 1
// }
//
// {
// "video": 1,
// "audio": 3,
// "usb": 1
// }
app.post('/output/:slot', (req, res) => {
if (typeof req.body.video === 'undefined' && typeof req.body.audio === 'undefined' && typeof req.body.usb === 'undefined')
{
return res.status(400).json({ message: 'At least the video, audio or usb input must be given.' })
}
logger.info(`Setting new route for slot ${req.params.slot}`)
async.series([
function (cb) {
if (typeof req.body.audio !== 'undefined')
{
logger.debug(`Setting new audio route for slot ${req.params.slot} to ${req.body.audio}`)
connection.setAudioRoute(req.params.slot, req.body.audio, function (err) {
cb(err)
})
}
else
{
cb()
}
},
function (cb) {
if (typeof req.body.video !== 'undefined')
{
logger.debug(`Setting new video route for slot ${req.params.slot} to ${req.body.video}`)
connection.setVideoRoute(req.params.slot, req.body.video, function (err) {
cb(err)
})
}
else
{
cb()
}
},
function (cb) {
if (typeof req.body.usb !== 'undefined')
{
logger.debug(`Setting new USB route for slot ${req.params.slot} to ${req.body.usb}`)
connection.setUsbRoute(req.params.slot, req.body.usb, function (err) {
cb(err)
})
}
else
{
cb()
}
}
], function (err) {
if (err)
{
logger.warn(`Error setting route for slot ${req.params.slot}`)
res.status(500).json({ message: err })
}
else
{
res.status(200).end()
}
})
})
}
}