Azure流分析错误:无法从IOT中心

时间:2018-01-11 12:02:44

标签: json azure asp.net-core-2.0 azure-iot-hub azure-stream-analytics

我创建了Stream Analytics作业,以便从IOT中心读取数据作为输入,并将数据存储到SQL DB。

以下是为Steam Analytics作业配置的一些重要输入详细信息 事件序列化格式:JSON 编码:utf-8

消息从Dotnet模拟代码发送到IOT Hub。

当我正在运行我的工作时,我收到以下错误:

 Could not deserialize the input event as Json. Some possible reasons: 
 1) Malformed events 
 2) Input source configured with incorrect serialization format

这是我的dotnet代码。

       private static async void ReceiveC2dAsync()
    {
        Console.WriteLine("\nReceiving cloud to device messages from service");
        while (true)
        {
            Message receivedMessage = await deviceClient.ReceiveAsync();
            if (receivedMessage == null) continue;

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Received message: {0}", Encoding.ASCII.GetString(receivedMessage.GetBytes()));
            Console.ResetColor();

            await deviceClient.CompleteAsync(receivedMessage);
        }
    }
    private static async void SendDeviceToCloudMessagesAsync()
    {
        double minTemperature = 20;
        double minHumidity = 60;
        int messageId = 1;
        Random rand = new Random();

        while (true)
        {
            double currentTemperature = minTemperature + rand.NextDouble() * 15;
            double currentHumidity = minHumidity + rand.NextDouble() * 20;

            var telemetryDataPoint = new
            {
                messageId = messageId++,
                deviceId = "myFirstDevice",
                temperature = currentTemperature,
                humidity = currentHumidity
            };
            var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
            string levelValue;
            string temperatureAlert = "false";


            if (rand.NextDouble() > 0.7)
            {
                if (rand.NextDouble() > 0.5)
                {
                    messageString = "This is a critical message";
                    levelValue = "critical";
                }
                else
                {
                    messageString = "This is a storage message";
                    levelValue = "storage";
                }
            }
            else
            {
                levelValue = "normal";
            }

            if(currentTemperature > 30)
            {
                temperatureAlert = "true";
            }

            var message = new Message(Encoding.UTF8.GetBytes(messageString));
            message.Properties.Add("level", levelValue);
            message.Properties.Add("temperatureAlert", temperatureAlert);

            await deviceClient.SendEventAsync(message);
            Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);

            await Task.Delay(1000);
        }
    }

1 个答案:

答案 0 :(得分:1)

看起来您的模拟设备生成了非json格式的消息,例如“这是一条关键消息”和“这是一条存储消息”。

基本上,您有两种方法可以解决此问题: 1.在模拟代码中评论此部分或 2.在Azure IoT Hub路由中为这些消息添加过滤器