Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. asynchron, callback, promise, await -> Hilfe

    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

    asynchron, callback, promise, await -> Hilfe

    This topic has been deleted. Only users with topic management privileges can see it.
    • AlCalzone
      AlCalzone Developer @bahnuhr last edited by

      @bahnuhr sagte in asynchron, callback, promise, await -> Hilfe:

      Hab dein Script noch nicht probiert. Wird aber vermutlich dann nicht laufen, oder ?

      Nein, das geht davon aus, dass 2 Updates kommen. Man könnte ggf. mit einem Timeout was basteln. Wenn nach z.B. 250ms kein 2. Update kommt, Nr. nicht gefunden. Ist aber äußerst unschön (und dauert), kann man an dem anderen Adapter nicht das Verhalten evtl. einstellen?

      1 Reply Last reply Reply Quote 0
      • AlCalzone
        AlCalzone Developer @bahnuhr last edited by

        @bahnuhr Ich würde folgendes vorschlagen:
        Wende dich mit deinem Problem an den Adapter-Entwickler. Der scheint ja recht aktiv zu sein.

        Außerdem ist für diese Form der Abfrage sendTo statt States zu empfehlen. Da kann der Ziel-Adapter etwas zielgerichteter antworten und dir z.B. mitteilen, dass es einen Fehler gab o.ä.
        Muss aber vom Adapter unterstützt werden.

        J.A.R.V.I.S. 1 Reply Last reply Reply Quote 1
        • J.A.R.V.I.S.
          J.A.R.V.I.S. Developer @AlCalzone last edited by J.A.R.V.I.S.

          @bahnuhr hatte im Test Thread zum Contact Adapter um Hilfe gebeten. Ich habe nun im Contact Adapter die Möglichkeit für die sendTo Abfrage hinzugefügt.

          Diese ist derzeit aber nur im Development Branch enthalten (https://github.com/WLAN-Kabel/ioBroker.contact/tarball/development) und kann über den Link installiert werden.

          Folgendermaßen muss die Anfrage dann aussehen (Beispiel):

          sendTo('contact.0', 'query', {phonenumber: '+49 1234 567890'}, (obj) => {
              if(obj.error) {
                  log('Error: ' + obj.error, 'error');
              } else {
                  log('Contact: ' + JSON.stringify(obj.contact));
              }
          });
          
          1 Reply Last reply Reply Quote 2
          • bahnuhr
            bahnuhr Forum Testing Most Active last edited by

            @J-A-R-V-I-S sagte in asynchron, callback, promise, await -> Hilfe:

            (https://github.com/WLAN-Kabel/ioBroker.contact/tarball/development

            So, habe development von git installiert.
            Instanz gestoppt und gestartet.

            Script laufen lassen mit einer bekannten Nr.
            Im log kommt gar nichts.
            Weder Error; noch einen Namen.
            2deebce0-4c27-4395-b042-fcd8abd89005-image.png

            Schalte nun mal um auf debug; vielleicht kommt da was.

            bahnuhr 1 Reply Last reply Reply Quote 0
            • bahnuhr
              bahnuhr Forum Testing Most Active @bahnuhr last edited by

              @bahnuhr sagte in asynchron, callback, promise, await -> Hilfe:

              Schalte nun mal um auf debug; vielleicht kommt da was.

              da kommt auch nichts.

              J.A.R.V.I.S. 1 Reply Last reply Reply Quote 0
              • J.A.R.V.I.S.
                J.A.R.V.I.S. Developer @bahnuhr last edited by

                @bahnuhr dann scheinst du noch die alte Version zu haben.

                1 Reply Last reply Reply Quote 0
                • bahnuhr
                  bahnuhr Forum Testing Most Active last edited by bahnuhr

                  wie folgt:
                  e52e9859-3d1e-4e20-897f-6eb3ac12f892-image.png

                  durchgelaufen mit exit 0:
                  5b861566-91f5-479d-bdf1-f2371c69beeb-image.png

                  Instanz neu gestartet:
                  bb3c5bf0-2457-4fc1-8f8b-6ae26a77e3cd-image.png

                  Und dann script gestartet:
                  9e5a8004-7401-4532-be5f-39d28e074d21-image.png

                  Es kommt nichts.
                  Was mach ich nun da wieder falsch?
                  Dachte es müsste doch so funktionieren.

                  1 Reply Last reply Reply Quote 0
                  • bahnuhr
                    bahnuhr Forum Testing Most Active last edited by

                    So, Rückinfo von mir:

                    Objekte gelöscht.
                    Instanz gelöscht.
                    Adapter gelöscht.
                    Browser cache gelöscht.

                    Und alles neu:
                    development installiert
                    Instanz installiert.
                    google neu verbunden
                    Objekte werden geladen.
                    Script gestartet.

                    Und ahaaaaaa

                    Jetzt kommt was:
                    0266f1b2-299d-4d37-ad84-9338f228c109-image.png

                    Danke.
                    Frage:
                    Warum musste ich erst alles löschen ?
                    Hätte nicht meine Vorgehensweise auch funktionieren können / müssen ?

                    mfg
                    Dieter

                    dslraser 1 Reply Last reply Reply Quote 0
                    • bahnuhr
                      bahnuhr Forum Testing Most Active last edited by

                      Nochmals Dank an
                      @AlCalzone
                      @J-A-R-V-I-S

                      so macht es wirklich Spaß.

                      Muss jetzt mal am Script basteln. Mal sehen was ich hinbekomme.

                      mfg
                      Dieter

                      1 Reply Last reply Reply Quote 1
                      • dslraser
                        dslraser Forum Testing Most Active @bahnuhr last edited by

                        @bahnuhr
                        vielleicht "oder wahrscheinlich" hätte ein upload gereicht ?

                        bahnuhr 1 Reply Last reply Reply Quote 0
                        • bahnuhr
                          bahnuhr Forum Testing Most Active @dslraser last edited by

                          @dslraser sagte in asynchron, callback, promise, await -> Hilfe:

                          vielleicht "oder wahrscheinlich" hätte ein upload gereicht ?

                          Ja, jetzt wo du dies scheibst hört sich das äußerst plausibel an.
                          Hatte ich aber nicht parat.
                          mfg

                          1 Reply Last reply Reply Quote 0
                          • bahnuhr
                            bahnuhr Forum Testing Most Active last edited by

                            So, ich muss nun doch noch einmal schreiben.
                            Könnt ihr mal über mein Script drüber schauen.
                            Versucht habe ich den sendto ohne async/await. Da war aber wieder das script schneller als sendto. Hat folglich nicht geklappt.
                            Dann habe ich das await script von alcazone wieder genommen und versucht auszubauen.
                            Und nun hänge ich bei der Erstellung eines array bzw. dann json. Ich raffs einfach nicht.

                            
                            // Script analysiert den DP:  fritzbox.0.history.allTableJSON
                            // Hierbei sollen die Tel.Nr. im json gegen die Namen ausgetauscht werden.
                            // Nach Ermittlung aller Namen soll wieder ein json erzeugt werden
                            // um dann die Darstellung in VIS vorzunehmen.
                            //
                            // Insbesondere meinen Dank an: @AlCalzone und @J-A-R-V-I-S
                            // 01/2020 bahnuhr
                            
                            // Variablen
                                //var obj = JSON.parse(getState("fritzbox.0.history.allTableJSON").val);
                                var obj = JSON.parse(getState("javascript.0.testjson").val);    // Test-json
                                var logging = true;
                            
                            
                            async function main() {
                                for (x = 0; x < obj.length; x++) {
                                    await test(obj[x].date, obj[x].externalNumber);
                                }
                             
                                async function test(Datum, Tel) {
                                    await sendTo('contact.0', 'query', { phonenumber: Tel }, (obj) => {
                                        if(obj.error) {
                                            log('Error: ' + obj.error, 'error');
                                        } else {
                                            Datum = Datum.substring(0,6) + " : " + Datum.substring(9,14);       // hier wird das Datum gekürzt auf z.B.:  "02.02. : 10:35"
                                            if (obj.contact.fullName == undefined) {
                                                Name = Tel;     // Telefon-Nr. soll erscheinen wenn Nr. nicht existiert in Kontakte
                                            } else {
                                                Name = obj.contact.fullName;
                                            }
                            
                                            //arr.push({ "Datum": Datum, "VollName": Name });
                                            //setState('javascript.0.Status.Telefon.json_Anrufe', JSON.stringify(arr));
                                             //   date: Datum,
                                               // VollName: Name
                                             //   }
                                        //setState('javascript.0.Status.Telefon.json_Anrufe', JSON.stringify(arr_neu));
                                        
                                        //log (JSON.stringify(getState("javascript.0.Status.Telefon.json_Anrufe").val));
                                        
                                        //log (Datum + " " + Tel + " " + Name);
                                        //log (JSON.stringify(arr_neu));
                                        //arr = JSON.parse(getState('javascript.0.Status.Telefon.json_Anrufe').val);
                                        //arr.push(arr_neu);
                                        //var obj2 = getState('javascript.0.Status.Telefon.json_Anrufe').val;
                                        //setState('javascript.0.Status.Telefon.json_Anrufe', obj2 + JSON.stringify(arr_neu));
                                        //var neu = JSON.parse(getState('javascript.0.Status.Telefon.json_Anrufe').val);
                                        //log (obj2);
                                        //setState('javascript.0.Status.Telefon.json_Anrufe', getState('javascript.0.Status.Telefon.json_Anrufe').val + JSON.stringify(arr_neu));
                                        //log (getState('javascript.0.Status.Telefon.json_Anrufe').val);
                                        }
                                    });
                                }
                            }
                            main();
                            
                            

                            Bei den // Stellen hab ich schon hin und her probiert.
                            Das Script soll nach der Zeile 32 ein array bzw. json erzeugen und in einen DP schreiben. Und zwar halt bei allen Tel.Nr. die die for x erzeugt.

                            Ich glaub ich muss ins Bett; raff jetzt nix mehr.

                            Danke nochmals für eure Hilfe.
                            Konnte man dies nachvollziehen was ich möchte ?

                            J.A.R.V.I.S. 1 Reply Last reply Reply Quote 0
                            • J.A.R.V.I.S.
                              J.A.R.V.I.S. Developer @bahnuhr last edited by J.A.R.V.I.S.

                              @bahnuhr ich habe es mal ein bisschen überarbeitet. Ist aber ungetestet.

                              // Variablen
                              
                              //var obj = JSON.parse(getState("fritzbox.0.history.allTableJSON").val);
                              
                              const obj = JSON.parse(getState("javascript.0.testjson").val);    // Test-json
                              
                              const logging = true;
                              
                              let array;
                              
                              async function main() {
                              
                                  array = [];
                              
                                  for (let x = 0; x < obj.length; x++) {
                              
                                      await test(obj[x].date, obj[x].externalNumber);
                                  }
                              
                                  setState('javascript.0.Status.Telefon.json_Anrufe', JSON.stringify(array));
                              }
                              
                              async function test(datum, tel) {
                              
                                  await sendTo('contact.0', 'query', { phonenumber: tel }, async (obj) => {
                              
                                      if(obj.error) {
                              
                                          log('Error: ' + obj.error, 'error');
                              
                                      } else {
                              
                                          datum = datum.substring(0,6) + " : " + datum.substring(9,14); // hier wird das Datum gekürzt auf z.B.:  "02.02. : 10:35"
                                          const name = (obj.contact.fullName) ? obj.contact.fullName : tel; // Telefon-Nr. soll erscheinen wenn Nr. nicht existiert in Kontakte
                              
                                          array.push({ Datum: datum, VollName: name });
                                      }
                                  });
                              }
                              
                              main();
                              

                              Bin mir nicht sicher, ob ich das jetzt komplett richtig verstanden habe, was du machen möchtest.

                              1 Reply Last reply Reply Quote 0
                              • bahnuhr
                                bahnuhr Forum Testing Most Active last edited by bahnuhr

                                Ja, so irgendwie hab ichs ja auch versucht.
                                In Zeile 20 soll dann ja das array geschrieben werden.
                                Es kommt aber im DP nichts an.

                                Wenn ich vor 36 mal ein log mache, so: log (datum + " " + name)

                                Dann erscheint im log:

                                21:33:45.741	info	javascript.1 script.js.Scripte.test5: 01.01. : 10:35 0xxxx092
                                21:33:45.749	info	javascript.1 script.js.Scripte.test5: 01.01. : 10:35 Alxxler
                                21:33:45.749	info	javascript.1 script.js.Scripte.test5: 01.01. : 10:35 Marxxdd
                                21:33:45.749	info	javascript.1 script.js.Scripte.test5: 01.01. : 10:35 Dietxxeit
                                

                                Soweit so gut; nur kommt es dann in der Zeile 20 nicht an ?!?!
                                66464052-721e-4668-bfb2-6e6f39b1dfd0-image.png

                                Müsste man nicht irgendwie das array an die aufrufende funktion wieder zurück geben ?
                                Und dies hab ich nicht hinbekommen.

                                mfg

                                J.A.R.V.I.S. 1 Reply Last reply Reply Quote 0
                                • J.A.R.V.I.S.
                                  J.A.R.V.I.S. Developer @bahnuhr last edited by

                                  @bahnuhr habe es nochmal abgeändert. Ich glaube im callback fehlt das async.

                                  bahnuhr 1 Reply Last reply Reply Quote 0
                                  • bahnuhr
                                    bahnuhr Forum Testing Most Active @J.A.R.V.I.S. last edited by

                                    @J-A-R-V-I-S sagte in asynchron, callback, promise, await -> Hilfe:

                                    @bahnuhr habe es nochmal abgeändert. Ich glaube im callback fehlt das async.

                                    Hallo Jarvis,

                                    keine Veränderung. Im DP kommt nix an.

                                    1 Reply Last reply Reply Quote 0
                                    • bahnuhr
                                      bahnuhr Forum Testing Most Active last edited by bahnuhr

                                      4fc88b56-0a58-43f8-8445-6fe8f9ef01d8-image.png

                                      Habe log eingebaut.
                                      Man sieht dass das array 4 mal beschrieben wird. Bis die 4 Tel.Nr. durchlaufen sind; passt auch soweit.

                                      Nur die Zeile 14 und 15 wird doch gar nicht ausgeführt. Ansonsten müsste doch ein log Eintrag wg. Zeile 14 kommen.
                                      Kann das die Lösung sein?

                                      J.A.R.V.I.S. 1 Reply Last reply Reply Quote 0
                                      • J.A.R.V.I.S.
                                        J.A.R.V.I.S. Developer @bahnuhr last edited by J.A.R.V.I.S.

                                        @bahnuhr probier mal folgendes:

                                        // Variablen
                                        
                                        //var obj = JSON.parse(getState("fritzbox.0.history.allTableJSON").val);
                                        
                                        const obj = JSON.parse(getState("javascript.0.testjson").val);    // Test-json
                                        
                                        const logging = true;
                                        
                                        let array;
                                        
                                        async function main() {
                                        
                                            array = [];
                                        
                                            obj = [{externalNumber: '+49 1573 1234567'}];
                                        
                                            for (let x = 0; x < obj.length; x++) {
                                        
                                                await test(obj[x].date, obj[x].externalNumber).then(async (prom) => {
                                                    
                                                    array.push(prom);
                                        
                                                }).catch((reason) => {
                                                    log(reason, 'error');
                                                });
                                            }
                                            
                                            setState('javascript.0.Status.Telefon.json_Anrufe', JSON.stringify(array));
                                        }
                                        
                                        async function test(datum, tel) {
                                        
                                            return new Promise(function (resolve, reject) {
                                        
                                                sendTo('contact.0', 'query', { phonenumber: tel }, (obj) => {
                                        
                                                    if(obj.error) {
                                        
                                                        reject('Error: ' + obj.error);
                                        
                                                    } else {
                                        
                                                        datum = "datum".substring(0, 6) + " : " + "datum".substring(9, 14); // hier wird das Datum gekürzt auf z.B.:  "02.02. : 10:35"
                                                        const name = (obj.contact.fullName) ? obj.contact.fullName : tel; // Telefon-Nr. soll erscheinen wenn Nr. nicht existiert in Kontakte
                                        
                                                        resolve({ Datum: datum, VollName: name });
                                                    }
                                                });
                                            });
                                        }
                                        
                                        main();
                                        
                                        AlCalzone 1 Reply Last reply Reply Quote 1
                                        • AlCalzone
                                          AlCalzone Developer @J.A.R.V.I.S. last edited by

                                          @bahnuhr @J-A-R-V-I-S sendTo gibt keinen Promise zurück, also kann man die Funktion nicht awaiten. Der Ansatz mit new Promise ist richtig und dürfte funktionieren.

                                          Allerdings stellen sich mir bei diesem await/.then-Mischmasch die Fußnägel hoch:

                                          await test(obj[x].date, obj[x].externalNumber)
                                            .then(async prom => {
                                              array.push(prom);
                                            })
                                            .catch(reason => {
                                              log(reason, "error");
                                            });
                                          

                                          so ist doch viel schöner:

                                          try {
                                            const prom = await test(obj[x].date, obj[x].externalNumber);
                                            array.push(prom);
                                          } catch (reason) {
                                            log(reason, "error");
                                          }
                                          
                                          1 Reply Last reply Reply Quote 1
                                          • bahnuhr
                                            bahnuhr Forum Testing Most Active last edited by

                                            Ich kann nur sagen: Klasse, beides funktioniert.

                                            Ihr beide seit super; da diskutiert ihr was schöner ist.

                                            Und ich bin froh wenn es überhaupt läuft 😉

                                            Nochmals Danke.

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

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            527
                                            Online

                                            31.7k
                                            Users

                                            79.9k
                                            Topics

                                            1.3m
                                            Posts

                                            javascript
                                            7
                                            60
                                            4427
                                            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