How to call a variable in another function?

Former Member
Former Member

How to call a variable in another function in same class?

var temperature;
var toTemperature;

function onUpdate(dc) {
    dc.drawBitmap(38, 65, Ui.loadResource( Rez.Drawables.cloud_icon ));
    dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_GREEN);
    dc.drawText(50, 85, Graphics.FONT_XTINY, toTemperature+"°", Graphics.TEXT_JUSTIFY_CENTER);
}

function onTemperature(ie) {
    temperature = ie[0]["current"]["temperature"];
    toTemperature = temperature.toString();
}

because toTemperature still null

  • Former Member
    0 Former Member in reply to jim_m_58

    I set onReceiveData function in temperatureMonitoringRequest (same class with onReceiveData in model) :

    function temperatureMonitoringRequests() {
        if (Toybox has :Communications) {
            ie = null; 
            Comm.makeWebRequest(temperatureMonitoringURL,
            {}, 
            {}, 
            method(:onReceiveData));
        }else {
            Sys.println("Communication\nnot\npossible");
        } 
    }

  • But what class are you doing the makeWebRequest and onReceiveData in?  In the case of a watch face, it would need to be in the background service.  Where is ie actually defined (var ie;)

  • Former Member
    0 Former Member in reply to jim_m_58

    I call the makeWebRequest and onReceiveData functions in the model class (TemperatureMonitoringModel). And I defined ie also in TemperatureMonitoringModel.

    In TemperatureMonitoringApp, I do like this :

    function onStart(state) {
        View = new TemperatureMonitoringView();
        Model = new TemperatureMonitoringModel(View.method(:onTemperature));
        Delegate = new TemperatureMonitoringDelegate(Model);
    }

  • What type of app is this? (watch face, widget, device-app).  Does it have a background service?  Is the background permission on?  If so, onStart() is called by both the main app as well as the background.

    It's sounding like your issue isn't using a variable in the same class, but in a different class or different instance of the class.  I'm really confused with your Delegate, as a input/behavior delegate is in it's own class.

  • Former Member
    0 Former Member in reply to jim_m_58

    application type is widget. I have added BluetoothLowEnergy and Communication permissions in manifest.xml. I seem to have found the problem, when the widget first appeared. it will contain null, but when I press the menu button and onSelect(item), it will show. What's wrong?

  • You don't need the Bluetooth permission if you are just doing makeWebRequests.  That's used if you're doing things with the Bluetooth module.

    I don't know what you are doing in onMenu or onSelect, but my guess is that on your onReceiveData function you need to do WatchUi.requestUpdate() at the end so onUpdate() will be called.

  • Former Member
    0 Former Member in reply to jim_m_58

    ok bro, I got it. For the last question. How do I update the API URL when the user select the menu in onselect(item) contained in Ui.Menu2InputDelegate into the model I have created?

    which I have done like this:

    This is MainMenuInputDelegate

    using Toybox.WatchUi as Ui;
    using Toybox.System as Sys;
    
    class MainMenuInputDelegate extends Ui.Menu2InputDelegate {
    	var jakartaAPI = "https://jakarta.com";
    	var surabayaAPI = "https://surabaya.com";
    	var medanAPI = "https://medan.com";
    	var bandungAPI = "https://bandung.com";
    	var semarangAPI = "https://semarang.com";
    	var palembangAPI = "https://palembang.com";
    	var makassarAPI = "https://makassar.com";
    	var bandarLampungAPI = "https://bandarlampung.com";
    	var batamAPI = "https://batam.com";
    	var pekanbaruAPI = "https://pekanbaru.com";
    	var padangAPI = "https://padang.com";
    	var malangAPI = "https://malang.com";
    	
        function initialize() {
            Menu2InputDelegate.initialize();
        }
    
        function onSelect(item) {
        	if( item.getId().equals("menuJakarta") ) {
            	Sys.println(jakartaAPI);
            } else if( item.getId().equals("menuSurabaya") ) {
            	Sys.println(surabayaAPI);
            } else if( item.getId().equals("menuMedan") ) {
            	Sys.println(medanAPI);
            } else if( item.getId().equals("menuBandung") ) {
            	Sys.println(bandungAPI);
            } else if( item.getId().equals("menuSemarang") ) {
            	Sys.println(semarangAPI);
            } else if( item.getId().equals("menuPalembang") ) {
            	Sys.println(palembangAPI);
            } else if( item.getId().equals("menuMakassar") ) {
            	Sys.println(makassarAPI);
            } else if( item.getId().equals("menuBandarLampung") ) {
            	Sys.println(bandarLampungAPI);
            } else if( item.getId().equals("menuBatam") ) {
            	Sys.println(batamAPI);
            } else if( item.getId().equals("menuPekanbaru") ) {
            	Sys.println(pekanbaruAPI);
            } else if( item.getId().equals("menuPadang") ) {
            	Sys.println(padangAPI);
            } else if( item.getId().equals("menuMalang") ) {
            	Sys.println(malangAPI);
            }
        }
    }

    And this is :

    using Toybox.Communications as Comm;
    using Toybox.System as Sys;
    
    class IndonesiaTemperatureMonitoring {
    	var sampleData = {};
    }
    
    class IndonesiaTemperatureMonitoringModel {
    	var ie = null;
    	var indonesiaTemperatureMonitoringURL = "<API_IN_HERE>";
    	var notify;
    
      	function initialize(handler)	{
      	   	notify = handler;
            indonesiaTemperatureMonitoringRequests();    				 
        }
        
        function indonesiaTemperatureMonitoringRequests() {
    		if (Toybox has :Communications) {
    			ie = null; 
    			Comm.makeWebRequest(indonesiaTemperatureMonitoringURL,
     			{}, 
     			{}, 
     			method(:onReceiveData));
    		}else {
          		Sys.println("Communication\nnot\npossible");
          	} 
        }
    
    	function onReceiveData(responseCode, data) {
            if(responseCode == 200) {
            	if(ie == null) {
                	ie = new IndonesiaTemperatureMonitoring();
    			}
             	for( var i = 0; i < 1; i++ ) {
             		ie.sampleData = [data];
    			}
               	notify.invoke(ie.sampleData);
            }else {
        		Sys.println("Data request failed\nwith response : ");
        		Sys.println(responseCode);
            }
        }
    }

  • you could pass the class that contains the URL to the menu delegate, and then in the delegate do something like "thatClass.url=malangAPI;"

    I'm not sure what you're aiming to do here. but could the GPS location be used to determine the URL?

  • Have you looked into using OpenWeatherMaps?  The API Key is free, and you pass it a lat/lon as the location, which is easy to do in a widget. (you can just start GPS)

  • Former Member
    0 Former Member in reply to jim_m_58

    I'm still confused bro, can you explain in more detail? what I made, it's different from OpenWeatherMaps