订阅频道/实时通知

时间:2016-02-02 08:09:01

标签: cumulocity

关于实时通知的问题..

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}
 }
]

我的回复:

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

收到clientId后..我运行了以下命令:

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

响应:

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

问题出在哪里?我正试图订阅“过热警报”! 它可能不存在?我可以阅读现有信息吗? 谢谢, ALIM

2 个答案:

答案 0 :(得分:1)

是的,你的怀疑是正确的。基本上有两种选择:

  • 订阅特定设备的所有闹钟或闹钟:使用" / cep / realtime"和频道" / alarms / * resp。频道" /闹钟/ [设备ID]"。
  • 创建一个处理规则,过滤掉过热警报并订阅该规则:使用" / cep / notifications"和频道" / [模块名称] / [声明名称]"。

单击"新模块"时,模块名称是您输入的名称。语句名称是您添加到语句中的名称,例如

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

(如果你不在那里写一个名字,它们将是name statement_1,statement_2,....)

要从Java获取通知,请查看获取设备更改通知的example。在subscribe()方法中,您传递" *"或设备ID。要获取通知,请传递SubscriptionListener的实现,特别是onNotification方法。您可以修改" channelPrefix" to" / alarms /"或" /测量/"获得其他通知。

答案 1 :(得分:0)

谢谢,安德烈。 我已经测试了以下Code Snippet ..它可以工作,但它不是最好的解决方案: - )

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

            }
        }
    }