This commit is contained in:
karl0ss 2019-07-15 23:03:18 +01:00
parent 87bf26a731
commit 088c4dac3d
8 changed files with 31 additions and 217 deletions

View File

@ -1,12 +1,5 @@
# CHANGELOG
## 1.1.0
* Add try-catch around packet decryption to guard against a service crash resulting from an occasional packet decryption error.
* Pluralize getConnectedSocket + alias previous name
* Enhance settings handling
* Enhance Orvibo logging
## 1.0.0
Initial release

View File

@ -10,4 +10,4 @@ COPY . .
EXPOSE 3000 10001
CMD [ "node", "Docker.js" ]
CMD [ "node", "server.js" ]

View File

@ -1,77 +0,0 @@
const Orvibo = require('./Orvibo');
const http = require('http');
const url = require('url');
const httpPort = 3000;
// Create a settings object to pass PK key and map sockets to names
const settings = {
LOG_PACKET: false, //Show incoming packet data from the socket
ORVIBO_KEY: '', // put your PK key here as plain text (See Readme)
plugInfo : [
// Add uid and a name so you can easily identify the connected sockets
{
uid :'53dd7fe74de7',
name: "Lamp in Kitchen"
},
],
};
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();
// Create a basic example HTTP server
// If there are no parameters it will return the uid, state, modelId and name of the socket
// You can then use the uid to toggle the state of the switch like
// http://localhost:3000?uid=5dcf7ff76e7a
const requestHandler = (request, response) => {
response.writeHead(200, {'Content-Type': 'application/json'});
let q = url.parse(request.url, true).query;
if (q.uid != null) {
orvibo.toggleSocket(q.uid);
}
// Get all currently connected sockets, their names and states
let sockets = orvibo.getConnectedSocket();
response.end(JSON.stringify(sockets));
};
const httpServer = http.createServer(requestHandler);
httpServer.listen(httpPort, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`http server is listening on ${httpPort}`)
});

84
dock.js
View File

@ -1,84 +0,0 @@
const Orvibo = require('./utils/Orvibo');
const http = require('http');
const url = require('url');
const httpPort = 3000;
const createArray = str => {
// split on each comma
const arr = str.split(',');
// put back elements by pairs
const pairs = [];
for (let i=0; i<arr.length; i+=2) {
let o = {};
o.uid = arr[i].split(':')[1];
o.name = arr[i+1].split(':')[1];
pairs.push(o);
}
return pairs;
}
// Create a settings object to pass PK key and map sockets to names
const settings = {
LOG_PACKET: true, //Show incoming packet data from the socket
ORVIBO_KEY: process.env.orviboPK,
plugInfo :
createArray(process.env.plugArray)
,
};
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();
// Create a basic example HTTP server
// If there are no parameters it will return the uid, state, modelId and name of the socket
// You can then use the uid to toggle the state of the switch like
// http://localhost:3000?uid=5dcf7ff76e7a
const requestHandler = (request, response) => {
response.writeHead(200, {'Content-Type': 'application/json'});
let q = url.parse(request.url, true).query;
if (q.uid != null) {
orvibo.toggleSocket(q.uid);
}
// Get all currently connected sockets, their names and states
let sockets = orvibo.getConnectedSocket();
response.end(JSON.stringify(sockets));
};
const httpServer = http.createServer(requestHandler);
httpServer.listen(httpPort, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`http server is listening on ${httpPort}`)
});

View File

@ -1,2 +0,0 @@
// Index file for npm
module.exports = require('./Orvibo');

View File

@ -23,9 +23,6 @@ body {
margin-bottom: 50px;
}
.container {
width: 960px;
margin: 0 auto;
@ -34,44 +31,18 @@ body {
grid-gap: 20px;
}
.person {
.socket {
text-align: center;
}
.person > * {
.socket > * {
margin-bottom: 5px;
}
.person-image {
.socket-image {
width: 100%;
height: 300px;
}
.profile {
display: flex;
height: 100vh;
width: 100%;
}
.profile-image {
height: 100%;
flex-grow: 1;
flex-basis: 50%;
}
.profile-details {
flex-basis: 30%;
flex-grow: 1;
padding: 50px;
display: flex;
justify-content: center;
flex-direction: column;
}
.profile-details > * {
margin-bottom: 20px;
}
.button {
height: 50px;
@ -81,9 +52,3 @@ body {
text-align: center;
font-size: 20px
}
.button-twitter {
color: #FFFFFF;
background-color: #659AF1;
border-radius: 5px;
}

View File

@ -59,7 +59,24 @@ orvibo.startServer();
app.get('/', (req, res) => {
let sockets = orvibo.getConnectedSocket();
const q = req.query
if (q.uid != null) {
orvibo.toggleSocket(q.uid);
}
sockets.forEach(socket => {
switch(socket.state) {
case 1:
socket.status = 'off'
break;
case 0:
socket.status = 'on'
break;
default:
}
});
res.render('index', {
title: 'Orvibo b25 Server',
sockets

View File

@ -2,11 +2,13 @@ extends default
block content
div.container
each modelId in sockets
div.person
h2.person-name
| #{modelId.name}
h2.person-name2
| #{modelId.uid}
a(href=`/?uid=${modelId.uid}`)
each socket in sockets
div.socket
h2.socket-name
| #{socket.name}
h2.socket-name2
| #{socket.uid}
h2.socket-name2
| #{socket.status}
a(href=`/?uid=${socket.uid}`)
| Toggle