Code Samples

Login and Logout: Java Sample Code

This sample Java Login and Logout code was created in Eclipse using the Axis2 library to convert the WSDLs to service proxies/stubs.

package com.yourcompany.m2mexample;

import org.apache.axis2.AxisFault;
import java.rmi.RemoteException;
import org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory;
import org.apache.axiom.om.OMElement;

import com.nphase.unifiedwebservice.v2.SessionServiceStub;
import com.nphase.unifiedwebservice.v2.LogIn;
import com.nphase.unifiedwebservice.v2.LogInResponse;
import com.nphase.unifiedwebservice.v2.LogOut;
import com.nphase.unifiedwebservice.v2.LogOutResponse;

import org.datacontract.schemas._2004._07.nphase_unifiedwebservice_apis_v2_contract_sessionservice.LogInRequest;
import org.datacontract.schemas._2004._07.nphase_unifiedwebservice_apis_v2_contract_sessionservice.LogOutRequest;

public class Wns {

  private static String sessionToken;
  private static SessionServiceStub stub;

  private static void Login(String username, String password) {

    // Create a login request object that will make the API call.
    // The LogInRequest class is autogenerated from the WSDL file.
    LogInRequest request = new LogInRequest();

    // Set the username and password for the request
    request.setUsername(username);
    request.setPassword(password);

    //Create a LogIn object to use the LogInRequest
    LogIn loginObj = new LogIn();
    loginObj.setInput(request);

    try {
      stub = new SessionServiceStub();

      //Send the request and get the response
      LogInResponse response = stub.logIn(loginObj);

      //Store the session token from the response
      sessionToken = response.getOutput().getSessionToken();
 
    } catch (AxisFault e1) {
      System.out.println(e1.getReason());
      System.out.println(e1.getFaultCodeElement());
    } catch (RemoteException e) {
      System.out.println(e.getMessage());
    }
  }

  private static void Logout() {
    LogOutRequest request = new LogOutRequest();
    request.setSessionToken(sessionToken);
    LogOut logoutObj = new LogOut();
    logoutObj.setInput(request);

    try {
      // Add the session token to the request header
      SOAP11Factory f = new SOAP11Factory();
      OMElement e = f.createOMElement("token", f.createOMNamespace("http://nphase.com/unifiedwebservice/v2", "v2"));
      e.setText(sessionToken);
      stub._getServiceClient().addHeader(e);

      // Send the request using the SessionServiceClient object created at login
      LogOutResponse response = stub.logOut(logoutObj);

    } catch (AxisFault e1) {
      // ...
    } catch (RemoteException e) {
      // ...
    }
  }

  public static void main(String[] args) {
    Wns.Login("username", "password");
    Wns.Logout();
  }
}

Login: C# Sample Code

The following C# sample code logs in to the ThingSpace Platform and gets a session token to use in further API request. It uses the ClientProxy and CommmonLib class libraries that are included in the SDK. This code was tested in Microsoft Visual Studio 2010, 2012, and 2013. Note that there is a one-line change for VS2013, which is in the code but commented out.

using CommonLib;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Configuration;

using ClientProxy.SessionService;

namespace M2MSampleCode
{
  class Wns
  {
    private string sessionToken;
    private SessionServiceClient sessionService;

    private void Login(string username, string password)
    {
      // Create a login request object that will make the API call.
      // The LogInRequest class is autogenerated from the wsdl file.
      LogInRequest request = new LogInRequest();

      // Set the username and password for the request.
      request.Username = username;
      request.Password = password;

      // Create a SessionServiceClient object. 
      // The SessionServiceClient class is autogenerated from the wsdl file.
      sessionService = new SessionServiceClient();

      // Add a behavior to the sessionService endpoint that will add
      // the session token to the header of all SessionService requests.
      // AddSessionTokenToHeaderBehavior() comes from the CommonLib library.

      sessionService.Endpoint.Behaviors.Add(new AddSessionTokenToHeaderBehavior(""));
      //VS2013 version
      //sessionService.Endpoint.EndpointBehaviors.Add(new AddSessionTokenToHeaderBehavior(""));

      try
      {
        // Call the LogIn() method with the request object to
        // send the request and get the response.
        LogInResponse response = sessionService.LogIn(request);

        // The token is a string that you get from the response.
        sessionToken = response.SessionToken;

        // Set the session token for the AddSessionToTokenBehavior in the endpoint.
        var requestHeader = sessionService.Endpoint.Behaviors.First(b => b is AddSessionTokenToHeaderBehavior);
        ((AddSessionTokenToHeaderBehavior)requestHeader).SessionToken = response.SessionToken;
      }
      // The service will return SOAP faults as fault exceptions,
      // such as for an invalid password
      catch (FaultException ex)
      {
        Console.WriteLine("FaultException:" + ex.Message);
      }
      // The service can throw a timeout exception (for instance, if the
      // SessionServiceClient endpoint address is not correct).
      catch (System.TimeoutException ex)
      {
        Console.WriteLine("TimeoutException:" + ex.Message);
      }
    }

