From 24008cbad6b4de5924ee550f685a1fbaedbc7b2a Mon Sep 17 00:00:00 2001 From: Dharmindra Kaila Date: Sat, 30 Nov 2019 16:30:42 +0000 Subject: [PATCH] latest commit. --- index.js | 58 ++++++++++++++++++++++++++++++++++------ modules/temp.js | 13 ++++----- views/index.handlebars | 5 +++- views/lights.handlebars | 2 +- views/temp.handlebars | 2 ++ website.db | Bin 0 -> 20480 bytes 6 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 website.db 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 0000000000000000000000000000000000000000..ba0e3c19308429b41deb4da6f8f36d1764a3f0e4 GIT binary patch literal 20480 zcmeI)%TD7~7yw{9hGC$ERHLG(LRJ1jR82IQT*x4p6-~h)PeOnY%227QPMnk2#FyY> z6R~PD?JIQA7vKf@1oH&lb=6fjU3FV^+5hCmMVgsvMk96cTZ(h+bN_4>|B21fVZ{zP zwEVzjArzI56jfEe08kX=0e+_OGu|{zOpOolTfLEfRq}z7eFF2CJN4mT%ECVjR|}tE zf;JK$0TLhq5+DH*AOR8}0TOuM0x#C4W)e$F>W`(6H5|@7(c^&_eNAuG^io}idg;rG z4x^JWJ3E^irj2bds&#!=ufajh*elgep{$=m>A1dcRI%=!Uae=~JmMm>eeb$-1|q=& zsO!%$d)USGV*dm}H3M+lB7bm!rE=Q=XEwMp^0aw3$RAq4vKSunlqtyQy1b zgHw>QoBzd079KHlnGmC-KoUNwJ|@Yh4`TNcj~=O~gZ{<2gCXm;g5%6RlixYiZjI?q zX3AqO&tT2;i)nQxkxZ&T4BH$dib*1NtDVVdfYdmBeTiRpLxgtd@X_>cOuEU18Q5KU ztBf+64CCT0<*x8y>SAkt=4;y1adT z;>cx=lN;=ZS&GvCI5wvglYQgiCyHVS!0vIGM9!*#t1FuzaOtLljWGjy%)=ZXI-cK? zLuMHAVuUQKI5=F}!@M4E0LI&P+XEh&S*UVEVbJ#dCJHqI7F^0E8jw3E^TB3>Vw7y^d^D;NH80Gr3wN)v=4F50`#4Eb<2I!mF`| zS@ZF~6)pa&68}B^Cw|dJ0wh2JBtQZrKmsH{0wh2JBtQZr@c$S1K+~4g(X%VDSyfA@ z@(}-Qx4hvU*QRVmp)P2}X?tC+pTaKkmAfGozI~WzSfyx4K`7a>wg2CV|0(|kngOb;lZ)Su+#I=+v%NLXPHCcX} Xw`>nz%)s{{i~#@Nz#j7^|77A{5o{-9 literal 0 HcmV?d00001