• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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.

Use of array within all module files

Scheduled Pinned Locked Moved Development
10 Posts 2 Posters 1.3k Views 2 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.
  • B Offline
    bdeelman
    last edited by bdeelman Apr 6, 2021, 11:04 AM Apr 6, 2021, 10:46 AM

    Hi,

    I convert a CSV file to a JSON array at the node_helper.js file. This array (subscriptions) is needed at several functions at the mmm-sample.js file.

    The array is available at other functions within the node_helper.js I use this.subscriptions = (jsonObj); within the CSVtoJSON function and declare var subscriptions = []; at the top of nodehelper.js and also at the mmm-sample.js.

    How can I achieve this? Do I need the sendSocketNotification functionality to send data back and forth between the files?

    Any help is much appreciated.

    Regards,
    Bert

    S 1 Reply Last reply Apr 6, 2021, 11:53 AM Reply Quote 1
    • S Away
      sdetweil @bdeelman
      last edited by sdetweil Apr 6, 2021, 11:54 AM Apr 6, 2021, 11:53 AM

      @bdeelman correct. the helper gets data, and sends it to the module to use

      I use the csvtojson library in my MyCovid19 module.

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      1 Reply Last reply Reply Quote 0
      • B Offline
        bdeelman
        last edited by Apr 7, 2021, 7:07 AM

        Hi Sam,

        I take a look at that code. I can read it. ;) But can’t find the answer.

        This are my thougts. From start() I send

        this.sendSocketNotification('CONFIG', this.config);
        

        The this.config payload is needed at the node_helper.js at two places. One to figure out the csv file and do the MQTT stuff.

        CSVToJSON({ ignoreEmpty: true, trim: true })
        		.fromFile(CSVfile)
        		.then((jsonObj) => {
        			//console.log(jsonObj);
        			this.subscriptions = (jsonObj); //JSON.parse
        			this.setconfig(payload);
        		})
        

        I forward the this.config to the MQTT function. The array subscriptions is available there. At the MQTT function:

        self.sendSocketNotification('MQTT_PAYLOAD', {
        				topic: topic,
        				value: value,
        				subs: self.subscriptions
        				)};
        

        These payloads arrived within the function at the module file. But I can not use the payload outside of that specific function.

        I am not a programmer as you can see…just using reverse engineering and read a lot of configurations. I am almost there, I think.

        Regards,
        Bert

        S 2 Replies Last reply Apr 7, 2021, 11:02 AM Reply Quote 0
        • S Away
          sdetweil @bdeelman
          last edited by Apr 7, 2021, 11:02 AM

          @bdeelman you would save the config value in a variable usable by all routines in the source file.

          u do not need to save in the modulename.js, as it is provided as this.config

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          B 1 Reply Last reply Apr 7, 2021, 11:34 AM Reply Quote 0
          • B Offline
            bdeelman @sdetweil
            last edited by Apr 7, 2021, 11:34 AM

            @sdetweil can you be more precise? I did not understand you.

            The subscriptions is the generate json array with all kind of information to build an element / divs etc. That array is build at node_helper.js and globally needed within modulename.js.

            As I understand the way of working well…?

            Regards,
            Bert

            1 Reply Last reply Reply Quote 0
            • S Away
              sdetweil @bdeelman
              last edited by Apr 7, 2021, 11:40 AM

              @bdeelman said in Use of array within all module files:

              These payloads arrived within the function at the module file. But I can not use the payload outside of that specific function.

              so in the receiving function, store the payload value somewhere that other functions can use it

              this.subscriptions=payload

              similar for the config info in the node_helper

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              B 1 Reply Last reply Apr 8, 2021, 7:32 AM Reply Quote 0
              • B Offline
                bdeelman @sdetweil
                last edited by Apr 8, 2021, 7:32 AM

                @sdetweil Thanks for your reply. Much appreciated!

                I think I need another approach to get the result that I need. The subscriptions array is (must be) part of the this.config. So I add subscriptions: [];to the defaults: at the modulename.js

                The thing now is add the generated JSON (csvtojson) to the this.config. :grinning_face_with_big_eyes: and I can use this.config as a payload.

                The array is something like this:

                subscriptions = [{
                    topic: 'homey/light/keuken/dimmer-fibaro-1/onoff', // MQTT Topic name
                    label: 'Licht keuken',
                    type: 'device',
                    display: { left: 50, top: 50  },
                  },
                {
                    topic: 'homey/sensor/washok/temp-hum-xiaomi-1/measure-temperature',
                    label: 'Washok Temp',
                    type: 'label',
                    display: { left: 265, top: 0, prefix: "", suffix: "°C", decimals: 0 },
                }
                
                S 1 Reply Last reply Apr 8, 2021, 10:30 AM Reply Quote 0
                • S Away
                  sdetweil @bdeelman
                  last edited by sdetweil Apr 8, 2021, 10:30 AM Apr 8, 2021, 10:30 AM

                  @bdeelman sorry I do not understand what you want

                  u can send anything with sendSocketNotification and you can store it anywhere, not just in this.config

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  B 1 Reply Last reply Apr 8, 2021, 1:50 PM Reply Quote 0
                  • B Offline
                    bdeelman @sdetweil
                    last edited by bdeelman Apr 8, 2021, 2:00 PM Apr 8, 2021, 1:50 PM

                    @sdetweil I am sorry and lost. ;)

                    The sendSocketNotification from node_helper.js

                    self.sendSocketNotification('MQTT_PAYLOAD', {
                    				topic: topic,
                    				value: value,
                    				subs: self.subscriptions
                    				)};
                    

                    So if I understand correctly…I should add this.subscriptions = payload; at the receiving function socketNotificationReceived at modulename.js

                    socketNotificationReceived: function(notification, payload) {
                    	this.subscriptions = payload;
                      if (notification === 'MQTT_PAYLOAD') {
                    // 
                    //
                      }
                    },
                    

                    I did that but it is not working. The array is not available at other functions within modulename.js.

                    At the node_helper part it is working. this.subscriptions = (jsonObj);and I can use the array at other functions. with

                    var self = this;
                    var l = self.subscriptions.length; // as an example.
                    
                    S 1 Reply Last reply Apr 8, 2021, 2:06 PM Reply Quote 0
                    • S Away
                      sdetweil @bdeelman
                      last edited by Apr 8, 2021, 2:06 PM

                      @bdeelman so, I would open the developers window., ctrl-shift-i on the pi MM window
                      select the sources tab, navigate the left nav modules/your module name, select the source file name modulename.js

                      scroll to the line for the socketNotificationReceived().
                      this.

                      and click the number at the left edge of that line, should turn blue

                      then hit f5 to refresh the page, and MM will reload and you should stop at the line…

                      move your mouse over the word ‘this’, and it should expand a box describing what this points to , which should be THIS modules instance data…

                      if not, then THAT is the problem…

                      Sam

                      How to add modules

                      learning how to use browser developers window for css changes

                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      1 / 1
                      • First post
                        1/10
                        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