    static void Main(string[] args)
    {
      Wns session = new Wns();
      session.Login("username", "password");
    }
  }
}

ChangeDeviceState-Activate: C# Sample Code

This first code example builds the pieces of the activation request from the inside out, starting with the innermost device IDs and creating the objects that contain the inner objects.

private void ActivateDevice(string account, string svcPlanCode)
{
  // Create a CarrierServiceClient object.
  CarrierServiceClient carrierService = new CarrierServiceClient();

  // Add the session token from login to the header.
  carrierService.Endpoint.Behaviors.Add(new AddSessionTokenToHeaderBehavior(sessionToken));
  // VS2013 version
  // carrierService.Endpoint.EndpointBehaviors.Add(new AddSessionTokenToHeaderBehavior(sessionToken));

  // Create the two Device Identifier objects required for a 4G device.
  // You must use the fully qualified name for the DeviceIdentifier class because
  // DeviceService and CarrierService both have DeviceIdentifier classes
  ClientProxy.CarrierService.DeviceIdentifier deviceId1 = new ClientProxy.CarrierService.DeviceIdentifier();
  deviceId1.Kind = "imei";
  deviceId1.Identifier = "990003420535573";
  ClientProxy.CarrierService.DeviceIdentifier deviceId2 = new ClientProxy.CarrierService.DeviceIdentifier();
  deviceId2.Kind = "iccid";
  deviceId2.Identifier = "89148000000800784257";

  // Put the Device Identifier objects into a list of identifiers.
  var deviceIds = new List<ClientProxy.CarrierService.DeviceIdentifier> { deviceId1, deviceId2 };

  // Create a DeviceIdentifierCollection object to hold the list of device identifiers.
  ClientProxy.CarrierService.DeviceIdentifierCollection deviceIdCollection = new
    ClientProxy.CarrierService.DeviceIdentifierCollection();
  deviceIdCollection.DeviceIdentifiers = deviceIds;

  // Create a list of DeviceIdentifierCollection objects to pass to ChangeDeviceState.
  // This is the list of all devices to be changed (only one in this case).
  var deviceList = new List<ClientProxy.CarrierService.DeviceIdentifierCollection> { deviceIdCollection };

  // Create request to change the device state.
  ChangeDeviceStateRequest cdsRequest = new ChangeDeviceStateRequest();
  cdsRequest.Activate = new ActivateDeviceRequest();
  cdsRequest.AccountName = account;
  cdsRequest.DeviceList = deviceList; //list of devices
  cdsRequest.Activate.ServicePlan = svcPlanCode;
  cdsRequest.Activate.MdnZipCode = "98115";

  try
  {
    // Send the activation request.
    ChangeDeviceStateResponse cdsResponse = carrierService.ChangeDeviceState(cdsRequest);

    // Store the returned request ID for later use.
    string requestId = cdsResponse.RequestId;

  }
  catch (FaultException ex)
  {
    Console.WriteLine("FaultException:" + ex.Message);
  }
  catch (System.TimeoutException ex)
  {
    Console.WriteLine("TimeOutException:" + ex.Message);
  }
}

This second example builds the request from the outside in, starting with the outer containing object and working in toward the device IDs.

