process.title = 'resource-system';

var io = require('socket.io')(),
    config = require('../Common/js/configService.js'),
    fs = require('fs');

var port = io.listen(0).httpServer.address().port;
console.log("Has started server on port", port);

config.registerService(port, "resource-service");



io.on('connection', function(socket) {
    console.log('client', socket.id, 'connecting');
    notifySubscribers(socket, stations);
    notifySubscribers(socket, units);


    function getUnit(id) {
        for (var i = 0; i < units.length; i++) {
            if (units[i].id === id) {
                return units[i];
            }
        }
        return null;
    }
    socket.on('assignResourceToCase', function(unitId, caseId) {
        console.log("Asked to assign resource", unitId, "to case", caseId);
        var aCase = events[caseId];
        aCase && (events[caseId].resource = unitId);
        var unit = getUnit(unitId);

        if (unit) {
            //Temporary
            unit.status = "U";
            notifySubscribers(io.sockets, [unit]);

            console.log("Should now find route to", aCase.latitude, aCase.longitude);
            routeConsumer.emit('getRouteForId', unit, aCase, unitId);
        }
    });

});

var events = {};

function processEvent(event) {
    events[event.id] = event;
}

var eventConsumer = require('../Common/js/serviceConsumer')('event-service', process.title,
    {
        'event': processEvent
    },
    true
);

function processRouteForId(id, route) {
    console.log("Received route for id", id, "==>", route);
    console.log(JSON.stringify(route));
}

var routeConsumer = require('../Common/js/serviceConsumer')('route-service', process.title,
    {
        'routeForId': processRouteForId
    },
    true
);

var stations = [];
var units    = [];


var notifySubscribers = function (sockets, resources) {
    sockets.emit('resourcesUpdated', resources);
};

var getStation = function (name, stationList) {
    var found = stationList.filter(function(station) { return name == station.name;});
    return found.length > 0 && found[0];
};

var calculateStartPositions = function (unitList, stationList) {
    unitList.forEach( function(unit) {
        var station = getStation(unit.homeStation, stationList);
        if (!station) {
            console.error("No station found for unit", unit);
        } else {
            unit.latitude = station.latitude;
            unit.longitude = station.longitude;
        }
    });
};

var readData = function (filename) {
    fs.readFile(filename, 'utf8', function (err, data) {
        if (err) { throw err;}
        var data = JSON.parse(data);

        carcounter = 0;
        stations = data.stations;
        units    = data.units;
        console.log("Has read data file", filename, stations.length, "stations and", units.length, "ambulances");
        notifySubscribers(io.sockets, stations);
        calculateStartPositions(units, stations);
        notifySubscribers(io.sockets, units);
    });
};

readData('resources.json');

@@ -1318,24 +1318,26 @@
 = event;%0A%7D%0A%0A
+//
 var eventCon
@@ -1335,32 +1335,36 @@
  eventConsumer =
+%0A   
  require('../Com
@@ -2627,20 +2627,16 @@
         
-var 
 data = J
@@ -2661,16 +2661,18 @@
         
+//
 carcount

remove some warning