来自云功能的Google Cloud IoT sendCommandToDevice显示服务不可用

时间:2018-11-05 10:01:27

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

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

Package.JSON “依赖关系”:{     “ firebase-admin”:“〜6.0.0”,     “ firebase-functions”:“ ^ 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);
          }
        });
    });
  });

日志: {错误:该服务当前不可用。在结算时(/user_code/node_modules/googleapis/node_modules/axios/lib/core/settle.js:18)在createError(/user_code/node_modules/googleapis/node_modules/axios/lib/core/createError.js:16:15)处: 12)位于Unzip.handleStreamEnd(/user_code/node_modules/googleapis/node_modules/axios/lib/adapters/http.js:201:11)位于Unzip.emit(events.js:91:20)(events.js: 185:7)位于process._tickDomainCallback(内部/process/next_tick.js:128:9)的_combinedTickCallback(内部/进程/next_tick.js:80:11)的endReadableNT(_stream_read.js:974:12)

2 个答案:

答案 0 :(得分:2)

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

就像我在Firebase函数中使用它一样,我只对所有没有特定子文件夹的命令使用firebase子文件夹

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

这里的功能取决于:

  "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的测试

似乎Google的“ IoT”还很年轻,需要大量工作

答案 1 :(得分:1)

我对Firebase云功能不太熟悉,但是使用Cloud Functions(https://console.cloud.google.com/functions)的内联编辑器却没有收到错误消息。您能告诉我何时开始出现此错误(如果仍然遇到此错误)吗?

作为参考,这是我使用的代码(基本上是您所使用的代码,但具有.join()的更明确的定义。

projectId, cloudRegion