<commit_before>var routeHandling = require('./routeHandling.js');
var unitMap = {};

function getUnit(id) {
    return unitMap[id];
}

function addUnit(unit) {
    return unitMap[unit.id] = unit;
}

function processRouteForId(id, route) {
    console.log("Received route for id", id, "==>", route);
    //console.log(JSON.stringify(route));
    var steps = routeHandling.convertGoogleRoute(route.routes);

    var unit = getUnit(id);
    unit.route.steps = steps;
    if (!unit.atSiteTime) {
        unit.atSiteTime = new Date(unit.acknowledgedTime.getTime() + steps[steps.length - 1].time * 1000);
        unit.loadedTime = new Date(unit.atSiteTime.getTime() + 10 * 60 * 1000);
        unit.route.startTime = unit.acknowledgedTime;
    } else {
        unit.atHospitalTime = new Date(unit.loadedTime.getTime() + steps[steps.length - 1].time * 1000);
        unit.route.startTime = unit.loadedTime;
    }
}

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

function moveUnitForTime(unit, time) {
    // console.log('unit', unit.name, unit.routing);
    var result = null;
    if (time) {
        //console.log('looking at unit', unit, 'for move');
        var elapsedTime = (time.getTime() - unit.route.startTime.getTime()) / 1000;
        if (elapsedTime > 0 && unit.route.steps.length >= 0) {
            console.log('Will move', unit.name, 'steps(', unit.route.steps.length, ')', elapsedTime);
            for (var i = unit.route.steps.length - 1 ; i >= 0 ; i--) {
                var aStep = unit.route.steps[i];
                if (elapsedTime > aStep.time) {
                    //console.log('Will move', unit.name, 'to', aStep);

                    unit.latitude = aStep.latitude;
                    unit.longitude = aStep.longitude;
                    result = unit;
                    if (i == unit.route.steps.length - 1) {
                        console.log('Vehicle', unit.name, 'moved to target location');
                        unit.route.steps = [];
                    }
                    break;
                }
            }
        } else {
            console.log('Unit', unit.name, 'is out of time');
        }
    }
    return result;
}


var copyProperties = function(src, dest) {
    for (var key in src) {
        if (src.hasOwnProperty(key)) {
            dest[key] = src[key];
        }
    }
};

var index = [
    'Förgiftning, överdos',
    'Bröstsmärtor/Hjärtsjukdom',
    'Extremitet/Sårskador/Mindre trauma',
    'Misstanke om sjukvårdsbehov',
    'Olyckor (Trauma)',
    'Livsfara - (temporär indexering)',
    'Våld-misshandel',
    'Medvetslös-vuxen',
    'Osäkra uppgifter/Svårt sjuk patient',
    'Liggande sjuktransport',
    'Buk/urinvägar',
    'Slaganfall (Stroke)-förlamningar',
    'Andningssvårigheter',
    'Blödning. ej trauma',
    'Huvudvärk. Yrsel',
    'Graviditet/förlossning (från v.20)',
    'Beställt uppdrag mellan vårdenheter',
    'Gyn-graviditet (före v.20)',
    'O-Interntransporter SU',
    'Diabetes',
    'Krampanfall',
    'Feber',
    'Medvetslös-barn',
    'Brännskada/Elskada',
    'Barn-sjukdom',
    'Ryggbesvär',
    'Suicidmisstanke-psykiatri',
    'Allergi',
    'Webbeställning (2.0)',
    'Ögon-öron-näsa-hals',
    'Hypo/Hypertermi',
    'Drunkningstillbud',
    'Barn-förgiftning',
    'Kemikalier-gaser',
    'Räddningsuppdrag',
    'Djurbett/Insektsstick',
    'Dykeriolycka',
    'Övrigt',
    'Helikopter 5920'];

var sahlgrenska = {latitude: 57.683088,longitude: 11.959884}; // Ej barn eller ortopedi
var molndal     = {latitude: 57.661244,longitude: 12.012948}; // Medicin och ortoped, ej barn, kirurg, trauma
var ostra       = {latitude: 57.7206,  longitude: 12.0535};   // Barn, medicin och kirurgi, ej vissa trauma och ortoped (men barnortopedi)
var kungalv     = {latitude: 57.878,   longitude: 11.969};    // medicin, kirurgi, ortoped (vissa trauma) ej barn)

var getLocationForCase = function(aCase) {

};


