Azure IoT边缘离线功能示例



我在如何使用IoT边缘模式的IoT Edge模式上有些困惑。我虽然是盒子外!

我的物联网位置位于美国西部。当我将边缘设备与网络断开连接时,什么都不会发生。在线重新连接数据后,数据未保存或重新连接。

我只有一个将数据发送到IoT中心的模块,我可以看到带有设备Explorer Twin App的数据,然后将数据保存在数据库中。

断开连接后,等待5分钟并重新连接,我看不到我在数据库中离线模式期间要发送的数据。在离线丢失时的所有消息(我正在使用DateTime邮票对消息进行排序)。

我是否错过了配置?

知道为什么离线模式对我不起作用吗?我正在使用Iot Edge运行时V1.0.6和Windows容器。

在这里我的测试模块的源代码:

using Microsoft.Azure.Devices.Client;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Runtime.Loader;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    class Program
    {
        static int monitoringInterval { get; set; } = 60;// 60 seconds
        static System.Timers.Timer testTimer;
        static ModuleClient ioTHubModuleClient;
        static void Main(string[] args)
        {
            Init().Wait();
            StartTestTimer();
            // Wait until the app unloads or is cancelled
            var cts = new CancellationTokenSource();
            AssemblyLoadContext.Default.Unloading += (ctx) => cts.Cancel();
            Console.CancelKeyPress += (sender, cpe) => cts.Cancel();
            WhenCancelled(cts.Token).Wait();
        }
        /// <summary>
        /// Handles cleanup operations when app is cancelled or unloads
        /// </summary>
        public static Task WhenCancelled(CancellationToken cancellationToken)
        {
            var tcs = new TaskCompletionSource<bool>();
            cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).SetResult(true), tcs);
            return tcs.Task;
        }
        /// <summary>
        /// Initializes the ModuleClient and sets up the callback to receive
        /// messages containing temperature information
        /// </summary>
        static async Task Init()
        {
            AmqpTransportSettings amqpSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);
            ITransportSettings[] settings = { amqpSetting };
            // Open a connection to the Edge runtime
            ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
            await ioTHubModuleClient.OpenAsync();
            Console.WriteLine("IoT Hub module client initialized.");
        }

        static void StartTestTimer()
        {
            Console.WriteLine("Start Monitoring Timer: " + monitoringInterval + " seconds");
            // Set up a timer that triggers every minute.
            testTimer = new System.Timers.Timer();
            testTimer.Interval = monitoringInterval * 1000; // 60 seconds
            testTimer.Elapsed += new System.Timers.ElapsedEventHandler(SendEvent);
            testTimer.Start();
            SendEvent(null, null);
        }

        async static void SendEvent(object sender, System.Timers.ElapsedEventArgs args)
        {
            DateTime today = DateTime.Now;
            Console.WriteLine("[" + today + "] Send Data has started...");
            try
            {                
                //IoT device connection string
                string connectionString = "HostName=xxxxxx.azure-devices.net;DeviceId=IOT-Device1;SharedAccessKey=ett8xxxxxxxxx";
                // Connect to the IoT hub using the MQTT protocol
                DeviceClient _DeviceClient = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Mqtt);
                _DeviceClient.OperationTimeoutInMilliseconds = 10000;
                Dictionary<string, Object> telemetryDataPoint = new Dictionary<string, Object>();
                string dateTime = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
                telemetryDataPoint.Add("DateTime", dateTime);               
                string messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                Message message = new Message(Encoding.ASCII.GetBytes(messageString));
                // Send the telemetry message
                Console.WriteLine("n*> Sending message: {0}",  messageString);
                await _DeviceClient.SendEventAsync(message).ConfigureAwait(false);
                Console.WriteLine("Message sent!");
            }
            catch (Exception e)
            {
                Console.WriteLine("Message not sent. Connection error to Iot Hub:" + e.Message);
            }
        }
    }

为什么代码在init()中创建模量,然后尝试使用sendevent()中的deviceClient直接向IoT Hub发送消息?这完全绕过边缘运行时(特别是EdgeHub),这是促进离线存储和向前的促进。

Div>

最新更新