Skip to content
Permalink
Browse files
latest commit.
  • Loading branch information
kailad committed Nov 30, 2019
1 parent b859789 commit 24008cbad6b4de5924ee550f685a1fbaedbc7b2a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
@@ -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})
@@ -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
}


@@ -14,6 +14,9 @@
<p>This is a secure page. Users need to have a valid account and be logged in to see it.</p>
<p><a href="/logout">log out</a></p>
<form action="/lights" method="get">
<p><input type="submit" value="Lights"></p>
<p><input type="submit" value="Lights page"></p>
</form>
<form action="/temp" method="get">
<p><input type="submit" value="temp page"></p>
</form>
</html>
@@ -11,7 +11,6 @@
<body>
<h1>Lights Page</h1>
<p>here you will be able to turn on or off lights.</p>
<p><a href="/">back button</a></p>
<form action="/FrontLightON" method="get">
<p><input type="submit" value="Turn front Light on"></p>
</form>
@@ -28,5 +27,6 @@
<p>Set Sensor:<br /><input type="text" name="Sensor" placeholder="value" autofocus></p>
<p><input type="submit" value="submit value"></p>
</form>
<p><a href="/">back home</a></p>
</body>
</html>
@@ -9,7 +9,9 @@
<body>
<h1>Temp</h1>
<p>Display tempature</p>
<h2>{{data2}}</h2>
<form action="/temp" method="get">
<p><input type="submit" value="refresh temp reading"></p>
</form>
<p><a href="/">back home</a></p>
</html>
BIN +20 KB website.db
Binary file not shown.

0 comments on commit 24008cb

Please sign in to comment.