SODashServer/routes/lighting_v1.js

98 lines
2.0 KiB
JavaScript
Raw Normal View History

'use strict';
2016-04-13 09:01:28 +00:00
/**
* Created by Martin on 09/02/2016.
*/
/*Var mqttConnect = require("../lib/mqtt/mqttConnect");
2016-05-13 12:44:57 +00:00
mqttConnect.doConnection();*/
2016-04-13 09:01:28 +00:00
var mqttConnect;
function doLightsOn(id) {
mqttConnect.doConnection().lightingOn(id);
2016-04-13 09:01:28 +00:00
}
function doLightsOff(id) {
mqttConnect.doConnection().lightingOff(id);
2016-04-13 09:01:28 +00:00
}
2016-05-13 12:44:57 +00:00
function doLightsCommand(id) {
console.log('doLightsCommand', id);
mqttConnect.doConnection().lightingCommand(id);
}
2016-04-13 09:01:28 +00:00
function setUseRef(ref) {
mqttConnect = ref;
2016-04-13 09:01:28 +00:00
}
module.exports = {
2016-05-13 12:44:57 +00:00
mqttConnect: null, socket: null, use: function(ref) {
setUseRef(ref);
}, turnoff: function(req, res) {
if (!req.body.light) {
res.status(400).send({
status: 'error',
error: 'missing required parameter'
});
return;
}
console.log(req.body.light);
if (req.body.hasOwnProperty('light')) {
var light = req.body.light;
doLightsOff(light);
}
res.json({});
}, turnon: function(req, res) {
console.log(req.body);
2016-05-13 12:44:57 +00:00
if (!req.body.light) {
res.status(400).send({
status: 'error',
error: 'missing required parameter'
});
return;
}
console.log(req.body.light);
if (req.body.hasOwnProperty('light')) {
console.log('Controling ', req.body.light);
2016-05-13 12:44:57 +00:00
var light = req.body.light;
doLightsOn(light);
}
res.json({});
}, command: function(req, res) {
if (!req.body.id) {
res.status(400).send({
status: 'error',
error: 'missing required parameter'
});
return;
}
console.log(req.body.id);
if (req.body.hasOwnProperty('id')) {
var id = req.body.id;
doLightsCommand(id);
}
res.json({});
}, setsocket: function(socket) {
this.socket = socket;
return this;
}, subscribe: function(socket) {
this.socket.subscribe('Lighting', 'LightingDataReceived');
}, doLightsOn: function(id) {
doLightsOn(id);
}, doLightsOff: function(id) {
doLightsOff(id);
}
};