我们已经在 Azure IOT Central 上注册了我们的 IOT 设备,并且能够在 IoT Central 上可视化遥测数据。但是,当我们使用 Azure 在 Android 示例应用程序中的 IOT 中央生成的主键发送遥测数据时,我们无法可视化数据。
我错过了什么吗?我们如何将 iOS 和 Android 注册为 IoT Central 上的设备以可视化数据?
我们已经在 Azure IOT Central 上注册了我们的 IOT 设备,并且能够在 IoT Central 上可视化遥测数据。但是,当我们使用 Azure 在 Android 示例应用程序中的 IOT 中央生成的主键发送遥测数据时,我们无法可视化数据。
我错过了什么吗?我们如何将 iOS 和 Android 注册为 IoT Central 上的设备以可视化数据?
以下屏幕片段显示了使用 REST Post 请求将遥测数据发送到 IoT Central 应用程序的示例:
点击按钮:
private async void FabOnClick(object sender, EventArgs eventArgs)
{
double tempValue = double.Parse(floatingTemperatureLabel.Text);
var payload = new { Temperature = tempValue };
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", sasToken);
await client.PostAsync(requestUri, new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json"));
}
View view = (View) sender;
Snackbar.Make(view, $"Sent: {tempValue}", Snackbar.LengthLong).SetAction("Action", (Android.Views.View.IOnClickListener)null).Show();
}
遥测数据被发送到 IoT Central 应用程序:
请注意,上面示例的requestUri和sasToken是在我的单独工具上创建的,但它们可以从基于 IoTC 应用程序的scopeId、deviceId和SAS 令牌的自定义 azure 函数中获取。
更新:
对于运行时注册(包括分配给设备模板)可以使用带有以下负载的REST PUT请求:
{
"registrationId":"yourDeviceId",
"payload": {
"__iot:interfaces": {
"CapabilityModelId":"yourCapabilityModelId"
}
}
}
请注意,授权标头(例如 sasToken)可以通过以下方式生成:
string sasToken = SharedAccessSignatureBuilder.GetSASToken($"{scopeId}/registrations/{deviceId}", deviceKey, "registration");
注册过程的状态可以通过REST GET 获取,其中DeviceRegistrationResult为您提供 IoT Central 应用程序的基础 Azure IoT 中心的命名空间,例如assignedHub属性。
正如我上面提到的,所有这些 REST API 调用都可以通过 azure 函数处理(实现),并且您的移动应用程序将根据您 IoTC 应用程序的 scopeId、deviceId、deviceTemplateId 和 sas-token 在发布响应中获取requestUrl和sasToken .
更新 2:
我正在添加 azure 函数 ( HttpTriggerGetConnectionInfo ) 的示例,以获取设备 http 协议到 IoT Central 应用程序的连接信息:
该功能需要将以下变量添加到应用程序设置中:
运行.csx:
#r "Newtonsoft.Json"
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
int retryCounter = 10;
int pollingTimeInSeconds = 3;
string deviceId = req.Query["deviceid"];
string cmid = req.Query["cmid"];
if (!Regex.IsMatch(deviceId, @"^[a-z0-9\-]+$"))
throw new Exception($"Invalid format: DeviceID must be alphanumeric, lowercase, and may contain hyphens");
string iotcScopeId = System.Environment.GetEnvironmentVariable("AzureIoTC_scopeId");
string iotcSasToken = System.Environment.GetEnvironmentVariable("AzureIoTC_sasToken");
if(string.IsNullOrEmpty(iotcScopeId) || string.IsNullOrEmpty(iotcSasToken))
throw new ArgumentNullException($"Missing the scopeId and/or sasToken of the IoT Central App");
string deviceKey = SharedAccessSignatureBuilder.ComputeSignature(iotcSasToken, deviceId);
string address = $"https://global.azure-devices-provisioning.net/{iotcScopeId}/registrations/{deviceId}/register?api-version=2019-03-31";
string sas = SharedAccessSignatureBuilder.GetSASToken($"{iotcScopeId}/registrations/{deviceId}", deviceKey, "registration");
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", sas);
client.DefaultRequestHeaders.Add("accept", "application/json");
string jsontext = string.IsNullOrEmpty(cmid) ? null : $"{{ \"__iot:interfaces\": {{ \"CapabilityModelId\":\"{cmid}\"}} }}";
var response = await client.PutAsync(address, new StringContent(JsonConvert.SerializeObject(new { registrationId = deviceId, payload = jsontext }), Encoding.UTF8, "application/json"));
var atype = new { errorCode = "", message = "", operationId = "", status = "", registrationState = new JObject() };
do
{
dynamic operationStatus = JsonConvert.DeserializeAnonymousType(await response.Content.ReadAsStringAsync(), atype);
if (!string.IsNullOrEmpty(operationStatus.errorCode))
{
throw new Exception($"{operationStatus.errorCode} - {operationStatus.message}");
}
response.EnsureSuccessStatusCode();
if (operationStatus.status == "assigning")
{
Task.Delay(TimeSpan.FromSeconds(pollingTimeInSeconds)).Wait();
address = $"https://global.azure-devices-provisioning.net/{iotcScopeId}/registrations/{deviceId}/operations/{operationStatus.operationId}?api-version=2019-03-31";
response = await client.GetAsync(address);
}
else if (operationStatus.status == "assigned")
{
string assignedHub = operationStatus.registrationState.assignedHub;
string cstr = $"HostName={assignedHub};DeviceId={deviceId};SharedAccessKey={deviceKey}";
string requestUri = $"https://{assignedHub}/devices/{deviceId}/messages/events?api-version=2020-03-01";
string deviceSasToken = SharedAccessSignatureBuilder.GetSASToken($"{assignedHub}/{deviceId}", deviceKey);
log.LogInformation($"IoTC DeviceConnectionString:\n\t{cstr}");
return new OkObjectResult(JObject.FromObject(new { iotHub = assignedHub, requestUri = requestUri, sasToken = deviceSasToken, deviceConnectionString = cstr }));
}
else
{
throw new Exception($"{operationStatus.registrationState.status}: {operationStatus.registrationState.errorCode} - {operationStatus.registrationState.errorMessage}");
}
} while (--retryCounter > 0);
throw new Exception("Registration device status retry timeout exprired, try again.");
}
}
public sealed class SharedAccessSignatureBuilder
{
public static string GetHostNameNamespaceFromConnectionString(string connectionString)
{
return GetPartsFromConnectionString(connectionString)["HostName"].Split('.').FirstOrDefault();
}
public static string GetSASTokenFromConnectionString(string connectionString, uint hours = 24)
{
var parts = GetPartsFromConnectionString(connectionString);
if (parts.ContainsKey("HostName") && parts.ContainsKey("SharedAccessKey"))
return GetSASToken(parts["HostName"], parts["SharedAccessKey"], parts.Keys.Contains("SharedAccessKeyName") ? parts["SharedAccessKeyName"] : null, hours);
else
return string.Empty;
}
public static string GetSASToken(string resourceUri, string key, string keyName = null, uint hours = 24)
{
try
{
var expiry = GetExpiry(hours);
string stringToSign = System.Web.HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
var signature = SharedAccessSignatureBuilder.ComputeSignature(key, stringToSign);
var sasToken = keyName == null ?
String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry) :
String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);
return sasToken;
}
catch
{
return string.Empty;
}
}
#region Helpers
public static string ComputeSignature(string key, string stringToSign)
{
using (HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(key)))
{
return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
}
}
public static Dictionary<string, string> GetPartsFromConnectionString(string connectionString)
{
return connectionString.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Split(new[] { '=' }, 2)).ToDictionary(x => x[0].Trim(), x => x[1].Trim(), StringComparer.OrdinalIgnoreCase);
}
// default expiring = 24 hours
private static string GetExpiry(uint hours = 24)
{
TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
return Convert.ToString((ulong)sinceEpoch.TotalSeconds + 3600 * hours);
}
public static DateTime GetDateTimeUtcFromExpiry(ulong expiry)
{
return (new DateTime(1970, 1, 1)).AddSeconds(expiry);
}
public static bool IsValidExpiry(ulong expiry, ulong toleranceInSeconds = 0)
{
return GetDateTimeUtcFromExpiry(expiry) - TimeSpan.FromSeconds(toleranceInSeconds) > DateTime.UtcNow;
}
public static string CreateSHA256Key(string secret)
{
using (var provider = new SHA256CryptoServiceProvider())
{
byte[] keyArray = provider.ComputeHash(UTF8Encoding.UTF8.GetBytes(secret));
provider.Clear();
return Convert.ToBase64String(keyArray);
}
}
public static string CreateRNGKey(int keySize = 32)
{
byte[] keyArray = new byte[keySize];
using (var provider = new RNGCryptoServiceProvider())
{
provider.GetNonZeroBytes(keyArray);
}
return Convert.ToBase64String(keyArray);
}
#endregion
}
配置mydeviceid并分配给urn:rk2019iotcpreview:Tester_653:1设备模板的函数的用法:
GET: https://yourFncApp.azurewebsites.net/api/HttpTriggerGetConnectionInfo?code=****&deviceid=mydeviceid&cmid=urn:rk2019iotcpreview:Tester_653:1
以下屏幕片段显示了我们可以从设备模板中获取CapabilityModelId (cmid) 的位置。请注意,错误值将返回:500 Internal Server Error响应代码。
移动应用程序中收到的响应将允许将遥测数据发送到 IoT Central 应用程序:
{
"iotHub": "iotc-xxxxx.azure-devices.net",
"requestUri": "https://iotc-xxxxx.azure-devices.net/devices/mydeviceid/messages/events?api-version=2020-03-01",
"sasToken": "SharedAccessSignature sr=iotc-xxxxx.azure-devices.net%2fmydeviceid&sig=xxxxxxxx&se=1592414760",
"deviceConnectionString": "HostName=iotc-xxxxx.azure-devices.net;DeviceId=mydeviceid;SharedAccessKey=xxxxxxx"
}
响应对象可以缓存在移动应用程序中,并在过期之前刷新。请注意,上述 sasToken 有效期为 24 小时(默认值)。
以下屏幕片段显示了将遥测数据从移动应用程序发送到 Azure IoT Central 应用程序的概念:
IoT Central 团队最近发布了适用于 iOS 和 Android 的移动设备即网关代码示例,可帮助您通过移动应用程序将 BLE 设备连接到 IoT Central。您可以修改示例以仅发送设备遥测而不是 BLE 数据。看看这里的示例代码:https ://github.com/iot-for-all/iotc-cpm-sample