您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用逻辑应用从 Azure 数字孪生空间接收通知Tutorial: Receive notifications from your Azure Digital Twins spaces by using Logic Apps

部署 Azure 数字孪生实例、预配空间并实现用于监视特定条件的自定义函数以后,即可在受监视的条件满足时通过电子邮件通知办公室主任。After you deploy your Azure Digital Twins instance, provision your spaces, and implement custom functions to monitor specific conditions, you can notify your office admin via email when the monitored conditions occur.

第一个教程中,你配置了一栋虚构大楼的空间图,In the first tutorial, you configured the spatial graph of an imaginary building. 其中一个房间包含用于移动、二氧化碳和温度的传感器。A room in the building contains sensors for motion, carbon dioxide, and temperature. 第二个教程中,你预配了图形和一个用户定义函数,用于监视这些传感器值并在房间为空且温度和二氧化碳处于某个令人舒适的范围内时触发通知。In the second tutorial, you provisioned your graph and a user-defined function to monitor these sensor values and trigger notifications when the room is empty, and the temperature and carbon dioxide are in a comfortable range.

本教程介绍如何将这些通知与 Azure 逻辑应用集成,以便在此类房间可用时发送电子邮件。This tutorial shows how you can integrate these notifications with Azure Logic Apps to send emails when such a room is available. 办公室主任可以根据此信息帮助员工预订工作效率最高的会议室。An office administrator can use this information to help the employees book the most productive meeting room.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 将事件与 Azure 事件网格集成。Integrate events with Azure Event Grid.
  • 使用逻辑应用发出事件通知。Notify events with Logic Apps.

先决条件Prerequisites

本教程假定你已配置预配 Azure 数字孪生设置。This tutorial assumes that you have configured and provisioned your Azure Digital Twins setup. 在继续操作之前,请确保已具备以下条件:Before proceeding, make sure that you have:

将事件与事件网格集成Integrate events with Event Grid

在此部分,请设置一个事件网格,以便从 Azure 数字孪生实例收集事件,然后将事件重定向到事件处理程序,例如逻辑应用。In this section, you set up Event Grid to collect events from your Azure Digital Twins instance, and redirect them to an event handler such as Logic Apps.

创建事件网格主题Create an event grid topic

事件网格主题提供一个界面,用于路由用户定义的函数生成的事件。An event grid topic provides an interface to route the events generated by the user-defined function.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 在左窗格中,选择“创建资源”。In the left pane, select Create a resource.

  3. 搜索并选择“事件网格主题”。Search for and select Event Grid Topic. 选择“创建”。Select Create.

  4. 为事件网格主题输入一个“名称”,然后选择“订阅”。Enter a Name for your event grid topic, and choose the Subscription. 选择用于数字孪生实例的或者为其创建的“资源组”,然后选择“位置”。Select the Resource group that you used or created for your Digital Twins instance, and the Location. 选择“创建”。Select Create.

    创建事件网格主题

  5. 从资源组浏览到事件网格主题,选择“概览”,将“主题终结点”的值复制到某个临时文件。Browse to the event grid topic from your resource group, select Overview, and copy the value for Topic Endpoint to a temporary file. 下一部分将需要此 URL。You'll need this URL in the next section.

  6. 选择“访问密钥”,将 YOUR_KEY_1YOUR_KEY_2 复制到某个临时文件。Select Access keys, and copy YOUR_KEY_1 and YOUR_KEY_2 to a temporary file. 在下一部分,将需要这些值来创建终结点。You'll need these values to create the endpoint in the next section.

    事件网格密钥

