Exemplo n.º 1
0
class GraphManager:
    def __init__(self):
        self.device_id = getenv(constants.device_id)
        self.device_tag = getenv(constants.device_tag)
        self.tag_value = getenv(constants.tag_value)
        self.module_id = getenv(constants.module_id)
        self.api_version = constants.topology_api_version

        self.registry_manager = IoTHubRegistryManager(getenv(constants.iot_connection_string))

        if self.device_id is None:
            self.device_list = self.get_device_list()

    def get_device_list(self):
        query_string = f"SELECT * FROM devices WHERE tags.{self.device_tag} = '{self.tag_value}'"
        query_spec = QuerySpecification(query=query_string)
        response = self.registry_manager.query_iot_hub(query_spec, None, None)
        return response.items

    def invoke(self, method_name, payload):
        if method_name == 'GraphTopologySet':
            self.graph_topology_set(payload)
            return

        if method_name == 'WaitForInput':
            print(payload['message'])
            input()
            return

        self.invoke_module_method(method_name, payload)

    def invoke_module_method(self, method_name, payload):
        # make sure '@apiVersion' has been set
        payload['@apiVersion'] = self.api_version

        module_method = CloudToDeviceMethod(
            method_name=method_name,
            payload=payload,
            response_timeout_in_seconds=30)

        device_id = self.device_id
        try:
            if device_id is None:
                for device in self.device_list:
                    device_id = device.device_id
                    self.invoke_device_module_method(device_id, method_name, module_method, payload)
            else:
                self.invoke_device_module_method(device_id, method_name, module_method, payload)

        except Exception as ex:
            if ex.response.status_code == 404:
                print(">>>>>>>>>> Warning: device '%s' does not have the '%s' module deployed, or the module has not yet initalized <<<<<<<<<<" %
                      (device_id, self.module_id))

    def invoke_device_module_method(self, device_id, method_name, module_method, payload):
        print("\n----------------------- Device: %s - Request: %s  --------------------------------------------------\n" % (device_id, method_name))
        print(json.dumps(payload, indent=4))

        resp = self.registry_manager.invoke_device_module_method(device_id, self.module_id, module_method)

        print("\n----------------------- Device: %s - Response: %s - Status: %s  ------------------------------------\n" %
              (device_id, method_name, resp.status))

        if resp.payload is not None:
            print(json.dumps(resp.payload, indent=4))

    def graph_topology_set(self, op_parameters):
        if op_parameters is None:
            raise Exception('Operation parameters missing')

        if op_parameters.get('topologyUrl') is not None:
            topology_json = read_url(op_parameters['topologyUrl'])
        elif op_parameters.get('topologyFile') is not None:
            topology_path = Path(__file__).parent.joinpath(op_parameters['topologyFile'])
            topology_json = topology_path.read_text()
        else:
            raise Exception('Neither topologyUrl nor topologyFile is specified')

        topology = json.loads(topology_json)

        self.invoke_module_method('GraphTopologySet', topology)
        x = 1
        for d in query_result.items:
            print_twin("{0}: {1}".format(title, x), d)
            x += 1
    else:
        print("No item found")


try:
    # Create IoTHubRegistryManager
    iothub_registry_manager = IoTHubRegistryManager(iothub_connection_str)

    query_specification = QuerySpecification(query="SELECT * FROM devices")

    # Get specified number of devices (in this case 4)
    query_result0 = iothub_registry_manager.query_iot_hub(
        query_specification, None, 4)
    print_query_result("Query 4 device twins", query_result0)

    # Get all device twins using query
    query_result1 = iothub_registry_manager.query_iot_hub(query_specification)
    print_query_result("Query all device twins", query_result1)

    # Paging... Get more devices (over 1000)
    continuation_token = query_result1.continuation_token
    if continuation_token:
        query_result2 = iothub_registry_manager.query_iot_hub(
            query_specification, continuation_token)
        print_query_result("Query all device twins - continued", query_result2)

except Exception as ex:
    print("Unexpected error {0}".format(ex))
Exemplo n.º 3
0
import sys
from time import sleep
from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub.models import Twin, TwinProperties, QuerySpecification, QueryResult

iothub_registry_manager = IoTHubRegistryManager("Service Connection String")

twin = iothub_registry_manager.get_twin("Device_id")
twin_patch = Twin(properties=TwinProperties(
    desired={'Vision_Model_Version': 1.2}))
twin = iothub_registry_manager.update_twin(DEVICE_ID, twin_patch, twin.etag)

query_spec = QuerySpecification(
    query=
    "SELECT * FROM devices WHERE properties.reported.Vision_Model_Version <> 1.2"
)
query_result = iothub_registry_manager.query_iot_hub(query_spec, None, 100)
print("Devices that did not update: {}".format(', '.join(
    [twin.device_id for twin in query_result.items])))

print()

query_spec = QuerySpecification(
    query=
    "SELECT * FROM devices WHERE tags.location.plant = 'Redmond43' AND properties.reported.connectivity = 'cellular'"
)
query_result = iothub_registry_manager.query_iot_hub(query_spec, None, 100)
print("Devices in Redmond43 plant using cellular network: {}".format(', '.join(
    [twin.device_id for twin in query_result.items])))