Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion clearblade/cloud/iot_v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@
"ListDeviceRegistryPager",
"ListDeviceRegistriesAsyncPager",
"ListDevicesPager",
"ListDevicesAsyncPager")
"ListDevicesAsyncPager",
"MqttState",
"HtttState",
"LogLevel",
"GatewayType",
"GatewayAuthMethod",
"PublicKeyCertificateFormat",
"PublicKeyFormat"
)
49 changes: 34 additions & 15 deletions clearblade/cloud/iot_v1/device_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import List

from .resources import GatewayType, LogLevel
from .utils import get_value


Expand All @@ -9,17 +9,16 @@ class Device():
"""
# TODO: find a better way to construct the Device object. I dont like so much parameter in a constructor

def __init__(self, id: str = None, name: str = None, num_id: str = None,
def __init__(self, id: str, num_id: str = None,
credentials: list = [], last_heartbeat_time: str = None, last_event_time: str = None,
last_state_time: str = None, last_config_ack_time: str = None,
last_config_send_time: str = None, blocked: bool = False,
last_error_time: str = None, last_error_status_code: dict = {"code":None, "message":""},
last_error_time: str = None, last_error_status_code: dict = None,
config: dict = {"cloudUpdateTime":None, "version":""} ,
state: dict = {"updateTime":None, "binaryData":None},
log_level: str = "NONE", meta_data: dict = {}, gateway_config : dict = {}) -> None:
log_level: str = LogLevel.NONE, meta_data: dict = {}, gateway_config : dict = {"gatewayType": GatewayType.NON_GATEWAY}) -> None:

self._id = id
self._name = name
self._num_id = num_id
self._credentials = credentials
self._last_heartbeat_time = last_heartbeat_time
Expand All @@ -38,7 +37,7 @@ def __init__(self, id: str = None, name: str = None, num_id: str = None,

@staticmethod
def from_json(json):
return Device(id=json['id'], name=json['name'], num_id=json['numId'],
return Device(id=json['id'], num_id=json['numId'],
credentials=json['credentials'], last_heartbeat_time=json['lastHeartbeatTime'],
last_event_time=json['lastEventTime'], last_state_time=json['lastStateTime'],
last_config_ack_time=json['lastConfigAckTime'], last_config_send_time=json['lastConfigSendTime'],
Expand All @@ -51,10 +50,6 @@ def from_json(json):
def id(self):
return self._id

@property
def name(self):
return self._name

@property
def num_id(self):
return self._num_id
Expand All @@ -63,6 +58,10 @@ def num_id(self):
def credentials(self):
return self._credentials

@credentials.setter
def credentials(self, credentials):
self._credentials = credentials

@property
def last_error_status(self):
return self._last_error_status_code
Expand All @@ -78,42 +77,62 @@ def state(self):
@property
def log_level(self):
return self._log_level

@log_level.setter
def log_level(self, log_level):
self._log_level = log_level

@property
def meta_data(self):
return self._meta_data

@meta_data.setter
def meta_data(self, meta_data):
self._meta_data = meta_data

@property
def gateway_config(self):
return self._gateway_config

@gateway_config.setter
def gateway_config(self, gateway_config):
self._gateway_config = gateway_config

@property
def log_level(self):
return self._log_level

@property
def last_heartbeat_time(self):
return self._last_heartbeat_time

@property
def blocked(self):
return self._blocked

@blocked.setter
def blocked(self, blocked):
self._blocked = blocked


# classes to mock googles request & response

class DeviceState():
def __init__(self, updated_time: str = None, binary_data:str = None) -> None:
self._updated_time = updated_time
def __init__(self, update_time: str = None, binary_data:str = None) -> None:
self._update_time = update_time
self._binary_data = binary_data

@property
def updated_time(self):
return self._updated_time
def update_time(self):
return self._update_time

@property
def binary_data(self):
return self._binary_data

@staticmethod
def from_json(response_json):
return DeviceState(updated_time=get_value(response_json, 'updateTime'),
return DeviceState(update_time=get_value(response_json, 'updateTime'),
binary_data=get_value(response_json, 'binaryData'))


Expand Down
4 changes: 2 additions & 2 deletions clearblade/cloud/iot_v1/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def _prepare_for_send_command(self,
return params,body

def _create_device_body(self, device: Device) :
return {'id':device.name, 'name':device.name,
return {'id':device.id,
'credentials':device.credentials, 'lastErrorStatus':device.last_error_status,
'config':device.config, 'state':device.state,
'loglevel':device.log_level, 'metadata':device.meta_data,
'logLevel':device.log_level, 'metadata':device.meta_data,
'gatewayConfig':device.gateway_config}

def _create_device_from_response(self, json_response) -> Device :
Expand Down
2 changes: 1 addition & 1 deletion clearblade/cloud/iot_v1/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _create_registry_body(self, registry: DeviceRegistry) :
if registry.event_notification_configs:
registry_json['eventNotificationConfigs']=registry.event_notification_configs
if registry.log_level:
registry_json['loglevel']=registry.log_level
registry_json['logLevel']=registry.log_level
return registry_json

def _prepare_params_for_registry_list(self, request:ListDeviceRegistriesRequest):
Expand Down
7 changes: 4 additions & 3 deletions clearblade/cloud/iot_v1/registry_types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .utils import get_value
from .resources import HttpState, MqttState, LogLevel

class EventNotificationConfig:
def __init__(self, pub_sub_topic_name, subfolder_matches=None) -> None:
Expand All @@ -17,9 +18,9 @@ class DeviceRegistry:
def __init__(self, id:str = None, name:str = None,
eventNotificationConfigs:list = [],
stateNotificationConfig:dict = {'pubsubTopicName': ''},
mqttConfig:dict = {'mqttEnabledState':'MQTT_ENABLED'},
httpConfig:dict = {'httpEnabledState':'HTTP_ENABLED'},
logLevel:str = None, credentials:list = []) -> None:
mqttConfig:dict = {'mqttEnabledState': MqttState.MQTT_ENABLED},
httpConfig:dict = {'httpEnabledState': HttpState.HTTP_ENABLED},
logLevel:str = LogLevel.NONE, credentials:list = []) -> None:
self._id = id
self._name = name
self._event_notification_configs = eventNotificationConfigs
Expand Down
44 changes: 22 additions & 22 deletions clearblade/cloud/iot_v1/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,57 @@ class MqttState():
r"""Indicates whether an MQTT connection is enabled or disabled.
See the field description for details.
"""
MQTT_STATE_UNSPECIFIED = 0
MQTT_ENABLED = 1
MQTT_DISABLED = 2
MQTT_STATE_UNSPECIFIED = "MQTT_STATE_UNSPECIFIED"
MQTT_ENABLED = "MQTT_ENABLED"
MQTT_DISABLED = "MQTT_DISABLED"


class HttpState():
r"""Indicates whether DeviceService (HTTP) is enabled or disabled
for the registry. See the field description for details.
"""
HTTP_STATE_UNSPECIFIED = 0
HTTP_ENABLED = 1
HTTP_DISABLED = 2
HTTP_STATE_UNSPECIFIED = "HTTP_STATE_UNSPECIFIED"
HTTP_ENABLED = "HTTP_ENABLED"
HTTP_DISABLED = "HTTP_DISABLED"


class LogLevel():
class LogLevel:
r"""**Beta Feature**

