NEWS

[gelöst] XML in Datenpunkte aufteilen



  • @Zygi
    clients ist ein Array. Verwende besser die for-Schleife:

            let clients = result.oscam.status[0].client;
            for(let i = 0; i < clients.length; i++) {
                let j = i + 1;
                if(j < 10) j = "0" + j;
                setState("javascript.0.Oscam.Client" + j + ".01-Name", clients[i].$.name);
                setState("javascript.0.Oscam.Client" + j + ".02-Type", clients[i].$.type);
                setState("javascript.0.Oscam.Client" + j + ".03-Protocol", clients[i].$.protocol);
                setState("javascript.0.Oscam.Client" + j + ".04-AU", clients[i].$.au);
            }
    

  • Starter

    Danke für die ganze Hilfe! Es läuft nun wie ich es mir vorgestellt habe.

    Hier mal der Code falls jemand etwas ähnliches vor hat.

    var imax, cmax;
    function myScript() {  // zusätzliche Zeile am Anfang
    const parser = require('xml2js').parseString;
    exec('wget -qO - -q -t=5 --user=admin --password=XXXX "http://192.168.10.1:83/oscamapi.html?part=status"', function (error, result, stderr) {
        let werte = result;
        parser(werte, function (err, result) {
             let clients = result.oscam.status[0].client;
                for(let i = 0; i < clients.length; i++) {
                     let j = i + 1;
                     if(j < 10) j = "0" + j;
                     imax=j
                    setState("javascript.0.Oscam.Client" + j + ".01-Name", clients[i].$.name);
                    setState("javascript.0.Oscam.Client" + j + ".02-Type", clients[i].$.type);
                    setState("javascript.0.Oscam.Client" + j + ".03-Protocol", clients[i].$.protocol);
                    setState("javascript.0.Oscam.Client" + j + ".04-AU", clients[i].$.au);   		
                }
            });
            imax = parseFloat(imax);
            while (!(imax > 10)) {
            cmax = ("0" + imax).slice(-2)
                setState("javascript.0.Oscam.Client"+cmax+".01-Name", '');
                setState("javascript.0.Oscam.Client"+cmax+".02-Type", '');
                setState("javascript.0.Oscam.Client"+cmax+".03-Protocol", '');
                setState("javascript.0.Oscam.Client"+cmax+".04-AU", '');
            imax = imax + 1;
        }
    });
    };
    schedule('*/5 * * * *', myScript);  // zusätzliches CRON
    

  • Starter

    @paul53

    Ich hab mich nachdem das ganze mit Oscam gut läuft mal an TVHeadend gewagt, habe aber ein komisches Problem. Und zwar läuft das Skript und trägt auch alles passend in Objekte ein. Ich bekomme aber im Log Warnungen. Wenn ich statt "setState" die Sachen in der Console ausgebe bekomme ich keine Warnungen.

    XML input

    <currentload>
    <systemload>0.770000,0.550000,0.570000</systemload>
    <recordings>
    <recording>
    <start>
    <date>2020/03/23</date>
    <time>19:30</time>
    <unixtime>1584988200</unixtime>
    <extra_start>240</extra_start>
    </start>
    <stop>
    <date>2020/03/24</date>
    <time>06:00</time>
    <unixtime>1585026000</unixtime>
    <extra_stop>240</extra_stop>
    </stop>
    <title>MediaShop</title>
    <status>Recording</status>
    </recording>
    </recordings>
    <subscriptions>1</subscriptions>
    </currentload>
    

    Skirpt

    const parser = require('xml2js').parseString;
    exec('wget -qO - -q -t=5 --user=TVH --password=xxxx "http://192.168.5.206:9981/status.xml"', function (error, result, stderr) {
    let werte = result
    parser(werte, function (err, result) {
       let xml1 = result.currentload;
            if (xml1.recordings != '\n') {
                let xml2 = result.currentload.recordings[0].recording;
                for(let i = 0; i < xml2.length; i++) { 
                let j = i + 1;
                if(j < 2) j = "0" + j; 
                    j = ("0" + j).slice(-2)
                    setState("javascript.0.TVHeadend.Record" + j + ".01-Name", xml2[i].title);
                    setState("javascript.0.TVHeadend.Record" + j + ".02-Start", xml2[i].start[0].time); 
                    setState("javascript.0.TVHeadend.Record" + j + ".03-Ende", xml2[i].stop[0].time);     
                    }
            }
     setState("javascript.0.TVHeadend.Zustand.01-Zustand", xml1.subscriptions)   
    });
    });
    

    Fehler im Log

    22:35:00.928	info	javascript.0 (3741) Stop script script.js.TVH.Status_abrufen
    22:35:01.082	info	javascript.0 (3741) Start javascript script.js.TVH.Status_abrufen
    22:35:01.104	info	javascript.0 (3741) script.js.TVH.Status_abrufen: registered 0 subscriptions and 0 schedules
    22:35:01.132	warn	javascript.0 (3741) at script.js.TVH.Status_abrufen:12:17
    22:35:01.137	warn	javascript.0 (3741) at script.js.TVH.Status_abrufen:4:1
    22:35:01.145	warn	javascript.0 (3741) at script.js.TVH.Status_abrufen:13:17
    22:35:01.150	warn	javascript.0 (3741) at script.js.TVH.Status_abrufen:4:1
    22:35:01.157	warn	javascript.0 (3741) at script.js.TVH.Status_abrufen:14:17
    22:35:01.162	warn	javascript.0 (3741) at script.js.TVH.Status_abrufen:4:1
    22:35:01.170	warn	javascript.0 (3741) at script.js.TVH.Status_abrufen:17:2
    22:35:01.175	warn	javascript.0 (3741) at script.js.TVH.Status_abrufen:4:1
    


  • @Zygi
    Das Log ist wenig aussagekräftig. Mehr Informationen erhält man im Tab "Log".

    Zu Zeile 10: Eine führende "0" fügt man Zahlen < 10 hinzu.


  • Starter

    @paul53

    Hier mal aus dem Log. Er scheint zu versuchen ein Array in einen String zu schreiben, die Frage ist aber wieso ich jetzt ein Array ausgegeben bekomme und bei dem anderen Skript nicht?

    2020-03-24 17:00:48.099  - info: javascript.0 (726) Start javascript script.js.TVH.Status_abrufen
    2020-03-24 17:00:48.128  - info: javascript.0 (726) script.js.TVH.Status_abrufen: registered 0 subscriptions and 0 schedules
    2020-03-24 17:00:48.154  - warn: javascript.0 (726) You are assigning a array to the state "javascript.0.TVHeadend.Record01.01-Name" which expects a string. Please fix your code to use a string or change the state type to array. This warning might become an error in future versions.
    2020-03-24 17:00:48.159  - warn: javascript.0 (726)     at setState (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1404:20)
    2020-03-24 17:00:48.160  - warn: javascript.0 (726)     at script.js.TVH.Status_abrufen:12:17
    2020-03-24 17:00:48.160  - warn: javascript.0 (726)     at Parser.<anonymous> (/opt/iobroker/node_modules/xml2js/lib/parser.js:304:18)
    2020-03-24 17:00:48.161  - warn: javascript.0 (726)     at Parser.emit (events.js:198:13)
    2020-03-24 17:00:48.161  - warn: javascript.0 (726)     at SAXParser.onclosetag (/opt/iobroker/node_modules/xml2js/lib/parser.js:262:26)
    2020-03-24 17:00:48.162  - warn: javascript.0 (726)     at emit (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:624:35)
    2020-03-24 17:00:48.162  - warn: javascript.0 (726)     at emitNode (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:629:5)
    2020-03-24 17:00:48.162  - warn: javascript.0 (726)     at closeTag (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:889:7)
    2020-03-24 17:00:48.163  - warn: javascript.0 (726)     at SAXParser.write (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:1436:13)
    2020-03-24 17:00:48.163  - warn: javascript.0 (726)     at Parser.exports.Parser.Parser.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:323:31)
    2020-03-24 17:00:48.163  - warn: javascript.0 (726)     at Parser.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:5:59)
    2020-03-24 17:00:48.164  - warn: javascript.0 (726)     at exports.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:369:19)
    2020-03-24 17:00:48.164  - warn: javascript.0 (726)     at script.js.TVH.Status_abrufen:4:1
    2020-03-24 17:00:48.165  - warn: javascript.0 (726)     at ChildProcess.exithandler (child_process.js:285:7)
    2020-03-24 17:00:48.165  - warn: javascript.0 (726)     at ChildProcess.emit (events.js:198:13)
    2020-03-24 17:00:48.165  - warn: javascript.0 (726)     at maybeClose (internal/child_process.js:982:16)
    2020-03-24 17:00:48.166  - warn: javascript.0 (726)     at Socket.stream.socket.on (internal/child_process.js:389:11)
    2020-03-24 17:00:48.166  - warn: javascript.0 (726)     at Socket.emit (events.js:198:13)
    2020-03-24 17:00:48.166  - warn: javascript.0 (726)     at Pipe._handle.close (net.js:607:12)
    2020-03-24 17:00:48.167  - warn: javascript.0 (726) You are assigning a array to the state "javascript.0.TVHeadend.Record01.02-Start" which expects a string. Please fix your code to use a string or change the state type to array. This warning might become an error in future versions.
    2020-03-24 17:00:48.170  - warn: javascript.0 (726)     at setState (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1404:20)
    2020-03-24 17:00:48.171  - warn: javascript.0 (726)     at script.js.TVH.Status_abrufen:13:17
    2020-03-24 17:00:48.171  - warn: javascript.0 (726)     at Parser.<anonymous> (/opt/iobroker/node_modules/xml2js/lib/parser.js:304:18)
    2020-03-24 17:00:48.172  - warn: javascript.0 (726)     at Parser.emit (events.js:198:13)
    2020-03-24 17:00:48.172  - warn: javascript.0 (726)     at SAXParser.onclosetag (/opt/iobroker/node_modules/xml2js/lib/parser.js:262:26)
    2020-03-24 17:00:48.173  - warn: javascript.0 (726)     at emit (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:624:35)
    2020-03-24 17:00:48.173  - warn: javascript.0 (726)     at emitNode (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:629:5)
    2020-03-24 17:00:48.173  - warn: javascript.0 (726)     at closeTag (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:889:7)
    2020-03-24 17:00:48.174  - warn: javascript.0 (726)     at SAXParser.write (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:1436:13)
    2020-03-24 17:00:48.174  - warn: javascript.0 (726)     at Parser.exports.Parser.Parser.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:323:31)
    2020-03-24 17:00:48.174  - warn: javascript.0 (726)     at Parser.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:5:59)
    2020-03-24 17:00:48.175  - warn: javascript.0 (726)     at exports.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:369:19)
    2020-03-24 17:00:48.175  - warn: javascript.0 (726)     at script.js.TVH.Status_abrufen:4:1
    2020-03-24 17:00:48.175  - warn: javascript.0 (726)     at ChildProcess.exithandler (child_process.js:285:7)
    2020-03-24 17:00:48.176  - warn: javascript.0 (726)     at ChildProcess.emit (events.js:198:13)
    2020-03-24 17:00:48.176  - warn: javascript.0 (726)     at maybeClose (internal/child_process.js:982:16)
    2020-03-24 17:00:48.176  - warn: javascript.0 (726)     at Socket.stream.socket.on (internal/child_process.js:389:11)
    2020-03-24 17:00:48.177  - warn: javascript.0 (726)     at Socket.emit (events.js:198:13)
    2020-03-24 17:00:48.177  - warn: javascript.0 (726)     at Pipe._handle.close (net.js:607:12)
    2020-03-24 17:00:48.178  - warn: javascript.0 (726) You are assigning a array to the state "javascript.0.TVHeadend.Record01.03-Ende" which expects a string. Please fix your code to use a string or change the state type to array. This warning might become an error in future versions.
    2020-03-24 17:00:48.181  - warn: javascript.0 (726)     at setState (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1404:20)
    2020-03-24 17:00:48.181  - warn: javascript.0 (726)     at script.js.TVH.Status_abrufen:14:17
    2020-03-24 17:00:48.182  - warn: javascript.0 (726)     at Parser.<anonymous> (/opt/iobroker/node_modules/xml2js/lib/parser.js:304:18)
    2020-03-24 17:00:48.183  - warn: javascript.0 (726)     at Parser.emit (events.js:198:13)
    2020-03-24 17:00:48.183  - warn: javascript.0 (726)     at SAXParser.onclosetag (/opt/iobroker/node_modules/xml2js/lib/parser.js:262:26)
    2020-03-24 17:00:48.184  - warn: javascript.0 (726)     at emit (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:624:35)
    2020-03-24 17:00:48.184  - warn: javascript.0 (726)     at emitNode (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:629:5)
    2020-03-24 17:00:48.184  - warn: javascript.0 (726)     at closeTag (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:889:7)
    2020-03-24 17:00:48.185  - warn: javascript.0 (726)     at SAXParser.write (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:1436:13)
    2020-03-24 17:00:48.185  - warn: javascript.0 (726)     at Parser.exports.Parser.Parser.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:323:31)
    2020-03-24 17:00:48.185  - warn: javascript.0 (726)     at Parser.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:5:59)
    2020-03-24 17:00:48.186  - warn: javascript.0 (726)     at exports.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:369:19)
    2020-03-24 17:00:48.186  - warn: javascript.0 (726)     at script.js.TVH.Status_abrufen:4:1
    2020-03-24 17:00:48.187  - warn: javascript.0 (726)     at ChildProcess.exithandler (child_process.js:285:7)
    2020-03-24 17:00:48.187  - warn: javascript.0 (726)     at ChildProcess.emit (events.js:198:13)
    2020-03-24 17:00:48.187  - warn: javascript.0 (726)     at maybeClose (internal/child_process.js:982:16)
    2020-03-24 17:00:48.188  - warn: javascript.0 (726)     at Socket.stream.socket.on (internal/child_process.js:389:11)
    2020-03-24 17:00:48.188  - warn: javascript.0 (726)     at Socket.emit (events.js:198:13)
    2020-03-24 17:00:48.189  - warn: javascript.0 (726)     at Pipe._handle.close (net.js:607:12)
    2020-03-24 17:00:48.190  - warn: javascript.0 (726) You are assigning a array to the state "javascript.0.TVHeadend.Zustand.01-Zustand" which expects a string. Please fix your code to use a string or change the state type to array. This warning might become an error in future versions.
    2020-03-24 17:00:48.192  - warn: javascript.0 (726)     at setState (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1404:20)
    2020-03-24 17:00:48.193  - warn: javascript.0 (726)     at script.js.TVH.Status_abrufen:17:2
    2020-03-24 17:00:48.193  - warn: javascript.0 (726)     at Parser.<anonymous> (/opt/iobroker/node_modules/xml2js/lib/parser.js:304:18)
    2020-03-24 17:00:48.194  - warn: javascript.0 (726)     at Parser.emit (events.js:198:13)
    2020-03-24 17:00:48.195  - warn: javascript.0 (726)     at SAXParser.onclosetag (/opt/iobroker/node_modules/xml2js/lib/parser.js:262:26)
    2020-03-24 17:00:48.195  - warn: javascript.0 (726)     at emit (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:624:35)
    2020-03-24 17:00:48.196  - warn: javascript.0 (726)     at emitNode (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:629:5)
    2020-03-24 17:00:48.196  - warn: javascript.0 (726)     at closeTag (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:889:7)
    2020-03-24 17:00:48.196  - warn: javascript.0 (726)     at SAXParser.write (/opt/iobroker/node_modules/xml2js/node_modules/sax/lib/sax.js:1436:13)
    2020-03-24 17:00:48.197  - warn: javascript.0 (726)     at Parser.exports.Parser.Parser.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:323:31)
    2020-03-24 17:00:48.197  - warn: javascript.0 (726)     at Parser.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:5:59)
    2020-03-24 17:00:48.198  - warn: javascript.0 (726)     at exports.parseString (/opt/iobroker/node_modules/xml2js/lib/parser.js:369:19)
    2020-03-24 17:00:48.198  - warn: javascript.0 (726)     at script.js.TVH.Status_abrufen:4:1
    2020-03-24 17:00:48.199  - warn: javascript.0 (726)     at ChildProcess.exithandler (child_process.js:285:7)
    2020-03-24 17:00:48.199  - warn: javascript.0 (726)     at ChildProcess.emit (events.js:198:13)
    2020-03-24 17:00:48.200  - warn: javascript.0 (726)     at maybeClose (internal/child_process.js:982:16)
    2020-03-24 17:00:48.200  - warn: javascript.0 (726)     at Socket.stream.socket.on (internal/child_process.js:389:11)
    2020-03-24 17:00:48.200  - warn: javascript.0 (726)     at Socket.emit (events.js:198:13)
    2020-03-24 17:00:48.201  - warn: javascript.0 (726)     at Pipe._handle.close (net.js:607:12)
    


  • @Zygi sagte:

    You are assigning a array to the state "javascript.0.TVHeadend.Zustand.01-Zustand" which expects a string.

    Davon gibt es mehrere Zeilen, in denen steht, was falsch ist. Bevor Du einen Wert mit setState() schreibst, schaue den Wert erst einmal per log an.

    EDIT:

                    setState("javascript.0.TVHeadend.Record" + j + ".01-Name", xml2[i].title[0]);
                    setState("javascript.0.TVHeadend.Record" + j + ".02-Start", xml2[i].start[0].time[0]); 
                    setState("javascript.0.TVHeadend.Record" + j + ".03-Ende", xml2[i].stop[0].time[0]);     
    

  • Starter

    @paul53

    Habe das ganze vorher alles im Log ausgegeben. Dort ist mir aber nicht bewusst aufgefallen das die Sachen wie folgt ausgegeben werden: ['Ausgabewert'] . Warum werden die Werte hier vom Parser anders ausgegeben?



  • @Zygi sagte :

    Warum werden die Werte hier vom Parser anders ausgegeben?

    Der Parser wandelt das um, was im XML vorhanden ist.


  • Starter

    @paul53
    Komisch ist, dass das Skript trotz Warnung richtig läuft. Alle Werte werden korrekt und ohne Klammern und Anführungszeichen in den State geschrieben. Die Warnungen würden aber den Log unnötig zumüllen.

    Gibt es eine einfache Methode ein Array in einen String umzuwandeln? Mit String.valueOf() bekomme ich einen Compile Fehler.



  • @Zygi sagte:

    Gibt es eine einfache Methode ein Array in einen String umzuwandeln?

    So wie ich es hier gezeigt habe: [0] anhängen.



Suggested Topics

1.9k
Online

32.4k
Users

39.0k
Topics

528.1k
Posts