diff --git a/index.js b/index.js index 75e0a24..03d83ba 100644 --- a/index.js +++ b/index.js @@ -13,14 +13,17 @@ const bodyParser = require('koa-bodyparser') const koaBody = require('koa-body')({multipart: true, uploadDir: '.'}) const session = require('koa-session') const alert = require('alert-node'); +const WebSocket = require('ws'); +const mqtt = require('mqtt'); /* IMPORT CUSTOM MODULES */ const User = require('./modules/user') const Lights = require('./modules/lights') -const Temp = require('./modules/temp') +//const Temp = require('./modules/temp') const app = new Koa() const router = new Router() +const data2 = {} /* CONFIGURING THE MIDDLEWARE */ app.keys = ['darkSecret'] @@ -34,7 +37,28 @@ const port = process.env.PORT || defaultPort const dbName = 'website.db' let user - +const wss = new WebSocket.Server({ + port: 7009, + perMessageDeflate: { + zlibDeflateOptions: { + // See zlib defaults. + chunkSize: 1024, + memLevel: 7, + level: 3 + }, + zlibInflateOptions: { + chunkSize: 10 * 1024 + }, + // Other options settable: + clientNoContextTakeover: true, // Defaults to negotiated value. + serverNoContextTakeover: true, // Defaults to negotiated value. + serverMaxWindowBits: 10, // Defaults to negotiated value. + // Below options specified as default values. + concurrencyLimit: 10, // Limits zlib concurrency for perf. + threshold: 1024 // Size (in bytes) below which messages + // should not be compressed. + } + }); /** * The secure home page. * @@ -127,12 +151,30 @@ router.post('/MontionSensor', async ctx => { router.get('/temp', async ctx => { try { - let data = {} - const temp = await new Temp(dbName) - data = await temp.reading() - alert('Sensor set') - console.log(data) - await ctx.render('temp', data) + //const temp = await new Temp(dbName) + //data2.two = await temp.reading() + alert('temp refreshed') + const connection = new WebSocket('ws://localhost:7009') + wss.on('connection', function connection(ws) { + const client = mqtt.connect('mqtt.coventry.ac.uk', { + host: 'mqtt.coventry.ac.uk', + protocol: 'mqtts', + username: '302CEM', + password: 'n3fXXFZrjw', + port: 8883, + }) + client.on('connect', function () { + client.subscribe('302CEM/Team2/temp01', function (err) { + }) + }) + client.on('message', function (topic, message) { + data2.two = message.toString() + //console.log(message.toString()) + console.log(data2.two) + + }) + }); + await ctx.render('temp', {data2: data2.two}) } catch(err) { await ctx.render('error', {message: err.message}) diff --git a/modules/temp.js b/modules/temp.js index b89418d..c2c1998 100644 --- a/modules/temp.js +++ b/modules/temp.js @@ -37,6 +37,7 @@ module.exports = class Temp { })() } async reading() { + let data = '40c' const connection = new WebSocket('ws://localhost:7009') wss.on('connection', function connection(ws) { const client = mqtt.connect('mqtt.coventry.ac.uk', { @@ -47,17 +48,17 @@ module.exports = class Temp { port: 8883, }) client.on('connect', function () { - client.subscribe('302CEM/Team2/temp', function (err) { + client.subscribe('302CEM/Team2/temp01', function (err) { }) }) client.on('message', function (topic, message) { - // message is Buffer - console.log(message.toString()) - const data = message.toString() - return data + data = message.toString() + //console.log(message.toString()) + console.log(data) + }) }); - + return data } diff --git a/views/index.handlebars b/views/index.handlebars index 2735a5f..05eb63a 100644 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -14,6 +14,9 @@

This is a secure page. Users need to have a valid account and be logged in to see it.

log out

-

+

+
+
+

\ No newline at end of file diff --git a/views/lights.handlebars b/views/lights.handlebars index a4a10b9..5dc1698 100644 --- a/views/lights.handlebars +++ b/views/lights.handlebars @@ -11,7 +11,6 @@

Lights Page

here you will be able to turn on or off lights.

-

back button

@@ -28,5 +27,6 @@

Set Sensor:

+

back home

\ No newline at end of file diff --git a/views/temp.handlebars b/views/temp.handlebars index 7af0f6f..c7728a3 100644 --- a/views/temp.handlebars +++ b/views/temp.handlebars @@ -9,7 +9,9 @@

Temp

Display tempature

+

{{data2}}

+

back home

\ No newline at end of file diff --git a/website.db b/website.db new file mode 100644 index 0000000..ba0e3c1 Binary files /dev/null and b/website.db differ