private void ActivateDevice(string account, string svcPlanCode)
{
  // Create a CarrierServiceClient object.
  CarrierServiceClient carrierService = new CarrierServiceClient();

  // Add the session token from login to the header.
  carrierService.Endpoint.Behaviors.Add(new AddSessionTokenToHeaderBehavior(sessionToken));
  // VS2013 version
  // carrierService.Endpoint.EndpointBehaviors.Add(new AddSessionTokenToHeaderBehavior(sessionToken));

  // Create a ChangeDeviceState request
  ChangeDeviceStateRequest request = new ChangeDeviceStateRequest();

  // The list of devices to be changed can be submitted as a DeviceList object,
  // which is a list of DeviceIdentifierCollection objects
  request.DeviceList = new List<ClientProxy.CarrierService.DeviceIdentifierCollection>();

  // Add a DeviceIdentifierCollection object for the device to be activated
  request.DeviceList.Add(new ClientProxy.CarrierService.DeviceIdentifierCollection());

  // Create a DeviceIdentifiers (plural) object, which is a list of DeviceIdentifier (singular) objects
  request.DeviceList[0].DeviceIdentifiers = new List<ClientProxy.CarrierService.DeviceIdentifier>();

  // Add a DeviceIdentifer object to the DeviceIdentifiers list
  request.DeviceList[0].DeviceIdentifiers.Add(new ClientProxy.CarrierService.DeviceIdentifier());
  request.DeviceList[0].DeviceIdentifiers[0].Kind = "imei";
  request.DeviceList[0].DeviceIdentifiers[0].Identifier = "990003420535572";

  // For activating 4G devices, add a second DeviceIdentifier object for the ICCID
  request.DeviceList[0].DeviceIdentifiers.Add(new ClientProxy.CarrierService.DeviceIdentifier());
  request.DeviceList[0].DeviceIdentifiers[1].Kind = "iccid";
  request.DeviceList[0].DeviceIdentifiers[1].Identifier = "89148000000800784253";

  request.AccountName = account;

  request.Activate = new ActivateDeviceRequest();
  request.Activate.ServicePlan = svcPlanCode;
  request.Activate.MdnZipCode = "98815";

  try
  {
    // Send the activation request.
    ChangeDeviceStateResponse cdsResponse = carrierService.ChangeDeviceState(request);

    // Store the returned request ID for later use.
    string requestId = cdsResponse.RequestId;
  }
  catch (FaultException ex)
  {
    Console.WriteLine("FaultException:" + ex.Message);
  }
  catch (System.TimeoutException ex)
  {
    Console.WriteLine("TimeOutException:" + ex.Message);
  }
}

ChangeDeviceState-Deactivate: C# Sample Code

var carrierService = new CarrierServiceClient();
ChangeDeviceStateRequest request = new ChangeDeviceStateRequest();
try
   {
      request.DeviceList = new List<ClientProxy.CarrierService.DeviceIdentifierCollection>();
      request.DeviceList.Add(new ClientProxy.CarrierService.DeviceIdentifierCollection());
      request.DeviceList[0].DeviceIdentifiers = new List<ClientProxy.CarrierService.DeviceIdentifier>();
      request.DeviceList[0].DeviceIdentifiers.Add(new ClientProxy.CarrierService.DeviceIdentifier());
      request.DeviceList[0].DeviceIdentifiers[0] = new ClientProxy.CarrierService.DeviceIdentifier();
      request.DeviceList[0].DeviceIdentifiers[0].Kind = "mdn";
      request.DeviceList[0].DeviceIdentifiers[0].Identifier = "1051770598";
      request.Deactivate = new DeactivateDeviceRequest();
      request.Deactivate.EtfWaiver = false;
      request.Deactivate.ReasonCode = “FF”;
      ChangeDeviceStateResponse response = carrierService.ChangeDeviceState(request);
   }
catch (Exception ex)
   {
      // HANDLE ERROR HERE
   }

GetDeviceInformation: C# Sample Code

The following sample code illustrates a programmatic call to GetDeviceInformationRequest. The development environment for this sample is Microsoft Visual Studio.

var deviceService = new DeviceServiceClient();
GetDeviceInformationRequest request = new GetDeviceInformationRequest();
try
{
    request.Device = new DeviceService.DeviceIdentifier();
    request.Device.Kind = ”mdn”;
    request.Device.Identifier = ”1051770598”;
    GetDeviceInformationResponse response = deviceService.GetDeviceInformation(request);
}
catch (Exception ex)
{
    // HANDLE ERROR HERE
}