Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Entwicklung
    4. Adapter Entwicklung - cheerio - $ is not defined

    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

    Adapter Entwicklung - cheerio - $ is not defined

    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      darkiop Most Active last edited by

      Hallo zusammen,

      ich möchte in einem Adapter cheerio einsetzen mittels $ den DOM auslesen, das Skript funktioniert außerhalb des Adapters ohne Fehler.

      Wieso gibt es hier Probleme?

      /* jshint -W097 */ // jshint strict:false
      /*jslint node: true */
      'use strict';
      
      // https://nodejs.org/api/child_process.html
      const {
          exec
      } = require('child_process');
      
      var utils = require(__dirname + '/lib/utils'); // Get common adapter utils
      var adapter = new utils.Adapter('stiebel-lwz');
      
      // is called when adapter shuts down - callback has to be called under any circumstances!
      adapter.on('unload', function (callback) {
          try {
              adapter.log.info('cleaned everything up...');
              callback();
          } catch (e) {
              callback();
          }
      });
      
      // is called if a subscribed object changes
      adapter.on('objectChange', function (id, obj) {
          // Warning, obj can be null if it was deleted
          adapter.log.info('objectChange ' + id + ' ' + JSON.stringify(obj));
      });
      
      // is called if a subscribed state changes
      adapter.on('stateChange', function (id, state) {
      
          // Warning, state can be null if it was deleted
          adapter.log.info('stateChange ' + id + ' ' + JSON.stringify(state));
      
          /**
           * SENDE PARAMETER AN ISG
           */
      
          // Sende Parameter an ISGweb
          var parameter, wert;
      
          function set_isg_para(isgweburl, parameter, wert) {
              exec('bash /opt/iobroker/node_modules/iobroker.stiebel-lwz/isg_set.sh ' + isgweburl + ' ' + parameter + ' ' + wert, (err, stdout, stderr) => {
                  if (err) {
                      console.error(err);
                      return;
                  }
                  console.log(stdout);
              });
          }
      
          // BETRIEBSART
          if (id == adapter.name + '.' + adapter.instance + '.Start.Betriebsart') {
              adapter.log.info('Setze Betriebsart auf: ' + state.val);
              set_isg_para(adapter.config.isgIP, 'BETRIEBSART', state.val);
          }
          // LUEFTUNGSSTUFEN
          else if (id == adapter.name + '.' + adapter.instance + '.Einstellungen.Lueften.Lueftungsstufen.STUFE_TAG') {
              adapter.log.info('Setze Lueftungsstufe STUFE-TAG auf: ' + state.val);
              set_isg_para(adapter.config.isgIP, 'LUEFTERSTUFETAG', state.val);
          }
          // RAUM TEMP TAG
          else if (id == adapter.name + '.' + adapter.instance + '.Einstellungen.Heizen.Raumtemperaturen_HK1.RAUMTEMP_TAG') {
              adapter.log.info('Setze Raumtemperaturen_HK1 RAUMTEMP. TAG auf: ' + state.val);
              set_isg_para(adapter.config.isgIP, 'RAUMTEMPTAG', state.val);
          }
          // WARMWASSER TEMP TAG
          else if (id == adapter.name + '.' + adapter.instance + '.Einstellungen.Warmwasser.WW-Temperaturen.WW_SOLL_TAG') {
              adapter.log.info('Setze WW-Temperaturen WW SOLL TAG auf: ' + state.val);
              set_isg_para(adapter.config.isgIP, 'WARMWASSERTEMPTAG', state.val);
          }
      
      });
      
      // is called when databases are connected and adapter received configuration.
      adapter.on('ready', function () {
          main();
      });
      
      function main() {
      
          // Ausgabe der eingestellten IP im Log
          adapter.log.info('isgIP: ' + adapter.config.isgIP);
      
          /**
           * LESE PARAMETER AUS ISG
           */
      
          // Lade aktuelle Betriebsart bei Start in Objekt
          function load_isg_parameters() {
      
              // https://github.com/IonicaBizau/cheerio-req    
              const cheerioReq = require("cheerio-req");
      
              // BETRIEBSART
              cheerioReq('http://' + adapter.config.isgIP, (err, $) => {
                  var betriebsart = $('input#aval39').val();
                  adapter.setState('Start.Betriebsart', betriebsart, true);
                  adapter.log.info('Aktuelle Betriebsart:' + betriebsart);
              });
      
          } // end of load_isg_parameters()
      
          // TEST
          function ISG() {
      
              var cheerio = require('cheerio');
              var request = require('request');
      
              request('http://' + adapter.config.isgIP + '/?s=1,0', function (error, response, body) {
      
                  $ = cheerio.load(body);
      
                  var d = $("td.value");
      
                  //console.log('tds: ' + d);
      
                  // The parseFloat() function parses a string and returns a floating point number.
                  // The text property sets or returns the text of an option element.
                  // The trim() method removes whitespace from both sides of a string.
                  // The .eq() selector selects an element with a specific index number. The index numbers start at 0, so the first element will have the index number 0 (not 1).
      
                  var AUSSENTEMPERATUR = d.eq(6).text().trim();
                  AUSSENTEMPERATUR = removeDegreeUnit(AUSSENTEMPERATUR);
                  adapter.setState('TEST.AUSSENTEMPERATUR', AUSSENTEMPERATUR, true);
                  adapter.log('AUSSENTEMPERATUR: ' + AUSSENTEMPERATUR);
      
                  function checkFalseBool(input, checkvalue) {
                      var result = (input != checkvalue);
                      return Boolean(result);
                  }
      
                  function checkTrueBool(input, checkvalue) {
                      var result = input == checkvalue;
                      return Boolean(result);
                  }
      
                  function removeUnit(input, unitLength, unit) {
                      if (unit)
                          unit.valueOf = unit.toSource = unit.toString = input.substring(input.length - unitLength, input.length);
                      var value = input.substring(0, input.length - unitLength);
                      value = value.replace(/,/, ".");
                      return parseFloat(value);
                  }
      
                  function removeHourUnit(input, unit) {
                      return removeUnit(input, 2, unit);
                  }
      
                  function removePowerUnit(input, unit) {
                      return removeUnit(input, 4);
                  }
      
                  function removePressureUnit(input, unit) {
                      return removeUnit(input, 4);
                  }
      
                  function removeDegreeUnit(input, unit) {
                      return removeUnit(input, 2, unit);
                  }
      
              }); // end of request()
      
          } // end of ISG()
      
          if (adapter.config.loadISGwebParameters === true) {
              load_isg_parameters();
              ISG();
          }
      
          // in this stiebel-lwz all states changes inside the adapters namespace are subscribed
          adapter.subscribeStates('*');
      
      } // end of main()
      
      

      Log:

      2018-07-13 20:40:59.857 - error: Caught by controller[0]: ReferenceError: $ is not defined
      2018-07-13 20:40:59.858 - error: Caught by controller[0]: at Request._callback (/opt/iobroker/node_modules/iobroker.stiebel-lwz/stiebel-lwz.js:112:15)
      2018-07-13 20:40:59.858 - error: Caught by controller[0]: at Request.self.callback (/opt/iobroker/node_modules/iobroker.stiebel-lwz/node_modules/request/request.js:185:22)
      2018-07-13 20:40:59.858 - error: Caught by controller[0]: at emitTwo (events.js:106:13)
      2018-07-13 20:40:59.858 - error: Caught by controller[0]: at Request.emit (events.js:191:7)
      2018-07-13 20:40:59.858 - error: Caught by controller[0]: at Request. (/opt/iobroker/node_modules/iobroker.stiebel-lwz/node_modules/request/request.js:1157:10)
      2018-07-13 20:40:59.858 - error: Caught by controller[0]: at emitOne (events.js:96:13)
      2018-07-13 20:40:59.858 - error: Caught by controller[0]: at Request.emit (events.js:188:7)
      2018-07-13 20:40:59.858 - error: Caught by controller[0]: at IncomingMessage. (/opt/iobroker/node_modules/iobroker.stiebel-lwz/node_modules/request/request.js:1079:12)
      2018-07-13 20:40:59.858 - error: Caught by controller[0]: at IncomingMessage.g (events.js:292:16)
      2018-07-13 20:40:59.858 - error: Caught by controller[0]: at emitNone (events.js:91:20)
      2018-07-13 20:40:59.858 - error: host.zeus-vm-iobroker instance system.adapter.stiebel-lwz.0 terminated with code 0 (OK)
      
      
      1 Reply Last reply Reply Quote 0
      • apollon77
        apollon77 last edited by

        Ich denke du nutzt es nicht korrekt. Ich finde weder ein

        var $

        Noch ein

        const $

        Kann seien das nur ein var fehlt bei der einen Zuweisung?

        Gesendet vom Handy …

        1 Reply Last reply Reply Quote 0
        • D
          darkiop Most Active last edited by

          Das wars, die Deklaration des $. Desweiteren fehlt bei beim adapter.log noch ein .info …

          Ist ioBroker da etwas spezieller? Der Code lief im JS-Adapter auch ohne var/const

          1 Reply Last reply Reply Quote 0
          • apollon77
            apollon77 last edited by

            Naja, eher der JavaScript Adapter ist da spezieller.

            Dort läuft alles in einer sandbox und denke eher nicht als „strict“ deklariert was für Adapter eher im Standard do ist. Und all sowas ;-))

            Gesendet vom Handy …

            1 Reply Last reply Reply Quote 0
            • D
              darkiop Most Active last edited by

              Ok Super, vielen Dank!!

              Komme aktuell bei dem Adapter relativ schnell weiter - wäre super wenn du nochmal drüber schauen könntest und mir meine TODO entsprechend erweitern könntest damit der irgendwann mal offiziell Aufgenommen werden kann.

              Grüße, Thorsten

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

              Support us

              ioBroker
              Community Adapters
              Donate

              902
              Online

              31.9k
              Users

              80.3k
              Topics

              1.3m
              Posts

              2
              5
              527
              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