2019-07-15 21:29:13 +01:00
|
|
|
const Orvibo = require('./server/utils/Orvibo');
|
2019-07-16 17:26:41 +01:00
|
|
|
const utils = require('./server/utils/Utils')
|
2019-07-15 21:29:13 +01:00
|
|
|
const express = require('express');
|
|
|
|
const app = express();
|
2019-07-14 11:37:20 +01:00
|
|
|
|
2019-07-15 21:29:13 +01:00
|
|
|
app.set('view engine', 'pug');
|
2019-07-15 23:13:06 +01:00
|
|
|
app.use(express.static(__dirname + '/server'));
|
2019-07-14 11:37:20 +01:00
|
|
|
|
|
|
|
const settings = {
|
|
|
|
LOG_PACKET: true, //Show incoming packet data from the socket
|
|
|
|
ORVIBO_KEY: process.env.orviboPK,
|
2019-07-14 16:12:26 +01:00
|
|
|
plugInfo :
|
2019-07-16 17:26:41 +01:00
|
|
|
utils.generatePlugArray(process.env.plugArray)
|
2019-07-14 16:12:26 +01:00
|
|
|
,
|
2019-07-14 11:37:20 +01:00
|
|
|
};
|
|
|
|
let orvibo = new Orvibo(settings);
|
|
|
|
// When a socket first connects and initiates the handshake it will emit the connected event with the uid of the socket;
|
|
|
|
orvibo.on('plugConnected', ({uid, name}) => {
|
|
|
|
console.log(`Connected ${uid} name = ${name}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
// If the socket state is updated this event will fire
|
|
|
|
orvibo.on('plugStateUpdated', ({uid, state , name}) => {
|
|
|
|
console.log(`Plug ${name} ${uid} updated state ${state}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
// The plug sends a hearbeat to let the server know it's still alive
|
|
|
|
orvibo.on('gotHeartbeat', ({uid, name}) => {
|
|
|
|
console.log(`Plug ${name} ${uid} sent heartbeat`);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Called when the plug disconnects
|
|
|
|
orvibo.on('plugDisconnected', ({uid, name }) => {
|
|
|
|
console.log(`Plug ${uid} - ${name} disconnected`);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Called when the plug disconnects with an error ie it's been unplugged
|
|
|
|
orvibo.on('plugDisconnectedWithError', ({uid, name }) => {
|
|
|
|
console.log(`Plug ${uid} - ${name} disconnected with error`);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Start the Orvibo socket server
|
|
|
|
orvibo.startServer();
|
|
|
|
|
2019-07-15 21:29:13 +01:00
|
|
|
app.get('/', (req, res) => {
|
2019-07-16 09:51:11 +01:00
|
|
|
// let sockets = orvibo.getConnectedSocket();
|
|
|
|
let sockets = [{name: "Plug1", state: 1, uid: "222222"},{name: "Plug2", state: 0, uid: "111111"}]
|
2019-07-15 23:18:30 +01:00
|
|
|
|
2019-07-15 23:03:18 +01:00
|
|
|
sockets.forEach(socket => {
|
|
|
|
switch(socket.state) {
|
|
|
|
case 1:
|
2019-07-16 16:58:42 +01:00
|
|
|
socket.state = 'OFF'
|
2019-07-15 23:03:18 +01:00
|
|
|
break;
|
|
|
|
case 0:
|
2019-07-16 16:58:42 +01:00
|
|
|
socket.state = 'ON'
|
2019-07-15 23:03:18 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-07-15 23:18:30 +01:00
|
|
|
const q = req.query
|
|
|
|
if (q.uid != undefined) {
|
|
|
|
orvibo.toggleSocket(q.uid);
|
|
|
|
}
|
|
|
|
|
2019-07-15 21:29:13 +01:00
|
|
|
res.render('index', {
|
|
|
|
title: 'Orvibo b25 Server',
|
|
|
|
sockets
|
|
|
|
})
|
|
|
|
});
|
2019-07-14 11:37:20 +01:00
|
|
|
|
2019-07-15 21:29:13 +01:00
|
|
|
const server = app.listen(3000, () => {
|
|
|
|
console.log(`Express running → PORT ${server.address().port}`);
|
2019-07-15 23:05:30 +01:00
|
|
|
});
|