为事件网格主题创建终结点Create an endpoint for the event grid topic

  1. 在命令窗口中,确保当前位置为数字孪生示例的 occupancy-quickstart\src 文件夹。In the command window, make sure you're in the occupancy-quickstart\src folder of the Digital Twins sample.

  2. 在 Visual Studio Code 编辑器中打开 actions\createEndpoints.yaml 文件。Open the file actions\createEndpoints.yaml in your Visual Studio Code editor. 确保其包含以下内容:Make sure it has the following contents:

    - type: EventGrid
      eventTypes:
      - SensorChange
      - SpaceChange
      - TopologyOperation
      - UdfCustom
      connectionString: Primary_connection_string_for_your_Event_Grid
      secondaryConnectionString: Secondary_connection_string_for_your_Event_Grid
      path: Event_Grid_Topic_Path
    
  3. 将占位符 Primary_connection_string_for_your_Event_Grid 替换为 YOUR_KEY_1 的值。Replace the placeholder Primary_connection_string_for_your_Event_Grid with the value of YOUR_KEY_1.

  4. 将占位符 Secondary_connection_string_for_your_Event_Grid 替换为 YOUR_KEY_2 的值。Replace the placeholder Secondary_connection_string_for_your_Event_Grid with the value of YOUR_KEY_2.

  5. 将占位符 Event_Grid_Topic_Path 替换为事件网格主题的路径。Replace the placeholder Event_Grid_Topic_Path with the path of the event grid topic. 主题终结点 URL 中删除 https:// 以及尾随的资源路径即可获取此路径。Get this path by removing https:// and the trailing resource paths from the Topic Endpoint URL. 它看起来应该类似于此格式:yourEventGridName.yourLocation.eventgrid.azure.netIt should look similar to this format: yourEventGridName.yourLocation.eventgrid.azure.net.

    重要

    输入所有值,不带任何引号。Enter all values without any quotes. 在 YAML 文件中,请确保在冒号之后至少有一个空格字符。Make sure there's at least one space character after the colons in the YAML file. 也可使用任何联机 YAML 验证程序(例如此工具)来验证 YAML 文件内容。You can also validate your YAML file contents by using any online YAML validator such as this tool.

  6. 保存并关闭该文件。Save and close the file. 在命令窗口中,当出现提示时,请运行以下命令并登录。In the command window, run the following command, and sign in when prompted.

    dotnet run CreateEndpoints
    

    此命令为事件网格创建终结点。This command creates the endpoint for Event Grid.

    事件网格的终结点

使用逻辑应用发出事件通知Notify events with Logic Apps

