From 088c4dac3deef4db2928cfe3fa4722f70ac1f4d2 Mon Sep 17 00:00:00 2001 From: karl0ss Date: Mon, 15 Jul 2019 23:03:18 +0100 Subject: [PATCH] latest --- CHANGELOG.md | 7 ---- Dockerfile | 2 +- Example.js | 77 ---------------------------------------- dock.js | 84 -------------------------------------------- index.js | 2 -- public/css/style.css | 41 ++------------------- server.js | 19 +++++++++- views/index.pug | 16 +++++---- 8 files changed, 31 insertions(+), 217 deletions(-) delete mode 100644 Example.js delete mode 100644 dock.js delete mode 100644 index.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 79cf5ca..4aa8ca4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Dockerfile b/Dockerfile index f16a057..8ea849d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,4 +10,4 @@ COPY . . EXPOSE 3000 10001 -CMD [ "node", "Docker.js" ] +CMD [ "node", "server.js" ] diff --git a/Example.js b/Example.js deleted file mode 100644 index 7f70ef8..0000000 --- a/Example.js +++ /dev/null @@ -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}`) -}); \ No newline at end of file diff --git a/dock.js b/dock.js deleted file mode 100644 index e162df8..0000000 --- a/dock.js +++ /dev/null @@ -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 { - 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}`) -}); diff --git a/index.js b/index.js deleted file mode 100644 index 4857bd0..0000000 --- a/index.js +++ /dev/null @@ -1,2 +0,0 @@ -// Index file for npm -module.exports = require('./Orvibo'); \ No newline at end of file diff --git a/public/css/style.css b/public/css/style.css index ec60c09..e46f593 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -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; -} diff --git a/server.js b/server.js index 96ec7a4..afd4e83 100644 --- a/server.js +++ b/server.js @@ -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 diff --git a/views/index.pug b/views/index.pug index f4b5966..a4c04f7 100644 --- a/views/index.pug +++ b/views/index.pug @@ -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 \ No newline at end of file