Source: plugins/inbound/katsys/parsers/parser_ILSLAU.js

const EventEmitter = require('events');
const gk = require('gauss-krueger');
const Operation = require(__dirname + '/../../../../models/core/core_operation.js');

class ParserLAU extends EventEmitter {

    statusMap = {
        '32770' : 'Priorisierter Sprechwunsch',
        '32771' : 'Einsatzbereit über Funk',
        '32772' : 'Einsatzbereit auf Wache',
        '32773' : 'Einsatz übernommen',
        '32774' : 'Am Einsatzort eingetroffen',
        '32775' : 'Sprechwunsch (einsatzbezogen)',
        '32776' : 'Nicht Einsatzbereit',
        '32777' : '7',
        '32778' : '8',
        '32779' : 'Fremdanmeldung'
    }

    statusMappingToStatusShort = {
        '32770' : 0,
        '32771' : 1,
        '32772' : 2,
        '32773' : 3,
        '32774' : 4,
        '32775' : 5,
        '32776' : 6,
        '32777' : 7,
        '32778' : 8,
        '32779' : 9
    }

    constructor() {
        super();
        // Alarm Data
        this.schlagwort_kurz = '';
        this.sds_abschnitt = '';
        this.sds_abschnitt_kurz_tts = '';
        this.sds_alarmdatum= '';
        this.sds_alarmuhrzeit= '';
        this.sds_alarmweg = '';
        this.sds_datum_kurz = '';
        this.sds_einsatzdatum = '';
        this.sds_einsatzmittel = '';
        this.sds_einsatznummer = '';
        this.sds_einsatzort = '';
        this.sds_erh_kbm = '';
        this.sds_freitext = '';
        this.sds_hausnummernzusatz = '';
        this.sds_komplett = '';
        this.sds_objekt = '';
        this.sds_objekt_kurz_tts = '';
        this.sds_ort = '';
        this.sds_ortsteil = '';
        this.sds_ortsteil_kurz = '';
        this.sds_plan = '';
        this.sds_schlagwort = '';
        this.sds_schlagwort_tts_navi = '';
        this.sds_schleifen = '';
        this.sds_stichwort = '';
        this.sds_strasse = '';
        this.sds_strasse_kurz_tts = '';
        this.sds_x = '';
        this.sds_y = '';
        this.lat = null;
        this.lon = null;
        this.floor = '';
        this.ils = '';
        // Status Data
        this.issi = null;
        this.radioStatus = null;
        this.radioStatusHumanReadable = null;
        this.radioStatusShort = null;
    }

    parseRAWAlarmData(rawAlarmDataArray) {
        console.log('PARSER-ILSLAU | PARSING RAW ALARM');
        console.log('----------- RAW DATA inside parser ---------- ');
        console.log(rawAlarmDataArray);
        console.log('----------- ENDE RAW DATA inside parser ----- ');
        this.operation = new Operation();
        this.operation.section = rawAlarmDataArray.sds_abschnitt;
        this.operation.alarmdate = rawAlarmDataArray.sds_alarmdatum;
        this.operation.alarmtime = rawAlarmDataArray.sds_alarmuhrzeit;
        this.operation.gear = rawAlarmDataArray.sds_einsatzmittel;
        this.operation.operationnumber = rawAlarmDataArray.sds_einsatznummer;
        this.operation.location = rawAlarmDataArray.sds_einsatzort;
        this.operation.message = rawAlarmDataArray.sds_freitext;
        this.operation.community = rawAlarmDataArray.sds_gemeinde;
        this.operation.rawdata = rawAlarmDataArray.sds_komplett;
        this.operation.object = rawAlarmDataArray.sds_objekt;
        this.operation.district = rawAlarmDataArray.sds_ortsteil;
        this.operation.keywordId = rawAlarmDataArray.sds_schlagwort_id;
        this.operation.keywordCategory = rawAlarmDataArray.sds_schlagwort_kategorie;
        this.operation.keywordRaw = rawAlarmDataArray.sds_schlagwort_komplett;
        this.operation.keywordName = rawAlarmDataArray.sds_schlagwort_name;
        this.operation.zveis = rawAlarmDataArray.sds_schleifen;
        this.operation.subject = rawAlarmDataArray.sds_stichwort;
        this.operation.street = rawAlarmDataArray.sds_strasse;
        this.operation.gkx = rawAlarmDataArray.sds_x;
        this.operation.gky = rawAlarmDataArray.sds_y;
        this.operation.floor = rawAlarmDataArray.sds_etage;
        this.operation.ils = rawAlarmDataArray.sds_ils;
        this.calculateLatLon();

        console.log('----------- Operation Object inside parser ---------- ');
        console.log(this.operation);
        console.log('----------- ENDE Operation Object inside parser ----- ');

        
        this.emit('alarm_parsed', null);
    }

