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.

    Module Position

    Scheduled Pinned Locked Moved Development
    34 Posts 5 Posters 15.6k Views 4 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.
    • M Offline
      maxbachmann
      last edited by maxbachmann

      'use strict';
      /* global Module */
      
      /* Magic Mirror
       * Module: MMM-Snips
       *
       * By Max Bachmann
       * MIT Licensed.
       */
      
      Module.register('MMM-Snips', {
      
        //defaultvalues that get taken when not defined differently in the config file
        defaults: {
          mqttServer: 'mqtt://192.168.178.35',
          topic: 'hermes/intent/captn2:module_hide_show',
        },
      
        start: function() {
          let self = this;
          Log.info('Starting module: ' + self.name);
          self.loaded = false;
          self.updateMqtt(self);
        },
      
        //call the Node_helper
        updateMqtt: function(self) {
          self.sendSocketNotification('MQTT_SERVER', 
            {mqttServer: self.config.mqttServer, topic: self.config.topic });
        },
      
        socketNotificationReceived: function(notification, payload) {
          let self = this;
          
          if (notification === 'HIDE_SHOW_MOVE') {
            
            //create a JSON object and retrieve the modulename, wether to show or hide it,
            //the position to move it to and wether it should get prepended or appended
            const obj = JSON.parse(payload.data.toString());
            let HideShow;
              for (let i = 0; i < obj.slots.length; ++i){
                if (obj.slots[i].slotName === "HIDE" ||
                  obj.slots[i].slotName === "SHOW") {
                  HideShow = obj.slots[i].slotName;
                }
            }
      
            let modulename;
            for (let i = 0; i < obj.slots.length; ++i){
              if (obj.slots[i].slotName === "MODULE") {
                modulename = obj.slots[i].value.value;
      	  break;
              }
            }
      
            let newposition;
            let TopBottom;
            for (let i = 0; i < obj.slots.length; ++i){
              if (obj.slots[i].slotName === "POSITION") {
                newposition = obj.slots[i].value.value;
                for (let i = 0; i < obj.slots.length; ++i){
                  if (obj.slots[i].slotName === "TOPBOTTOM") {
                    TopBottom = obj.slots[i].value.value;
      	      break;
                  }
                }
      	  break;
              }
            }
      
            // When the Module is know and where to move it it get's moved to the new position
            if (newposition != null && modulename != null){
              MM.getModules().enumerate((module) => {
                if (module.name === modulename) {
      	    const instance = document.getElementById(module.identifier);
      	    const region = document.querySelector(`div.region.${targetRegion} div.container`);
      	    if (TopBottom === "BOTTOM"){
                    region.appendChild(instance);
                  }else{
      	      region.insertBefore(instance, region.childNodes[0])
                  }
                  region.style.display = 'block';
      	  }
              });
            }
      
            self.loaded = true;
      
            // When the Module is know and wether to show or hide it then it gets shown/hidden
            if (HideShow != null && modulename != null){
              switch(modulename) {
      	  case "PAGEONE":
      	          
      	    break;
      	  case "PAGETWO":
      	          
      	    break;
      	  case "PAGETHREE":
      	          
      	    break;
      	  case "PAGEFOUR":
      	          
      	    break;
      	  default:
      	    self.sendNotification(HideShow + "_" + modulename);
      	}
            }
          }
        }
      });
      
      
      'use strict';
      
      /* Magic Mirror
       * Module: MMM-Snips
       *
       * By Max Bachmann
       * MIT Licensed.
       */
      const NodeHelper = require('node_helper');
      let mqtt = require('mqtt');
      
      module.exports = NodeHelper.create({
        start: function() {
          console.log('MMM-mqtt_display started ...');
        },
      
        getMqtt: function(payload) {
          let self = this;
          //connects to a mqtt client
          let client = mqtt.connect(payload.mqttServer);
      
          //after connecting to the mqtt client subscribes to 
          //'hermes/intent/captn2:module_hide_show'
          client.on('connect', function() {
            client.subscribe(payload.topic);
          });
      
          //When a message arrives that is in the right topic, the voicesession gets continued
          //by sending a message with session id and ah answer text to 
          //'hermes/dialogueManager/continueSession', so the hotword is not needed again
          //Afterwards the original message gets send back to MMM-Snips
          client.on('message', function(topic, message) {
            if (topic === 'hermes/intent/captn2:module_hide_show') {
              const messagestring = message.toString();
      	const obj = JSON.parse(messagestring);
      	let continueobj = {
      	  "sessionId":"",
      	  "text":"Kann ich sonst noch etwas für dich tun?"
      	}
      	continueobj.sessionId = obj.sessionId;
      	client.publish('hermes/dialogueManager/continueSession',
      	  JSON.stringify(continueobj))
            	self.sendSocketNotification('HIDE_SHOW', {'data':messagestring})
            }
          });
        },
      
        //after getting called the getMqtt function gets called
        socketNotificationReceived: function(notification, payload) {
          let self = this;
          if (notification === 'MQTT_SERVER') {
            self.getMqtt(payload);
          }
        }
      });
      

      Thats my full code I use: Are there still things that should be different for performance, or just because thats not a usual way to do it ;)

      1 Reply Last reply Reply Quote 0
      • M Offline
        maxbachmann
        last edited by

        @maxbachmann said in Module Position:

        self.sendNotification(HideShow + “_” + modulename);

        tells the other module to hide. I saw there is the function
        modulename.hide() , but I could not get it to work. Whats the name I have to use there to make it work?

        strawberry 3.141S 1 Reply Last reply Reply Quote 0
        • strawberry 3.141S Offline
          strawberry 3.141 Project Sponsor Module Developer @maxbachmann
          last edited by

          @maxbachmann

          I can see that you are still refering to targetRegion, but it’s nowhere defined.

          const region = document.querySelector(`div.region.${targetRegion} div.container`);
          

          modulename.hide() can’t work, in your case modulename is just a string and a string doesn’t have a hide function, what you want instead is calling the hide function of the module instance, which would be in your case something like:

          MM.getModules().enumerate((module) => {
              if (module.name === modulename) {
          	module.hide();
              }
          });
          

          There are more things to do, but what I spotted shouldn’t break anything.

          Please create a github issue if you need help, so I can keep track

          1 Reply Last reply Reply Quote 0
          • M Offline
            maxbachmann
            last edited by

            1. yes already saw that yesterday forgot to replace it ;)

            2. yes decided to do some majopr changes to improve it (hope to finish them today)

            3. I am new to javascript why I asked because I don’t need to learn the stuff in a wrong way from start off. So any tips on improving the code that are a general thing so that I should think off when doing other stuff with javascript I am really interested in

            1 Reply Last reply Reply Quote 1
            • S Offline
              shbatm Module Developer
              last edited by shbatm

              @maxbachmann -

              This version of the MMM-Carousel module can move modules to different position on different “slides” – the slides can be changed automatically (interval based) or manually (keyboard/navigation buttons) or via Socket Notifications.

              See the very bottom of the README file for how to set it up in your config.

              1 Reply Last reply Reply Quote 0
              • M Offline
                maxbachmann
                last edited by

                @shbatm looks nice ;) I might use this slide indicator for my module, thats a really nice idea

                strawberry 3.141S 1 Reply Last reply Reply Quote 0
                • strawberry 3.141S Offline
                  strawberry 3.141 Project Sponsor Module Developer @maxbachmann
                  last edited by

                  @maxbachmann If you create a github repo, than I can make suggestions what you could improve

                  Please create a github issue if you need help, so I can keep track

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    maxbachmann
                    last edited by

                    @strawberry-3-141 I will when I finished, but have to do some other stuff for the speech recognition first. Will let you know when I posted it on github

                    strawberry 3.141S 1 Reply Last reply Reply Quote 0
                    • strawberry 3.141S Offline
                      strawberry 3.141 Project Sponsor Module Developer @maxbachmann
                      last edited by

                      @maxbachmann you dont need to wait until its finished, git is a version control, so it should be used as early as possible.

                      Please create a github issue if you need help, so I can keep track

                      1 Reply Last reply Reply Quote 0
                      • M Offline
                        maxbachmann
                        last edited by

                        @strawberry-3-141 yes but I will probably use the voice control in a different way than I hought I would. Thats why I am not sure how I will change the code. Makes not much sence to waste your time on correcting the faults when I change pretty much every part of the program again right afterwards (and have to get used to git since I did not use it yet)

                        1 Reply Last reply Reply Quote 0
                        • 1
                        • 2
                        • 3
                        • 4
                        • 1 / 4
                        • 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