
class SetPID {

    static initSetPID() {
        if (window.sigApi !== undefined) {
            const fm = new SetPID(window.sigApi);
        } else {
            // ist sigApi noch nicht geladen, als EventListener fuer 'SigApiReady' registrieren
            window.addEventListener('SigApiReady', () => {
                const fm = new SetPID(window.sigApi);
            });
        }
    }

    constructor(api) {
        this.events = null;
        this.eventMediator = null;
        this.datapointAPI = null;
        this._initAPIs(api);
        this._registerEvents();
    }


    _initAPIs(api) {
        if ((api !== null) && (api !== undefined)) {
            if (api.datapointManager !== undefined) {
                this.datapointAPI = api.datapointManager;
            }
            if (api.eventMediator !== undefined) {
                this.eventMediator = api.eventMediator;
            }
            if (api.events !== undefined) {
                this.events = api.events;
            }
        }
    }

    _registerEvents() {

        let i;
        let PowerLimitation = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
        let Limit0 = 0;
        let Limit1 = 0;
        let Limit2 = 0;

        let KpDuese;
        let TnDuese;
        let TvDuese;

        let KpAnschluss;
        let TnAnschluss;
        let TvAnschluss;

        let KpVerteiler;
        let TnVerteiler;
        let TvVerteiler;

        if ((this.eventMediator !== null) && (this.eventMediator !== undefined) && (this.events !== null) && (this.events !== undefined)) {
            this.eventMediator.subscribe(this.events.getUserDefinedInternalEvent('SetPIDParameter'), () => {

                this.datapointAPI.readDataPoint('10:Zone1.PowerLimitationVal', 0)
                    .then((resolve) => {
                        PowerLimitation[0] = resolve.value;
                        this.datapointAPI.readDataPoint('10:Zone2.PowerLimitationVal', 0)
                            .then((resolve) => {
                                PowerLimitation[1] = resolve.value;
                                this.datapointAPI.readDataPoint('10:Zone3.PowerLimitationVal', 0)
                                    .then((resolve) => {
                                        PowerLimitation[2] = resolve.value;
                                        this.datapointAPI.readDataPoint('10:Zone4.PowerLimitationVal', 0)
                                            .then((resolve) => {
                                                PowerLimitation[3] = resolve.value;
                                                this.datapointAPI.readDataPoint('10:Zone5.PowerLimitationVal', 0)
                                                    .then((resolve) => {
                                                        PowerLimitation[4] = resolve.value;
                                                        this.datapointAPI.readDataPoint('10:Zone6.PowerLimitationVal', 0)
                                                            .then((resolve) => {
                                                                PowerLimitation[5] = resolve.value;
                                                                this.datapointAPI.readDataPoint('10:Zone7.PowerLimitationVal', 0)
                                                                    .then((resolve) => {
                                                                        PowerLimitation[6] = resolve.value;
                                                                        this.datapointAPI.readDataPoint('10:Zone8.PowerLimitationVal', 0)
                                                                            .then((resolve) => {
                                                                                PowerLimitation[7] = resolve.value;
                                                                                this.datapointAPI.readDataPoint('10:Zone9.PowerLimitationVal', 0)
                                                                                    .then((resolve) => {
                                                                                        PowerLimitation[8] = resolve.value;
                                                                                        this.datapointAPI.readDataPoint('10:Zone10.PowerLimitationVal', 0)
                                                                                            .then((resolve) => {
                                                                                                PowerLimitation[9] = resolve.value;
                                                                                                this.datapointAPI.readDataPoint('10:Zone11.PowerLimitationVal', 0)
                                                                                                    .then((resolve) => {
                                                                                                        PowerLimitation[10] = resolve.value;
                                                                                                        this.datapointAPI.readDataPoint('10:Zone12.PowerLimitationVal', 0)
                                                                                                            .then((resolve) => {
                                                                                                                PowerLimitation[11] = resolve.value;
                                                                                                                this.datapointAPI.readDataPoint('10:Zone13.PowerLimitationVal', 0)
                                                                                                                    .then((resolve) => {
                                                                                                                        PowerLimitation[12] = resolve.value;
                                                                                                                        this.datapointAPI.readDataPoint('10:Zone14.PowerLimitationVal', 0)
                                                                                                                            .then((resolve) => {
                                                                                                                                PowerLimitation[13] = resolve.value;
                                                                                                                                this.datapointAPI.readDataPoint('10:Zone15.PowerLimitationVal', 0)
                                                                                                                                    .then((resolve) => {
                                                                                                                                        PowerLimitation[14] = resolve.value;
                                                                                                                                        this.datapointAPI.readDataPoint('10:Zone16.PowerLimitationVal', 0)
                                                                                                                                            .then((resolve) => {
                                                                                                                                                PowerLimitation[15] = resolve.value;
                                                                                                                                                this.datapointAPI.readDataPoint('10:Zone17.PowerLimitationVal', 0)
                                                                                                                                                    .then((resolve) => {
                                                                                                                                                        PowerLimitation[16] = resolve.value;
                                                                                                                                                        this.datapointAPI.readDataPoint('10:Zone18.PowerLimitationVal', 0)
                                                                                                                                                            .then((resolve) => {
                                                                                                                                                                PowerLimitation[17] = resolve.value;
                                                                                                                                                                this.datapointAPI.readDataPoint('10:Zone19.PowerLimitationVal', 0)
                                                                                                                                                                    .then((resolve) => {
                                                                                                                                                                        PowerLimitation[18] = resolve.value;
                                                                                                                                                                        this.datapointAPI.readDataPoint('10:Zone20.PowerLimitationVal', 0)
                                                                                                                                                                            .then((resolve) => {
                                                                                                                                                                                PowerLimitation[19] = resolve.value;
                                                                                                                                                                                this.datapointAPI.readDataPoint('10:Zone21.PowerLimitationVal', 0)
                                                                                                                                                                                    .then((resolve) => {
                                                                                                                                                                                        PowerLimitation[20] = resolve.value;
                                                                                                                                                                                        this.datapointAPI.readDataPoint('10:Zone22.PowerLimitationVal', 0)
                                                                                                                                                                                            .then((resolve) => {
                                                                                                                                                                                                PowerLimitation[21] = resolve.value;
                                                                                                                                                                                                this.datapointAPI.readDataPoint('10:Zone23.PowerLimitationVal', 0)
                                                                                                                                                                                                    .then((resolve) => {
                                                                                                                                                                                                        PowerLimitation[22] = resolve.value;
                                                                                                                                                                                                        this.datapointAPI.readDataPoint('10:Zone24.PowerLimitationVal', 0)
                                                                                                                                                                                                            .then((resolve) => {
                                                                                                                                                                                                                PowerLimitation[23] = resolve.value;
                                                                                                                                                                                                                this.datapointAPI.readDataPoint('10:SelectedAssistant1.Limit_1', 0)
                                                                                                                                                                                                                    .then((resolve) => {
                                                                                                                                                                                                                        Limit1 = resolve.value;
                                                                                                                                                                                                                        this.datapointAPI.readDataPoint('10:SelectedAssistant1.Limit_2', 0)
                                                                                                                                                                                                                            .then((resolve) => {
                                                                                                                                                                                                                                Limit2 = resolve.value;
                                                                                                                                                                                                                                this.datapointAPI.readDataPoint('10:Global_Set1.PowerOnTestStart', 0)
                                                                                                                                                                                                                                    .then((resolve) => {
                                                                                                                                                                                                                                        Limit0 = resolve.value;
                                                                                                                                                                                                                                        this.datapointAPI.readDataPoint('10:SelectedAssistant1.PDuese', 0)
                                                                                                                                                                                                                                            .then((resolve) => {
                                                                                                                                                                                                                                                KpDuese = resolve.value;
                                                                                                                                                                                                                                                this.datapointAPI.readDataPoint('10:SelectedAssistant1.IDuese', 0)
                                                                                                                                                                                                                                                    .then((resolve) => {
                                                                                                                                                                                                                                                        TnDuese = resolve.value;
                                                                                                                                                                                                                                                        this.datapointAPI.readDataPoint('10:SelectedAssistant1.DDuese', 0)
                                                                                                                                                                                                                                                            .then((resolve) => {
                                                                                                                                                                                                                                                                TvDuese = resolve.value;
                                                                                                                                                                                                                                                                this.datapointAPI.readDataPoint('10:SelectedAssistant1.PAnschluss', 0)
                                                                                                                                                                                                                                                                    .then((resolve) => {
                                                                                                                                                                                                                                                                        KpAnschluss = resolve.value;
                                                                                                                                                                                                                                                                        this.datapointAPI.readDataPoint('10:SelectedAssistant1.IAnschluss', 0)
                                                                                                                                                                                                                                                                            .then((resolve) => {
                                                                                                                                                                                                                                                                                TnAnschluss = resolve.value;
                                                                                                                                                                                                                                                                                this.datapointAPI.readDataPoint('10:SelectedAssistant1.DAnschluss', 0)
                                                                                                                                                                                                                                                                                    .then((resolve) => {
                                                                                                                                                                                                                                                                                        TvAnschluss = resolve.value;
                                                                                                                                                                                                                                                                                        this.datapointAPI.readDataPoint('10:SelectedAssistant1.PVerteiler', 0)
                                                                                                                                                                                                                                                                                            .then((resolve) => {
                                                                                                                                                                                                                                                                                                KpVerteiler = resolve.value;
                                                                                                                                                                                                                                                                                                this.datapointAPI.readDataPoint('10:SelectedAssistant1.IVerteiler', 0)
                                                                                                                                                                                                                                                                                                    .then((resolve) => {
                                                                                                                                                                                                                                                                                                        TnVerteiler = resolve.value;
                                                                                                                                                                                                                                                                                                        this.datapointAPI.readDataPoint('10:SelectedAssistant1.DVerteiler', 0)
                                                                                                                                                                                                                                                                                                            .then((resolve) => {
                                                                                                                                                                                                                                                                                                                TvVerteiler = resolve.value;
                                                                                                                                                                                                                                                                                                            //    this.datapointAPI.readDataPoint('10:Global_Set1.ZielSollTemperaturRampe', 0)
                                                                                                                                                                                                                                                                                                            //        .then((resolve) => {
                                                                                                                                                                                                                                                                                                             //           this.datapointAPI.writeDataPoint('10:PIDGroup1.SetPoint', resolve.value);


                                                                                                                                                                                                                for (i = 0; i < 24; i++) {

                                                                                                                                                                                                                    // Duese:
                                                                                                                                                                                                                    if ((PowerLimitation[i]> Limit0) && (PowerLimitation[i]<= Limit1)) {
                                                                                                                                                                                                                        this.setPIDParameter(i,KpDuese,TnDuese,TvDuese);
                                                                                                                                                                                                                    }
                                                                                                                                                                                                                    // Anschluss:
                                                                                                                                                                                                                    else if ((PowerLimitation[i]> Limit1) && (PowerLimitation[i]<= Limit2)) {
                                                                                                                                                                                                                        this.setPIDParameter(i,KpAnschluss,TnAnschluss,TvAnschluss);
                                                                                                                                                                                                                    }

                                                                                                                                                                                                                    // Verteiler:
                                                                                                                                                                                                                    else if (PowerLimitation[i]> Limit2)  {
                                                                                                                                                                                                                        this.setPIDParameter(i,KpVerteiler,TnVerteiler,TvVerteiler);
                                                                                                                                                                                                                    }



                                                                                                                                                                                                                }

                                                                                                                                                                                                            });});});});});});});});});});});});});});});});});});});});});});});});});});});//});
                                    });});});});});});});});});





                                                                                                                                                                                                            });

        }
    }

