Lambda上的Google Actions智能家居无法正常工作

时间:2019-07-15 06:22:31

标签: actions-on-google

我一直在尝试让我的Google Actions智能家居(nodejs)在AWS lambda中运行。但是,它不起作用。每当我在Google Home应用程序上连接它时,我只会收到一条消息“无法更新设置...”。我已经正确配置了API网关,并将处理程序设置为“ index.smarthome”,如下面的图片链接所示。 为什么不起作用,如何使我的lambda google action智能家居正常工作?

Image Link

虽然我的Firebase版本正在运行(从https://codelabs.developers.google.com/codelabs/smarthome-washer/#2的洗衣机示例中修改)。

const functions = require('firebase-functions');
const {smarthome} = require('actions-on-google');

const app = smarthome();

app.onSync(body => {
  return {
    requestId: 'ff36a3cc-ec34-11e6-b1a0-64510650abcf',
    payload: {
      agentUserId: '123',
      devices: [{
        id: 'washer',
        type: 'action.devices.types.WASHER',
        traits: [
          'action.devices.traits.OnOff',
          'action.devices.traits.StartStop',
          'action.devices.traits.RunCycle',
          'action.devices.traits.Modes',
          'action.devices.traits.Toggles',
        ],
        name: {
          defaultNames: ['My Washer'],
          name: 'Washer',
          nicknames: ['Washer']
        },
        deviceInfo: {
          manufacturer: 'Acme Co',
          model: 'acme-washer',
          hwVersion: '1.0',
          swVersion: '1.0.1'
        },
        attributes: {
          pausable: true,
          availableModes: [{
              name: 'load',
              name_values: [{
                  name_synonym: ['load'],
                  lang: 'en'
                }],
              settings: [{
                  setting_name: 'small',
                  setting_values: [{
                      setting_synonym: ['small'],
                      lang: 'en'
                    }]
                  }, {
                  setting_name: 'large',
                  setting_values: [{
                      setting_synonym: ['large'],
                      lang: 'en'
                    }]
                }],
              ordered: true
            }],
          availableToggles: [{
              name: 'Turbo',
              name_values: [{
                  name_synonym: ['turbo'],
                  lang: 'en'
              }]
          }]
        }
    }]
    }
  };
});

app.onExecute((body) => {
  const {requestId} = body;
  const payload = {
    commands: [{
      ids: [],
      status: 'SUCCESS',
      states: {
        online: true,
      },
    }],
  };
  for (const input of body.inputs) {
    for (const command of input.payload.commands) {
      for (const device of command.devices) {
        const deviceId = device.id;
        payload.commands[0].ids.push(deviceId);
        for (const execution of command.execution) {
          const execCommand = execution.command;
          const {params} = execution;
          switch (execCommand) {
            case 'action.devices.commands.OnOff':
              payload.commands[0].states.on = params.on;
              break;
            case 'action.devices.commands.StartStop':
              payload.commands[0].states.isRunning = params.start;
              break;
            case 'action.devices.commands.PauseUnpause':
              payload.commands[0].states.isPaused = params.pause;
              break;
            case 'action.devices.commands.SetModes':
              break;
            case 'action.devices.commands.SetToggles':
              break;
          }
        }
      }
    }
  }
  return {
    requestId: requestId,
    payload: payload,
  };
});

exports.smarthome = functions.https.onRequest(app);

这是我在AWS lambda函数中使用的代码。我引用了https://github.com/actions-on-google/actions-on-google-nodejscreating dialogflow v2 project with serverless以使其与lambda兼容。 lambda版本和firebase版本之间的主要区别是“ exports.smarthome”代码。


const {smarthome} = require('actions-on-google');

const app = smarthome();

app.onSync(body => {
  return {
    requestId: 'ff36a3cc-ec34-11e6-b1a0-64510650abcf',
    payload: {
      agentUserId: '123',
      devices: [{
        id: 'washer',
        type: 'action.devices.types.WASHER',
        traits: [
          'action.devices.traits.OnOff',
          'action.devices.traits.StartStop',
          'action.devices.traits.RunCycle',
          'action.devices.traits.Modes',
          'action.devices.traits.Toggles',
        ],
        name: {
          defaultNames: ['My Washer'],
          name: 'Washer',
          nicknames: ['Washer']
        },
        deviceInfo: {
          manufacturer: 'Acme Co',
          model: 'acme-washer',
          hwVersion: '1.0',
          swVersion: '1.0.1'
        },
        attributes: {
          pausable: true,
          availableModes: [{
              name: 'load',
              name_values: [{
                  name_synonym: ['load'],
                  lang: 'en'
                }],
              settings: [{
                  setting_name: 'small',
                  setting_values: [{
                      setting_synonym: ['small'],
                      lang: 'en'
                    }]
                  }, {
                  setting_name: 'large',
                  setting_values: [{
                      setting_synonym: ['large'],
                      lang: 'en'
                    }]
                }],
              ordered: true
            }],
          availableToggles: [{
              name: 'Turbo',
              name_values: [{
                  name_synonym: ['turbo'],
                  lang: 'en'
              }]
          }]
        }
    }]
    }
  };
});

app.onExecute((body) => {
  const {requestId} = body;
  const payload = {
    commands: [{
      ids: [],
      status: 'SUCCESS',
      states: {
        online: true,
      },
    }],
  };
  for (const input of body.inputs) {
    for (const command of input.payload.commands) {
      for (const device of command.devices) {
        const deviceId = device.id;
        payload.commands[0].ids.push(deviceId);
        for (const execution of command.execution) {
          const execCommand = execution.command;
          const {params} = execution;
          switch (execCommand) {
            case 'action.devices.commands.OnOff':
              payload.commands[0].states.on = params.on;
              break;
            case 'action.devices.commands.StartStop':
              payload.commands[0].states.isRunning = params.start;
              break;
            case 'action.devices.commands.PauseUnpause':
              payload.commands[0].states.isPaused = params.pause;
              break;
            case 'action.devices.commands.SetModes':
              break;
            case 'action.devices.commands.SetToggles':
              break;
          }
        }
      }
    }
  }
  return {
    requestId: requestId,
    payload: payload,
  };
});

exports.smarthome = function(event, context, callback) {
  app.handler(event, {})
    .then((res) => {
      if (res.status != 200) {
        callback(null, {
          "fulfillmentText": `I got status code: ${res.status}`
        });
      } else {
        callback(null, res.body);
      }
    }).catch((e) => {
      callback(null, {
        "fulfillmentText": `There was an error\n${e}`
      });
    });
};

2 个答案:

答案 0 :(得分:0)

检查您的AWS CloudWatch日志,看看调用lambda时会发生什么。您可以在lambda中打印到stdout并将其显示在这些日志中。

答案 1 :(得分:0)

除了Cloudwatch日志外,您还可以查看Stackdriver日志。