Class

base/ConnectionPoller

base/ConnectionPoller(checkStatus, onStatusChange, optsopt)

Constructor

# new base/ConnectionPoller(checkStatus, onStatusChange, optsopt)

Internet or network connection poller.

Configuration

There are two ways to configure the poller.

  • Parameters in the constructor.
  • Configuration parameters.

Connection poller will be initialised with default value if parameters cannot be found in the constructor and configuration. In XDK3, the configuration parameters is set at a per-device-package level, which means the configuration for device package A can be different from device package B.

Consecutive Failure Threshold

It defines how many polling failures before XDK considers it is disconnected from the internet.

Attribute Value
Type Number
Default 3
Constructor parameter opts.consecFailureThreshold
Configuration parameter `device-details.${package-name}.network.polling.consec-failure-threshold`

Polling Interval

It defines how often, in seconds, that the connection poller should call the connection-checking function.

Attribute Value
Type Number
Default 30
Constructor parameter opts.pollingInterval
Configuration parameter `device-details.${package-name}.network.polling.interval`

Initial Network State

Define whether the connection poller should assume the device is connected to the internet before the first check.

Attribute Value
Type Boolean
Default True
Constructor parameter opts.networkStatus
Configuration parameter Not Available

Verbosity of Connection Poller

Control whether the regular polling log should be logged to console or not. See module:base/console for further details.

Attribute Value
Type Boolean
Default False
Constructor parameter Not Available
Configuration parameter "console.log.ConnectionPoller"
Parameters:
Name Type Attributes Default Description
checkStatus CheckNetworkStatus

Connection-checking function.

onStatusChange OnNetworkStatusChange

Callback that handles network status changed.

opts Object <optional>

Optional parameter.

networkStatus Boolean <optional>
true

The assumed network state before the first check. Set it to false to assume the device is originally disconnected from the internet.

consecFailureThreshold Number <optional>
3

How many polling failures before XDK considers it is disconnected from the internet.

pollingInterval Number <optional>
30

Number of seconds between two polling calls.

  • base/ConnectionPoller.event:EVT_STATUS_CHANGED
Example

Initialise connection poller with a fictional device API.

var myConfig = {
  consecFailureThreshold: 1,
  pollingInterval: 30
}

function myChecker(checkStatus) {
  checkStatus(dummyDeviceAPI.isConnected());
}

function myListener(newConnectionStatus) {
  system.dispatchEvent(ISystem.EVT_NETWORK_STATUS_CHANGED, newStatus);
}

var myPoller = new ConnectionPoller(myChecker, myListener, myConfig);

myPoller.startPolling(); // Start polling
myPoller.stopPolling(); // Stop polling.

Classes

base/ConnectionPoller

Members

# static constant POLLING_INTERVAL_LOWER_BOUND

The default polling interval, which will be used in various places within this module.

Methods

# protected _checkStatus(checkStatus)

Reference to the CheckNetworkStatus callback, provided during the construction of the connection poller.

Parameters:
Name Type Description
checkStatus OnNetworkCheckDone

Callback that takes the latest connection status.

# protected _onStatusChange(newStatus)

Reference to the OnNetworkStatusChange callback, provided during the construction of the connection poller.

Parameters:
Name Type Description
newStatus Boolean

The new connection status. True means connected, false otherwise.

# getPollingInterval() → {Number}

Get the polling interval.

polling interval in seconds.

Number

# isConnected() → {Boolean}

Get the latest connection status from polling.

True when connected, false otherwise.

Boolean

# setPollingInterval(interval) → {Boolean}

Set the polling interval. If the parameter provide is invalid, the existing interval value will not be updated.

If the interval is smaller than the lower bound recommended, which is 30 seconds, a warning message will be printed to the console. but the new value will still be applied in the next polling call.

Otherwise, the value will simply be updated.

Parameters:
Name Type Description
interval Number

New polling interval, in seconds.

True when set, false otherwise.

Boolean

# startPolling()

Start the polling.

# stopPolling()

Stop the polling.

Type Definitions

# CheckNetworkStatus(checkStatus)

The function that is responsible for checking whether the application is connected to the internet. The poller is designed this way to allow the checking function to be created specifically for the targeted device.

The callback OnNetworkCheckDone will be passed as the parameter, the caller should call this callback, with the latest connection status, once the check is done.

Parameters:
Name Type Description
checkStatus OnNetworkCheckDone

Callback that takes the latest connection status.

# OnNetworkCheckDone(newStatus)

The callback that is passed into CheckNetworkStatus. This callback should be called, with the latest connection status, once the check is done.

Parameters:
Name Type Description
newStatus Boolean

The new connection status. True means connected, false otherwise.

# OnNetworkStatusChange(newStatus)

The callback that will be executed by the connection poller whenever the new connection status is different from the previous check.

Parameters:
Name Type Description
newStatus Boolean

The new connection status. True means connected, false otherwise.