-->

Cumulocity Event Language - call external API

2019-06-26 05:43发布

问题:

Form what I can see on your documentation, it's possible to query the database for additional data when writing CEL, but is it possible to call an external API? Is it also possible to update a Measurement to populate a missing value.

For example, if I want to update a measurement by adding the "alt" value of the "c8y_Position" segment by calling a specific API: https://maps.googleapis.com/maps/api/elevation/json?locations=40.714728,-73.998672

Is it possible write this kind of statement:

expression string js:getElevation(lng, lat) [
    function request(lng, lat, callback) {
    var xobj = new XMLHttpRequest();
    // true parameter denotes asynchronous
    xobj.open('GET', 'https://maps.googleapis.com/maps/api/elevation/json?locations=' + lat + ', ' + lng + ', true);
    xobj.onreadystatechange = function () {
            if (xobj.readyState == 4 && xobj.status == "200") {
                callback(xobj.responseText);
            }
        };
        xobj.send(null);
    }
    request(lng, lat, function (data) {
        return data.results.elevation;
    });
]
insert into UpdateMeasurement
select
  e.id as id,
  getElevation(
    getNumber(e, "c8y_Position.lng.value"),
    getNumber(e, "c8y_Position.lat.value")
   ) as c8y_Position.alt
from MeasurementCreated e

Is it possible to make this kind of processing. Do you have more samples or documentation on CEL?

回答1:

Update:

It is since a while possible to call external APIs from CEL using the SendRequest stream (https://www.cumulocity.com/guides/event-language/data-model#sendrequest). Results can be received through the ResponseReceived (https://www.cumulocity.com/guides/event-language/data-model/#response-received) stream.

Previous response:

You can get realtime events into Zapier using the Cumulocity application (https://zapier.com/zapbook/cumulocity/) and from there into numerous other applications. Besides the 500+ applications in Zapier, there is also a generic Webhooks application (https://zapier.com/zapbook/webhook/) that allows to send any kind of REST request based on Cumulocity events.

It may not work for your immediate example, but it's still a very useful tool.

(You cannot update measurements in Cumulocity, you would have to create a new one; also you need to create events for location updates that can be shown in maps.)



回答2:

For the record, I also had this problem, and ended up using Amazon Web Services (AWS) Lambda and Cumulocity's REST API to solve it. You could potentially use scheduled Webtasks instead of AWS/Lambda for a simpler solution. The basic solution was:

  • On a schedule, run an AWS Lambda function which retrieves data from external sources, performs some processing, and creates events in Cumulocity using the REST API.

  • Write CEL in Cumulocity to handle the new events and update device registers.



回答3:

It is currently not possible to freely query external services outside of Cumulocity.

Currently you are limited to the built-in services that will connect to e.g. sms or phone calls



标签: cumulocity