Power Platform 低代码开发手机App(3)-Power Apps 通过Power BI 磁贴显示物联网设备实时数据曲线

本文是Power Platform 低代码开发物联网App 系列文章的第三讲:

《 概览 -Power Platform低代码开发物联网App

(1)-Power Platform 试用账号注册

(2)-Power Apps 连接Azure SQL Database 读取物联网设备清单

(3)-Power Apps 通过Power BI 磁贴显示物联网设备实时数据曲线

(4.1)-将Azure IoT Service SDK 集成到 Azure Function并发布 》

(4.2)-Power Apps 通过Power Automate 发起Http请求调用Azure functions 进行设备远程控制

(5.1)-利用Azure Stream Analytics 将物联网遥测历史消息写入Azure SQL Database 》

(5.2)-Power Apps 查询物联网设备历史遥测消息

 

 

本文介绍:

在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中查看实时数据;

 

 

 

本例是一个综合的案例,关于Power BI 流数据集和IoT的相关信息,不再进行文字描述,可以看视频了解详细步骤。

https://www.51azure.cloud/post/2021/1/6/power-apps-power-bi-iot-real-time-data

 

 

本文中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;}

    }
}

 

 

 

如果不想使用IoT Hub,可以直接参考之前的文章,通过程序模拟的方式,生成实时数据:

《使用Power BI API 向流数据集推送实时数据并在仪表板可视化》

 

 

 





声明:

 

点击可查阅本站文章目录 《文章分类目录》

本站所有内容仅代表个人观点,如与官文档冲突,请以官方文档为准。

可在本页面下方留言或通过下方联系方式联系我:

微信:wxyusz;邮箱:[email protected]

欢迎关注公众号“云计算实战”,接收最新文章推送。



知识共享许可协议

本作品由Sean Yu 采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
欢迎转载、使用、重新发布,但务必保留文章链接:https://www.51azure.cloud,且不得用于商业目的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章