Foundations.Comms.PalmCall

Wrappers for async Ajax calls and a Palm bus service call. All calls are made asynchronously and are implemented using 'Futures', Palm's mechanism for async calls. See the Futures documentation for more information. The examples provided with each call demonstrate how Futures are used.


PalmCall.call

PalmCall.call is a wrapper for making a call to a service on the Palm message bus. This can be done with or without a subscription.

Syntax

    future PalmCall.call(service, method, parameters);

Parameters

Argument Required Type Description
service Yes string Service to call, i.e., "com.palm.db"
method Yes string Service method to call.
parameters Yes object Service method parameters as JSON object.

Returns

Response object from service.

Example 1 - Get device's unique ID from system properties

The device ID, or nduid, is a 20-byte (160 bit) SHA-1 digest unique to each device. You can obtain it from the com.palm.preferences service.

var libraries = MojoLoader.require({ name: "foundations", version: "1.0" });
var Future    = libraries["foundations"].Control.Future;
var PalmCall  = libraries["foundations"].Comms.PalmCall;

var future1 = PalmCall.call("palm://com.palm.preferences/systemProperties", "Get", {"key": "com.palm.properties.nduid" });
future1.then(function(future)
{
  var result = future.result;
  if (result.returnValue == true)
  {
      //** The "com.palm.properties.nduid" field contains periods, which makes object dot notation references problematic.
      //** Instead, since I know it is the first name/value field, I am going to "split" it out.
      var result1 = JSON.stringify(result);
      var resultArray = result1.split('"');
      Mojo.Log.info("Unique Device ID = " + resultArray[3]);
  }
  else Mojo.Log.info("Failure = " + JSON.stringify(result));
});

Example 1 Output

Unique Device ID = 8fe37d5a9c7793af12fc0f6a129470721027d81b

Example 2 - Subscribed service call

The following is a Triton script example that subscribes to the locations service, gets a few position fixes, then quits.

To handle a subscribed service call using PalmCall you need to make sure to add a new then() clause after each response comes in, and, to do that, you need to use a named function (rather than an anonymous function) as the argument to then().

var libraries = MojoLoader.require({ name: "foundations", version: "1.0" });
var Future    = libraries["foundations"].Control.Future;   
var PalmCall  = libraries["foundations"].Comms.PalmCall;

function main(args) {
  startApplicationLoop();
  var fixCount=3;
  Mojo.Log.info("Getting "+fixCount+" position fixes");
  
  var fixFuture = PalmCall.call("palm://com.palm.location", "startTracking",
      {subscribe: true});
  
  fixFuture.onError(function(future) {
      Mojo.Log.info("Error: "+future.exception);
      future.then(function() {
          quit();
      });
  });
  fixFuture.then(function print_it(future) {
      Mojo.Log.info("Got a fix:");
      for (var key in future.result) {
          Mojo.Log.info("\t"+key+":\t"+future.result[key]);
      }
      if (--fixCount > 0) {
          //register a new "then" with this same function
          future.then(print_it);
      } else {
          // cancel service handle
          PalmCall.cancel(fixFuture);
          quit();
      }
  });
}