iotcs.messaging.policyfunctions module

class iotcs.messaging.policyfunctions.DeviceFunction(id)

Bases: object

DeviceFunction is an abstraction of a policy device function.

Create an instance of DeviceFunction.

class ApplyBucketData(buckets, bucket)

Bases: tuple

Create new instance of ApplyBucketData(buckets, bucket)

bucket

Alias for field number 1

buckets

Alias for field number 0

class Bucket(initialValue)

Bases: object

This class is used to implement the window slide functionality.

See mean()

Create an instance of Bucket

terms
value
DEVICE_FUNCTIONS = None
class GetBucketData(bucketsPerWindow, bucketsPerSlide, bucketZero, buckets)

Bases: tuple

Create new instance of GetBucketData(bucketsPerWindow, bucketsPerSlide, bucketZero, buckets)

bucketZero

Alias for field number 2

buckets

Alias for field number 3

bucketsPerSlide

Alias for field number 1

bucketsPerWindow

Alias for field number 0

class NetworkCost

Bases: enum.IntEnum

In the order of increasing cost.

CELLULAR = 1
ETHERNET = 0
SATELLITE = 2
ZERO = 0.0
classmethod addDataItem(message, field, value)

Update the message with field and value.

Parameters:
  • message – message to update
  • field – data item field
  • value – data item value
Raises:

ArgumentException if value is not a valid type.

apply(deviceAnalog, attribute, configuration, data, value)

Perform the function.

This method returns True if the conditions for the function have been met. Only when this function’s apply method returns true will the next function in the pipeline be applied.

After this method returns True, use get() to retrieve the value from the function.

Parameters:
  • deviceAnalog – the VirtualDevice, never None
  • attribute – the DeviceModelAttribute, which may be None if the function is being applied at the device model level
  • configuration – the parameters for this function from the device policy
  • data – a place for storing data between invocations of the function
  • value – the value to which the function is being applied
Returns:

True if the conditions for the function have been satisfied.

classmethod compute(node, deviceAnalog)

Return the computed value of node.

Delegates to Formula.compute()

Parameters:
  • node – a FormulaParserNode to compute
  • deviceAnalog – a representation of a VirtualDevice
Returns:

the computed value.

classmethod convertArg(deviceAnalog, argtype, arg)

Return the argument value. arg in an ACTION is a formula ACTION type is represented as JSON of form:

{
     "name": "name-of-the-action",
     "arguments": [
         "formula:argument-1",
        "formula:argument-2"
     ]
}

field in an ALERT is also a formula ALERT type is represented as JSON of form:

{
    "urn": "URN-of-the-alert",
    "fields": {
        "name-of-the-field-1": "<formula>",
        "name-of-the-field-2": "<formula>"
        ...
     },
    "severity": "severity level"
}
Parameters:
Returns:

the converted argument value or None

Raises:

ArgumentException if arg cannot be coverted or argtype is an invalid type.

classmethod convertFormula(deviceAnalog, formula)

Return a value for formula.

Parameters:
  • deviceAnalog – a representation of a VirtualDevice
  • formula – a formula
Returns:

return the formula value else float(‘nan’)

classmethod createAlert(deviceAnalog, configuration)

Return an AlertMessage instance.

Parameters:
  • deviceAnalog – a representation of a VirtualDevice
  • configuration – the parameters for this function from the device policy
Returns:

a AlertMessage instance

Raises:

ArgumentException if the format is an invalid alert format.

classmethod createInProcessMapKey(endpointId, deviceModelURN, attribute)

Return the key to retrieve a value from inProcessValues.

The format is:

<endpointId>/deviceModels/<deviceModelURN>:attributes/<attribute>
Parameters:
  • endpointId – a device endpoint id
  • deviceModelURN – a device model urn
  • attribute – a device model attribute name
Returns:

a key to return a value from inProcessValues

classmethod gcd(x, y)

Return the greatest common divisor of x and y.

