Google Cloud IoT sendCommandToDevice node.js отправляет команду, но ловит ошибку - PullRequest
0 голосов
/ 15 апреля 2020

У меня есть это в Google App Engine (node.js). Мое устройство получает все команды, но я все еще получаю команду Не удалось отправить. Устройство подключено? ошибка.

Кстати, уже пробовал это: Ожидание функции до завершения () И тот же результат.

Попытка следовать этому примеру Кстати:

https://cloud.google.com/nodejs/docs/reference/iot/0.2.x/v1.DeviceManagerClient#sendCommandToDevice

const express = require('express');
var bodyParser = require('body-parser');
const app = express();

var urlencodedParser = bodyParser.urlencoded({
extended: false
})

const iot = require('@google-cloud/iot');

app.get('/', urlencodedParser, (req, res) => {
    res.setHeader('Content-Type', 'application/json');
    const projectId = req.query.proyecto;
    const cloudRegion = req.query.region;
    const registryId = req.query.registro;
    const numSerie = req.query.numSerie;
    const command = req.query.command;

const client = new iot.v1.DeviceManagerClient();
if (client === undefined) {
    console.log('Did not instantiate client.');
} else {
    console.log('Did instantiate client.');
    sendCom();
}

async function sendCom() {
    const formattedName = client.devicePath(projectId, cloudRegion, registryId, numSerie)
    const binaryData = Buffer.from(command);
    const request = {
        name: formattedName,
        binaryData: binaryData,
    };
    return client.sendCommandToDevice(request).then(responses => res.status(200).end(JSON.stringify({
        data: OK
    }))).catch(err => res.status(404).end('Could not send command. Is the device connected?'));
}
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});

module.exports = app;

С моей стороны я должен получить статус 200 и ОК, но этого не происходит.

...