NEWS
Adapter Entwicklung - cheerio - $ is not defined
-
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) -
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
-
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
Hey! Du scheinst an dieser Unterhaltung interessiert zu sein, hast aber noch kein Konto.
Hast du es satt, bei jedem Besuch durch die gleichen Beiträge zu scrollen? Wenn du dich für ein Konto anmeldest, kommst du immer genau dorthin zurück, wo du zuvor warst, und kannst dich über neue Antworten benachrichtigen lassen (entweder per E-Mail oder Push-Benachrichtigung). Du kannst auch Lesezeichen speichern und Beiträge positiv bewerten, um anderen Community-Mitgliedern deine Wertschätzung zu zeigen.
Mit deinem Input könnte dieser Beitrag noch besser werden 💗
Registrieren Anmelden