developerforce / Force.com-Toolkit-for-Jasper

The Force.com Toolkit for Jasper is an Apex client library for the Jasper Control Center API
BSD 3-Clause "New" or "Revised" License
9 stars 8 forks source link

Force.com Toolkit for Jasper

The Force.com Toolkit for Jasper is an Apex client library for the Jasper Control Center API. The toolkit comprises

The toolkit is currently in an early stage of development, but all Jasper Control Center API operations should work.

Prerequisites

1. Jasper account credentials and SIM

You can get a trial Control Center account with test SIMs from any of Jasper’s partner operators, listed at http://www.jasper.com/operators/where-to-get

2. Salesforce Environment

Either a sandbox, or a Developer Edition. If you don't already have one, sign up and click the activation link in the email.

Installation

The easiest way to install the toolkit is via an unmanaged package. Click this link, login to your Salesforce environment, if necessary, and follow the instructions.

You can also clone the GitHub project and deploy the code into your Salesforce environment via the Force.com IDE, the Force.com CLI or the Force.com Migration Tool.

1. Configure Jasper credentials

On Salesforce:

NOTE - the settings may be displayed in alphabetical order - password above username!

2. Configure Push API

A Visualforce Page acts as the endpoint for automation rules in Jasper.

On Salesforce:

On Jasper:

3. Create Device Records

By default, device records must be created manually, although this, of course, could be automated. Go to the device list in Jasper, and, for each device of interest, copy the ICCID and create a Device record in Salesforce. Optionally, you can set the Asset lookup on the device record.

4. Schedule Retrieval of Device Usage

The toolkit includes a schedulable Apex class, GetDataUsage, that retrieves data, SMS and voice usage data from Control Center. You can schedule the class to run daily:

The job will retrieve cumulative monthly statistics from Jasper and calculate the daily data, SMS and voice usage. Note - if you start the job partway through the month, them the usage for the first day will be the usage for the month so far. You can correct the usage records manually if required.

Running the Sample

  1. Click ‘Jasper’ in the app menu.

  2. Navigate to a Device by clicking the Devices tab and selecting one, or clicking from the related list of an Asset.

  3. You should see the Device Visualforce page. Note that the device status, and other data is retrieved from Jasper dynamically as the page loads, and is not stored in Salesforce.

  4. You can change the status of the SIM - note, this takes effect immediately, no ‘Save’ required. The change is not obvious on a phone, but can be seen in the Jasper terminal.

  5. Change the status again in the Jasper terminal.

  6. The device page should automatically update (via the Streaming API) and you should see the updated status and new SIM State Change record.

  7. If the Status is ‘Activated’ you can send an SMS to the phone.

Getting Started with the Control Center API

Retrieving the Jasper Control Center credentials

JasperSettings__c settings = JasperSettings__c.getInstance(UserInfo.getUserId());

Common API Operations

// Get the terminal API client
JasperTerminal.TerminalPort terminalPort = 
    new JasperTerminal.TerminalPort(
        settings.Username__c, 
        settings.Password__c, 
        settings.License_Key__c,
        settings.API_Server__c);

// Get a list of terminals
JasperAPI.iccids_element terminals = terminalPort.GetModifiedTerminals(null, null);
for (String iccid : terminals.iccid) {
    // ICCID identifies a SIM card
    System.debug(iccid);
}

// Get more detail on the terminals
JasperAPI.terminals_element terminalDetails = terminalPort.GetTerminalDetails(iccids);    
for (JasperAPI.TerminalType terminal : terminalDetails.terminal) {
    System.debug(terminal.iccid + ' status is ' + terminal.status);
}

// Activate a SIM card
// Change type '3' sets the status - see JasperAPI.xsd for details of change types,
// SIM status values etc
JasperAPI.EditTerminalResponse_element res = terminalPort.EditTerminal(iccid, 
    null, 'ACTIVATED_NAME', '3');
System.debug('SIM ' + res.iccid + ' changed, effective ' + res.effectiveDate);

// Get the SMS API client
JasperTerminal.SmsPort smsPort = 
    new JasperTerminal.SmsPort(
        settings.Username__c, 
        settings.Password__c, 
        settings.License_Key__c,
        settings.API_Server__c);

// Send an SMS
Long smsMsgId = smsPort.SendSMS(iccid, 'Hello world!', null);
System.debug('Sent SMS. Message ID = ' + smsMsgId);

// Get SMS details
JasperAPI.smsMsgIds_element msgIds = new JasperAPI.smsMsgIds_element();
msgIds.smsMsgId = new List<Long>{smsMsgId};
JasperAPI.smsMessages_element messages = smsPort.GetSMSDetails(msgIds);
System.debug('SMS ' + messages.smsMessage[0].smsMsgId + ' status is ' + messages.smsMessage[0].status);

Notes

  1. Jasper APIs should be considered single-threaded; there is nothing in the current implementation to prevent concurrent Jasper API calls.

  2. The JasperPush Visualforce Page accepts XML in the ‘data’ form parameter and creates a record representing the Jasper event. This record creation can then be used in a trigger, workflow, process, etc. At present, a single event type, ‘SIM State Change’, is supported. To add more, create an SObject to hold the event, with name corresponding to the rule trigger in Jasper (e.g. ‘SIM Data Limit’), and fields for whatever you’re sending in the email. Extend the objectNames and fieldNames maps in the JasperPushController Apex class to handle the new event. The Jasper notification messages are defined in the Jasper API Schema; each has a corresponding type ending 'InfoType', for example, SimImeiChangeInfoType.

  3. See Integrating the Jasper Control Center API with Force.com for screenshots and a demo.