var express = require('express');
|
|
var bodyParser = require('body-parser')
|
|
var cors = require('cors')
|
|
var Database = require('./database.js');
|
|
var HardwareDriver = require("./hardwaredriver/hardwaredriver").HardwareDriver;
|
|
var DRIVERTYPE = require("./hardwaredriver/hardwaredriver").DRIVERTYPE;
|
|
|
|
//import {HardwareDriver, driverType} from './hardwaredriver/hardwaredriver'
|
|
|
|
var db = undefined;
|
|
var driver = undefined;
|
|
|
|
function init() {
|
|
|
|
initDB()
|
|
.then(() => {
|
|
db.getBoards()
|
|
.then(boards => {
|
|
console.log(boards)
|
|
initBoards(boards).then(() => {
|
|
app = express();
|
|
app.use(cors())
|
|
// parse application/x-www-form-urlencoded
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
|
// parse application/json
|
|
app.use(bodyParser.json())
|
|
app.listen(3002, () => {
|
|
console.log('Server running on port 3002')
|
|
})
|
|
initRoutes()
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
function initDB() {
|
|
db = new Database()
|
|
return db.connect()
|
|
}
|
|
|
|
function initBoards(boards) {
|
|
driver = new HardwareDriver(DRIVERTYPE.DUMMY)
|
|
return driver.init(boards)
|
|
}
|
|
|
|
|
|
function demoDriver(channelConfig, colors) {
|
|
console.log(channelConfig)
|
|
console.log(colors['r'])
|
|
channelConfig.forEach(channel => {
|
|
console.log(channel)
|
|
console.log(`Setting channel ${channel.channel} on board ${channel.address} to ${colors[channel.color]}. `)
|
|
});
|
|
}
|
|
|
|
function initRoutes() {
|
|
|
|
app.get('/getLamps', (req, res) => {
|
|
db.getLamps()
|
|
.then(result => {
|
|
res.send(result)
|
|
})
|
|
})
|
|
|
|
app.get('/getLampChannels', (req, res) => {
|
|
if (req.query.id == undefined) {
|
|
res.sendStatus(400)
|
|
}
|
|
db.getLampChannelConfig(req.query.id)
|
|
.then(result => {
|
|
res.send(result)
|
|
})
|
|
.catch(err => {
|
|
res.sendStatus(500)
|
|
})
|
|
})
|
|
|
|
app.get('/getPresets', (req, res) => {
|
|
db.getPresets()
|
|
.then(result => {
|
|
res.send(result)
|
|
})
|
|
})
|
|
|
|
app.post('/setLamp', (req, res) => {
|
|
if (req.body.id == undefined || req.body.color == undefined) {
|
|
res.sendStatus(400)
|
|
} else {
|
|
db.getLampChannelConfig(req.body.id)
|
|
.then(lamp => {
|
|
if (driver) {
|
|
driver.setRGB(req.body.id, lamp, req.body.color);
|
|
console.log(req.body.color)
|
|
res.sendStatus(200)
|
|
} else {
|
|
console.log("Board not found")
|
|
demoDriver(lamp, req.body.color)
|
|
res.sendStatus(200)
|
|
}
|
|
})
|
|
.catch(err => {
|
|
console.log(err)
|
|
res.sendStatus(500)
|
|
})
|
|
}
|
|
|
|
})
|
|
|
|
app.post('/setChannel', (req, res) => {
|
|
if (driver) {
|
|
driver.setChannel(req.body.data.address, req.body.data.channel, req.body.data.value)
|
|
} else {
|
|
console.log("demoDriver set ")
|
|
}
|
|
res.sendStatus(200)
|
|
})
|
|
|
|
app.post('/setPreset', (req, res) => {
|
|
console.log(req.body)
|
|
db.setPreset(req.body)
|
|
.then(result => {
|
|
res.sendStatus(result)
|
|
})
|
|
.catch(error => {
|
|
res.sendStatus(result)
|
|
})
|
|
})
|
|
|
|
app.post('/setByPreset', (req, res) => {
|
|
|
|
db.getPreset(req.body.id)
|
|
.then(preset => {
|
|
console.log(preset)
|
|
preset.forEach(lamp => {
|
|
driver.setChannel(lamp.address, lamp.channel, lamp.value)
|
|
})
|
|
res.sendStatus(200)
|
|
})
|
|
.catch(error => {
|
|
console.log(error)
|
|
res.sendStatus(500)
|
|
})
|
|
})
|
|
|
|
app.post('/deletePreset', (req, res) => {
|
|
db.deletePreset(req.body.id)
|
|
.then(() => {
|
|
res.sendStatus(200)
|
|
})
|
|
.catch((error) => {
|
|
console.log(error)
|
|
res.sendStatus(500)
|
|
})
|
|
})
|
|
|
|
app.get('/getBoards', (req, res) => {
|
|
db.getBoards()
|
|
.then(boards => {
|
|
res.send(boards)
|
|
})
|
|
})
|
|
|
|
app.get('/getTypes', (req, res) => {
|
|
db.getLampTypes()
|
|
.then(types => {
|
|
res.send(types)
|
|
})
|
|
})
|
|
|
|
app.post('/deleteLamp', (req, res) => {
|
|
db.deleteLamp(req.body.id)
|
|
.then(() => {
|
|
res.sendStatus(200)
|
|
})
|
|
.catch((error) => {
|
|
console.log(error)
|
|
res.sendStatus(500)
|
|
})
|
|
})
|
|
|
|
app.post('/createLamp', (req, res) => {
|
|
console.log(req.body.light)
|
|
db.createLamp(req.body.light)
|
|
.then(()=>{
|
|
console.log("Success: ")
|
|
res.sendStatus(200)
|
|
})
|
|
.catch(err => {
|
|
console.log("Result: " + err)
|
|
res.sendStatus(500)
|
|
})
|
|
})
|
|
|
|
}
|
|
|
|
|
|
init()
|