-2

I need an example of working interconnected sensors.

For example:

  1. Device X sends event to C8Y platform, the platform requests data or triggers some procedure device Y,
  2. Device X triggers directly device Y to collect data or run procedure .
Catalin
  • 3
  • 1
  • Hey there Catalin. I think your question and explanation needs a little more context for others to understand the specifics. Flesh you question out a bit more. SO does tend to be hard on open questions that require involved open answers. – Cam_Aust May 23 '18 at 09:46
  • All I had as source of information is the Cumulocity customer documentation at https://www.cumulocity.com/dev-center/ ; so my ask if for a simple working example of interconnected sensors, e.g. when one device X sensor resource (e.g. temperature) data event (less then zero degrees Celsius) is received by Cumulocity , to trigger an action on another device Y sensor resource (e.g. start the heating etc. ); hope you got the idea ? – Catalin May 24 '18 at 10:25
  • Catalin, I am not about to offer you a solution to your particular problem. Others hopefully will. I suggest edit your question to include Cumulocity. Something like: Cumulocity, interconnected sensors, programming one sensor to trigger another. I assume there is code you have used to try and get this to work. Add a code block in the body of your question, of the key part of the current code that you have tried but does not work so far. People on this SO site are here to help you get code to work. In which case they need code you have tried, that is not working, to help you. – Cam_Aust May 25 '18 at 13:40

1 Answers1

1

Catalin. I'm not sure why your question got downvotes. It makes sense to me.

You will find most of what you are looking for at http://cumulocity.com/guides/reference/real-time-notifications/. You need to do the handshake, subscribe, and connect in order to get notifications from another device. In my case, I also had to do a lookup from the device's external ID to its c8y ID, so that I could provide the correct subscription string. Here is some Node.js code that shows all of that:

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()

Please be aware that I extracted this from a larger implementation, so I do not guarantee that you can paste this and run it as-is. But it should provide you with a framework for how to accomplish what you are after.

Note that at the top of the Notifications web page, it shows that you can subscribe to several different notification types, not just measurements as shown here.

greymatter
  • 840
  • 1
  • 10
  • 25