def connect_from_environment(self, transport_type): print("connecting from environment") auth_provider = auth.from_environment() self.client = IoTHubModuleClient.from_authentication_provider( auth_provider, transport_type ) async_helper.run_coroutine_sync(self.client.connect())
def send_output_event(self, output_name, event_body): logger.info("sending output event") async_helper.run_coroutine_sync( self.client.send_message_to_output( convert.test_script_object_to_outgoing_message(event_body), output_name)) logger.info("send confirmation received")
def disconnect2(self): # disconnect2 keeps the object around. We might use it again async_helper.run_coroutine_sync(self.client.disconnect()) packets_left = self.get_inflight_packet_count() logger.info( "disconnect2: {} packets still in flight".format(packets_left)) assert packets_left == 0
def connect(self, transport_type, connection_string, cert): print("connecting using " + transport_type) auth_provider = auth.from_connection_string(connection_string) if "GatewayHostName" in connection_string: auth_provider.ca_cert = cert self.client = IoTHubDeviceClient.from_authentication_provider( auth_provider, transport_type) async_helper.run_coroutine_sync(self.client.connect())
def destroy(self): if self.client: async_helper.run_coroutine_sync(self.client.disconnect()) packets_left = self.get_inflight_packet_count() logger.info( "destroy: {} packets still in flight".format(packets_left)) self.client = None heap_check.assert_all_iothub_objects_have_been_collected() assert packets_left == 0
def invoke_device_method(self, device_id, method_invoke_parameters): logger.info("Invoking a method on the module.") method_response = async_helper.run_coroutine_sync( self.client.invoke_method(device_id=device_id, method_params=method_invoke_parameters)) logger.info("Method Invoked and response received.") return method_response
def wait_for_input_message(self, input_name): print("Waiting for input message") message = async_helper.run_coroutine_sync( self.client.receive_input_message(input_name) ) print("Message received") return message_to_object(message)
def wait_for_method_and_return_response(self, methodName, requestAndResponse): # receive method request logger.info("Waiting for method request") request = async_helper.run_coroutine_sync( self.client.receive_method_request(methodName)) logger.info("Method request received") # verify name and payload expected_name = methodName expected_payload = requestAndResponse.request_payload["payload"] if request.name == expected_name: if request.payload == expected_payload: logger.info( "Method name and payload matched. Returning response") resp_status = requestAndResponse.status_code resp_payload = requestAndResponse.response_payload else: logger.info("Request payload doesn't match") logger.info("expected: " + expected_payload) logger.info("received: " + request.payload) resp_status = 500 resp_payload = None else: logger.info("Method name doesn't match") logger.info("expected: '" + expected_name + "'") logger.info("received: '" + request.name + "'") resp_status = 404 resp_payload = None # send method response response = MethodResponse(request_id=request.request_id, status=resp_status, payload=resp_payload) async_helper.run_coroutine_sync( self.client.send_method_response(response)) logger.info("Method response sent")
def wait_for_desired_property_patch(self): logger.info("Waiting for desired property patch") patch = async_helper.run_coroutine_sync( self.client.receive_twin_desired_properties_patch()) logger.info("patch received") return patch
def connect2(self): async_helper.run_coroutine_sync(self.client.connect())
def connect(self, transport_type, connection_string, cert): logger.info("connecting using " + transport_type) self.create_from_connection_string(transport_type, connection_string, cert) async_helper.run_coroutine_sync(self.client.connect())
def wait_for_input_message(self, input_name): logger.info("Waiting for input message") message = async_helper.run_coroutine_sync( self.client.receive_message_on_input(input_name)) logger.info("Message received") return convert.incoming_message_to_test_script_object(message)
def wait_for_c2d_message(self): logger.info("Waiting for c2d message") message = async_helper.run_coroutine_sync( self.client.receive_message()) logger.info("Message received") return convert.incoming_message_to_test_script_object(message)
def send_twin_patch(self, props): logger.info("setting reported property patch") async_helper.run_coroutine_sync( self.client.patch_twin_reported_properties(props)) logger.info("done setting reported properties")
def wait_for_c2d_message(self): print("Waiting for c2d message") message = async_helper.run_coroutine_sync( self.client.receive_c2d_message()) print("Message received") return message_to_object(message)
def send_output_event(self, output_name, event_body): print("sending output event") async_helper.run_coroutine_sync( self.client.send_to_output(normalize_event_body(event_body), output_name) ) print("send confirmation received")
def send_event(self, event_body): print("sending event") async_helper.run_coroutine_sync( self.client.send_event(normalize_event_body(event_body)) ) print("send confirmation received")
def disconnect(self): print("disconnecting") if self.client: async_helper.run_coroutine_sync(self.client.disconnect()) self.client = None
def connect_from_environment(self, transport_type): logger.info("connecting from environment") self.create_from_environment(transport_type) async_helper.run_coroutine_sync(self.client.connect())
def get_twin(self): logger.info("getting twin") twin = async_helper.run_coroutine_sync(self.client.get_twin()) logger.info("done getting twin") return {"properties": twin}