需要互连传感器的工作示例,即当一个传感器触发另一个传感器的动作/工作时

时间:2018-05-23 09:19:26

标签: cumulocity

我需要一个工作互连传感器的例子。

例如:

  1. 设备XC8Y平台发送事件,平台请求数据或触发某些程序设备Y
  2. 设备X直接触发设备Y来收集数据或运行程序。

1 个答案:

答案 0 :(得分:0)

他汀。我不确定您的问题为什么会被否决。对我来说很有意义。

您将在http://cumulocity.com/guides/reference/real-time-notifications/上找到大部分所需的内容。您需要进行握手,订阅和连接,才能从另一台设备获取通知。就我而言,我还必须从设备的外部ID到其c8y ID进行查找,以便提供正确的订阅字符串。这是一些展示所有这些的Node.js代码:

const superagent = require('superagent')
const WebSocket = require('ws')
var ws = new WebSocket('ws://tenant.cumulocity.com/cep/realtime', {
    perMessageDeflate: false
})

// process websocket messages
ws.on('message', function incoming(data) {
    res = JSON.parse(data)
    if( res[0].channel === '/meta/handshake' ) {
        // process successful handshake
        wsClientId = res[0].clientId
        console.log('wsClientId = ' + wsClientId)
    } else if( res[0].channel === '/meta/subscribe' ) {
        // nothing to do here
    } else {
        // this is where you get the measurements from devices you are subscribed to
    }
}


function lookupDevice(serialNumber) {
  superagent.get(`https://tenant.cumulocity.com/identity/externalIds/c8y_Serial/${serialNumber}`)
    .set('Authorization', 'Basic ' + basicAuthBase64)
    .then(function (res) {
        success = res.statusCode
        if (success == 200) {
            devId = res.body.managedObject.id
            return devId
        } else {
            return null
        }
    })
}

async function wsHandshake() {
    let request = [{
        "channel": "/meta/handshake",
        "ext": {
            "com.cumulocity.authn": {
                "token": "bGVlLmdyZXl...6cXdlcjQzMjE="
            }
        },
        "version": "1.0",
        "mininumVersion": "1.0beta",
        "supportedConnectionTypes": ["websocket"],
        "advice": {"timeout": 120000, "interval": 30000}
    }]
    ws.send(JSON.stringify(request), function ack(err) {
        if( err )
            console.log('wsHandshake returned error '+err)
    })
}

async function wsSubscribe(subscriptionPath) {
    let request = [{
            "channel": "/meta/subscribe",
            "clientId": wsClientId,
            "subscription": subscriptionPath,
    }]
    ws.send(JSON.stringify(request), function ack(err) {
        if( err )
            console.log('wsSubscribe returned error '+err)
    })
}

async function wsConnect() {
    let request = [{
            "channel": "/meta/connect",
            "clientId": wsClientId,
            'connectionType': 'websocket',
            "advice": {"timeout":1200000,"interval":30000},
    }]
    ws.send(JSON.stringify(request), function ack(err) {
        if( err )
            console.log('wsConnect returned error '+err)
    })
}

// this is sort of pseudo-code that does not properly handle the asynchronous aspects of the flow
await wsHandshake()
let devId = lookupDevice('ABC123')
await wsSubscribe(`/measurements/${devId}`)
wsConnect()

请注意,我是从一个较大的实现中提取出来的,因此不保证您可以粘贴并按原样运行。但是它应该为您提供一个框架,帮助您实现所追求的目标。

请注意,在通知网页的顶部,它表明您可以订阅几种不同的通知类型,而不仅仅是此处所示的度量。