IBM Bluemix IotFoundation:Iotfclient处于脱机状态。重试连接

问题描述 投票:0回答:2

我正在尝试关注Coursera上的IBM Bluemix课程。

  1. 我的steup:作为设备(客户端)的覆盆子pi,它作为注册客户端连接到Watson IoT Platform。它每秒发出连续的随机数流。
  2. 我在IBM Bluemix上部署了我的自定义Nodejs应用程序(Coursera上提供的代码)。 var express = require('express'); var app = express(); var Client = require('ibmiotf'); var appConfig; var serverPort = process.env.VCAP_APP_PORT || 3000; var serverHost = process.env.VCAP_APP_HOST || 'localhost'; if (process.env.VCAP_SERVICES) { var env = JSON.parse(process.env.VCAP_SERVICES); appConfig = { 'org' : env["iotf-service"][0].credentials.org, 'id' : 'dna-nodeserver', 'auth-key' : env["iotf-service"][0].credentials.apiKey, 'auth-token' : env["iotf-service"][0].credentials.apiToken } } else { appConfig = require('./application.json'); } var responseString = 'Hello Coursera'; var appClient = new Client.IotfApplication(appConfig); app.get('/', function(req, res) { res.send(responseString); }); var server = app.listen(serverPort, serverHost, function() { var host = server.address().address; var port = server.address().port; console.log('Listening at http://%s:%s', host, port); appClient.connect(); appClient.on('connect', function() { appClient.subscribeToDeviceEvents('raspberrypi'); }); appClient.on("error", function (err) { console.log("Error : "+err); }); appClient.on('deviceEvent', function(deviceType, deviceId, eventType, format, payload) { responseString = "Device Event from :: "+deviceType+" : "+deviceId+" of event "+eventType+" with payload : "+payload; console.log("Device Event from :: "+deviceType+" : "+deviceId+" of event "+eventType+" with payload : "+payload); }); });

我所面临的问题是以下屏幕截图:

Error

此外,由于我从树莓派接收连续事件...网页(由res.send(responseString)提供)应自动显示更改...而无需我手动刷新页面。但这似乎并没有发生。

任何帮助,将不胜感激。谢谢!

node.js ibm-cloud iot
2个回答
0
投票

您可能有多个应用程序正在侦听事件。停止Bluemix上的上一个Node Red Application。而只是运行上面显示的Nodejs应用程序。


0
投票

老线程,但感谢阿列克谢指出我正确的方向。

在我的情况下,当生产版本仍在IBM Cloud中运行时,测试本地版本的事件代理的行为相同。

如果生成其他API密钥,保存两组凭据并检查合适的环境变量以查看要应用的集合,则可以通过这种方式拥有多个侦听器。

在我的应用程序中,我将它们包装在节点中的函数中:

function getEventBrokerCredentials(siteCode) {

    var codeToCredentials = {
        'REMOTE_BROKER': {
            'orgId': 'abcde',
            'id': 'ThisIsABroker',
            'apikey': '111111111111111',
            'authtoken': '2222222222222'
        },
        'LOCAL_BROKER': {
            'orgId': 'abcde',
            'id': 'ThisIsALocalBroker',
            'apikey': '3333333333333333',
            'authtoken': '4444444444444'
        }
    };

    return codeToCredentials[siteCode];
}


var brokerCredentials = getEventBrokerCredentials(process.env.BROKER_HOST || 'REMOTE_BROKER');


var appClient = new IOTClient.IotfApplication({
    'org': brokerCredentials.orgId,
    'id': brokerCredentials.id,
    'auth-key': brokerCredentials.apikey,
    'auth-token': brokerCredentials.authtoken
});

然后使用

BROKER_HOST=LOCAL_BROKER nodemon

用于本地测试和开发。

当然,这个主题有很多变化。

© www.soinside.com 2019 - 2024. All rights reserved.