Skip to content
Permalink
172c338d18
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
162 lines (155 sloc) 5.21 KB
'use strict'
const WebSocket = require('ws');
const mqtt = require('mqtt');
const sqlite = require('sqlite-async')
const wss = new WebSocket.Server({
port: 3000,
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.
}
});
module.exports = class Lights {
constructor(dbName = ':memory:') {
return (async() => {
this.db = await sqlite.open(dbName)
// we need this table to store the user accounts
const sql = 'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, pass TEXT);'
await this.db.run(sql)
return this
})()
}
async frontOn() {
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',
protocol: 'mqtts',
username: '302CEM',
password: 'n3fXXFZrjw',
port: 8883,
})
client.on('connect', function () {
client.subscribe('302CEM/Team2/FrontLight/LED/', function (err) {
if (!err){
var options={
retain:false,
clear: true,
qos:1};
client.publish('302CEM/Team2/FrontLight/LED/', 'ON', options)
client.end()
}
})
})
});
}
async frontOff() {
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',
protocol: 'mqtts',
username: '302CEM',
password: 'n3fXXFZrjw',
port: 8883,
})
client.on('connect', function () {
client.subscribe('302CEM/Team2/FrontLight/LED/', function (err) {
if (!err){
client.publish('302CEM/Team2/FrontLight/LED/', 'OFF')
client.end()
}
})
})
});
}
async FrontSensorOn() {
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',
protocol: 'mqtts',
username: '302CEM',
password: 'n3fXXFZrjw',
port: 8883,
})
client.on('connect', function () {
client.subscribe('302CEM/Team2/FrontLight/PIR/', function (err) {
if (!err){
var options={
retain:false,
clear: true,
qos:1};
client.publish('302CEM/Team2/FrontLight/PIR/', 'ENABLE', options)
client.end()
}
})
})
});
}
async FrontSensorOff() {
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',
protocol: 'mqtts',
username: '302CEM',
password: 'n3fXXFZrjw',
port: 8883,
})
client.on('connect', function () {
client.subscribe('302CEM/Team2/FrontLight/PIR/', function (err) {
if (!err){
var options={
retain:false,
clear: true,
qos:1};
client.publish('302CEM/Team2/FrontLight/PIR/', 'DISABLE', options)
client.end()
}
})
})
});
}
async sensorDelay(sensor) {
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',
protocol: 'mqtts',
username: '302CEM',
password: 'n3fXXFZrjw',
port: 8883,
})
client.on('connect', function () {
client.subscribe('302CEM/Team2/FrontLight/PIR/DELAY/', function (err) {
if (!err){
var options={
retain:false,
clear: true,
qos:1,
dup: false
};
client.publish('302CEM/Team2/FrontLight/PIR/DELAY/', `${sensor}`)
client.end()
}
})
})
});
}
}