    /**
     * injects a test alarm in parser to test following process
     * FOR TESTING
     */
    TestparseRAWAlarmData() {
        console.log('PARSER-ILSLAU | TEST | PARSING RAW ALARM');

        this.operation = new Operation();

        this.operation.section = 'Kreuzung';
        this.operation.alarmdate = 'ALARMDATUM';
        this.operation.alarmtime = 'ALARMZEIT';
        this.operation.gear = 'EINSATZMITTEL';
        this.operation.operationnumber = 'EINSATZNUMMER';
        this.operation.location = 'LOCATION';
        this.operation.message = 'MITTEILUNG';
        this.operation.community = 'GEMEINDE';
        this.operation.rawdata = 'KOMPLETTE SDS';
        this.operation.object = 'OBJEKT';
        this.operation.district = 'ORTSTEIL';
        this.operation.keywordId = 'STICHWORT-ID';
        this.operation.keywordCategory = 'STICHWORT KATEGORIE';
        this.operation.keywordRaw = 'STICHWORT RAW';
        this.operation.keywordName = 'SCHLAGWORT NAME';
        this.operation.zveis = 'SCHLEIFEN';
        this.operation.subject = 'STICHWORT';
        this.operation.street = 'STRASSE';
        this.operation.gkx = '4447706.33';
        this.operation.gky = '5485688.77';
        this.operation.floor = '1. OG';
        this.operation.ils = 'ILS Nürnberg';

        this.calculateLatLon();
        this.emit('alarm_parsed', null);
    }

    parseRAWZVEIData(rawZVEIDataArray) {
        console.log('PARSER-ILSLAU | parseRAWZVEIData: ');
        this.zveicode = rawZVEIDataArray.data.code;
        this.zveidescription = rawZVEIDataArray.data.description;
        this.emit('zveiParsed', null);
    }

    parseRAWStatusData(rawStatusDataArray) {
        console.log('PARSER-ILSLAU | parseRAWStatusData: ' + rawStatusDataArray);
        this.radioStatus = rawStatusDataArray.data.radioStatus;
        this.issi = rawStatusDataArray.data.issi;
        this.translateStatusNumberToHumanReadable();
        this.emit('statusParsed', null);
    }

    translateStatusNumberToHumanReadable() {
        if (typeof this.statusMap[this.radioStatus] != 'undefined') {
            this.radioStatusHumanReadable = this.statusMap[this.radioStatus];

            if (typeof this.statusMappingToStatusShort[this.radioStatus] != 'undefined') {
                this.radioStatusShort = this.statusMappingToStatusShort[this.radioStatus];
            }

            this.emit('statusTranslate_successful', this.radioStatusHumanReadable);
        } else {
            this.emit('statusTranslate_error', null);
        }
    }

    calculateLatLon() {

        if ((this.operation.gky.value.length > 0) && (this.operation.gkx.value.length > 0)) {
            // SDS delivered gauss krueger coordinates
            let wgs84 = gk.toWGS({x: parseFloat(this.operation.gkx.value), y: parseFloat(this.operation.gky.value)});
            this.operation.lat = wgs84.latitude;
            this.operation.lon = wgs84.longitude;
            this.emit('geocoding_successful', null);
        } else {
            // SDS did not deliver gauss krueger coordinates
            this.operation.lat = 0;
            this.operation.lon = 0;
            this.emit('geocoding_error', null);
        }
    }
}

module.exports = ParserLAU;