{"size": 1000, "ext": "js", "max_stars_repo_path": "routes/sockets.js", "max_stars_repo_name": "Henri93/Senior-Design-2020", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "routes/sockets.js", "max_issues_repo_name": "Henri93/Senior-Design-2020", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "routes/sockets.js", "max_forks_repo_name": "Henri93/Senior-Design-2020", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.0, "max_line_length": 60, "alphanum_fraction": 0.601}
var Pump = require('../models/pump.js')

/*
 * Route for hub connecting
 */
var onHubConnect = function(io, data) {
	console.log("Hub connected");
	//add pumps to db
	var pumps = data["pumps"]
	for(var i = 0; i < pumps.length; i++){
	  	console.log("connecting pump " + pumps[i])
	  	var p = new Pump({
	    	port: pumps[i],
	    	isRunning: false
	  	})
	  	p.save(function(err) {
	    	if (err) {
		      	console.log(err)
	    	} 
	  	})
	}

	io.emit('hub_connect', data);
};

/*
 * Route for hub disconnecting
 */
var onHubDisconnect = function(io, data) {
	console.log("Hub disconnected");
	//remove pumps from db
	var pumps = data["pumps"]
	for(var i = 0; i < pumps.length; i++){
		console.log("disconnecting pump " + pumps[i])
		Pump.deleteOne({'port': pumps[i]}, function(err, result) {
	    if (err) {
	      console.log(err)
	    }
	  })
	}
	
	io.emit('hub_disconnect', data);
};

var sockets = { 
  hub_connect: onHubConnect,
  hub_disconnect: onHubDisconnect
};

module.exports = sockets;