本文介绍了用于监视边缘模块运行状况的Azure功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想要创建一个azure函数来监视iothub中我的边缘模块的运行状况.通过获取模块的twin属性,我将能够做到.
实际上,我有一个C#代码,它给出了预期的结果.但是我对azure函数的概念很陌生,并且对如何使用它们感到困惑.
以下是我用来检索模块twin属性的代码

TwinSample.cs
-------------------------------------------------- -------------------------------------------------- ---
使用Microsoft.Azure.Devices.Shared;
使用Microsoft.Azure.Amqp;
使用系统;
使用System.Threading.Tasks;

命名空间Microsoft.Azure.Devices.Client.Samples
{
   公共课TwinSample
    {
       私人ModuleClient _moduleClient;

       公共TwinSample(ModuleClient moduleClient)
        {
            _moduleClient = moduleClient ??抛出新的ArgumentNullException(nameof(moduleClient));
        }

       公共异步Task< string> RunSampleAsync()
        {

            Console.WriteLine(正在检索孪生...");
           双胞胎twin =等待_moduleClient.GetTwinAsync().ConfigureAwait(false);

            Console.WriteLine("\ t收到的初始孪生值:");

            Console.WriteLine($"\ t {twin.ToJson()}");
           返回twin.ToJson();
        }


    }

-------------------------------------------------- -------------------------------------------------- ---------------------------
program.cs
-------------------------------------------------- -------------------------------------------------- --------------------------
使用Newtonsoft.Json;
使用系统;
使用Microsoft.Azure.Amqp;
使用Microsoft.Azure.Devices.Shared;



命名空间Microsoft.Azure.Devices.Client.Samples
{
   公共课程计划
    {

       私有静态字符串s_moduleConnectionString = Environment.GetEnvironmentVariable("IOTHUB_MODULE_CONN_STRING");
       私有静态TransportType s_transportType = TransportType.Amqp;


       公共静态void Main(string [] args)
        {
           如果(string.IsNullOrEmpty(s_moduleConnectionString)&& args.Length> 0)
            {
                s_moduleConnectionString = args [0];
            }

                ModuleClient moduleClient = ModuleClient.CreateFromConnectionString(s_moduleConnectionString,s_transportType);

                var sample = new TwinSample(moduleClient);
                Sample.RunSampleAsync().GetAwaiter().GetResult();
                Console.ReadLine();


        }
    }
}

-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------
谁能解释一下如何在azure函数中使相同的功能起作用?

谢谢

I  want to create an azure function to monitor the health of my edge modules in the iothub. By Retrieving the twin Properties of the module i will be able to do it.
Infact i have a C# code which is giving the desired result. But I am very new to the concept of azure functions and i am stuck on how to work with them.
following is the code i am using to retrieve module twin properties

TwinSample.cs
-------------------------------------------------------------------------------------------------------
using Microsoft.Azure.Devices.Shared;
using Microsoft.Azure.Amqp;
using System;
using System.Threading.Tasks;

namespace Microsoft.Azure.Devices.Client.Samples
{
    public class TwinSample
    {
        private ModuleClient _moduleClient;

        public TwinSample(ModuleClient moduleClient)
        {
            _moduleClient = moduleClient ?? throw new ArgumentNullException(nameof(moduleClient));
        }

        public async Task<string> RunSampleAsync()
        {

            Console.WriteLine("Retrieving twin...");
            Twin twin = await _moduleClient.GetTwinAsync().ConfigureAwait(false);

            Console.WriteLine("\tInitial twin value received:");

            Console.WriteLine($"\t{twin.ToJson()}");
            return twin.ToJson();
        }


    }

-------------------------------------------------------------------------------------------------------------------------------
program.cs
------------------------------------------------------------------------------------------------------------------------------
using Newtonsoft.Json;
using System;
using Microsoft.Azure.Amqp;
using Microsoft.Azure.Devices.Shared;



namespace Microsoft.Azure.Devices.Client.Samples
{
    public class Program
    {

        private static string s_moduleConnectionString = Environment.GetEnvironmentVariable("IOTHUB_MODULE_CONN_STRING");
        private static TransportType s_transportType = TransportType.Amqp;


        public static void Main(string[] args)
        {
            if (string.IsNullOrEmpty(s_moduleConnectionString) && args.Length > 0)
            {
                s_moduleConnectionString = args[0];
            }

                ModuleClient moduleClient = ModuleClient.CreateFromConnectionString(s_moduleConnectionString, s_transportType);

                var sample = new TwinSample(moduleClient);
                Sample.RunSampleAsync().GetAwaiter().GetResult();
                Console.ReadLine();


        }
    }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------
Can any one explain me how to make the same functionality work in azure function?

Thank You.

推荐答案

某种输出消息表明它们已连接?您要将输出存储在某个地方吗? 

Some sort of output message that states they are connected? Did you want that output stored somewhere? 


这篇关于用于监视边缘模块运行状况的Azure功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 15:12