Permalink
Cannot retrieve contributors at this time
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?
SmartHome/modules/lights.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
162 lines (155 sloc)
5.21 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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() | |
} | |
}) | |
}) | |
}); | |
} | |
} |