init
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* File: app/helper.js
|
||||
* Author: Gerrit Linnemann
|
||||
*
|
||||
* Swiss-knife.
|
||||
*/
|
||||
|
||||
|
||||
// load the things we need
|
||||
var os = require('os')
|
||||
, dns = require('dns')
|
||||
, Log = require('./logging')
|
||||
, Crypto = require('crypto')
|
||||
, interfaceAddresses = require('interface-addresses');
|
||||
|
||||
|
||||
var app = null;
|
||||
var ConfReader = null;
|
||||
|
||||
|
||||
exports.init = function(express) {
|
||||
app = express;
|
||||
|
||||
ConfReader = app.get('ConfReader');
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
exports.timeSince = function timeSince(date) {
|
||||
|
||||
var seconds = Math.floor((new Date() - date) / 1000);
|
||||
|
||||
var interval = Math.floor(seconds / 31536000);
|
||||
|
||||
if (interval > 1) {
|
||||
return interval + " years";
|
||||
}
|
||||
interval = Math.floor(seconds / 2592000);
|
||||
if (interval > 1) {
|
||||
return interval + " months";
|
||||
}
|
||||
interval = Math.floor(seconds / 86400);
|
||||
if (interval > 1) {
|
||||
return interval + " days";
|
||||
}
|
||||
interval = Math.floor(seconds / 3600);
|
||||
if (interval > 1) {
|
||||
return interval + " hours";
|
||||
}
|
||||
interval = Math.floor(seconds / 60);
|
||||
if (interval > 1) {
|
||||
return interval + " minutes";
|
||||
}
|
||||
return Math.floor(seconds) + " seconds";
|
||||
}
|
||||
|
||||
exports.isResultAvailable = function(res) {
|
||||
return (
|
||||
typeof res !== 'undefined' &&
|
||||
res != null &&
|
||||
typeof res.result !== 'undefined' &&
|
||||
res.result != null
|
||||
);
|
||||
}
|
||||
|
||||
exports.isTypeAvailable = function(res) {
|
||||
return (
|
||||
typeof res !== 'undefined' &&
|
||||
res != null &&
|
||||
typeof res.type !== 'undefined' &&
|
||||
res.type != null
|
||||
);
|
||||
}
|
||||
|
||||
exports.isJSON = function(toCheck) {
|
||||
try {
|
||||
JSON.parse(str);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
exports.isDefinedAndNotNull = function(toCheck) {
|
||||
return (
|
||||
typeof toCheck !== 'undefined' &&
|
||||
toCheck != null
|
||||
);
|
||||
}
|
||||
|
||||
exports.cnt = function(db) {
|
||||
var cnt = 0;
|
||||
|
||||
if(db.constructor === Array) { // do it the array-way
|
||||
cnt = db.length;
|
||||
|
||||
} else { // oh boy, no array ...
|
||||
var cnt_ = 0;
|
||||
for (var key_ in db) { cnt_++; }
|
||||
cnt = cnt_;
|
||||
}
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
exports.each = function(db, callback) {
|
||||
if(db.constructor === Array) { // do it the array-way
|
||||
for(i=0; i < db.length; i++) {
|
||||
var entry = db[i];
|
||||
var isLast = ((i+1) == db.length);
|
||||
|
||||
callback(entry, isLast);
|
||||
}
|
||||
|
||||
} else { // oh boy, no array ...
|
||||
var cnt_ = 0;
|
||||
for (var key_ in db) { cnt_++; }
|
||||
|
||||
var cnt = 0;
|
||||
for (var key in db) {
|
||||
cnt++;
|
||||
var entry = db[key];
|
||||
|
||||
callback(entry, (cnt_==cnt));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
exports.checkValueEquality = function(foo, bar) {
|
||||
return doValueEqualityCheck(foo, bar);
|
||||
}
|
||||
|
||||
exports.simpleHash = function(foo) {
|
||||
var hash = Crypto.createHash('md5').update(foo).digest('hex');
|
||||
return hash;
|
||||
}
|
||||
|
||||
/* private */
|
||||
function doValueEqualityCheck(foo, bar) {
|
||||
var key;
|
||||
for(key in foo) {
|
||||
Log.debug( "Compare \"" + foo[key] + "\" with \"" + bar[key] + "\" for key \"" + key + "\"" );
|
||||
|
||||
if(typeof foo[key] === 'object' && typeof bar[key] === 'object') {
|
||||
var isEquals = doValueEqualityCheck(foo, bar);
|
||||
|
||||
if(!isEquals) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if(foo[key].localeCompare(bar[key]) != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* File: app/logging.js
|
||||
* Author: Gerrit Linnemann
|
||||
*
|
||||
* Logging with several log-level.
|
||||
* - debug: Printed only in development-state.
|
||||
* - log: Needed log-output.
|
||||
* - error: Prints log in error-level.
|
||||
* - inspect: Prints a message and a detail vier on an object.
|
||||
*/
|
||||
|
||||
|
||||
// load the things we need
|
||||
var Conf = require('../config.json')
|
||||
, Inspect = require('eyespect').inspector()
|
||||
, Helper = require('./helper.js')
|
||||
, util = require('util')
|
||||
, DateFormat = require('dateformat');
|
||||
|
||||
|
||||
|
||||
var app = null;
|
||||
var eyeFriendly = null;
|
||||
var isTrace = null;
|
||||
var isDebug = null;
|
||||
|
||||
|
||||
exports.init = function(express) {
|
||||
app = express;
|
||||
eyeFriendly = (Conf.logging.mode == 'friendly');
|
||||
isDebug = ('development' == app.get('env'));
|
||||
isTrace = (isDebug && Conf.logging.trace == 'yes');
|
||||
|
||||
Helper.init(app);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
exports.isEyeFriendlyMode = function() {
|
||||
return eyeFriendly;
|
||||
}
|
||||
|
||||
exports.isTrace = function() {
|
||||
return isTrace;
|
||||
}
|
||||
|
||||
exports.isDebug = function() {
|
||||
return isDebug;
|
||||
}
|
||||
|
||||
exports.trace = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " TRACE " + msg;
|
||||
|
||||
if(isTrace) {
|
||||
if(eyeFriendly) {
|
||||
util.puts(msg);
|
||||
} else {
|
||||
consoleOut(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.debug = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " DEBUG " + msg;
|
||||
|
||||
if(isDebug) {
|
||||
if(eyeFriendly) {
|
||||
util.puts(msg);
|
||||
} else {
|
||||
consoleOut(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.log = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " INFO " + msg;
|
||||
|
||||
if(eyeFriendly) {
|
||||
util.puts(msg);
|
||||
} else {
|
||||
consoleOut(msg);
|
||||
}
|
||||
}
|
||||
|
||||
exports.error = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " ERROR " + msg;
|
||||
|
||||
if(eyeFriendly) {
|
||||
util.error(msg);
|
||||
} else {
|
||||
consoleOut(msg);
|
||||
}
|
||||
}
|
||||
|
||||
exports.inspect = function(msg, content) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " INSPECT " + msg;
|
||||
|
||||
if(eyeFriendly) {
|
||||
Inspect(content, msg);
|
||||
} else {
|
||||
var msgOut = msg + (Helper.isDefinedAndNotNull(content) ? ": "+JSON.stringify(content) : "");
|
||||
consoleOut(msgOut);
|
||||
}
|
||||
}
|
||||
|
||||
/* private */
|
||||
function consoleOut(msg, content) {
|
||||
var hasContent = Helper.isDefinedAndNotNull(content);
|
||||
|
||||
if(hasContent) {
|
||||
var msgAddon = (hasContent ? " | " + content : "");
|
||||
if(Helper.isDefinedAndNotNull(msg)) {
|
||||
console.log(msg + msgAddon);
|
||||
}
|
||||
} else if(Helper.isDefinedAndNotNull(msg)) {
|
||||
console.log(msg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: app/radio.js
|
||||
* Author: Gerrit Linnemann
|
||||
*
|
||||
* Where the magic happens.
|
||||
*/
|
||||
|
||||
|
||||
// load the things we need
|
||||
//nothing yet
|
||||
|
||||
|
||||
var App = null;
|
||||
var Log = null
|
||||
var Helper = null;
|
||||
var Conf = null;
|
||||
|
||||
|
||||
exports.init = function(Express, Configuration) {
|
||||
App = Express;
|
||||
Conf = Configuration;
|
||||
Log = App.get('Log');
|
||||
Helper = App.get('Helper');
|
||||
|
||||
Log.debug('Radio: ' + exports.countChannels() + ' channels configured');
|
||||
Helper.each(Conf.channels, function(channel, isLast) {
|
||||
//Log.inspect('Channel', channel);
|
||||
Log.log('Radio: Found channel ' + channel.title);
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
exports.countChannels = function() {
|
||||
return Conf.channels.length;
|
||||
}
|
||||
|
||||
exports.getChannels = function() {
|
||||
return Conf.channels;
|
||||
}
|
||||
Reference in New Issue
Block a user