get(deviceAnalog, attribute, configuration, data)

Return the value from the function.

This method should only be called after apply() returns True, or when a window expires.

Parameters:
  • deviceAnalog – the VirtualDevice, never None
  • attribute – the DeviceModelAttribute, which may be None if the function is being applied at the device model level
  • configuration – the parameters for this function from the device policy
  • data – a place for storing data between invocations of the function
Returns:

the value from having applied the function

classmethod getActionArgs(deviceAnalog, configuration)

Return a list of action arguments.

The returned list of action arguments maps to the list of arguments in the configuration. The list will contain all None entries if there is no device model action map, or the action map is empty.

Parameters:
  • deviceAnalog – a representation of a VirtualDevice
  • configuration – the device function parameters
Returns:

a dict of action arguments.

classmethod getApplyBucketData(id, configuration, data, defaultbucketvalue)

Return a ApplyBucketData tuple.

An ApplyBucketTuple is::
[ ‘buckets’, ‘bucket’ ]
Parameters:
  • id – function name prefix
  • configuration
  • data
  • defaultbucketvalue
Returns:

getDetails(configuration)

Return a string representation of this function.

Useful for logging. :param configuration: the parameters for this function from the device

policy
Returns:a string representation of this function
classmethod getDeviceFunction(functionId)

Return the DeviceFunction for this functionId.

Parameters:functionId – the function id
Returns:the DeviceFunction for functionId
classmethod getGetBucketData(id, configuration, data)

Return a BucketData tuple.

A GetBucketTuple is::
[‘bucketsPerWindow’, ‘bucketsPerSlide’,
‘bucketZero’, ‘buckets’]
Parameters:
  • id – function name prefix
  • configuration
  • data
Returns:

getId()

Return the function id.

This is the unique id from the function definition. :return: the function id

classmethod getInProcessValue(endpointId, deviceModelURN, attribute)

Return the in process value for attribute.

Parameters:
  • endpointId – a device endpoint id
  • deviceModelURN – a device model urn
  • attribute – a device model attribute name
Returns:

the in process attribute value or None

classmethod getLogger()

Return a logger for *iotcs.messaging.client.device.DeviceFunction.

classmethod getPersistedBatchedData(batchByPersistence, deviceAnalog)

Return persisted batch by data.

Parameters:
  • batchByPersistence – the persisteence engine
  • deviceAnalog – a representation of a VirtualDevice
Returns:

persisted batch by date else None if not persisting data

classmethod getSlide(configuration, window)

Return a slide value from a configuration.

Parameters:configuration – the parameters for this function from the device policy

:param window the corresponding window for the slide :return: the configured slide value, or window if there is no slide or

slide is zero
classmethod getWindow(configuration)

Return a window value from a configuration.

Parameters:configuration – the parameters for this function from the device policy
Returns:a window value, or -1 if the configuration is not time based
id
inProcessValues = {}
classmethod putInProcessValue(endpointId, deviceModelURN, attribute, value)

Add attribte and value to inProcessValues.

Parameters:
  • endpointId – a device endpoint id
  • deviceModelURN – a device model urn
  • attribute – a device model attribute name
  • value – the attribute value
classmethod removeInProcessValue(endpointId, deviceModelURN, attribute)

Return and remove attribute value from inProcessValues.

Parameters:
  • endpointId – a device endpoint id
  • deviceModelURN – a device model urn
  • attribute – a device model attribute name
Returns:

the attribute value

class iotcs.messaging.policyfunctions.ValueProviderImpl(deviceAnalog)

Bases: iotcs.messaging.policy.ValueProvider

This class encapsulates a device function value.

Create a ValueProviderImpl instance.

deviceAnalog
getCurrentValue(key)

Return the value of the attribute key

Parameters:key – attribute name
Returns:the value of attribute key
getInProcessValue(key)

Return the in processs value of attribute key

Parameters:key – attribute name
Returns:the in process value of attribute key