Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Asynchrone Funktionen innerhalb eine Subscribtion aufrufen

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    Asynchrone Funktionen innerhalb eine Subscribtion aufrufen

    This topic has been deleted. Only users with topic management privileges can see it.
    • Ben1983
      Ben1983 @ticaki last edited by

      Ich scheine es gefunden zu haben.
      Es war wieder mal, dass ich true oder false zurückgegeben habe, kein Objekt. 😞

      so geht es (Haben ein Objekt mit result zurückgegeben)

      const automatikfreigabe = {
          beleuchtung: {
              aktiv: false,
              bypass: false,
              checkRelease: async function(kinderSindDa){
                  try{
                      //Zeit in Bereich zuweisen
                      const timeOK = await messageToAsync("TimeAfterOrEqualNow",{time:this.zeitAbendsWennKinderDa},{timeout:5000});
                      return {result:(this.aktiv &&
                      // Entweder der Bypass ist gesetzt
                              (this.bypass ||
                      // Oder der Zustand wurde schon gesetzt
                              (this.zustand &&
                              (!kinderSindDa || timeOK.result))))};  
                  }
                  catch(error){
                      log(error);
                  }
              },
          }
      }
      
      async function test(){
          log("start")
          try{
              if(await automatikfreigabe.beleuchtung.checkRelease(true)){
                  log("Mitte")
              }
          }
          catch(error){
              log(error);
          }
          log("Ende");
      };
      
      test();
      
      
      
      
      
      
      
      
      
      
      
      
      T 1 Reply Last reply Reply Quote 0
      • T
        ticaki Developer @Ben1983 last edited by ticaki

        @ben1983
        Ich vermute viel eher das das ergebnis vorher undefinied war und damit false, jetzt ist es ein Object und damit true.

        Ben1983 1 Reply Last reply Reply Quote 0
        • Ben1983
          Ben1983 @ticaki last edited by

          @ticaki ok. Verstehe trotzdem nicht, warum es nicht geht.
          Nein, denn ein direkter log der Ergebnisses nach dem Aufruf führte zu nichts.
          Naja jetzt geht es jedefalls

          T 1 Reply Last reply Reply Quote 0
          • T
            ticaki Developer @Ben1983 last edited by

            @ben1983
            Naja wenns geht gut, ich hätte es mit Klassen für Funktionen und Json für Konstanten gelöst 🙂

            Ben1983 1 Reply Last reply Reply Quote 0
            • Ben1983
              Ben1983 @ticaki last edited by

              @ticaki ???? Wie meinst Du das?

              T 1 Reply Last reply Reply Quote 0
              • T
                ticaki Developer @Ben1983 last edited by ticaki

                @ben1983
                Ist schlecht ein Beispiel zu erstellen, da der Vorteil ja erst bei Gemeinsamkeiten zum Tragen kommt aber sowas z.B.:

                // Prüfen,ob die angegebenen Zeit vor oder gleich der aktuellen ist
                onMessage("TimeBeforeOrEqualNow",(messagedata,callback) => {
                    const now=new Date();
                    const inputTime=new Date(`${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}-${messagedata.time}`);
                    if (inputTime) {
                        callback({ result: inputTime<=now });
                    }
                    else {
                        callback({ result: false });
                    }
                });
                
                const automatikfreigabe={
                    beleuchtung: {
                        aktiv: idAutomatikfreigabeBeleuchtungAktiv,
                        bypass: idAutomatikfreigabeBeleuchtungBypass,
                        zustand: idAutomatikfreigabeBeleuchtungZustand,
                        kinderSindDa: idAutomatikfreigabeBeleuchtungKidnerSindDa,
                        zeitAbendsWennKinderDa: gidAutomatikfreigabeBeleuchtungZeitAbendsWennKinderDaSind,
                
                    },
                }
                
                var beleuchtung=null;
                class Beleuchtung {
                    options={};
                    dpOptions = {};
                    // kein async für constructor möglich
                    constructor(options) {
                        for (const a in options) {
                            if (options[a]) this.dpOptions[a]=options[a];
                        }
                    }
                    async updateDp() {
                        for (const a in this.dpOptions[a]) {
                            this.options[a]=getState(this.dpOptions[a]).val;
                        }
                    }
                    async checkRelease(kinderSindDa) {
                        // Es muss die Automatikfreigabe aktiv sein
                        try {
                            const result=this.options.aktiv&&
                                // Entweder der Bypass ist gesetzt
                                (this.options.bypass||
                                    // Oder der Zustand wurde schon gesetzt
                                    (this.options.zustand&&
                                        (!kinderSindDa||(await messageToAsync("TimeAfterOrEqualNow",{ time: "21:30" },{ timeout: 5000 })).result)));
                        } catch (e) {
                            log(e)
                            result={};
                        }
                        return result;
                    }
                }
                
                async function main() {
                    beleuchtung=new Beleuchtung(automatikfreigabe.beleuchtung);
                    await beleuchtung.updateDp()
                    log(JSON.stringify(await beleuchtung.checkRelease(true)));
                }
                

                habs auch nicht getestet, ist mehr ein "Muster"

                Ben1983 1 Reply Last reply Reply Quote 0
                • Ben1983
                  Ben1983 @ticaki last edited by

                  @ticaki ok.
                  Erkenne jetzt nicht grad ein Vorteil, aber ok

                  T 1 Reply Last reply Reply Quote 0
                  • T
                    ticaki Developer @Ben1983 last edited by ticaki

                    @ben1983
                    Glaube nicht das json oder klassen pauschal einen Vorteil haben. Hängt von den Umständen ab und persönlichen Vorlieben.

                    Ben1983 1 Reply Last reply Reply Quote 0
                    • Ben1983
                      Ben1983 @ticaki last edited by

                      @ticaki ok. Aber ist das Objekt bei mir kein JSON? Ist doch das gleiche, oder übersehe ich was? Ist halt nur noch die Funktion direkt drin.

                      T 1 Reply Last reply Reply Quote 0
                      • T
                        ticaki Developer @Ben1983 last edited by

                        @ben1983
                        Du benutzt ein Json. Ich wollte damit sagen das weder deine Art, noch meine Art nicht pauschal besser sind.

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post

                        Support us

                        ioBroker
                        Community Adapters
                        Donate

                        901
                        Online

                        31.8k
                        Users

                        80.0k
                        Topics

                        1.3m
                        Posts

                        2
                        22
                        634
                        Loading More Posts
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes
                        Reply
                        • Reply as topic
                        Log in to reply
                        Community
                        Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                        The ioBroker Community 2014-2023
                        logo