【问题标题】:Google Cloud IoT sendCommandToDevice from cloud funcctions showing Service Unavailable来自云功能的 Google Cloud IoT sendCommandToDevice 显示服务不可用
【发布时间】:2019-04-08 16:13:00
【问题描述】:

我尝试从云功能发送命令,但出现错误:服务当前不可用。

包.JSON “依赖”:{ "firebase-admin": "~6.0.0", “firebase 功能”:“^2.0.3”, “googleapis”:“34.0.0” }

const parentName = `projects/${projectId}/locations/${cloudRegion}`;
const registryName = `${parentName}/registries/${reqData.registryId}`;
const binaryData = Buffer.from(JSON.stringify(reqData.message)).toString('base64');
const request = {
    name: `${registryName}/devices/${reqData.deviceId}`,
    binaryData: binaryData
};

google.auth.getClient().then((authClient) => {
    const discoveryUrl =
        `${DISCOVERY_API}?version=${API_VERSION}`;
    if (authClient.createScopedRequired && authClient.createScopedRequired()) {
      // Scopes can be specified either as an array or as a single,
      // space-delimited string.
      authClient = authClient.createScoped([
        'https://www.googleapis.com/auth/cloud-platform'
      ]);
    }

    google.options({
      auth: authClient
    });

    google.discoverAPI(discoveryUrl).then((client, err) => {
      if (err) {
        console.log('Error during API discovery', err);
        return undefined;
      }
      client.projects.locations.registries.devices.sendCommandToDevice(request,
        (err, data) => {
          if (err) {
            console.log('Could not send command:', request);
            console.log('Message: ', err);
          } else {
            console.log('Success :', data.statusText);
          }
        });
    });
  });

日志: { 错误:服务当前不可用。在 createError (/user_code/node_modules/googleapis/node_modules/axios/lib/core/createError.js:16:15) 在结算 (/user_code/node_modules/googleapis/node_modules/axios/lib/core/settle.js:18: 12) 在 Unzip.emit (events.js: 185:7) 在 endReadableNT (_stream_readable.js:974:12) 在 _combinedTickCallback (internal/process/next_tick.js:80:11) 在 process._tickDomainCallback (internal/process/next_tick.js:128:9)

【问题讨论】:

  • 我遇到了同样的错误,似乎没有人知道为什么...... UGH

标签: google-cloud-platform google-cloud-functions google-cloud-iot


【解决方案1】:

问题是subfolder必须被指定,并且必须不能是空字符串。

当我在 Firebase 函数中使用它时,我只使用 firebase 子文件夹来发送任何没有特定子文件夹的命令

const request = {
    name: `${registryName}/devices/${deviceId}`,
    binaryData: Buffer.from(JSON.stringify(commandMessage)).toString("base64"),
    subfolder: 'firebase'
}

这里是函数deps:

  "dependencies": {
    "firebase-admin": "^6.4.0",
    "firebase-functions": "^2.1.0",
    "fs-extra": "^7.0.0",
    "googleapis": "^36.0.0",
  },

这可能是由于

  1. 节点库中的错误
  2. Google 端点中的错误
  3. Google 方面缺乏测试

看来谷歌的“物联网”还很年轻,需要做很多工作

【讨论】:

  • 真正奇怪的是,我无法在使用 Node 本地运行的机器上重现这种行为,这让我相信这是 GCF 特有的。
【解决方案2】:

我对 Firebase 云函数不太熟悉,但使用 Cloud Functions 的内联编辑器 (https://console.cloud.google.com/functions) 没有收到错误。你能告诉我你什么时候开始收到这个错误(如果你仍然遇到它)?

作为参考,这是我使用的代码(基本上是你所拥有的,但对projectId, cloudRegion 有更明确的定义。

const {google} = require('googleapis');

const API_VERSION = 'v1';
const DISCOVERY_API = 'https://cloudiot.googleapis.com/$discovery/rest';

exports.sendCommand = (req, res) => {
  let reqData = req.body;

  const projectId = reqData.projectId || process.env.GCLOUD_PROJECT;
  const cloudRegion = reqData.cloudRegion || process.env.GCLOUD_REGION;

  const parentName = `projects/${projectId}/locations/${cloudRegion}`;
  const registryName = `${parentName}/registries/${reqData.registryId}`;
  const binaryData = Buffer.from(JSON.stringify(reqData.message)).toString('base64');
  const request = {
      name: `${registryName}/devices/${reqData.deviceId}`,
      binaryData: binaryData
  };

  google.auth.getClient().then((authClient) => {
      const discoveryUrl =
          `${DISCOVERY_API}?version=${API_VERSION}`;
      if (authClient.createScopedRequired && authClient.createScopedRequired()) {
        // Scopes can be specified either as an array or as a single,
        // space-delimited string.
        authClient = authClient.createScoped([
          'https://www.googleapis.com/auth/cloud-platform'
        ]);
      }

      google.options({
        auth: authClient
      });

      google.discoverAPI(discoveryUrl).then((client, err) => {
        if (err) {
          console.log('Error during API discovery', err);
          return undefined;
        }
        client.projects.locations.registries.devices.sendCommandToDevice(request,
          (err, data) => {
            if (err) {
              console.log('Could not send command:', request);
              console.log('Message: ', err);
            } else {
              console.log('Success :', data.statusText);
            }
          });
      });
    });
  res.status(200).send(reqData.message);
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-30
    • 2019-06-14
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多