Browse Source

Hoist TM sensor code to a separate file on the server side (uglily).

master
flabbergast 8 years ago
parent
commit
2a17e26176
  1. 37
      modules/sensors/TM.js
  2. 8
      modules/sensors/index.js
  3. 17
      modules/socket_server.js

37
modules/sensors/TM.js

@ -0,0 +1,37 @@
/*
* TM.js
* temperature sensor / server side handling code
*/
/* functions that are required:
* - init(socket) : bind some listeners to the socket should some interaction
* with the main page be required
* - onUserConnected(socket) : serve data over the socket when someone
* views the main page
* - onDataOverSerial(sockets, message) : process the message received over
* the serial connection and send the update over the socket
*/
var TMtemp = "??";
var TMbatt = "??";
exports.init = function(socket){
};
exports.onUserConnected = function(socket){
socket.emit('received-TM-temp', { content: ("Temp: "+TMtemp+" ºC") });
socket.emit('received-TM-batt', { content: ("Batt: "+TMbatt+" V") });
};
exports.onDataOverSerial = function(sockets,message){
if(message.substring(0,4) == "TMPA") {
TMtemp = message.substring(4,9);
sockets.emit('received-TM-temp',
{ content: ("Temp: "+TMtemp+" ºC")});
}
if(message.substring(0,4) == "BATT") {
TMbatt = message.substring(4,9);
sockets.emit('received-TM-batt',
{ content: ("Batt: "+TMbatt+" V")});
}
};

8
modules/sensors/index.js

@ -0,0 +1,8 @@
// Load `*.js` under current directory as properties
// i.e., `User.js` will become `exports['User']` or `exports.User`
require('fs').readdirSync(__dirname + '/').forEach(function(file) {
if (file.match(/.+\.js/g) !== null && file !== 'index.js') {
var name = file.replace('.js', '');
exports[name] = require('./' + file);
}
});

17
modules/socket_server.js

@ -12,8 +12,7 @@ var serport = require('./serial_node');
var lhelper = require('./llap_helper');
var logger = require('./log_backend');
var activeUser;
var TMtemp = "??";
var TMbatt = "??";
var sensors = require('./sensors');
exports.init = function(io){
@ -53,16 +52,7 @@ exports.onDataOverSerial = function(data){
var message = lhelper.message(msg);
switch (lhelper.deviceName(msg)) {
case "TM":
if(message.substring(0,4) == "TMPA") {
TMtemp = message.substring(4,9);
sockets.emit('received-TM-temp',
{ content: ("Temp: "+TMtemp+" ºC")});
}
if(message.substring(0,4) == "BATT") {
TMbatt = message.substring(4,9);
sockets.emit('received-TM-batt',
{ content: ("Batt: "+TMbatt+" V")});
}
sensors.TM.onDataOverSerial(sockets,message);
break;
default:
// let all the clients know about the message|
@ -77,8 +67,7 @@ exports.onDataOverSerial = function(data){
var onUserConnected = function(socket)
{
serport.writeNumber(socket.user.id);
socket.emit('received-TM-temp', { content: ("Temp: "+TMtemp+" ºC") });
socket.emit('received-TM-batt', { content: ("Batt: "+TMbatt+" V") });
sensors.TM.onUserConnected(socket);
logger.emit_latest_logs(socket);
socket.on('requesting-logs-refresh', function (data) {

Loading…
Cancel
Save