<commit_before>#!/usr/bin/env node

var express = require('express');
var http = require('http')
var path = require('path');
var jade = require('jade');
var fs = require('fs');

var httpreq = require('httpreq');

var utils = require('./utils');

var net = require('net');
var socketio = require('socket.io');
var _ = require('underscore');

var Arduino = require('./arduino');
var arduino = new Arduino();


var settingsdb = require('./settingsdb');
var settings = {};

settingsdb.load(function (err, _settings) {
	settings = _settings;
});





// Webserver:

var app = express();

app.configure(function(){
	app.set('port', process.env.PORT || 3000);
	app.set('views', __dirname + '/views');
	app.set('view engine', 'jade');
	app.use(express.favicon());
	// app.use(express.logger('dev'));
	app.use(express.bodyParser());
	app.use(express.methodOverride());
	app.use(app.router);
	app.use(require('stylus').middleware(__dirname + '/public'));
	app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
	app.use(express.errorHandler());
});

var webserver = http.createServer(app).listen(app.get('port'), function(){
	console.log("Express server listening on port " + app.get('port') + " - v 0.1.0");
});


// Socket io:
var io = socketio.listen(webserver);
io.set('log level', 0);

io.sockets.on('connection', function (socket) {
	socket.on('disconnect', function() {
		console.log('> user disconnected, disabling soundgraph monitoring');
		arduino.disableSoundgraphMonitoring();
	});
});


// Arduino:

arduino.on('soundlevelChanged', function (soundlevel) {
	// console.log('soundlevelChanged', soundlevel);

	// send to UI:
	io.sockets.emit('soundlevelChanged', soundlevel);
});

arduino.on('soundstateChanged', function (state) {
	// state.channel;
	// state.soundstate;

	// console.log('soundstateChanged', state);


	// check if we need to send a http event to somewhere:
	var channel = _.find(settings.channels, function (channel) {
		return channel.id == state.channel;
	});


	if(channel.onsoundevent_enabled) {

		var json = null;
		try{
			json = JSON.parse(channel.onsoundevent_body);
		}catch(e){}

		if(json){
			httpreq[channel.onsoundevent_type](channel.onsoundevent_endpoint, {json: json}, function (err, res) {
				if(err) return console.log('error when sending http event for channel ' + state.channel, err);
			});
		}
	}

	if(channel.onsilenceevent_enabled) {

		var json = null;
		try{
			json = JSON.parse(channel.onsilenceevent_body);
		}catch(e){}

		if(json){
			httpreq[channel.onsilenceevent_type](channel.onsilenceevent_endpoint, {json: json}, function (err, res) {
				if(err) return console.log('error when sending http event for channel ' + state.channel, err);
			});
		}
	}



	// send to UI:
	io.sockets.emit('soundstateChanged', state);
});

arduino.on('incommingSettings', function (channel_arduino) {
	// console.log('incommingSettings', channel_arduino);

	// check if channel settings are different from local channel settings:
	var channel_local = _.find(settings.channels, function (channel) {
		return channel.id == channel_arduino.id;
	});

	// console.log('--------------------------');
	// console.log(channel_local);
	// console.log(channel_arduino);

	var isDifferent = false;

	if(channel_local.silenceThreshold != channel_arduino.silenceThreshold)
		isDifferent = true;

	if(channel_local.soundThreshold != channel_arduino.soundThreshold)<commit_msg>added try catch to external event (just to be sure)<commit_after>#!/usr/bin/env node

var express = require('express');
var http = require('http')
var path = require('path');
var jade = require('jade');
var fs = require('fs');

var httpreq = require('httpreq');

var utils = require('./utils');

var net = require('net');
var socketio = require('socket.io');
var _ = require('underscore');

var Arduino = require('./arduino');
var arduino = new Arduino();


var settingsdb = require('./settingsdb');
var settings = {};

settingsdb.load(function (err, _settings) {
	settings = _settings;
});





// Webserver:

var app = express();

app.configure(function(){
	app.set('port', process.env.PORT || 3000);
	app.set('views', __dirname + '/views');
	app.set('view engine', 'jade');
	app.use(express.favicon());
	// app.use(express.logger('dev'));
	app.use(express.bodyParser());
	app.use(express.methodOverride());
	app.use(app.router);
	app.use(require('stylus').middleware(__dirname + '/public'));
	app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
	app.use(express.errorHandler());
});

var webserver = http.createServer(app).listen(app.get('port'), function(){
	console.log("Express server listening on port " + app.get('port') + " - v 0.1.0");
});


// Socket io:
var io = socketio.listen(webserver);
io.set('log level', 0);

io.sockets.on('connection', function (socket) {
	socket.on('disconnect', function() {
		console.log('> user disconnected, disabling soundgraph monitoring');
		arduino.disableSoundgraphMonitoring();
	});
});


// Arduino:

arduino.on('soundlevelChanged', function (soundlevel) {
	// console.log('soundlevelChanged', soundlevel);

	// send to UI:
	io.sockets.emit('soundlevelChanged', soundlevel);
});

arduino.on('soundstateChanged', function (state) {
	// state.channel;
	// state.soundstate;

	// console.log('soundstateChanged', state);

	io.sockets.emit('soundstateChanged', state);


	// check if we need to send a http event to somewhere:
	var channel = _.find(settings.channels, function (channel) {
		return channel.id == state.channel;
	});

	try{
		if(channel.onsoundevent_enabled) {

			var json = null;
			try{
				json = JSON.parse(channel.onsoundevent_body);
			}catch(e){}

			if(json){
				httpreq[channel.onsoundevent_type](channel.onsoundevent_endpoint, {json: json}, function (err, res) {
					if(err) return console.log('error when sending http event for channel ' + state.channel, err);
				});
			}
		}

		if(channel.onsilenceevent_enabled) {

			var json = null;
			try{
				json = JSON.parse(channel.onsilenceevent_body);
			}catch(e){}

			if(json){
				httpreq[channel.onsilenceevent_type](channel.onsilenceevent_endpoint, {json: json}, function (err, res) {
					if(err) return console.log('error when sending http event for channel ' + state.channel, err);
				});
			}
		}
	}catch(e){}



	// send to UI:

});

arduino.on('incommingSettings', function (channel_arduino) {
	// console.log('incommingSettings', channel_arduino);

	// check if channel settings are different from local channel settings:
	var channel_local = _.find(settings.channels, function (channel) {
		return channel.id == channel_arduino.id;
	});

	// console.log('--------------------------');
	// console.log(channel_local);
	// console.log(channel_arduino);

	var isDifferent = false;

	if(channel_local.silenceThreshold != channel_arduino.silenceThreshold)
		isDifferent = true;

	if(channel_local.soundThreshold != channel_arduino.soundThreshold)
{'fraction_non_alphanumeric': 0.17660550458715596, 'fraction_numerical': 0.0022935779816513763, 'mean_word_length': 4.524148851939826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1020400', 'n_tokens_mistral': 2417, 'n_tokens_neox': 2234, 'n_words': 480}