    setPIDParameter(i, Kp, Tn, Tv){

        switch (i) {
            case 0:
                this.datapointAPI.writeDataPoint('10:Zone1.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone1.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone1.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork1.RamParticipation', 1);
                break;
            case 1:
                this.datapointAPI.writeDataPoint('10:Zone2.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone2.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone2.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork2.RamParticipation', 1);
                break;
            case 2:
                this.datapointAPI.writeDataPoint('10:Zone3.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone3.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone3.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork3.RamParticipation', 1);
                break;
            case 3:
                this.datapointAPI.writeDataPoint('10:Zone4.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone4.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone4.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork4.RamParticipation', 1);
                break;
            case 4:
                this.datapointAPI.writeDataPoint('10:Zone5.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone5.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone5.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork5.RamParticipation', 1);
                break;
            case 5:
                this.datapointAPI.writeDataPoint('10:Zone6.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone6.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone6.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork6.RamParticipation', 1);
                break;
            case 6:
                this.datapointAPI.writeDataPoint('10:Zone7.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone7.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone7.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork7.RamParticipation', 1);
                break;
            case 7:
                this.datapointAPI.writeDataPoint('10:Zone8.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone8.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone8.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork8.RamParticipation', 1);
                break;
            case 8:
                this.datapointAPI.writeDataPoint('10:Zone9.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone9.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone9.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork9.RamParticipation', 1);
                break;
            case 9:
                this.datapointAPI.writeDataPoint('10:Zone10.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone10.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone10.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork10.RamParticipation', 1);
                break;
            case 10:
                this.datapointAPI.writeDataPoint('10:Zone11.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone11.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone11.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork11.RamParticipation', 1);
                break;
            case 11:
                this.datapointAPI.writeDataPoint('10:Zone12.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone12.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone12.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork12.RamParticipation', 1);
                break;
            case 12:
                this.datapointAPI.writeDataPoint('10:Zone13.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone13.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone13.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork13.RamParticipation', 1);
                break;
            case 13:
                this.datapointAPI.writeDataPoint('10:Zone14.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone14.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone14.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork14.RamParticipation', 1);
                break;
            case 14:
                this.datapointAPI.writeDataPoint('10:Zone15.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone15.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone15.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork15.RamParticipation', 1);
                break;
            case 15:
                this.datapointAPI.writeDataPoint('10:Zone16.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone16.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone16.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork16.RamParticipation', 1);
                break;
            case 16:
                this.datapointAPI.writeDataPoint('10:Zone17.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone17.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone17.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork17.RamParticipation', 1);
                break;
            case 17:
                this.datapointAPI.writeDataPoint('10:Zone18.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone18.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone18.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork18.RamParticipation', 1);
                break;
            case 18:
                this.datapointAPI.writeDataPoint('10:Zone19.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone19.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone19.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork19.RamParticipation', 1);
                break;
            case 19:
                this.datapointAPI.writeDataPoint('10:Zone20.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone20.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone20.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork20.RamParticipation', 1);
                break;
            case 20:
                this.datapointAPI.writeDataPoint('10:Zone21.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone21.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone21.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork21.RamParticipation', 1);
                break;
            case 21:
                this.datapointAPI.writeDataPoint('10:Zone22.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone22.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone22.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork22.RamParticipation', 1);
                break;
            case 22:
                this.datapointAPI.writeDataPoint('10:Zone23.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone23.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone23.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork23.RamParticipation', 1);
                break;
            case 23:
                this.datapointAPI.writeDataPoint('10:Zone24.HeatKp', Kp);
                this.datapointAPI.writeDataPoint('10:Zone24.HeatTn', Tn);
                this.datapointAPI.writeDataPoint('10:Zone24.HeatTv', Tv);
                this.datapointAPI.writeDataPoint('10:ModeOfWork24.RamParticipation', 1);
                break;
        }
    }
}

SetPID.initSetPID();