Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Einfügen von JS-Klassen aus eigenem NPM-Modul

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    Einfügen von JS-Klassen aus eigenem NPM-Modul

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

      @uwe72

      https://github.com/ticaki/script-library-example

      Hab noch nicht versucht, das im iobroker einzubinden, das complilieren geht schon mal. Hab console und name aus der javascript-d.ts raus werfen müssen.

      Im Arbeitsverzeichnis npx tsc eingeben.

      Ne geht doch noch nicht. 🙂

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

        Update: Habe das aus Github 1:1 reinkopiert und scheint zu funktionieren über "npx tsc"

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

          @uwe72
          Hm im log steht doch alles? Es geht trotzdem noch nicht.

          U 1 Reply Last reply Reply Quote 1
          • U
            uwe72 @ticaki last edited by

            @ticaki Ja, es kommt nun keine Fehlermeldung mehr. Die lib.js wird leider nicht erstellt

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

              @uwe72 sagte in Einfügen von JS-Klassen aus eigenem NPM-Modul:

              @ticaki Ja, es kommt nun keine Fehlermeldung mehr. Die lib.js wird leider nicht erstellt

              Jo, das hab ich jetzt alles hinbekommen. Aber der Javascript-Adapter kennt die iobroker eigenen Skriptbefehle nicht, wenn sie in einer externen Datei stecken. Nachvollziehbar.

              Kann man zwar lösen in dem man beim Erzeugen der Klasse die Funktionen übergibt. Das ist aber nicht schön.

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

                @ticaki said in Einfügen von JS-Klassen aus eigenem NPM-Modul:

                Jo, das hab ich jetzt alles hinbekommen.

                Was hast Du noch geändert im Vergleich zu zuvor?

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

                  @uwe72
                  Habs auf Github aktualisiert, aber die Verwendung von iobroker eigenen Befehlen endet in undefined oder error. Dafür müsste ich mich wohl durch den Javascript-Adapter arbeiten um zu verstehen wie das genau verarbeitet wird... Em nö 🙂

                  EDIT: Ach und eine Änderung am Importfile verlangt nach einem Restart der Javascriptinstanze.

                  U 2 Replies Last reply Reply Quote 0
                  • U
                    uwe72 @ticaki last edited by

                    @ticaki Danke! Ja, lib.js wird nun erstellt. Für alles weitere habe ich heute keine Zeit mehr.

                    Grüße!

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

                      @ticaki Ja, ist bei mir auch so. sendTo() beispielsweise wird dann im iobroker Script nicht erkannt.

                      lib.ts:

                      class Person {
                          private nachname: string;
                          private vorname: string;
                          private alter: string;
                          constructor(vorname, nachname, alter) {
                            this.vorname = vorname;
                            this.nachname = nachname;
                            this.alter = alter;
                          }
                        
                          information(): string {
                      
                                          sendTo("email.0", {
                                              from:    "uwe.clement@gmail.com",                                                                
                                              to:      "uwe.clement@gmail.com",
                                              subject: "Test1",
                                              html: "test2"
                                          });	
                      
                            return (`Mein Name ist ${this.vorname} ${this.nachname} und ich bin ${this.alter} Jahre alt!!!!1`);
                          }
                          log(): void {
                              log(this.information());
                          }
                        }
                       
                      module.exports = { Person};
                      

                      ioBroker:

                      const { Person } = require('/opt/iobroker/my_scripts/lib.js');
                      const myPerson = new Person();
                      log(myPerson.information());
                      

                      Fehlermeldung:

                      javascript.1
                      2023-11-12 12:45:31.652	error	at Script.runInContext (node:vm:135:12)
                      
                      javascript.1
                      2023-11-12 12:45:31.652	error	at script.js.common.TEST_IMPORT_TS:8:14
                      
                      javascript.1
                      2023-11-12 12:45:31.652	error	at Person.information (/opt/iobroker/my_scripts/lib.js:8:9)
                      
                      javascript.1
                      2023-11-12 12:45:31.652	error	ReferenceError: sendTo is not defined
                      
                      javascript.1
                      2023-11-12 12:45:31.652	error	^
                      
                      javascript.1
                      2023-11-12 12:45:31.652	error	sendTo("email.0", {
                      
                      javascript.1
                      2023-11-12 12:45:31.652	error	script.js.common.TEST_IMPORT_TS: /opt/iobroker/my_scripts/lib.js:8
                      
                      U 1 Reply Last reply Reply Quote 0
                      • U
                        uwe72 @uwe72 last edited by uwe72

                        @fastfoot Wollte Dich fragen, ob Du eine Idee hast wie man es schafft im externen Script "Dinge" wie sendTo() zu verwenden? Extern lässt es sich fehlerfrei in ein *.js umwandeln. In Iobroker wird der Inhalt des Scriptes angemeckert, da sendTo() unbekannt.

                        U F 2 Replies Last reply Reply Quote 0
                        • U
                          uwe72 @uwe72 last edited by

                          Unter https://forum.iobroker.net/topic/69840/erledigt-typescript-viele-common-global-scripte-cpu/34?_=1699802021717
                          gibt's ganz unten auch noch Informationen zum Thema. Sorry, dass dies nun parallelisiert ist.

                          1 Reply Last reply Reply Quote 0
                          • F
                            fastfoot @uwe72 last edited by

                            @uwe72 nee, keine Idee. Damit Funktionen genutzt werden können müssen Sie halt vorher importiert werden

                            OliverIO 1 Reply Last reply Reply Quote 0
                            • OliverIO
                              OliverIO @fastfoot last edited by

                              @fastfoot

                              zur vollständigkeit, hier die Lösung
                              https://forum.iobroker.net/topic/69840/erledigt-typescript-viele-common-global-scripte-cpu/36

                              U 1 Reply Last reply Reply Quote 2
                              • U
                                uwe72 @OliverIO last edited by

                                Habe für mich mal eine funktionierende Lösung in einem neuen Post aufgeschrieben:
                                https://forum.iobroker.net/topic/78632/info-auslagerung-von-scripte-aus-global-aufs-filesystem

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

                                Support us

                                ioBroker
                                Community Adapters
                                Donate

                                773
                                Online

                                31.7k
                                Users

                                79.7k
                                Topics

                                1.3m
                                Posts

                                4
                                51
                                2698
                                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