Subcribe a channel / real-time notification

▼魔方 西西 提交于 2019-12-11 11:57:39

问题


Question about real-time notification..

Post: https://xxxiot.cumulocity.com/cep/realtime
Body: 
[
 { 
  "channel": "/meta/handshake",
  "version": "1.0",
  "mininumVersion": "1.0beta",
  "supportedConnectionTypes": ["long-polling","callback-polling"],
  "advice":{"timeout":120000,"interval":30000}
 }
]

My Response:

[
 {
  "minimumVersion": "1.0",
  "supportedConnectionTypes": [
  "smartrest-long-polling",
  "long-polling"
],
  "successful": true,
  "channel": "/meta/handshake",
  "ext": {
  "ack": true
},
 "clientId": "5o0ghvle7yy4ix41on423v6k3j87",
 "version": "1.0"
}

]

After received the clientId.. I have run the following command:

Post: https://xxxiot.cumulocity.com/cep/realtime
Body:
[
 {
  "channel": "/meta/subscribe",
  "clientId": "5o0ghvle7yy4ix41on423v6k3j87",
  "subscription": "/alarms/overHeatAlarms"
 }
]

Response:

[
 {
 "error": "403:denied_by_security_policy:create_denied",
 "subscription": "/alarms/overHeatAlarms",
 "successful": false,
 "channel": "/meta/subscribe"
 }
]

Where is the problem? I'm trying to subcribing to "overheatAlarms"! It may be that it does not exist? Can I read the existing information? Thanks, Alim


回答1:


Yes, your suspicion is correct. There are basically two options for you:

  • Subscribe to all alarms or alarms from a particular device: Use "/cep/realtime" and channel "/alarms/* resp. channel "/alarms/[device ID]".
  • Create a processing rule that filters out overheat alarms and subscribe to that rule: Use "/cep/notifications" and channel "/[module name]/[statement name]".

The module name is what you enter as name when you click "New module". The statement name is what you add to the statement, e.g.

@Name('overHeatAlarms')
select * from AlarmsCreated where [your condition for overheat alarms]

(If you don't put a name there, they will be name statement_1, statement_2, ....)

To get notifications from Java, have a look at an example of getting notifications for changes in devices. In the subscribe() method, you pass "*" or the device ID. To get the notification, pass an implementation of SubscriptionListener, in particular the onNotification method. You can modify "channelPrefix" to "/alarms/" or "/measurements/" to get other notifications.




回答2:


Thanks, André. I've tested following Code Snippet.. it works, but it is not the best solution :-)

    MeasurementApi measurementApi = getMeasurementApi();
    MeasurementFilter measurementFilter = new MeasurementFilter();
    while (true) {
        Calendar cal = Calendar.getInstance();

        Date toDate = cal.getTime();
        cal.add(Calendar.SECOND, -25);
        Date fromDate = cal.getTime();
        measurementFilter.byDate(fromDate, toDate);
        measurementFilter.byFragmentType(TemperatureMeasurement.class);
        measurementFilter.bySource(new GId(DEVICE_SIMULATOR));
        MeasurementCollection mc = measurementApi
                .getMeasurementsByFilter(measurementFilter);

        MeasurementCollectionRepresentation measurements = mc.get();
        for (; measurements != null; measurements = mc
                .getNextPage(measurements)) {
            for (MeasurementRepresentation measurement : measurements
                    .getMeasurements()) {
                TemperatureMeasurement temperatureSensor = measurement
                        .get(TemperatureMeasurement.class);

                System.out.println(measurement.getSource().getId() + " "
                        + measurement.getTime()+ " " + temperatureSensor.getTemperature() );

            }
        }
    }


来源:https://stackoverflow.com/questions/35148052/subcribe-a-channel-real-time-notification

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!