The logging verbosity for device activity. Specifies which events
should be written to logs. For example, if the LogLevel is ERROR,
only events that terminate in errors will be logged. LogLevel is
inclusive; enabling INFO logging will also enable ERROR logging.
"""
LOG_LEVEL_UNSPECIFIED = 0
NONE = 10
ERROR = 20
INFO = 30
DEBUG = 40
LOG_LEVEL_UNSPECIFIED = "LOG_LEVEL_UNSPECIFIED"
NONE = "NONE"
ERROR = "ERROR"
INFO = "INFO"
DEBUG = "DEBUG"


class GatewayType():
class GatewayType:
r"""Gateway type."""
GATEWAY_TYPE_UNSPECIFIED = 0
GATEWAY = 1
NON_GATEWAY = 2
GATEWAY_TYPE_UNSPECIFIED = "GATEWAY_TYPE_UNSPECIFIED"
GATEWAY = "GATEWAY"
NON_GATEWAY = "NON_GATEWAY"


class GatewayAuthMethod():
r"""The gateway authorization/authentication method. This setting
determines how Cloud IoT Core authorizes/authenticate devices to
access the gateway.
"""
GATEWAY_AUTH_METHOD_UNSPECIFIED = 0
ASSOCIATION_ONLY = 1
DEVICE_AUTH_TOKEN_ONLY = 2
ASSOCIATION_AND_DEVICE_AUTH_TOKEN = 3
GATEWAY_AUTH_METHOD_UNSPECIFIED = "GATEWAY_AUTH_METHOD_UNSPECIFIED"
ASSOCIATION_ONLY = "ASSOCIATION_ONLY"
DEVICE_AUTH_TOKEN_ONLY = "DEVICE_AUTH_TOKEN_ONLY"
ASSOCIATION_AND_DEVICE_AUTH_TOKEN = "ASSOCIATION_AND_DEVICE_AUTH_TOKEN"


class PublicKeyCertificateFormat():
r"""The supported formats for the public key."""
UNSPECIFIED_PUBLIC_KEY_CERTIFICATE_FORMAT = 0
X509_CERTIFICATE_PEM = 1
UNSPECIFIED_PUBLIC_KEY_CERTIFICATE_FORMAT = "UNSPECIFIED_PUBLIC_KEY_CERTIFICATE_FORMAT"
X509_CERTIFICATE_PEM = "X509_CERTIFICATE_PEM"


class PublicKeyFormat:
Expand Down
2 changes: 1 addition & 1 deletion samples/clearblade/create_device_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async def sample_create_device_async():
"asia-east1",
"test-asia-east1")

device = iot_v1.Device(id="Python_SDK", name="Python_SDK")
device = iot_v1.Device(id="Python_SDK")
request = iot_v1.CreateDeviceRequest(parent=parent, device=device)

response = await async_client.create_device(request)
Expand Down
8 changes: 4 additions & 4 deletions samples/clearblade/create_device_registry_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ async def sample_create_device_registry():
# Create a client
client = iot_v1.DeviceManagerAsyncClient()

registry = iot_v1.DeviceRegistry(id='test-registry', name='test-registry',
mqttConfig={'mqttEnabledState':'MQTT_ENABLED'},
httpConfig={'httpEnabledState':'HTTP_ENABLED'},
logLevel='ERROR',
registry = iot_v1.DeviceRegistry(id='rajas-dummy-registry',
mqttConfig={'mqttEnabledState':iot_v1.resources.MqttState.MQTT_ENABLED},
httpConfig={'httpEnabledState':iot_v1.resources.HttpState.HTTP_ENABLED},
logLevel=iot_v1.resources.LogLevel.ERROR,
eventNotificationConfigs=[{'pubsubTopicName':'projects/ingressdevelopmentenv/topics/deleting'}]
)

Expand Down
7 changes: 3 additions & 4 deletions samples/clearblade/create_device_registry_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ def sample_create_device_registry():

registry = iot_v1.DeviceRegistry(
id='test-registry',
name='test-registry',
mqttConfig={'mqttEnabledState':'MQTT_ENABLED'},
httpConfig={'httpEnabledState':'HTTP_ENABLED'},
logLevel='ERROR',
mqttConfig={'mqttEnabledState':iot_v1.MqttState.MQTT_ENABLED},
httpConfig={'httpEnabledState':iot_v1.HttpState.HTTP_ENABLED},
logLevel=iot_v1.LogLevel.NONE,
eventNotificationConfigs=[{'pubsubTopicName':'projects/api-project-320446546234/topics/deleting'}]
)

Expand Down
6 changes: 5 additions & 1 deletion samples/clearblade/create_device_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ def sample_create_device():
"us-central1",
"test-registry")

device = iot_v1.Device(id="Python_11", name="Python_11")
device = iot_v1.Device(
id="Python_12",
gateway_config={"gatewayType": iot_v1.GatewayType.NON_GATEWAY},
log_level=iot_v1.LogLevel.ERROR)

request = iot_v1.CreateDeviceRequest(parent=parent, device=device)

response = client.create_device(request)
Expand Down
3 changes: 1 addition & 2 deletions samples/clearblade/create_device_sync_es256.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ def create_device_in_dev_iot(name, keyFile):

device = iot_v1.Device(
id="my_test_device",
name=name,
credentials=[
{
"publicKey": {
"format": "ES256_PEM",
"format": iot_v1.PublicKeyFormat.ES256_PEM,
"key": public_key,
}
}])
Expand Down
8 changes: 4 additions & 4 deletions samples/clearblade/create_device_sync_rs256.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ def create_device_in_dev_iot(name, keyFile):

parent = client.registry_path(
"api-project-320446546234",
"asia-east1",
"test-asia-east1")
"us-central1",
"test-registry")

with io.open(keyFile) as f:
public_key = f.read()

device = iot_v1.Device(
id="python_sdk_device_dummy",
name=name,
id=name,
credentials=[
{
"publicKey": {
Expand All @@ -32,4 +31,5 @@ def create_device_in_dev_iot(name, keyFile):

device_name = "python_sdk_device_dummy"
key_path = "../api-client/manager/resources/ec_public.pem"
os.environ["CLEARBLADE_CONFIGURATION"] = "/Users/rajas/Downloads/test-credentials.json"
create_device_in_dev_iot(device_name, key_path)
2 changes: 1 addition & 1 deletion samples/clearblade/update_device_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async def sample_update_device_async():
"us-central1",
"test-registry")

device = iot_v1.Device(id="test-dev-1", blocked=True, log_level='NONE')
device = iot_v1.Device(id="test-dev-1", blocked=True, log_level=iot_v1.LogLevel.ERROR)

request = iot_v1.UpdateDeviceRequest(
parent=registry_path,
Expand Down
4 changes: 2 additions & 2 deletions samples/clearblade/update_device_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def sample_update_device():
"us-central1",
"test-registry")

device = iot_v1.Device(id="test-dev-1", blocked=True, log_level='NONE')
device = iot_v1.Device(id="python_11", blocked=True, log_level=iot_v1.LogLevel.ERROR)

request = iot_v1.UpdateDeviceRequest(
parent=registry_path,
Expand All @@ -24,5 +24,5 @@ def sample_update_device():
print(response)


os.environ["CLEARBLADE_CONFIGURATION"] = "/Users/DummyUser/Downloads/test-credentials.json"
os.environ["CLEARBLADE_CONFIGURATION"] = "/Users/rajas/Downloads/test-credentials.json"
sample_update_device()