Accessing RPC inside a RPC

If you are asking a question, please follow this template:

  1. My goal is: [Accessing device info and transfer it over MQTT]
  2. My actions are:
    So basically I want to access Sys.GetInfo RPC locally and send its response over mqtt and here is code I try :-

`

let  deviceInfo = "test" ;
RPC.addHandler('Token_verify', function(args) {	

RPC.call(RPC.LOCAL, 'Sys.GetInfo', null, function(resp, ud) {
  deviceInfo =  JSON.stringify(resp);
}, null); 
if (typeof(args) === 'object' && typeof(args.token) === 'number') {
  MQTT.pub('my/topic',args.token );
  MQTT.pub('my/topic',deviceInfo );
return deviceInfo;
} else {
return "Fail"
}
})

`

  1. The result I see is:
    First time I call this RPC it returns test and second time it return device structure.

  2. My expectation & question is: So here expectation is result should be returned correctly at first time.

Thanks

PS: I am new to js.

The problem here is that the rpc calls are asynchronous, and deviceInfo will get a value AFTER the handler function will be finished. When you call Token_verify the second time, deviceInfo will have the value returned by the first call.

The solution is to pass args as the user data argument of RPC.call. The arguments of the handler should be resp, err_code, err_msg, ud, not resp, ud.

IMO, a better way to handle embedded rpc calls is in C/C++ where you have more control on handling errors and even returning data. Fortunately, in this case, Sys.GetInfo will never fail.

just checking if this is the best way to get the current IP address?
I tried it and it doesn’t appear to be working for me.

 RPC.call(RPC.LOCAL, 'Sys.GetInfo', null, function( resp, err_code, err_msg, ud) {
   deviceInfo =  JSON.stringify(resp);
 }, null);

Note the above is not the example mentioned in the docs ie
https://mongoose-os.com/docs/mongoose-os/howtos/get-device-info.md

Cfg.get('wifi.sta.ip');

let myip = Cfg.get('wifi.sta.ip'); // current ip address

this value is blank after boot-up

It will stop being blank once you get an IP. Your station is using WiFi and getting an address via DHCP, you will not have an IP address until you get it from the DHCP server

I did a [ mos call config.get ] after wifi connection from console and value is still blank

mos callwill try to connect to your device over the serial port somehow, and that most likely includes resetting it to establish the connection. Try using RPC instead: call via HTTP or WS or run your mJS code after the proper network event has fired

thanks I have issued the Cfg.get('wifi.sta.ip') after MQTT connection so it will/should have an IP address

further experimentation the following works !!

	let id = Cfg.get('device.id'); // friendly name??
		RPC.call(id, 'Sys.GetInfo', null, function(resp, err_code, err_msg, ud) {
			   if (err_code !== 0) {
				   print("Error: (" + JSON.stringify(err_code) + ') ' + err_msg);
				 } else {
				   print('Result: ' + JSON.stringify(resp));
				 }
				
				 MQTT.pub(topicDevice+"/config" ,  JSON.stringify({"device": deviceid ,"ip": resp.wifi.sta_ip })  );
			}, null);

This doesn’t work using RPC Loop-back address

	RPC.call(RPC.LOCAL, 'Sys.GetInfo', null, function(resp, err_code, err_msg, ud) {
		   if (err_code !== 0) {
			   print("Error: (" + JSON.stringify(err_code) + ') ' + err_msg);
			 } else {
			   print('Result: ' + JSON.stringify(resp));
			 }
			
			 MQTT.pub(topicDevice+"/config" ,  JSON.stringify({"device": deviceid ,"ip": resp.wifi.sta_ip })  );
		}, null);