Module: caf_iot/bundles

Creates timed bundles of commands.

An example:

   var bundle = newInstance({foo: ['name', 'val'], bar: ['myArg']}, 2000)
   bundle.foo(100, ['joe', 34]).bar(250, [43]).bar(100, [98]);
   bundle.__iot_freeze__((new Date()).getTime() + 1000)

It creates a bundle with three commands: 'foo', ' bar', and 'bar' again.

The first one executes after 100msec of bundle activation, the second 250 msec after the first command starts, and so on...

The number of arguments needed for each command are described in the constructor by providing the argument names. The CAs iot plugin introspects the iot_methods.js at run time to create that configuration.

The bundle will activate in 3 seconds (1 sec plus the 2 second margin), if it arrives to the target before that time.

If we want to re-issue the bundle at a different time:

   var newBundle = bundle.__iot_clone__();
   newBundle.__iot_freeze__(NOW)

and now the bundle will start executing as soon as it arrives to the target.

Source:

Members

MARGIN_IN_MSEC :number

Default extra time to allow bundle propagation.

Type:
  • number
Source:

NOW :number

Force the bundle to execute as soon as it arrives to its destination.

Type:
  • number
Source:

Methods

fromObject(bundleVanilla, methodsopt, marginopt) → {bundleObjectType}

Creates a bundle from a 'vanilla' object representation.

Parameters:
Name Type Attributes Description
bundleVanilla bundleType

A 'vanilla' object representation of the bundle. See toObject.

methods bundleDescriptionType <optional>

Optional object with method names and arguments.

margin number <optional>

A safety time margin in msec added to the starting time to allow bundle propagation. It defaults to exports.MARGIN_IN_MSEC.

Source:
Returns:

A patched bundle.

Type
bundleObjectType

__iot_clone__() → {bundleObjectType}

Clones this bundle to allow further modifications after freezing.

Source:
Returns:

A cloned bundle.

Type
bundleObjectType

__iot_freeze__(newStartopt) → {bundleObjectType}

Fully initializes this bundle by setting the start execution time explicitly, or based on current time.

After this method a bundle is frozen, and needs to be cloned before it can be modified.

The starting time newStart could be:

  • NOW: whenever the bundle arrives to its target, it will start executing right away.
  • undefined: use the current time plus the margin specified in the factory method.
  • number: start time in msec since 1970/01/01 UTC plus the margin as above.
Parameters:
Name Type Attributes Description
newStart number <optional>

An optional starting time (see above).

Source:
Returns:

This bundle fully initialized.

Type
bundleObjectType

__iot_parse__(bundleString) → {bundleObjectType}

Parses a serialized string to initialize this bundle with commands.

Parameters:
Name Type Description
bundleString string

A serialized bundle.

Source:
Returns:

This bundle initialized with commands.

Type
bundleObjectType

__iot_serialize__() → {string}

Creates a string representation of a fully initialized bundle.

Source:
Returns:

A serialized bundle.

Type
string

newInstance(methodsopt, marginopt) → {bundleObjectType}

Constructor.

Create timed bundle of commands.

The type bundleDescriptionType is:

    Object<string, Array.<string>>

where the key is the name of the command, and the value an array of strings with the argument names of this command.

The command should be a method described in the iot_methods.js file.

Parameters:
Name Type Attributes Description
methods bundleDescriptionType <optional>

Optional object with method names and arguments.

margin number <optional>

A safety time margin in msec added to the starting time to allow bundle propagation. It defaults to exports.MARGIN_IN_MSEC

Source:
Returns:

A new bundle with no commands.

Type
bundleObjectType

toObject(bundle) → {bundleType}

Creates a 'vanilla' object representation of a bundle.

The type bundleType is:

     {start: number, commands: Array.<commandType>}

and commandType:

     {after: mumber, method: string, args: Array.<jsonType>}
Parameters:
Name Type Description
bundle bundleObjectType

A bundle to transform.

Source:
Returns:

A 'vanilla' object representation of the bundle.

Type
bundleType