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

Async Functions, Sockets, Arrays, and Figuring this out

Scheduled Pinned Locked Moved Troubleshooting
3 Posts 3 Posters 261 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.
  • J Offline
    jcsindelar
    last edited by Mar 21, 2021, 4:47 AM

    Hello,
    I am fairly new to Javascript, and I am working on developing my first MMM. I am trying to pass the values from an array into an existing stock ticker module. My array is populated dynamically with values from a user’s portfolio, thus the values inside the array are returned from an Async function. Here is the code snippet from the async function:

    (async() => {
    const getAcctResult = await tdaclient.accounts.getAccount(configGetAcct);

    const acctPositions = await lod.get(getAcctResult, 'securitiesAccount.positions');
    const acctSymbols = await lod.map(acctPositions, 'instrument.symbol');
    acctSymbols.forEach(function(value) {
        if (value.length <= 4)
        {
            tickerArray.push(value);
        }
    });
    

    I would like to pass the first 5 values from “tickerArray,” and feed them into this existing function in the node_helper.js file:

    sendRequest: function (urls) {
    	var self = this;
    
    	var results = {};
    
    	async.eachSeries(urls, function(url, done) {
    		request({ url: url, method: "GET" }, function (error, response, body) {
    			if (!error && response.statusCode == 200) {
    				var result = JSON.parse(body);
    				if(result["Error Message"]) {
    					console.log("No such symbol!");
    				} else {
    					var meta = result["Meta Data"];
    					if(!meta){
    						return;
    					}
    					var data = result["Time Series (Daily)"];
    					var compName = meta["2. Symbol"];
    					var count = 0;
    					for (var key in data) {
    						if (!data.hasOwnProperty(key)) {continue;}
    						var obj = data[key];
    						if(!results[compName]){
    							results[compName] = [];
    						}
    						results[compName].push(obj);
    						count++;
    						if(count == 2) {
    							break;
    						}
    					}
    				}
    			}
    			done();
    		});
    
    	}, function(err) {
    		if (err) {
    			throw err;
    		}
    		self.sendSocketNotification("STOCK_RESULT", results);
    	});
    },
    

    Effectively, I aim to replace the variable “urls” with tickerArray. The URLs variable is passed in from the config file. By nature this data in the config file is static, and I would like to make it dynamic. This is probably too big of an ask, but if anyone here has a clue on how I can figure this out let me know.

    The original module for the stock ticker is here:

    https://github.com/hakanmhmd/MMM-Stock

    Again, all I really want to do is replace the default array with a dynamic version from the result of my above async function. If this module simply won’t play nice, I can probably eventually just write my own. Thats said, it just seems like it would be a lot easier if I could swap the config data with my array.

    Thanks for any help or thoughts you can offer me, and if you need more information let me know.

    S L 2 Replies Last reply Mar 21, 2021, 12:41 PM Reply Quote 0
    • S Offline
      sdetweil @jcsindelar
      last edited by sdetweil Mar 21, 2021, 12:46 PM Mar 21, 2021, 12:41 PM

      @jcsindelar doesn’t look too hard. u are just missing the step that builds the url string from the stock symbol.

      the existing function assumes an array of urls.

      instead of storing the stock symbol, store the url with the stock symbol. the array doesn’t care.
      then after the foreach loop call the existing function, passing tickerArray

      also the acct symbols to tickerarray is checking the length of the stock symbol not the ticker array

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      1 Reply Last reply Reply Quote 0
      • L Offline
        lavolp3 Module Developer @jcsindelar
        last edited by lavolp3 Mar 24, 2021, 2:50 PM Mar 24, 2021, 2:44 PM

        @jcsindelar if I understand correctly you want to call stockdata from a dynamic and changing range of stocks.
        In some modules the array is provided by the config with every call to node_helper, in some modules just at the start.

        Another option would be to include a notification in the stock ticker module and use that to change the array.
        I’m currently working on a successor to MMM-AVStock that also has a ticker.
        If you’re interested we could implement your request into the code.
        Meaning: your module sends a notification “CHANGE_STOCKS” and my module receives it and acts accordingly.

        notificationReceived: function(noti, payload) {
          if (noti === "CHANGE_STOCKS") {
            this.config.symbols = payload
            this.sendSocketNotification("GET_STOCKDATA", payload)
          }
        }
        

        However, consider that the actual config will obviously not be safed in case of restart. But I guess your module will do the rest.

        How to troubleshoot modules
        MMM-soccer v2, MMM-AVStock

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