可以通过 Azure 逻辑应用服务为从其他服务接收的事件创建自动化任务。You can use the Azure Logic Apps service to create automated tasks for events received from other services. 在此部分,请设置逻辑应用,以便借助事件网格主题为从空间传感器路由的事件创建电子邮件通知。In this section, you set up Logic Apps to create email notifications for events routed from your spatial sensors, with the help of an event grid topic.

  1. Azure 门户的左窗格中,选择“创建资源”。In the left pane of the Azure portal, select Create a resource.

  2. 搜索并选择新的“逻辑应用”资源。Search and select a new Logic App resource. 选择“创建”。Select Create.

  3. 为逻辑应用资源输入一个名称,然后选择你的订阅资源组位置Enter a Name for your Logic App resource, and then select your Subscription, Resource group, and Location. 选择“创建”。Select Create.

    创建逻辑应用资源

  4. 在逻辑应用资源部署后将其打开,然后打开“逻辑应用设计器”窗格。Open your Logic Apps resource when it's deployed, and then open the Logic App Designer pane.

  5. 选择“当事件网格事件发生时”触发器。Select the When an Event Grid event occurs trigger. 当系统提示时,使用 Azure 帐户登录到租户。Sign in to your tenant with your Azure account when prompted. 当系统提示时,针对事件网格资源选择“允许访问”。Select Allow access for your Event Grid resource when prompted. 选择“继续”。Select Continue.

  6. 在“当资源事件发生时(预览版)”窗口中,执行以下操作:In the When a resource event occurs (Preview) window:

    a.a. 选择曾经用于创建事件网格主题的订阅Select the Subscription that you used to create the event grid topic.

    b.b. 选择 Microsoft.EventGrid.Topics 作为资源类型Select Microsoft.EventGrid.Topics for Resource Type.

    c.c. 资源名称对应的下拉框中选择事件网格资源。Select your Event Grid resource from the drop-down box for Resource Name.

    “逻辑应用设计器”窗格

  7. 选择“新步骤”按钮。Select the New step button.

  8. 在“选择操作”窗口中,执行以下操作:In the Choose an action window:

    a.a. 搜索“分析 json”短语,然后选择“分析 JSON”操作。Search the phrase parse json, and select the Parse JSON action.

    b.b. 在“内容”字段的“动态内容”列表中选择“正文”。In the Content field, select Body from the Dynamic content list.

    c.c. 选择“使用示例有效负载生成架构”。Select Use sample to payload to generate schema. 粘贴以下 JSON 有效负载,然后选择“完成”。Paste the following JSON payload, and then select Done.

    {
    "id": "32162f00-a8f1-4d37-aee2-9312aabba0fd",
    "subject": "UdfCustom",
    "data": {
      "TopologyObjectId": "20efd3a8-34cb-4d96-a502-e02bffdabb14",
      "ResourceType": "Space",
      "Payload": "\"Air quality is poor.\"",
      "CorrelationId": "32162f00-a8f1-4d37-aee2-9312aabba0fd"
    },
    "eventType": "UdfCustom",
    "eventTime": "0001-01-01T00:00:00Z",
    "dataVersion": "1.0",
    "metadataVersion": "1",
    "topic": "/subscriptions/a382ee71-b48e-4382-b6be-eec7540cf271/resourceGroups/HOL/providers/Microsoft.EventGrid/topics/DigitalTwinEventGrid"
    }
    

    此有效负载包含虚构值。This payload has fictitious values. 逻辑应用使用此示例有效负载生成一个架构Logic Apps uses this sample payload to generate a schema.

    适用于事件网格的逻辑应用“分析 JSON”窗口

  9. 选择“新步骤”按钮。Select the New step button.

  10. 在“选择操作”窗口中,执行以下操作:In the Choose an action window:

    a.a. 选择“控制”>“条件”或从“操作”列表中搜索“条件”。Select Control > Condition or search Condition from the Actions list.

    b.b. 在第一个“选择值”文本框的“分析 JSON”窗口的“动态内容”列表中选择“eventType”。In the first Choose a value text box, select eventType from the Dynamic content list for the Parse JSON window.

    c.c. 在第二个“选择值”文本框中输入“UdfCustom”。In the second Choose a value text box, enter UdfCustom.

    所选条件

  11. If true 窗口中,执行以下操作:In the If true window:

    a.a. 选择“添加操作”,然后选择“Office 365 Outlook”。Select Add an action, and select Office 365 Outlook.

    b.b. 从“操作”列表中选择“发送电子邮件”。From the Actions list, select Send an email. 选择“登录”,使用电子邮件帐户凭据。Select Sign in and use your email account credentials. 当系统提示时,请选择“允许访问”。Select Allow access when prompted.

    c.c. 在“收件人”框中,输入用于接收通知的电子邮件 ID。In the To box, enter your email ID to receive notifications. 在“主题”中输入文本“有关空间中空气质量差的数字孪生通知”,In Subject, enter the text Digital Twins notification for poor air quality in space. 然后从分析 JSON 的“动态内容”列表中选择“TopologyObjectId”。Then select TopologyObjectId from the Dynamic content list for Parse JSON.

    d.d. 在同一窗口中的“正文”下,输入与此类似的文本:检测到房间内空气质量差,需要调整温度Under Body in the same window, enter text similar to this: Poor air quality detected in a room, and temperature needs to be adjusted. 请根据需要使用“动态内容”列表中的元素进行详细阐述。Feel free to elaborate by using elements from the Dynamic content list.

    逻辑应用的“发送电子邮件”选项

  12. 选择“逻辑应用设计器”窗格顶部的“保存”按钮。Select the Save button at the top of the Logic App Designer pane.

  13. 确保模拟传感器数据,方法是:在命令窗口中浏览到数字孪生示例的 device-connectivity 文件夹,然后运行 dotnet runMake sure to simulate sensor data by browsing to the device-connectivity folder of the Digital Twins sample in a command window, and running dotnet run.

数分钟后,就会开始从此逻辑应用资源中获取电子邮件通知。In a few minutes, you should start getting email notifications from this Logic Apps resource.

电子邮件通知

若要停止接收这些电子邮件,请转到门户中的逻辑应用资源,然后选择“概览”窗格。To stop receiving these emails, go to your Logic Apps resource in the portal, and select the Overview pane. 选择“禁用”。Select Disable.

清理资源Clean up resources

如果不希望继续探索 Azure 数字孪生,可以删除本教程中创建的资源:If you want to stop exploring Azure Digital Twins at this point, feel free to delete resources created in this tutorial:

  1. Azure 门户的左菜单中依次选择“所有资源”、数字孪生资源组、“删除”。From the left menu in the Azure portal, select All resources, select your Digital Twins resource group, and select Delete.

    提示

    如果在删除数字孪生实例时遇到麻烦,请使用已推出的包含修补程序的服务更新。If you experienced trouble deleting your Digital Twins instance, a service update has been rolled out with the fix. 请重新尝试删除实例。Please retry deleting your instance.

  2. 可以根据需要删除工作计算机上的示例应用程序。If necessary, delete the sample applications on your work machine.

后续步骤Next steps

若要了解如何可视化传感器数据、分析趋势和查找异常,请继续阅读下一教程:To learn how to visualize your sensor data, analyze trends, and spot anomalies, go to the next tutorial:

也可详细了解 Azure 数字孪生中的空间智能图和对象模型:You can also learn more about the spatial intelligence graphs and object models in Azure Digital Twins: