if __name__ == '__main__': # Creating EdgeSystem edge_system = Dell5KEdgeSystem(config['EdgeSystemName']) # Encapsulates Identity #identity = Identity(root_ca_cert=config['broker_root_ca_cert'], username=None, password=None, # cert_file=config['edge_system_cert_file'], key_file=config['edge_system_key_file']) # Encapsulate TLS parameters # tls_conf = TLSConf(config['cert_required'], config['tls_version'], config['cipher']) # Encapsulate QoS related parameters #qos_details = QoSDetails(config['in_flight'], config['queue_size'], config['retry']) # Connecting to AWSIoT # AWSIoT broker doesn't support session persistence. So, always use "clean_session=True" # Custom Publish Topic for an EdgeSystem mqtt_msg_attr = MqttMessagingAttributes(pub_topic=config['CustomPubTopic']) watsoniot = WatsonIoT(WatsonMqttDccComms(organization=config['organization'], deviceType=config['deviceType'], deviceId=config['deviceId'], authKey = config['authKey'], authToken=config['authToken'])) # Registering EdgeSystem reg_edge_system = watsoniot.register(edge_system) # Creating CPU Metric cpu_utilization = Metric( name="CPUUtilization", unit=None, interval=10, aggregation_size=2, sampling_function=read_cpu_utilization ) # Registering Metric and creating Parent-Child relationship reg_cpu_utilization = watsoniot.register(cpu_utilization)
def run(self, registry): import copy import pint from liota.lib.transports.mqtt import MqttMessagingAttributes from liota.entities.metrics.metric import Metric from liota.entities.devices.simulated_device import SimulatedDevice # create a pint unit registry ureg = pint.UnitRegistry() # Acquire resources from registry aws_iot = registry.get("aws_iot") aws_iot_edge_system = copy.copy(registry.get("aws_iot_edge_system")) # Get values from configuration file config_path = registry.get("package_conf") config = {} execfile(config_path + '/sampleProp.conf', config) # Create metrics self.metrics = [] # Creating CPU Metric cpu_utilization = Metric(name="CPUUtilization", unit=None, interval=10, aggregation_size=2, sampling_function=read_cpu_utilization) # Registering Metric and creating Parent-Child relationship reg_cpu_utilization = aws_iot.register(cpu_utilization) aws_iot.create_relationship(aws_iot_edge_system, reg_cpu_utilization) # Publish topic for this Metric reg_cpu_utilization.msg_attr = MqttMessagingAttributes( pub_topic=config['CustomPubTopic']) # Publishing Registered CPU Utilization Metric to AWSIoT Dcc reg_cpu_utilization.start_collecting() self.metrics.append(reg_cpu_utilization) # Creating Simulated Device dht_sensor = SimulatedDevice("SimulatedDHTSensor") # Registering Device and creating Parent-Child relationship reg_dht_sensor = aws_iot.register(dht_sensor) aws_iot.create_relationship(aws_iot_edge_system, reg_dht_sensor) # Creating Temperature Metric temp_metric = Metric(name="LivingRoomTemperature", entity_type="Metric", unit=ureg.degC, interval=1, aggregation_size=5, sampling_function=get_living_room_temperature) # Registering Metric and creating Parent-Child relationship reg_temp_metric = aws_iot.register(temp_metric) aws_iot.create_relationship(reg_dht_sensor, reg_temp_metric) # Publish topic for this Metric reg_temp_metric.msg_attr = MqttMessagingAttributes( pub_topic=config['LivingRoomTemperatureTopic']) # Publishing Registered Temperature Metric to AWSIoT Dcc reg_temp_metric.start_collecting() self.metrics.append(reg_temp_metric) # Creating Humidity Metric hum_metric = Metric(name="LivingRoomHumidity", entity_type="Metric", unit=None, interval=1, aggregation_size=5, sampling_function=get_living_room_humidity) # Registering Metric and creating Parent-Child relationship reg_hum_metric = aws_iot.register(hum_metric) aws_iot.create_relationship(reg_dht_sensor, reg_hum_metric) # Publish topic for this Metric reg_hum_metric.msg_attr = MqttMessagingAttributes( pub_topic=config['LivingRoomHumidityTopic']) # Publishing Registered Humidity Metric to AWSIoT Dcc reg_hum_metric.start_collecting() self.metrics.append(reg_hum_metric) # Creating Simulated Device light_sensor = SimulatedDevice("SimDigLightSensor") # Registering Device and creating Parent-Child relationship reg_light_sensor = aws_iot.register(light_sensor) aws_iot.create_relationship(aws_iot_edge_system, reg_light_sensor) # Creating Light Metric light_metric = Metric(name="LivingRoomLight", entity_type="Metric", unit=None, interval=10, aggregation_size=1, sampling_function=get_living_room_luminance) # Registering Metric and creating Parent-Child relationship reg_light_metric = aws_iot.register(light_metric) aws_iot.create_relationship(reg_light_sensor, reg_light_metric) # Publish topic for this Metric reg_light_metric.msg_attr = MqttMessagingAttributes( pub_topic=config['LivingRoomLightTopic']) # Publishing Registered Light Metric to AWSIoT DCC reg_light_metric.start_collecting() self.metrics.append(reg_light_metric)
def __init__(self, edge_system_name, url, port, identity=None, tls_conf=None, qos_details=None, client_id="", clean_session=False, protocol="MQTTv311", transport="tcp", keep_alive=60, mqtt_msg_attr=None, enable_authentication=False, conn_disconn_timeout=10): """ :param edge_system_name: EdgeSystem's name for auto-generation of topic :param url: MQTT Broker URL or IP :param port: MQTT Broker Port :param tls_conf: TLSConf object :param identity: Identity object :param qos_details: QoSDetails object :param client_id: Client ID :param clean_session: Connect with Clean session or not :param userdata: userdata is user defined data of any type that is passed as the "userdata" parameter to callbacks. :param protocol: allows explicit setting of the MQTT version to use for this client :param transport: Set transport to "websockets" to use WebSockets as the transport mechanism. Set to "tcp" to use raw TCP, which is the default. :param keep_alive: KeepAliveInterval :param mqtt_msg_attr: MqttMessagingAttributes object or None. In case of None, topics will be auto-generated. User provided topic will be used otherwise. :param enable_authentication: Enable user-name password authentication or not :param conn_disconn_timeout: Connect-Disconnect-Timeout """ self.client_id = client_id if mqtt_msg_attr is None: # pub-topic and sub-topic will be auto-generated log.info("pub-topic and sub-topic is auto-generated") self.msg_attr = MqttMessagingAttributes(edge_system_name) if self.client_id is None or self.client_id == "": # local_uuid generated will be the client ID self.client_id = systemUUID().get_uuid(edge_system_name) log.info("generated local uuid will be the client ID") else: log.info("Client ID is provided by user") # Storing edge_system name and generated local_uuid which will be used in auto-generation of pub-sub topic store_edge_system_uuid(entity_name=edge_system_name, entity_id=self.client_id, reg_entity_id=None) elif isinstance(mqtt_msg_attr, MqttMessagingAttributes): log.info("User configured pub-topic and sub-topic") self.msg_attr = mqtt_msg_attr else: log.error( "mqtt_mess_attr should either be None or of type MqttMessagingAttributes" ) raise TypeError( "mqtt_mess_attr should either be None or of type MqttMessagingAttributes" ) self.url = url self.port = port self.identity = identity self.tls_conf = tls_conf self.qos_details = qos_details self.clean_session = clean_session self.userdata = Queue.Queue() self.protocol = protocol self.transport = transport self.keep_alive = keep_alive self.enable_authentication = enable_authentication self.conn_disconn_timeout = conn_disconn_timeout self._connect()
def __init__(self, dcc_identity, edge_system_identity, tls_details, qos_details, url, port, client_id=None, clean_session=False, userdata=None, protocol="MQTTv311", transport="tcp", mqtt_msg_attr=None, keep_alive=60, enable_authentication=False, conn_disconn_timeout=10): """ :param dcc_identity: RemoteSystemIdentity object :param edge_system_identity: EdgeSystemIdentity object :param tls_details: TLSDetails object :param qos_details: QoSDetails object :param url: MQTT Broker URL or IP :param port: MQTT Broker Port :param client_id: Client ID :param clean_session: Connect with Clean session or not :param userdata: userdata is user defined data of any type that is passed as the "userdata" parameter to callbacks. :param protocol: allows explicit setting of the MQTT version to use for this client :param transport: Set transport to "websockets" to use WebSockets as the transport mechanism. Set to "tcp" to use raw TCP, which is the default. :param mqtt_msg_attr: MqttMessagingAttributes object or None :param keep_alive: KeepAliveInterval :param enable_authentication: Enable user-name password authentication or not :param conn_disconn_timeout: Connect-Disconnect-Timeout """ self.dcc_identity = dcc_identity self.edge_system_identity = edge_system_identity if mqtt_msg_attr is None: # pub-topic and sub-topic will be auto-generated self.msg_attr = MqttMessagingAttributes( edge_system_identity.edge_system_name) elif isinstance(mqtt_msg_attr, MqttMessagingAttributes): self.msg_attr = mqtt_msg_attr else: log.error( "mqtt_mess_attr should either be None or of type MqttMessagingAttributes" ) raise TypeError( "mqtt_mess_attr should either be None or of type MqttMessagingAttributes" ) self.tls_details = tls_details self.url = url self.port = port self.client_id = client_id self.clean_session = clean_session self.userdata = userdata self.protocol = protocol self.transport = transport self.keep_alive = keep_alive self.qos_details = qos_details self.enable_authentication = enable_authentication self.conn_disconn_timeout = conn_disconn_timeout self._connect()
def run(self, registry): import copy from liota.lib.utilities.identity import Identity from liota.dccs.iotcc import IotControlCenter from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms from liota.lib.transports.mqtt import MqttMessagingAttributes from liota.dccs.dcc import RegistrationFailure from liota.lib.utilities.tls_conf import TLSConf # Acquire resources from the registry # Creating a copy of edge_system object to keep original object "clean" edge_system = copy.copy(registry.get("edge_system")) # Encapsulates Identity # Acquire credentials and required certificates from the registry identity = Identity(root_ca_cert=registry.get("broker_root_ca_cert"), username=registry.get("broker_username"), password=registry.get("broker_password"), cert_file=registry.get("edge_system_cert_file"), key_file=registry.get("edge_system_key_file")) # Encapsulate TLS parameters tls_conf = TLSConf(cert_required="CERT_REQUIRED", tls_version="PROTOCOL_TLSv1_2", cipher=None) # Initialize DCC object with MQTT transport mqtt_msg_attr = MqttMessagingAttributes( pub_topic="liota/" + registry.get("broker_username") + "/request", sub_topic="liota/" + registry.get("broker_username") + "/response") self.iotcc = IotControlCenter( MqttDccComms(edge_system_name=edge_system.name, url=registry.get("broker_ip"), port=registry.get("broker_port"), identity=identity, tls_conf=tls_conf, client_id=registry.get("broker_username"), enable_authentication=True, mqtt_msg_attr=mqtt_msg_attr)) try: # Register edge system (gateway) self.iotcc_edge_system = self.iotcc.register(edge_system) # System Properties has to be set only for the registered edge system before it is stored in the package # manager registry, all the devices will internally inherit the the system properties from the # registered edge system self.iotcc.set_system_properties(self.iotcc_edge_system, registry.get("system_properties")) # Set the properties for edge system as key:value pair, you can also set the location # by passing the latitude and longitude as a property in the user package self.iotcc.set_properties(self.iotcc_edge_system, { "key1": "value1", "key2": "value2" }) registry.register("iotcc_mqtt", self.iotcc) # Store the registered edge system object in liota package manager registry after the # system properties are set for it registry.register("iotcc_mqtt_edge_system", self.iotcc_edge_system) except RegistrationFailure: print "EdgeSystem registration to IOTCC failed"
def run(self, registry): """ The execution function of a liota package. Establishes connection with IoTControlCenter DCC using MqttDccComms :param registry: the instance of ResourceRegistryPerPackage of the package :return: """ import copy import time from liota.lib.utilities.identity import Identity from liota.dccs.iotcc import IotControlCenter from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms from liota.lib.transports.mqtt import MqttMessagingAttributes from liota.dccs.dcc import RegistrationFailure from liota.lib.utilities.tls_conf import TLSConf # Acquire resources from the registry # Creating a copy of edge_system object to keep original object "clean" edge_system = copy.copy(registry.get("edge_system")) # Encapsulates Identity # Acquire credentials and required certificates from the registry identity = Identity(root_ca_cert=registry.get("broker_root_ca_cert"), username=registry.get("broker_username"), password=registry.get("broker_password"), cert_file=registry.get("edge_system_cert_file"), key_file=registry.get("edge_system_key_file")) # Encapsulate TLS parameters tls_conf = TLSConf(cert_required="CERT_REQUIRED", tls_version="PROTOCOL_TLSv1_2", cipher=None) # Initialize DCC object with MQTT transport mqtt_msg_attr = MqttMessagingAttributes(pub_topic="liota/" + registry.get("broker_username") + "/request", sub_topic="liota/" + registry.get("broker_username") + "/response") self.iotcc = IotControlCenter(MqttDccComms(edge_system_name=edge_system.name, url=registry.get("broker_ip"), port=registry.get("broker_port"), identity=identity, tls_conf=tls_conf, client_id=registry.get("broker_username"), enable_authentication=True, mqtt_msg_attr=mqtt_msg_attr)) try: # Register edge system (gateway) self.iotcc_edge_system = self.iotcc.register(edge_system) # Set the properties for edge system as key:value pair, you can also set the location # by passing the latitude and longitude as a property in the user package # If the set_properties or register call fails due to DCC_Comms Publish exception # the optional retry mechanism can be implemented in the following way attempts = 0 max_retry_attempts = 3 while attempts < max_retry_attempts: try: # Register edge system (gateway) self.iotcc.set_properties(self.iotcc_edge_system, {"key1": "value1", "key2": "value2"}) break except Exception: # In the third attempt if get exception raise it if attempts == max_retry_attempts: raise attempts += 1 # The sleep time before re-trying depends on the infrastructure requirement of broker to restart # It can be modified or removed as per the infrastructure requirement time.sleep(5) registry.register("iotcc_mqtt", self.iotcc) # Store the registered edge system object in liota package manager registry after the registry.register("iotcc_mqtt_edge_system", self.iotcc_edge_system) except RegistrationFailure: print "EdgeSystem registration to IOTCC failed"
def run(self, registry): """ The execution function of a liota package. Establishes connection with IoTControlCenter DCC using MqttDccComms :param registry: the instance of ResourceRegistryPerPackage of the package :return: """ import copy from liota.lib.utilities.identity import Identity from liota.dccs.iotcc import IotControlCenter from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms from liota.lib.transports.mqtt import MqttMessagingAttributes from liota.lib.utilities.tls_conf import TLSConf # Acquire resources from the registry # Creating a copy of edge_system object to keep original object "clean" edge_system = copy.copy(registry.get("edge_system")) # Encapsulates Identity # Acquire credentials and required certificates from the registry identity = Identity(root_ca_cert=registry.get("broker_root_ca_cert"), username=registry.get("broker_username"), password=registry.get("broker_password"), cert_file=registry.get("edge_system_cert_file"), key_file=registry.get("edge_system_key_file")) # Encapsulate TLS parameters tls_conf = TLSConf(cert_required="CERT_REQUIRED", tls_version="PROTOCOL_TLSv1_2", cipher=None) # Initialize DCC object with MQTT transport mqtt_msg_attr = MqttMessagingAttributes(pub_topic="liota/" + registry.get("broker_username") + "/request", sub_topic="liota/" + registry.get("broker_username") + "/response") # Attempts for establishing MQTT Connection conn_attempts = 0 try: # Trying to establish MQTT Connection with retry attempts in case of exception while conn_attempts <= retry_attempts: try: self.iotcc = IotControlCenter( MqttDccComms(edge_system_name=edge_system.name, url=registry.get("broker_ip"), port=registry.get("broker_port"), identity=identity, tls_conf=tls_conf, client_id=registry.get("broker_username"), enable_authentication=True, mqtt_msg_attr=mqtt_msg_attr)) break except Exception as e: if conn_attempts == retry_attempts: raise conn_attempts += 1 log.error('MQTT Connection failed - {0}'.format(str(e))) log.info('Trying MQTT Connection: Attempt - {0}'.format(str(conn_attempts))) time.sleep(mqtt_connection_delay_retries) # Attempts for Edge System Registration reg_attempts = 0 # Edge System Registration with retry attempts in case of exception while reg_attempts <= retry_attempts: try: self.iotcc_edge_system = self.iotcc.register(edge_system) break except Exception as e: if reg_attempts == retry_attempts: raise reg_attempts += 1 log.error('Exception while registering Edge System- {0}'.format(str(e))) log.info('Trying Edge System {0} Registration: Attempt - {1}'.format(edge_system.name, str(reg_attempts))) time.sleep(delay_retries) registry.register("iotcc_mqtt", self.iotcc) registry.register("iotcc_mqtt_edge_system", self.iotcc_edge_system) # Attempts for setting edge system properties prop_attempts = 0 # Set multiple properties by passing Dictonary object for Edge System with the retry attempts # in case of exceptions while prop_attempts < retry_attempts: try: self.iotcc.set_properties(self.iotcc_edge_system, {"Country": "USA-G", "State": "California", "City": "Palo Alto", "Location": "VMware HQ", "Building": "Promontory H Lab", "Floor": "First Floor"}) break except Exception as e: prop_attempts += 1 log.error( 'Exception while setting Property for Edge System {0} - {1}'.format(edge_system.name, str(e))) log.info('Trying setting properties for Edge System {0}: Attempt - {1}'.format(edge_system.name, str(prop_attempts))) time.sleep(delay_retries) except Exception: log.error("EdgeSystem registration to IOTCC failed even after all the retries, starting connection cleanup") # Disconnecting MQTT self.iotcc.comms.client.disconnect() raise
identity = Identity(root_ca_cert=config['broker_root_ca_cert'], username=config['broker_username'], password=['broker_password'], cert_file=None, key_file=None) # Encapsulate TLS parameters tls_conf = TLSConf(config['cert_required'], config['tls_version'], config['cipher']) # Encapsulate QoS related parameters qos_details = QoSDetails(config['in_flight'], config['queue_size'], config['retry']) # Connecting to AWSIoT # AWSIoT broker doesn't support session persistence. So, always use "clean_session=True" # Custom Publish Topic for an EdgeSystem mqtt_msg_attr = MqttMessagingAttributes(pub_topic=config['CustomPubTopic']) wavefront = Wavefront(MqttDccComms( edge_system_name=edge_system.name, url=config['BrokerIP'], port=config['BrokerPort'], identity=identity, tls_conf=tls_conf, qos_details=qos_details, clean_session=True, protocol=config['protocol'], transport=['transport'], conn_disconn_timeout=config['ConnectDisconnectTimeout'], mqtt_msg_attr=mqtt_msg_attr), buffering_params=offline_buffering)