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

      @uwe72
      Morgen kann ich das mal ausprobieren, aber ich bin schon vor 2h müde gewesen. 🙂

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

        @ticaki Danke dir!! 🙂

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

          @ticaki @fastfoot

          Anbei nochmals das nicht funktionierende Beispiel und den aktuellen Stand in übersichtlicher Form:

          Hier mein externes TS-File.

          class Buch  {
          
              public getCurrentWeekdayAsString() : string {
                  var now = new Date();
                  let weekday = now.getDay();
          
                              sendTo("email.0", {
                                  from:    "uwe.clement@gmail.com",                                                                
                                  to:      "uwe.clement@gmail.com",
                                  subject: "Test1",
                                  html: "test2"
                              });	
          
          
                  return this.getWeekdayAsString(weekday);
              }
          
              public getWeekdayAsString(weekday: number) : string {
                  let weekdayAsString;
                  if (weekday == 1) {
                      weekdayAsString = "Montag";
                  } else if (weekday == 2) {
                      weekdayAsString = "Dienstag";
                  } else if (weekday == 3) {
                      weekdayAsString = "Mittwoch";
                  } else if (weekday == 4) {
                      weekdayAsString = "Donnerstag";
                  } else if (weekday == 5) {
                      weekdayAsString = "Freitag";
                  } else if (weekday == 6) {
                      weekdayAsString = "Samstag";
                  } else if (weekday == 7) {
                      weekdayAsString = "Sonntag";
                  } else if (weekday == 0) {
                      weekdayAsString = "Sonntag";
                  }
                  return weekdayAsString;
              }  
          
          }
          
          module.exports = { Buch};
          

          Ohne das "sendTo" funktionierte es. Mit eben nicht.

          Habe im gleichen Verzeichnis diese Datei tsconfig.json:

          {
            "compileOnSave": true,
            "compilerOptions": {
              "noEmit": true,
              "allowJs": true,
              "checkJs": true,
              "module": "commonjs",
              "moduleResolution": "node",
              "esModuleInterop": true,
              "resolveJsonModule": true,
              "strict": true,
              "noImplicitAny": false,
              "target": "es2018",
              "typeRoots": [
                ".iobroker/types",
                "node_modules/@types"
              ]
            },
            "include": [
              "**/*.js",
              "**/*.ts",
              ".iobroker/types/javascript.d.ts"
            ],
            "exclude": [
              "node_modules/**"
            ]
          }
          

          Auf der Konsole gebe ich folgendes ein:

          npx tsc --build tsconfig.json
          

          Führt zu diesen Fehlern:

          buch.ts:7:21 - error TS2304: Cannot find name 'sendTo'.
          
          7                     sendTo("email.0", {
                                ~~~~~~
          
          buch.ts:42:1 - error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
          
          42 module.exports = { Buch};
             ~~~~~~
          
          
          Found 2 errors.
          

          Beide Dateien liegen im gleichen Verzeichnis, in dem ich auch den Befehl "npx tsc --build tsconfig.json" eingebe:
          4ccda497-e58f-4019-8f71-ac27a25da5e7-image.png

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

            @uwe72

            "include": [
                "**/*.js",
                "**/*.ts",
                ".iobroker/types/javascript.d.ts"
              ],
            

            da muß die javascript.d.ts datei sein. Kannst das natürlich anpassen.

            1 Reply Last reply Reply Quote 0
            • 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

                                        1.2k
                                        Online

                                        31.7k
                                        Users

                                        79.7k
                                        Topics

                                        1.3m
                                        Posts

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