-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-apis.js
More file actions
65 lines (56 loc) · 1.54 KB
/
react-apis.js
File metadata and controls
65 lines (56 loc) · 1.54 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
const express = require('express')
const router = express.Router()
const soapbuilder = require('./soapbuilder')
const switches = require('./switches.json')
router.get('/getAllWemos', async (req, res) => {
const transformedSwitches = []
switches.forEach(el => {
transformedSwitches.push({
ip: el[0],
port: el[1],
name: el[2]
})
})
await res.send({
success: true,
data: transformedSwitches
})
})
router.get('/getState', async (req, res) => {
const ip = req.query.ip
const port = parseInt(req.query.port)
try {
const state = await soapbuilder.getBinaryState(ip, port)
res.send({
success: true,
data: state.state
})
} catch (error) {
res.status(500).send({
success: false,
error: error.message
})
}
})
router.patch('/setState', async (req, res) => {
const { ip, port, state } = req.body;
if (typeof ip === 'undefined' || typeof port === 'undefined' || typeof state === 'undefined') {
res.status(400).send({
success: false,
error: 'IP, port, or state was not provided.'
})
} else {
try {
await soapbuilder.setBinaryState(ip, parseInt(port), state)
res.send({
success: true
})
} catch (error) {
res.status(500).send({
success: false,
error: error.message
})
}
}
})
module.exports = router