Hello @Sander van de Velde ,
thank you very much for the quick and detailed answer, this helped!
I would like to try both methods:
1) Use Eventgrid
I implemented the EventGrid pattern like this:
function.json:
{
"scriptFile": "init.py",
"bindings": [
{
"type": "eventGridTrigger",
"name": "eventGridEvent",
"direction": "in"
}
]
}
init.py:
import json
import logging
import azure.functions as func
import datetime
import random
import sys
from azure.iot.hub import IoTHubRegistryManager
MESSAGE_COUNT = 1
AVG_WIND_SPEED = 10.0
MSG_TXT = "{\"service client sent a message\": %.2f}"
CONNECTION_STRING = "HostName=XXXX.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=XXXXX"
DEVICE_ID = "XXXX"
def iothub_messaging_sample_run():
try:
# Create IoTHubRegistryManager
registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
for i in range(0, MESSAGE_COUNT):
print ( 'Sending message: {0}'.format(i) )
data = MSG_TXT % (AVG_WIND_SPEED + (random.random() * 4 + 2))
props={}
# optional: assign system properties
props.update(messageId = "Return_message_%d" % i)
props.update(correlationId = "correlation_%d" % i)
props.update(contentType = "application/json")
# optional: assign application properties
prop_text = "PropMsg_%d" % i
props.update(testProperty = prop_text)
registry_manager.send_c2d_message(DEVICE_ID, data, properties=props)
except Exception as ex:
print ( "Unexpected error {0}" % ex )
return
except KeyboardInterrupt:
print ( "IoT Hub C2D Messaging service sample stopped" )
def main(eventGridEvent: func.EventGridEvent) -> None:
result = json.dumps({
'id': eventGridEvent.id,
'data': eventGridEvent.get_json(),
'topic': eventGridEvent.topic,
'subject': eventGridEvent.subject,
'event_type': eventGridEvent.event_type,
})
logging.info('Python EventGrid trigger processed an event: %s', result)
iothub_messaging_sample_run()
msg_data = eventGridEvent.get_json()
logging.info(msg_data)
if 'properties' in msg_data and 'position' in msg_data['properties']:
logging.info(msg_data['properties']['position'])
As a first step i printed the position and returned a simulated value. It works. But i am not sure this is good and efficient. Is it better or possible to return it to the IoT Hub and so on to the device via an EventGrid output binding?
Likes this:
function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"type": "eventGridTrigger",
"name": "eventGridEvent",
"direction": "in"
},
{
"type": "eventGrid",
"name": "outputEvent",
"topicEndpointUri": "MyEventGridTopicUriSetting",
"topicKeySetting": "MyEventGridTopicKeySetting",
"direction": "out"
}
],
"disabled": false
}
init.py:
import json
import logging
import azure.functions as func
import datetime
def main(eventGridEvent: func.EventGridEvent,
outputEvent: func.Out[func.EventGridOutputEvent]) -> None:
result = json.dumps({
'id': eventGridEvent.id,
'data': eventGridEvent.get_json(),
'topic': eventGridEvent.topic,
'subject': eventGridEvent.subject,
'event_type': eventGridEvent.event_type,
})
logging.info('Python EventGrid trigger processed an event: %s', result)
# ToDo: Write correct outputEvent.set(...)
outputEvent.set(
func.EventGridOutputEvent(
id="test-id",
data={"tag1": "value1", "tag2": "value2"},
subject="test-subject",
event_type="test-event-1",
event_time=datetime.datetime.utcnow(),
data_version="1.0"))
If this is possible. Where is the "topicEndpointUri" and the "topicKeySetting" from the function.json file to find?
2) Using build-in eventhub compatible endpoint
Could you give me an introduction bechause i tried it but i am very confused how to do it.
Thank you very much.
P.S. With the region i meant that the option to add an event subscription to the IoT hub wasn't availible. But from this day this option is unlocked. Not all regions offer all features.
With best regards
Patrick