Skip to content
Permalink
Browse files
minor chnage
  • Loading branch information
kailad committed Oct 29, 2019
1 parent 973afb0 commit 43c2e464fc03a0e18a45cc2ce93326c53566f34d
Showing 1 changed file with 32 additions and 17 deletions.
@@ -10,6 +10,10 @@ const render = require('koa-ejs')
const router = Router();
const port = 9001


var LightOnOff = 'false';
var LightConnected = 'false';

render(app, {
root: path.join(__dirname, 'views'),
layout: 'layout',
@@ -40,14 +44,16 @@ const wss = new WebSocket.Server({
// should not be compressed.
}
});
const connection = new WebSocket('ws://localhost:3000')

router.post('/ExteriorLightOn', on);
router.post('/ExteriorLightOnOff', onoff);

router.get('/Lights', async ctx =>{
await ctx.render('lights');

const connection = new WebSocket('ws://localhost:3000')
wss.on('connection', function connection(ws) {
await ctx.render('lights');
if (LightConnected == "false"){
lightConnected = 'true';
const connection = new WebSocket('ws://localhost:3000')
wss.on('connection', function connection(ws) {
const client = mqtt.connect('mqtt.coventry.ac.uk', {
host: 'mqtt.coventry.ac.uk',
protocol: 'mqtts',
@@ -63,15 +69,13 @@ router.get('/Lights', async ctx =>{
}
})
})

client.on('message', function(topic, message, packet){
ws.send(`${message}`)
})
});
});
}
else {
}
})

async function on(ctx) {
const connection = new WebSocket('ws://localhost:3000')
async function onoff(ctx) {
wss.on('connection', function connection(ws) {
const client = mqtt.connect('mqtt.coventry.ac.uk', {
host: 'mqtt.coventry.ac.uk',
@@ -80,18 +84,29 @@ async function on(ctx) {
password: 'n3fXXFZrjw',
port: 8883,
})


if (LightOnOff == 'false'){
client.on('connect', function () {
client.subscribe('302CEM/Team2/kailad', function (err) {
if (!err){
LightOnOff = 'true';
client.publish('302CEM/Team2/kailad', 'Turn on the Exterior Light')
client.end()
}
})
})

client.on('message', function(topic, message, packet){
ws.send(`${message}`)
})
}
else if (LightOnOff == 'true'){
client.on('connect', function () {
client.subscribe('302CEM/Team2/kailad', function (err) {
if (!err){
LightOnOff = 'false';
client.publish('302CEM/Team2/kailad', 'Turn off the Exterior Light')
client.end()
}
})
})
}
});
ctx.redirect('/lights')
}

0 comments on commit 43c2e46

Please sign in to comment.