MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    A little help with my own module

    Scheduled Pinned Locked Moved Development
    7 Posts 3 Posters 3.4k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      kichilron
      last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • R Offline
        roramirez Core Contributors
        last edited by

        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-Template

        Maybe some pieces of code there can help you.

        Easy module development with MagicMirror Module Template

        1 Reply Last reply Reply Quote 0
        • K Offline
          kichilron
          last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • tosti007T Offline
            tosti007 Module Developer
            last edited by

            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?

            If there is anything don't hesitate to contact me!
            ProfileSwitcher, TouchNotifications

            K 1 Reply Last reply Reply Quote 0
            • K Offline
              kichilron @tosti007
              last edited by

              @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.

              tosti007T 1 Reply Last reply Reply Quote 0
              • tosti007T Offline
                tosti007 Module Developer @kichilron
                last edited by

                @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.

                If there is anything don't hesitate to contact me!
                ProfileSwitcher, TouchNotifications

                K 1 Reply Last reply Reply Quote 0
                • K Offline
                  kichilron @tosti007
                  last edited by

                  @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.

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Enjoying MagicMirror? Please consider a donation!
                  MagicMirror created by Michael Teeuw.
                  Forum managed by Sam, technical setup by Karsten.
                  This forum is using NodeBB as its core | Contributors
                  Contact | Privacy Policy