Skip to content

Commit

Permalink
done some code formatting optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdpunkt committed Apr 5, 2014
1 parent 0fffd01 commit c9506b9
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.out
*.pid
*.gz
*.off
.idea

.idea/
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Aquastream XT application for Linux
## Introduction

This is a Node.js application for displaying Aquastream XT pump information, such as water temperature, flow, current frequency, etc.
It is built on top of [node-aquastreamxt-api](node-aquastreamxt-api), which is a native Node.js addon I wrote for communicating with the hardware.
It is built on top of [node-aquastreamxt-api](https://github.com/adick/node-aquastreamxt-api), which is a native Node.js addon I wrote for communicating with the hardware.

Currently it is *read-only*, so you can't change any settings yet. I still have some problems with writing data to the device but this is planned to be supported some day.

Expand All @@ -31,7 +31,7 @@ This guide is for Debian based distros (Debian, Ubuntu. LMDE/Mint, ...) so you m

$ npm install

This installs all required dependencies (including [node-aquastreamxt-api](node-aquastreamxt-api)).
This installs all required dependencies (including [node-aquastreamxt-api](https://github.com/adick/node-aquastreamxt-api)).


### Done!
Expand All @@ -41,7 +41,7 @@ Run the app with

and visit [http://localhost:8080](http://localhost:8080) - you should see the interface:

![screenshot](https://github.com/adick/node-aquastreamxt/master/screenshot.png)
![screenshot](https://raw.githubusercontent.com/adick/node-aquastreamxt/master/screenshot.png)


## Troubleshooting
Expand Down
8 changes: 4 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ conf = require('nconf');
fs = require('fs');

var express = require('express'),
http = require('http'),
socketio = require('socket.io'),
routes = require('./routes'),
path = require('path');
http = require('http'),
socketio = require('socket.io'),
routes = require('./routes'),
path = require('path');

// Load config file
conf.file({file: 'config.json'});
Expand Down
172 changes: 89 additions & 83 deletions public/js/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,44 @@
* @author Alexander Dick <[email protected]>
*/

jQuery(function() {
jQuery(function () {

/**
* Register Handlebars Helpers
*/
for(var h in Helper.Handlebars)
for (var h in Helper.Handlebars)
Handlebars.registerHelper(h, Helper.Handlebars[h]);


/**
* Pump data and info tables
*/
var pumpDataTemplate =
'<h3>Current values</h3>\
<table class="table table-condensed">\
<tr><th>Frequency</th><td>{{frequency}} Hz</td></tr>\
<tr><th>Max. Frequency</th><td>{{frequencyMax}} Hz</td></tr>\
<tr><th>Power Consumption</th><td>{{pumpCurrent}} mA</td></tr>\
<tr><th>Power</th><td>{{round pumpPower}} W</td></tr>\
<tr><th>Voltage</th><td>{{round voltage}} V</td></tr>\
</table>';

pumpDataTemplate = Handlebars.compile(pumpDataTemplate);

var pumpInfoTemplate =
'<h3>Hardware</h3>\
<table class="table table-condensed">\
<tr><th>Pump Mode</th><td>{{pumpMode mode}}</td></tr>\
<tr><th>Firmware</th><td>{{hardware.firmware}}</td></tr>\
<tr><th>Hardware</th><td>{{hardware.hardware}}</td></tr>\
<tr><th>Serial</th><td>{{hardware.serial}}</td></tr>\
<tr><th>Public Key</th><td>{{join hardware.publicKey}}</td></tr>\
</table>';


pumpInfoTemplate = Handlebars.compile(pumpInfoTemplate);
var pumpInfoHtml = pumpInfoTemplate(pumpData);
jQuery('#pump-info').html(pumpInfoHtml);
var pumpDataTemplate =
'<h3>Current values</h3>\
<table class="table table-condensed">\
<tr><th>Frequency</th><td>{{frequency}} Hz</td></tr>\
<tr><th>Max. Frequency</th><td>{{frequencyMax}} Hz</td></tr>\
<tr><th>Power Consumption</th><td>{{pumpCurrent}} mA</td></tr>\
<tr><th>Power</th><td>{{round pumpPower}} W</td></tr>\
<tr><th>Voltage</th><td>{{round voltage}} V</td></tr>\
</table>';

pumpDataTemplate = Handlebars.compile(pumpDataTemplate);

var pumpInfoTemplate =
'<h3>Hardware</h3>\
<table class="table table-condensed">\
<tr><th>Pump Mode</th><td>{{pumpMode mode}}</td></tr>\
<tr><th>Firmware</th><td>{{hardware.firmware}}</td></tr>\
<tr><th>Hardware</th><td>{{hardware.hardware}}</td></tr>\
<tr><th>Serial</th><td>{{hardware.serial}}</td></tr>\
<tr><th>Public Key</th><td>{{join hardware.publicKey}}</td></tr>\
</table>';


pumpInfoTemplate = Handlebars.compile(pumpInfoTemplate);
var pumpInfoHtml = pumpInfoTemplate(pumpData);
jQuery('#pump-info').html(pumpInfoHtml);

var pumpSettingsTemplate =
'<h3>Settings</h3>\
Expand All @@ -51,7 +51,6 @@ jQuery(function() {
<tr><th>Deaeration mode</th><td>{{deaerationMode pumpMode}}</td></tr>\
</table>';


pumpSettingsTemplate = Handlebars.compile(pumpSettingsTemplate);
var pumpSettingsHtml = pumpSettingsTemplate(pumpSettings);
jQuery('#pump-settings').html(pumpSettingsHtml);
Expand All @@ -60,20 +59,20 @@ jQuery(function() {
/**
* Save Flow Sensor settings on change
*/
jQuery('#impulses-per-liter').on('change', function() {
jQuery('#impulses-per-liter').on('change', function () {
jQuery.post('/config', {
key: 'aquastreamXt:flowSensor:impulsesPerLiter',
value: this.value
}, function(conf) {
}, function (conf) {
// overwrite config with updated values
window.config = conf;
}, 'json');
});
jQuery('#measuring-impulses').on('change', function() {
jQuery('#measuring-impulses').on('change', function () {
jQuery.post('/config', {
key: 'aquastreamXt:flowSensor:measuringImpulses',
value: this.value
}, function(conf) {
}, function (conf) {
// overwrite config with updated values
window.config = conf;
}, 'json');
Expand Down Expand Up @@ -112,9 +111,9 @@ jQuery(function() {
}
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%d.%m.%Y %H:%M:%S', this.x) +'<br/>'+
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%d.%m.%Y %H:%M:%S', this.x) + '<br/>' +
Highcharts.numberFormat(this.y, 1);
}
},
Expand All @@ -124,7 +123,7 @@ jQuery(function() {
},
plotOptions: {
area: {
lineWidth : 1,
lineWidth: 1,
lineColor: '#666666',
marker: {
enabled: true,
Expand All @@ -139,71 +138,78 @@ jQuery(function() {
}
},

series: [{
name: 'Flow',
type: 'area',
data: [{x: new Date().getTime(), y: 0}],
color: '#b3c58d',
fillColor : {
linearGradient : [0, 0, 0, 300],
stops : [
[0, 'rgba(137,165,78,.5)'],
[1, 'rgba(137,165,78,1)']
]
}
}, {
name: 'Water Temperature',
type: 'area',
data: [{x: new Date().getTime(), y: 0}],
color: '#948ddf',
fillColor : {
linearGradient: [0, 0, 0, 300],
stops : [
[0, 'rgba(146,146,235,.5)'],
[1, 'rgba(146,146,235,1)']
]
series: [
{
name: 'Flow',
type: 'area',
data: [
{x: new Date().getTime(), y: 0}
],
color: '#b3c58d',
fillColor: {
linearGradient: [0, 0, 0, 300],
stops: [
[0, 'rgba(137,165,78,.5)'],
[1, 'rgba(137,165,78,1)']
]
}
},
{
name: 'Water Temperature',
type: 'area',
data: [
{x: new Date().getTime(), y: 0}
],
color: '#948ddf',
fillColor: {
linearGradient: [0, 0, 0, 300],
stops: [
[0, 'rgba(146,146,235,.5)'],
[1, 'rgba(146,146,235,1)']
]
}
}
}]
]
});

/**
* Update charts and controls periodically
*/
var socket = io.connect();
var maxPointsPerSeries = (config.ui.chart.period / (config.ui.updateInterval / 1000)) * config.ui.chart.period;
var numSeries = chart.series.length;
var socket = io.connect();
var maxPointsPerSeries = (config.ui.chart.period / (config.ui.updateInterval / 1000)) * config.ui.chart.period;
var numSeries = chart.series.length;

var updateUi = function(data) {
var updateUi = function (data) {

// Remove points older than config.ui.chart.period
var numPoints = chart.series[0].points.length - 1;
if(numPoints == maxPointsPerSeries) {
for(var i = 0; i < numSeries; i++)
if (numPoints == maxPointsPerSeries) {
for (var i = 0; i < numSeries; i++)
chart.series[i].points.shift().remove();
}

// Add new points
var time = new Date().getTime();

var flow = data.current.flow;
if(flow) {
flow = Helper.convert.flow(
flow,
config.aquastreamXt.flowSensor.impulsesPerLiter,
config.aquastreamXt.flowSensor.measuringImpulses
);
flow = Helper.round(flow, 1);
var flowPlot = [time, flow];
chart.series[0].addPoint(flowPlot, true, false);
}

var waterTemp = Helper.round(data.current.temperature.water, 1);
var waterTempPlot = [time, waterTemp];
if (flow) {
flow = Helper.convert.flow(
flow,
config.aquastreamXt.flowSensor.impulsesPerLiter,
config.aquastreamXt.flowSensor.measuringImpulses
);
flow = Helper.round(flow, 1);
var flowPlot = [time, flow];
chart.series[0].addPoint(flowPlot, true, false);
}

var waterTemp = Helper.round(data.current.temperature.water, 1);
var waterTempPlot = [time, waterTemp];
chart.series[1].addPoint(waterTempPlot, true, false);

// Update data table with current data
var pumpDataHtml = pumpDataTemplate(data.current);
jQuery('#pump-data').html(pumpDataHtml);
// Update data table with current data
var pumpDataHtml = pumpDataTemplate(data.current);
jQuery('#pump-data').html(pumpDataHtml);
};

// Initial update with current pumpData assigned in layout.jade
Expand Down
32 changes: 16 additions & 16 deletions public/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

var Helper = {

round: function(number, decimals) {
round: function (number, decimals) {

if (arguments.length == 1)
return Math.round(number);
Expand All @@ -19,60 +19,60 @@ var Helper = {

format: {

current: function(val) {
current: function (val) {
return parseInt(val) + ' mA';
},

frequency: function(val) {
frequency: function (val) {
return parseInt(val) + ' Hz';
},

power: function(val) {
power: function (val) {
return Helper.round(val, 2) + ' W';
},

voltage: function(val) {
voltage: function (val) {
return Helper.round(val, 2) + ' V';
}

},

convert: {

flow: function(rawFlow, impulsesPerLiter, measuringImpulses) {
if(rawFlow >= 600000) {
flow: function (rawFlow, impulsesPerLiter, measuringImpulses) {
if (rawFlow >= 600000) {
return 0;
} else {
var time = 46875 * 3600;
var cal = (measuringImpulses / 2) / impulsesPerLiter;
var time = 46875 * 3600;
var cal = (measuringImpulses / 2) / impulsesPerLiter;
return time / (rawFlow / cal);
}
}

},

Handlebars: {
round: function(val) {
round: function (val) {
val = Handlebars.Utils.escapeExpression(val);
return Helper.round(val, 2);
},
join: function(val) {
join: function (val) {
return val.join(':');
},
pumpMode: function(mode) {
pumpMode: function (mode) {

if(mode.aquastreamModeUltra)
if (mode.aquastreamModeUltra)
return 'Ultra';

if(mode.aquastreamModeAdvanced)
if (mode.aquastreamModeAdvanced)
return 'Advanced';

return 'Standard';
},
frequencyMode: function(pumpMode) {
frequencyMode: function (pumpMode) {
return pumpMode.autoPumpMaxFrequency ? 'auto' : 'manual';
},
deaerationMode: function(pumpMode) {
deaerationMode: function (pumpMode) {
return pumpMode.deaeration ? 'on' : 'off';
}
}
Expand Down

0 comments on commit c9506b9

Please sign in to comment.