Read the statement by Michael Teeuw here.
A little help with my own module
-
Hey everyone,
I am new to this whole thing and I am having a few issues with a module I am trying to develop.
The aim here is to parse the information displayed on a server with cheerio. The original version of the method
updateSens()
is working (and still working), I tested it manually before trying to develop my own module. Everything else is working as it should, the communication between the node_helper and the module is fine, it updates as it should, but the entire block in the
request ({});
is not working at all.
Here you can find the entire module:
/* * Node Helper zur Abfrage von Probe- und Sensordaten * Requested Daten vom Monitoring Server und parsed * diese mithilfe von Cheerio * * Entwickelt von Sönke Thiele / kichilron */ // Definiere require const NodeHelper = require('node_helper'); const request = require('request'); const cheerio = require('cheerio'); // Definiere Konstanten für die Anzeige des Textes const gruenText = "Grün: "; const alarmText = "Alarm: "; const bestText = "Bestätigt: "; const pauseText = "Pausiert: "; const warnungText = "Warnung: "; const ungewText = "Ungewöhnlich: "; // Erzeuge den Helper module.exports = NodeHelper.create({ /* * Funktion, die die eigentliche Abfrage an den Monitoring Server stellt */ updateSens: function() { const self = this; self.sensorenTable = []; // Stelle Request an den Monitoring Server request('omitted', function (error, response, html) { // Überprüfe Status und Error Code von Server Response if (!error && response.statusCode == 200) { // Inhalt vom Monitoring Server fürs Parsen laden var $ = cheerio.load(html); // Finde Elemente, die "TreeOpen" heißen und iteriere über diese $('div.treeopen').each(function (i, element) { // Finde nächstes Element, welches unser nutzbares Element ist var a = $(this).next(); // Gib Text aus == Probe-Name var c = a.text(); // Wenn der Text "Gerät" nicht vorkommt, ist es der Name der Probe if (!(c.indexOf("Gerät") >= 0)) { var probeName = c; // Pushe ProbeName in den Array self.sensorenTable.push({ probe: true, loaded: true, probeName: c, }); // Sonst suchen wir nach Anzahl der Sensoren } else { // Gehe von treesens zwei Mal im Tree runter und suche nach Klasse "sens" var m = a.children().children().nextAll('div.sens'); // Finde erstes Child var erster = m.children().first(); var gruenSens = ""; var pauseSens = ""; var ungewSens = ""; var warnSens = ""; var bestSens = ""; var alarmSens = ""; var undefSens = ""; // Überprüfe, welche Art von Child das erste ist if (erster.hasClass("sg")) { gruenSens = gruenText + erster.text(); } else if (erster.hasClass("sr")) { alarmSens = alarmText + erster.text(); } else if (erster.hasClass("sb")) { pauseSens = pauseText + erster.text(); } else if (erster.hasClass("sp")) { ungewSens = ungewText + erster.text(); } else if (erster.hasClass("sy")) { warnSens = warnungText + erster.text(); } else if (erster.hasClass("so")) { bestSens = bestText + erster.text(); } else { undefSens = alarmText + erster.text(); } var naechster = erster.next(); while (!(naechster.text() == "")) { if (naechster.hasClass("sg")) { gruenSens = gruenText + naechster.text(); } else if (naechster.hasClass("sr")) { alarmSens = alarmText + naechster.text(); } else if (naechster.hasClass("sb")) { pauseSens = pauseText + naechster.text(); } else if (naechster.hasClass("sp")) { ungewSens = ungewText + naechster.text(); } else if (naechster.hasClass("sy")) { pauseSens = pauseText + naechster.text(); } else if (naechster.hasClass("so")) { bestSens = bestText + naechster.text(); } else { undefSens = alarmText + naechster.text(); } // Nächste Zelle untersuchen in der While Schleife naechster = naechster.next(); } self.sensorenTable.push({ probe: false, loaded: true, gruenSens: gruenSens, alarmSens: alarmSens, pauseSens: pauseSens, ungewSens: ungewSens, pauseSens: pauseSens, bestSens: bestSens, undefSens: undefSens, }); } }); } else { // Abfrage vom Server fehlgeschlagen, Text zurückgeben self.sensorenTable.push({ probe: false, loaded: false, }); } }); // Senden der Tabelle an das Hauptmodul self.sendSocketNotification("Loaded", self.sensorenTable); // Update schedulen, um Abfrage erneut durchzuführen self.scheduleUpdate(10000); }, /* * Sobald die Notifikation vom Modul erhalten wurde * sollte ein Update gescheduled werden */ socketNotificationReceived: function(notification, payload) { const self = this; self.updateSens(); }, /* * Reiht das nächste Update ein, basierend auf Millisekunden */ scheduleUpdate: function(delay) { // Nächstes mal in 10 Sekunden / 10.000 Millisekunden laden var nextLoad = 10000; const self = this; // Timeout-Funktion definieren clearTimeout(this.updateTimer); this.updateTimer = setTimeout(function() { self.updateSens(); }, nextLoad); }, });
There still are a few things I have to work out, especially as I’m completely new to JS and had to learn everything from scratch. That’s probably also why I have a noob-ish error hidden somewhere, which every normal human being will find immediately.
Sadly, I can not give you the full link of the request, rest assured, that it’s working when I test it manually.
-
You can use the simple console.log for will check where is passing the code.
I working on a test module to help a new modules developers. You can see in
https://github.com/roramirez/MagicMirror-Module-TemplateMaybe some pieces of code there can help you.
-
Hey, thanks for your reply.
I have already figured out that the entire request-block is the problem and was hoping for someone to look over it and maybe find what my problem with it is. There must be some logical error hidden inside there somewhere - which I don’t know of, as I only just started working with JS.
Which means I am as far as knowing that entire request-block isn’t executed at all, which I know because not even if I add log-commands directly after the request-statement, will they be executed.
-
I am really not familiair with requests and such, but I did notice this line:
var a = $(this).next();
Are you sure you want to use this here and not self?
-
@tosti007 Hi, yeah - what I’m trying to achieve with that line is to work with the each statement. I’m working within the scope of the each loop and try to find the next object after the one that was found there in the document.
-
@kichilron alright :)
Does your console show any errors when you run your code?
Since the callback isn’t called at all it might also be a problem with the
request("omitted"
part. Try doing something simple (only log something) inside the callback. If it doesn’t show up then I think the problem lays in the request function that might be called wrong. -
@tosti007 I will have to try again, but what I did was similar to what you suggested.Atleast I believe it was.
The thing that’s mainly bothering me is the fact, that the same code executed on its own and not automated within the node_helper is working fine.