Skip to content

Commit

Permalink
Working on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Apr 17, 2019
1 parent eebc9ff commit 5c00028
Show file tree
Hide file tree
Showing 13 changed files with 505 additions and 323 deletions.
18 changes: 10 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
app = express(),
//jsonStore = reqlib('/lib/jsonStore.js'),
jsonStore = reqlib('/lib/jsonStore.js'),
cors = require('cors'),
//store = reqlib('config/store.js'),
store = reqlib('config/store.js'),
debug = reqlib('/lib/debug')('App'),
utils = reqlib('/lib/utils.js');

Expand All @@ -29,17 +29,19 @@ app.use(cors());

//get settings
app.get('/api/settings', function(req, res) {
var clients = [];
res.json({success:true, clients: clients});
res.json({success:true, clients: jsonStore.get(store.clients), values: jsonStore.get(store.values)});
});

//get clients
app.get('/api/clients', function(req, res) {
res.json({success:true, clients: jsonStore.get(store.clients)});
});

//update settings
app.post('/api/settings', function(req, res) {
jsonStore.put(store.settings, req.body)
app.post('/api/clients', function(req, res) {
jsonStore.put(store.clients, req.body.clients)
.then(data => {
res.json({success: true, message: "Configuration updated successfully"});
gw.close();
startGateway();
}).catch(err => {
debug(err);
res.json({success: false, message: err.message})
Expand Down
181 changes: 96 additions & 85 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,99 @@
* Module dependencies.
*/

var app = require('../app');
var reqlib = require('app-root-path').require;
var debug = reqlib('/lib/debug')('App');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '8100');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
var app = require('../app')
var reqlib = require('app-root-path').require
var store = reqlib('/config/store.js')
var debug = reqlib('/lib/debug')('App')
var conf = reqlib('/config/app.js')
var jsonStore = reqlib('/lib/jsonStore.js')
var http = require('http')

// jsonstore is a singleton instance that handles the json configuration files
// used in the application. Init it before anything else than start app.
// if jsonstore fails exit the application
jsonStore.init(store)
.then((data) => {

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string' ?
'Pipe ' + port :
'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string' ?
'pipe ' + addr :
'port ' + addr.port;
debug('Listening on ' + bind);
}

/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || conf.port);
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

})
.catch(err => debug(err))
5 changes: 5 additions & 0 deletions config/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// config/app.js
module.exports = {
'storeDir': 'store',
'port': 8100
};
5 changes: 5 additions & 0 deletions config/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// config/store.js
module.exports = {
'clients' : {file: 'clients.json', default: []} ,
'values' : {file: 'values.json', default: []} ,
};
67 changes: 67 additions & 0 deletions lib/jsonStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

// eslint-disable-next-line one-var
var jsonfile = require('jsonfile'),
reqlib = require('app-root-path').require,
storeDir = reqlib('config/app.js').storeDir,
Promise = require('bluebird'),
debug = reqlib('/lib/debug')('Store'),
utils = reqlib('lib/utils.js')

debug.color = 3

function getFile (config) {
return new Promise((resolve, reject) => {
jsonfile.readFile(utils.joinPath(utils.getPath(true), storeDir, config.file), function (err, data) {
if (err && err.code !== 'ENOENT') {
reject(err)
} else {
if (err && err.code === 'ENOENT') { debug(config.file, 'not found') }

resolve({file: config.file, data: data || config.default})
}
})
})
}

/**
Constructor
**/
function StorageHelper () {
this.store = {}
}

StorageHelper.prototype.init = function (config) {
return new Promise((resolve, reject) => {
storage_helper.config = config
Promise.map(Object.keys(config), function (model) {
return getFile(config[model])
}).then(results => {
for (var i = 0; i < results.length; i++) {
storage_helper.store[results[i].file] = results[i].data
}
resolve(storage_helper.store)
})
.catch(err => reject(err))
})
}

StorageHelper.prototype.get = function (model) {
if (storage_helper.store[model.file]) { return storage_helper.store[model.file] } else { throw Error('Requested file not present in store: ' + model.file) }
}

StorageHelper.prototype.put = function (model, data) {
return new Promise((resolve, reject) => {
jsonfile.writeFile(utils.joinPath(utils.getPath(true), storeDir, model.file), data, function (err) {
if (err) {
reject(err)
} else {
storage_helper.store[model.file] = data
resolve(storage_helper.store[model.file])
}
})
})
}

// eslint-disable-next-line camelcase
var storage_helper = module.exports = exports = new StorageHelper()
17 changes: 15 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"ejs": "~2.5.7",
"express": "~4.16.0",
"http-errors": "~1.6.2",
"jsonfile": "^5.0.0",
"morgan": "~1.9.0",
"vue": "^2.5.2",
"vue-router": "^3.0.1",
Expand Down
25 changes: 23 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<v-app>
<v-app dark>

<v-navigation-drawer
clipped-left
Expand Down Expand Up @@ -73,6 +73,8 @@

<script>
import ConfigApis from "@/apis/ConfigApis";
export default {
name: 'app',
methods: {
Expand Down Expand Up @@ -152,11 +154,30 @@ export default {
],
drawer: false,
topbar: [],
title: 'Configuration',
title: 'MQTT Clients',
mini: true,
snackbar: false,
snackbarText: ""
}
},
mounted() {
var self = this;
ConfigApis.getSettings()
.then(data => {
if (!data.success) {
self.showSnackbar(
"Error while retriving settings, check console"
);
console.log(response);
} else {
self.$store.dispatch("init", data);
}
})
.catch(e => {
self.showSnackbar("Error while retriving settings, check console");
console.log(e);
});
},
watch: {
'$route': function(value) {
Expand Down
Loading

0 comments on commit 5c00028

Please sign in to comment.