iotcs.common module

class iotcs.common.Closeable

Bases: abc.ABC

A Closeable is a source or destination of data that can be closed. The close method is invoked to release resources that the object is holding (such as open files).

close()

Closes this instance releasing any system resources associated with it. If the instance is already closed then invoking this method has no effect. :raises IOException: if there is an error closing the instance

class iotcs.common.Filter

Bases: abc.ABC

A Filter that can be used for queries. The following literal operators are supported ‘$and’, ‘$or’, ‘$not’, ‘$like’, ‘$in’, ‘$eq’, ‘$lt’, ‘$lte’, ‘$gt’, ‘$gte’, ‘$ne’, ‘$exists’

Example:

Devices activated in the last hour that implement a “com:acme:device” device model.

The following code:

``` f = Filter.and(

Filter.gte(Device.Field.CREATED.alias(),
(System.currentTimeMillis() - 3600000L)),

Filter.eq(Device.Field.STATE.alias(), “ACTIVATED”), Filter.like(Device.Field.DEVICE_MODELS.alias() + “.urn”,

```

will create the following JSON object:

``` {“$and”:[

{“created”:{“$gte”:1457137772894}}, {“state”:{“$eq”:”ACTIVATED”}}, {“deviceModels.urn”:{“$like”:”urn:com:acme:device:%”}}

]}

classmethod and_(*filters)

Creates a query filter that performs the logical AND between sub filters provided as argument: (filter1 and filter2 and …).

Parameters:filters – sub filters to combine
Returns:a new filter { “$and”: [ filter1, filter2, …. ] }
classmethod eq(op1, op2)

Creates a query filter that performs (op1 == op2).

Parameters:
  • op1 – first operand
  • op2 – second operand
Returns:

a new filter { op1: { “$eq”: op2 } }

classmethod exists(op1, e)

Creates a query filter that checks if op1 exists

Parameters:
  • op1 – first operand
  • e – either {true} to check that exists, or {@code false} to check that not exists
Returns:

a new filter performing { op1: { “$exists” : true/false }}

classmethod gt(op1, op2)

Creates a query filter that performs(op1 > op2).

Parameters:
  • op1 – first operand
  • op2 – second operand
Returns:

a new filter { op1: { “$gt”: op2 } }

classmethod gte(op1, op2)

Creates a query filter that performs(op1 >= op2).

Parameters:
  • op1 – first operand
  • op2 – second operand
Returns:

a new filter { op1: { “$gte”: op2 } }

classmethod in_(key, values)

Creates a query filter that performs (key IN [value1, value2, …])

Parameters:
  • key – the key to check
  • values – the list of values to compare with
Returns:

a new filter { key: { “$in” : [value1, value2, …] }}

classmethod like(value, pattern)

Creates a query filter that checks if the value matches the pattern

Parameters:
  • value – the value to check
  • pattern – the pattern to match
Returns:

a new filter performing { op1: { “$like” : pattern }}

classmethod lt(op1, op2)

Creates a query filter that performs (op1 < op2).

Parameters:
  • op1 – first operand
  • op2 – second operand
Returns:

a new filter { op1: { “$lt”: op2 } }

classmethod lte(op1, op2)

Creates a query filter that performs(op1 <= op2).

Parameters:
  • op1 – first operand
  • op2 – second operand
Returns:

a new filter { op1: { “$lte”: op2 } }

classmethod ne(op1, op2)

Creates a query filter that performs (op1 != op2).

Parameters:
  • op1 – first operand
  • op2 – second operand
Returns:

a new filter { op1: { “$ne”: op2 } }

classmethod not_(filter)

Creates a query filter that performs the logical NOT on the filter provided as argument: (not filter).

Parameters:filter – on which to apply the NOT
Returns:a new filter { “$not”: filter1 }
classmethod or_(*filters)

Creates a query filter that performs the logical OR between sub filters provided as argument: (filter1 or filter2 or …).

Parameters:filters – sub filters to combine
Returns:a new filter { “$or”: [ filter1, filter2, …. ] }
toJson()

Return the JSONObject for this query filter.

Returns:a JSON representation of the filter.
class iotcs.common.Pair(key, value)

Bases: object

A convenience container to manage a single key and value.

Create a iotcs.client.impl.Pair.

Parameters:
  • key – the pair identifier
  • value – the pair value
getKey()

Return the key.

Returns:the key
getValue()

Return the value.

Returns:the value
class iotcs.common.RestApi(version)

Bases: object

RestApi provides the REST api root URL.

The configuration property use_webapi defined in the rest_api section of iotcs.config.config.py is used to determine if the web REST api variants should be used for requests to the IoT server. If use_webapi is`True` then the request URL will use /iot/webapi/, else /iot/api will be used.

Initialize the RestApi constants.

Parameters:version – the rest api version to identify the rest resource
V1 = <iotcs.common.RestApi object>
V2 = <iotcs.common.RestApi object>
getPrivateRoot()

Return the private REST api request root URL.

getReqRoot()

Return the REST api request root URL.

isWebApi()

Return True if the web api REST resource URL must be used.

If the iotcs.config.config.py rest_api section property use_webapi is True then the web api REST resource URL must be used to make requests to the IoT server. :return: True if the use_webapi configuration property is True else False

class iotcs.common.Singleton(decorated)

Bases: object

A non-thread-safe helper class to ease implementing singletons. This should be used as a decorator – not a metaclass – to the class that should be a singleton.

The decorated class can define one __init__ function that takes only the self argument. Also, the decorated class cannot be inherited from. Other than that, there are no restrictions that apply to the decorated class.

To get the singleton instance, use the Instance method. Trying to use __call__ will result in a TypeError being raised.

getInstance()

Returns the singleton instance. Upon its first call, it creates a new instance of the decorated class and calls its __init__ method. On all subsequent calls, the already created instance is returned.

class iotcs.common.StatusCode

Bases: enum.IntEnum

An enumeration.

ACCEPTED = 202
BAD_GATEWAY = 502
BAD_REQUEST = 400
CONFLICT = 409
CREATED = 201
FINISHED = 205
FORBIDDEN = 403
GATEWAY_TIMEOUT = 504
INTERNAL_SERVER_ERROR = 500
METHOD_NOT_ALLOWED = 405
NON_AUTHORITATIVE_INFORMATION = 203
NOT_ACCEPTABLE = 406
NOT_FOUND = 404
NOT_IMPLEMENTED = 501
NO_CONTENT = 204
OK = 200
OTHER = -1
PARTIAL_CONTENT = 206
PAYMENT_REQUIRED = 402
PRECON_FAILED = 412
REQUEST_TIMEOUT = 408
SERVICE_UNAVAILABLE = 503
TOO_MANY_REQUESTS = 429
UNAUTHORIZED = 401
class iotcs.common.TimeManager

Bases: object

static currentTimeMillis(date=None)
dateFromMillis()
diffWithServerMilis = 0
static getFormattedTime(timestamp=None)
static setCurrentTimeMillis(serverTime)
class iotcs.common.Util

Bases: object

A utility class for the library.

CONFIG_DEVICE_MESSAGING_SECTION = 'device_messaging'
SECRET_HASH_ALGO = 'HmacSHA256'
classmethod getConfig(modulePath=None, configFile=None)

Return the configParser dictionary for the library. This method Assumes there is a subdirectory of the module called config and contains a file called config.ini. The configparser used is configured for ExtendedInterpolation :param modulePath: the module path in dot notation. If None the

top module of the library will be used iotcs
Returns:None if there is no config subdirectory or no confif.ini file in the config directory
Raises:any exception possible from the use of ConfigParser, importlib.util.find_spec, or os.path methods.
classmethod getOSName()
classmethod getOSversion()
classmethod getPropertyInt(defaultVal)
classmethod strtobool(boolstr)