首页 > 解决方案 > 谷歌云功能向物联网设备发送命令

问题描述

我正在尝试编写一个将命令发送到谷歌云物联网核心中定义的设备的函数。从控制台发送命令可以正常工作。

我正在使用向设备发送命令中的以下示例

// const cloudRegion = 'us-central1';
// const deviceId = 'my-device';
// const commandMessage = 'message for device';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
const iot = require('@google-cloud/iot');
const iotClient = new iot.v1.DeviceManagerClient({
  // optional auth parameters.
});

const formattedName = iotClient.devicePath(
  projectId,
  cloudRegion,
  registryId,
  deviceId
);
const binaryData = Buffer.from(commandMessage);
const request = {
  name: formattedName,
  binaryData: binaryData,
};

try {
  const responses = await iotClient.sendCommandToDevice(request);

  console.log('Sent command: ', responses[0]);
} catch (err) {
  console.error('Could not send command:', err);
}

但是,在我的函数日志中,我收到消息“错误:Node.js v10.0.0 是最低要求。”

我在我的项目中运行了 node -v,结果我得到了 v10.16.3,所以我很困惑。任何人都可以提供任何建议吗?

gcloud 函数描述结果:

availableMemoryMb: 256
entryPoint: updateDevice
eventTrigger:
  eventType: providers/cloud.firestore/eventTypes/document.create
  failurePolicy: {}
  resource: projects/project/databases/(default)/documents/pubsubMessages/{docID}
  service: firestore.googleapis.com
ingressSettings: ALLOW_ALL
labels:
  deployment-tool: cli-firebase
name: projects/project/locations/us-central1/functions/updateDevice
runtime: nodejs8
serviceAccountEmail: email@appspot.gserviceaccount.com
sourceUploadUrl: https://storage.googleapis.com/gcf-upload-us-central1-xxx
status: ACTIVE
timeout: 60s
updateTime: '2020-05-18T09:56:29.144Z'
versionId: '46'

包.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "dependencies": {
    "@google-cloud/iot": "^2.0.0",
    "@google-cloud/pubsub": "^1.7.3",
    "express": "^4.17.1",
    "firebase": "^7.14.4",
    "firebase-admin": "^8.12.1",
    "firebase-functions": "^3.6.1",
    "google-auth-library": "^6.0.0",
    "googleapis": "^51.0.0"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

标签: google-cloud-functionsmqtt

解决方案


推荐阅读