diff --git a/index.js b/index.js index 07ae0ef..8b2cb47 100644 --- a/index.js +++ b/index.js @@ -11,8 +11,8 @@ const router = Router(); const port = 9001 -var LightOnOff = 'false'; -var LightConnected = 'false'; +var LightOnOff = false; +var LightConnected = false; render(app, { root: path.join(__dirname, 'views'), @@ -44,15 +44,14 @@ const wss = new WebSocket.Server({ // should not be compressed. } }); -const connection = new WebSocket('ws://localhost:3000') router.post('/ExteriorLightOnOff', onoff); router.get('/Lights', async ctx =>{ await ctx.render('lights'); - if (LightConnected == "false"){ - lightConnected = 'true'; - const connection = new WebSocket('ws://localhost:3000') + 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', @@ -76,6 +75,7 @@ await ctx.render('lights'); }) async function onoff(ctx) { + 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', @@ -85,24 +85,22 @@ async function onoff(ctx) { port: 8883, }) - if (LightOnOff == 'false'){ + if (LightOnOff == false){ client.on('connect', function () { client.subscribe('302CEM/Team2/kailad', function (err) { if (!err){ - LightOnOff = 'true'; + LightOnOff = true; client.publish('302CEM/Team2/kailad', 'Turn on the Exterior Light') - client.end() } }) }) } - else if (LightOnOff == 'true'){ + else if (LightOnOff == true){ client.on('connect', function () { client.subscribe('302CEM/Team2/kailad', function (err) { if (!err){ - LightOnOff = 'false'; + LightOnOff = false; client.publish('302CEM/Team2/kailad', 'Turn off the Exterior Light') - client.end() } }) })