Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#!/usr/bin/env node
//Routes File
'use strict'
/* MODULE IMPORTS */
const Koa = require('koa')
const Router = require('koa-router')
const views = require('koa-views')
const staticDir = require('koa-static')
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 app = new Koa()
const router = new Router()
const data2 = {}
const data3= {}
let tempfloat = 0
/* CONFIGURING THE MIDDLEWARE */
app.keys = ['darkSecret']
app.use(staticDir('public'))
app.use(bodyParser())
app.use(session(app))
app.use(views(`${__dirname}/views`, { extension: 'handlebars' }, {map: { handlebars: 'handlebars' }}))
const defaultPort = 8080
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.
}
});
const wss2 = new WebSocket.Server({
port: 8980,
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.
*
* @name Home Page
* @route {GET} /
* @authentication This route requires cookie-based authentication.
*/
router.get('/', async ctx => {
try {
await ctx.render('index')
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
/**
* The user registration page.
*
* @name Register Page
* @route {GET} /register
*/
router.get('/lights', async ctx => {
try {
//const temp = await new Temp(dbName)
//data2.two = await temp.reading()
const connection = new WebSocket('ws://localhost:8980')
wss2.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/Lights01/', function (err) {
})
})
client.on('message', function (topic, message) {
data2.three = message.toString()
//console.log(message.toString())
console.log(data2.three)
})
});
await ctx.render('lights', {data2: data2.three})
}
catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/FrontLightON', koaBody, async ctx => {
try {
const lights = await new Lights(dbName)
await lights.frontOn()
alert('front light is on')
await ctx.render('lights')
}
catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/FrontLightOFF', koaBody, async ctx => {
try {
const lights = await new Lights(dbName)
await lights.frontOff()
alert('front light is off')
await ctx.render('lights')
}
catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.post('/MontionSensor', async ctx => {
try {
const body = ctx.request.body
console.log(body.Sensor)
const lights = await new Lights(dbName)
await lights.sensorDelay(body.Sensor)
alert('Sensor Delay set')
await ctx.render('lights')
}
catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/TurnSensorOn', async ctx => {
try {
const lights = await new Lights(dbName)
await lights.FrontSensorOn()
alert('Front Sensor turned on')
await ctx.render('lights')
}
catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/TurnSensorOff', async ctx => {
try {
const lights = await new Lights(dbName)
await lights.FrontSensorOff()
alert('Front Sensor turned off')
await ctx.render('lights')
}
catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/temp', async ctx => {
try {
//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()
tempfloat = parseFloat(data2.two)
//console.log(message.toString())
console.log(data2.two)
})
});
if (tempfloat < 28)
{
data3.heating = 'heating is on'
}
else
{
data3.heating = 'heating is off'
}
await ctx.render('temp', {data2: data2.two, data3: data3.heating})
}
catch(err) {
await ctx.render('error', {message: err.message})
}
})
app.use(router.routes())
module.exports = app.listen(port, async() => console.log(`listening on port ${port}`))