【问题标题】:Can I create an IoT Hub Device from browser?我可以从浏览器创建 IoT 中心设备吗?
【发布时间】:2017-04-18 00:03:50
【问题描述】:

目标:使用节点模块 azure-iot-hub 从浏览器 (angular2) 创建一个 azure iot hub 设备。

问题: azure-iot-common 依赖于一个包 crypto ,它在浏览器中不起作用。

重建步骤:

import { Component, OnInit } from '@angular/core';
let iothub = require(‘azure-iothub’);

const connectionString = ‘HostName=<my-host>.azure-devices.net;SharedAccessKeyName=<my-key-name>;SharedAccessKey=<my-key>=’;

@Component({
  selector: 'acn-shop',
  template: `
<div class="al-main">
  <div class="al-content container-fluid">
    <h1>Azure IoT Hub Devices</h1>
  </div>
</div>`
})
export class ShopComponent implements OnInit {

  constructor() {
  }

  public ngOnInit() {
    this.connect();
  }

  public connect() {
    console.log('Calling connect()');
    const registry = iothub.Registry.fromConnectionString(connectionString);
  }
}

从 Chrome 工具控制台

Error: Uncaught (in promise): TypeError: crypto.createHmac is not a function
TypeError: crypto.createHmac is not a function
    at Object.hmacHash (authorization.js:36)
    at Function.create (shared_access_signature.js:67)
    at Object.create (shared_access_signature.js:15)
    at Function.fromConnectionString (registry.js:65)
    at ShopComponent.Array.concat.ShopComponent.connect (shop.component.ts:32)
   … (goes on for a bit) ...

潜在解决方案:crypto 切换为 webcrypto - 需要重写 azure-iot-common/lib/authorization.js

问题:

  1. 是否有人使用节点模块 azure-iot-hub 从浏览器创建集线器设备?
  2. 是否有人使用其他方法从浏览器创建集线器设备?
  3. 如果对 Q1,2 否决 - 我的潜在解决方案是否可行?

【问题讨论】:

  • 您现在可以在 Azure 门户中为 IoT 中心创建设备。转到 Azure IoT Hub 仪表板,然后打开设备资源管理器菜单。

标签: azure-iot-hub azure-iot-sdk


【解决方案1】:

azure-iothub 节点模块是 service 客户端 SDK,用于创建后端应用程序,用于管理 IoT 中心实例,而不是用于设备。

在设备方面,您需要使用设备客户端 SDK 模块azure-iot-device。 也就是说,即使您解决了各种依赖问题,例如您发现的加密问题,这仍然不起作用,因为 IoT 中心服务不支持 CORS,这意味着它不会接受来自 Web 客户端的请求。对 IoT 中心的 CORS 支持在我们的待办事项中,但尚未优先考虑,因此我们没有 ETA。

您可以尝试解决此限制的方法是在网站后端运行设备客户端节点模块,当新的网络浏览器客户端连接到您的站点时创建设备客户端的新实例。

【讨论】:

  • 计划将逻辑放入 Azure 函数中
【解决方案2】:

https://github.com/PeculiarVentures/webcrypto-liner 库将为您提供一个加密对象,您可以在浏览器中使用(甚至是低级/IE)https://github.com/PeculiarVentures/node-webcrypto-ossl 将为您提供一个用于 Node 的对象。

切换到 webcrypto 应该没有问题,有关如何拨打电话的示例,请参阅 https://github.com/diafygi/webcrypto-examples#hmac

【讨论】:

  • 感谢您的回答。
猜你喜欢
  • 2023-03-17
  • 2014-06-02
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多