【问题标题】:Need example of working of interconnected sensors, i.e. when one sensor is triggering action/ work of the other需要互连传感器工作的示例,即当一个传感器触发另一个传感器的动作/工作时
【发布时间】:2018-06-21 05:17:57
【问题描述】:

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

例如:

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

【问题讨论】:

  • 你好,Catalin。我认为您的问题和解释需要更多的背景信息才能让其他人理解细节。肉你问题多一点。 SO 确实倾向于对需要公开答案的开放性问题提出质疑。
  • 我拥有的所有信息来源是 cumulocity.com/dev-center 的 Cumulocity 客户文档;所以我问是否有一个互连传感器的简单工作示例,例如当 Cumulocity 接收到一个设备 X 传感器资源(例如温度)数据事件(小于零摄氏度)时,触发对另一个设备 Y 传感器资源的操作(例如开始加热等);希望你明白了吗?
  • Catalin,我不打算为您提供特定问题的解决方案。其他人希望会。我建议编辑您的问题以包括 Cumulocity。比如:积聚,相互连接的传感器,对一个传感器进行编程以触发另一个。我假设有你用来尝试让它工作的代码。在您的问题正文中添加一个代码块,该代码块是您尝试过但目前还没有工作的当前代码的关键部分。这个 SO 网站上的人在这里帮助您让代码工作。在这种情况下,他们需要您尝试过但不起作用的代码来帮助您。

标签: cumulocity


【解决方案1】:

加泰罗尼亚语。我不确定为什么你的问题被否决了。这对我来说很有意义。

您可以在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()

请注意,我是从更大的实现中提取的,因此我不保证您可以粘贴并按原样运行它。但它应该为您提供一个框架,告诉您如何完成您所追求的目标。

请注意,在通知网页的顶部,它显示您可以订阅多种不同的通知类型,而不仅仅是此处显示的测量值。

【讨论】:

  • 是否会删除回复开头的“嗨”?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-21
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多