var Unit = function(args) {
    var that    = this;
    that.id     = -1;
    that.type   = 'A';
    that.status = 'K';
    that.route  = {
        startTime: null,
        steps: null
    };
    that.assignedTime = null;
    that.acknowledgedTime = null;
    that.atSiteTime = null;
    that.loadedTime = null;
    that.atHospitalTime = null;
    that.currentCase = null;

    if (args.constructor === Array) {
        args.forEach(function(unit, index, list){
            list[index] = new Unit(unit);
        });
        return args;
    } else {
        copyProperties(args, this);
    }

    that.atSite = function() {
        that.status = 'F';
        that.route.steps = null;
    };

    that.moveToCase = function() {
        that.route.steps = null;
        that.status = 'U';
        routeConsumer.emit('getRouteForId', that, that.currentCase, that.id);
    };

    that.moveToHospital = function() {
        that.route.startTime = that.loadedTime;
        that.route.steps = null;
        that.status = 'L';

        routeConsumer.emit('getRouteForId', that, getLocationForCase(that.currentCase), that.id);<commit_msg>Fix problem with no hospital set for a case<commit_after>var routeHandling = require('./routeHandling.js');
var unitMap = {};

function getUnit(id) {
    return unitMap[id];
}

function addUnit(unit) {
    return unitMap[unit.id] = unit;
}

function processRouteForId(id, route) {
    console.log("Received route for id", id, "==>", route);
    //console.log(JSON.stringify(route));
    var steps = routeHandling.convertGoogleRoute(route.routes);

    var unit = getUnit(id);
    unit.route.steps = steps;
    if (!unit.atSiteTime) {
        unit.atSiteTime = new Date(unit.acknowledgedTime.getTime() + steps[steps.length - 1].time * 1000);
        unit.loadedTime = new Date(unit.atSiteTime.getTime() + 10 * 60 * 1000);
        unit.route.startTime = unit.acknowledgedTime;
    } else {
        unit.atHospitalTime = new Date(unit.loadedTime.getTime() + steps[steps.length - 1].time * 1000);
        unit.route.startTime = unit.loadedTime;
    }
}

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

function moveUnitForTime(unit, time) {
    // console.log('unit', unit.name, unit.routing);
    var result = null;
    if (time) {
        //console.log('looking at unit', unit, 'for move');
        var elapsedTime = (time.getTime() - unit.route.startTime.getTime()) / 1000;
        if (elapsedTime > 0 && unit.route.steps.length >= 0) {
            console.log('Will move', unit.name, 'steps(', unit.route.steps.length, ')', elapsedTime);
            for (var i = unit.route.steps.length - 1 ; i >= 0 ; i--) {
                var aStep = unit.route.steps[i];
                if (elapsedTime > aStep.time) {
                    //console.log('Will move', unit.name, 'to', aStep);

                    unit.latitude = aStep.latitude;
                    unit.longitude = aStep.longitude;
                    result = unit;
                    if (i == unit.route.steps.length - 1) {
                        console.log('Vehicle', unit.name, 'moved to target location');
                        unit.route.steps = [];
                    }
                    break;
                }
            }
        } else {
            console.log('Unit', unit.name, 'is out of time');
        }
    }
    return result;
}


var copyProperties = function(src, dest) {
    for (var key in src) {
        if (src.hasOwnProperty(key)) {
            dest[key] = src[key];
        }
    }
};

var index = [
    'Förgiftning, överdos',
    'Bröstsmärtor/Hjärtsjukdom',
    'Extremitet/Sårskador/Mindre trauma',
    'Misstanke om sjukvårdsbehov',
    'Olyckor (Trauma)',
    'Livsfara - (temporär indexering)',
    'Våld-misshandel',
    'Medvetslös-vuxen',
    'Osäkra uppgifter/Svårt sjuk patient',
    'Liggande sjuktransport',
    'Buk/urinvägar',
    'Slaganfall (Stroke)-förlamningar',
    'Andningssvårigheter',
    'Blödning. ej trauma',
    'Huvudvärk. Yrsel',
    'Graviditet/förlossning (från v.20)',
    'Beställt uppdrag mellan vårdenheter',
    'Gyn-graviditet (före v.20)',
    'O-Interntransporter SU',
    'Diabetes',
    'Krampanfall',
    'Feber',
    'Medvetslös-barn',
    'Brännskada/Elskada',
    'Barn-sjukdom',
    'Ryggbesvär',
    'Suicidmisstanke-psykiatri',
    'Allergi',
    'Webbeställning (2.0)',
    'Ögon-öron-näsa-hals',
    'Hypo/Hypertermi',
    'Drunkningstillbud',
    'Barn-förgiftning',
    'Kemikalier-gaser',
    'Räddningsuppdrag',
    'Djurbett/Insektsstick',
    'Dykeriolycka',
    'Övrigt',
    'Helikopter 5920'];

var sahlgrenska = {latitude: 57.683088,longitude: 11.959884}; // Ej barn eller ortopedi
var molndal     = {latitude: 57.661244,longitude: 12.012948}; // Medicin och ortoped, ej barn, kirurg, trauma
var ostra       = {latitude: 57.7206,  longitude: 12.0535};   // Barn, medicin och kirurgi, ej vissa trauma och ortoped (men barnortopedi)
var kungalv     = {latitude: 57.878,   longitude: 11.969};    // medicin, kirurgi, ortoped (vissa trauma) ej barn)

var getHospitalLocationForCase = function(aCase) {
    return sahlgrenska;
};


var Unit = function(args) {
    var that    = this;
    that.id     = -1;
    that.type   = 'A';
    that.status = 'K';
    that.route  = {
        startTime: null,
        steps: null
    };
    that.assignedTime = null;
    that.acknowledgedTime = null;
    that.atSiteTime = null;
    that.loadedTime = null;
    that.atHospitalTime = null;
    that.currentCase = null;

    if (args.constructor === Array) {
        args.forEach(function(unit, index, list){
            list[index] = new Unit(unit);
        });
        return args;
    } else {
        copyProperties(args, this);
    }

    that.atSite = function() {
        that.status = 'F';
        that.route.steps = null;
    };

    that.moveToCase = function() {
        that.route.steps = null;
        that.status = 'U';
        routeConsumer.emit('getRouteForId', that, that.currentCase, that.id);
    };

    that.moveToHospital = function() {
        that.route.startTime = that.loadedTime;
        that.route.steps = null;
        that.status = 'L';

        routeConsumer.emit('getRouteForId', that, getHospitalLocationForCase(that.currentCase), that.id);
{'fraction_non_alphanumeric': 0.14169648902214915, 'fraction_numerical': 0.01779669213657027, 'mean_word_length': 2.3311855670103094, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '3129389', 'n_tokens_mistral': 3716, 'n_tokens_neox': 3517, 'n_words': 703}