Power Apps 通过Power BI 磁贴展示实时数据(IoT遥测数据)案例

在Power Apps中通过Power BI 磁贴展示实时数据(物联网设备遥测数据);

示意图如下:

1.物联网设备通过SDK将遥测的温湿度值发送到IoT Hub;

2.使用Azure Functions 的IoT Hub触发读取遥测消息,并将遥测消息post到Power BI的流数据集中;

3.在Power BI中创建dashboard,并将流数据集制作成磁贴;

4.在Power Apps中添加Power BI 磁贴并保存;

5.在手机版Power Apps中查看实时数据; 

本文中Azure Functions的示例代码:

using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.EventHubs;
using System.Text;
using System.Net.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Net;
using System.IO;

namespace Company.Function
{
    public static class IotHubTriggerCSharp1
    {
        private static HttpClient client = new HttpClient();

        [FunctionName("IotHubTriggerCSharp1")]
        public static void Run([IoTHubTrigger("messages/events", Connection = "iotconnstring")]EventData message, ILogger log)
        {

            string msgBody=Encoding.UTF8.GetString(message.Body.Array);

            log.LogInformation($"C# IoT Hub trigger function processed a message: {msgBody}");
             string url="your pbi stream dataset endpoint";

             IoTDeviceMsg msg = JsonConvert.DeserializeObject<IoTDeviceMsg>(msgBody);

             var telemetryDataPoint=new {
                temperature = msg.temperature,
                humidity = msg.humidity,
                deviceid = "device001",
                telemetrydt=DateTime.Now
                };

            var messageString=JsonConvert.SerializeObject(telemetryDataPoint) ;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.Timeout = 8000;
            req.ContentType = "application/json";
            byte[] data = Encoding.UTF8.GetBytes("[" + messageString + "]");
                req.ContentLength = data.Length;

                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                    reqStream.Close();
                }

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();
                 if (resp.StatusCode == HttpStatusCode.OK)
                {
                     log.LogInformation($"OK: {messageString}");
                }
        }

    }

    public class IoTDeviceMsg
    {
        public decimal temperature{get;set;}
        public decimal humidity{get;set;}

    }
}

个人建站、企业建站,域名注册、小程序、云服务器、物理主机等业务,咨询蓝队云客服微信:landui507 蓝队云官方网站:www.landui.com/?codepub

未经允许不得转载:云技术 » Power Apps 通过Power BI 磁贴展示实时数据(IoT遥测数据)案例

赞 (0)