1

我在 Cumulocity 使用 Apama。每当在 Cumulocity 中创建托管对象(设备)时,我想为其提供一些初始参数,这里是设备在被认为不可用之前需要向 Cumulocity 报告的所需时间间隔。

我的问题是,在 Apama 中,我似乎没有办法区分创建和更新事件。因此,如果我收到一个托管对象,向它添加一些参数并将其发送回托管对象通道,我最终会陷入循环。

我当然可以在收到事件后进行一些检查,但我更愿意只过滤托管对象的创建事件而不执行任何 IF 检查。

有什么方法可以只过滤创建事件吗?CHANNEL 和 UPDATE_CHANNEL 有什么区别?我使用哪一个似乎没有什么区别。

我当前的代码如下所示。我想要实现的是避免使用 IF 语句并直接在侦听器中的创建事件上进行过滤。

monitor InitializeDevice {
    action onload() {

        monitor.subscribe(ManagedObject.CHANNEL);
        on all ManagedObject(type = "c8y_MQTTDevice") as mo {
            log "###Received managed object. Content is: " + mo.toString() at INFO;
            if (mo.params.hasKey("c8y_RequiredAvailability")) {
                //Assuming an interval has already been set, do nothing.
                log "###Received managed object with required availability fragment. Doing nothing." at INFO;
            }
            else {
                //Set the response interval on the managed object
                dictionary<string,any> params := mo.params;
                dictionary<string,any> paramssub := new dictionary<string,any>;
                paramssub.add("responseInterval",3);
                params.add("c8y_RequiredAvailability",paramssub);
                mo.params := params;
                log "###Added required interval to managed object. Content is: " + mo.toString() at INFO;
                send mo to ManagedObject.UPDATE_CHANNEL;
            }
        }
    }
}

当我执行这个监视器并创建一个新的托管对象时,这就是打印到日志的内容:


2019-05-27 16:15:07.310 INFO  [12648] - InitializeDevice [6] ###Received managed object. Content is: com.apama.cumulocity.ManagedObject("5708279","c8y_MQTTDevice","some-device",[],[],[],[],[],[],{},{"c8y_IsDevice":any(dictionary<any,any>,{}),"owner":any(string,"some-owner")})
2019-05-27 16:15:07.310 INFO  [12648] - InitializeDevice [6] ###Added required interval to managed object. Content is: com.apama.cumulocity.ManagedObject("5708279","c8y_MQTTDevice","some-device",[],[],[],[],[],[],{},{"c8y_IsDevice":any(dictionary<any,any>,{}),"c8y_RequiredAvailability":any(dictionary<string,any>,{"responseInterval":any(integer,3)}),"owner":any(string,"some-owner")})
2019-05-27 16:15:07.310 INFO  [12648] - InitializeDevice [6] ###Received managed object. Content is: com.apama.cumulocity.ManagedObject("5708279","c8y_MQTTDevice","some-device",[],[],[],[],[],[],{},{"c8y_IsDevice":any(dictionary<any,any>,{}),"c8y_RequiredAvailability":any(dictionary<string,any>,{"responseInterval":any(integer,3)}),"owner":any(string,"some-owner")})
2019-05-27 16:15:07.310 INFO  [12648] - InitializeDevice [6] ###Received managed object with required availability fragment. Doing nothing.
2019-05-27 16:15:08.244 INFO  [7868] - InitializeDevice [6] ###Received managed object. Content is: com.apama.cumulocity.ManagedObject("5708279","c8y_MQTTDevice","some-device",[],[],[],[],[],[],{},{"c8y_Availability":any(dictionary<any,any>,{any(string,"lastMessage"):any(dictionary<any,any>,{any(string,"date"):any(integer,27),any(string,"day"):any(integer,1),any(string,"hours"):any(integer,16),any(string,"minutes"):any(integer,15),any(string,"month"):any(integer,4),any(string,"seconds"):any(integer,7),any(string,"time"):any(integer,1558966507220),any(string,"timezoneOffset"):any(integer,-120),any(string,"year"):any(integer,119)}),any(string,"status"):any(string,"AVAILABLE")}),"c8y_Connection":any(dictionary<any,any>,{any(string,"status"):any(string,"DISCONNECTED")}),"c8y_IsDevice":any(dictionary<any,any>,{}),"c8y_RequiredAvailability":any(dictionary<any,any>,{any(string,"responseInterval"):any(integer,3)}),"owner":any(string,"some-owner")})
2019-05-27 16:15:08.244 INFO  [7868] - InitializeDevice [6] ###Received managed object with required availability fragment. Doing nothing.

有没有办法直接过滤创建事件?为什么更新后我收到了两份打印报表?

谢谢马蒂亚斯

4

1 回答 1

0

经过一番调查,似乎没有任何方法可以区分创建和更新消息。因此,您目前使用的代码可能是执行此操作的唯一方法。

编辑:

但是问题的第二部分:

为什么更新后我收到了两份打印报表?

  1. c8y将托管对象发送到 MO.CHANNEL -> Apama 监视器监视器添加c8y_RequiredAvailability
  2. 监视器将更新的托管对象发送到 MO.UPDATE_CHANNEL -> c8y
  3. c8y发送更新的托管对象,包含c8y_RequiredAvailability-> Apama 监视器
  4. c8y发送托管对象 + c8y_Availability-> Apama 监视器

所以 3 是确认您的更新,而 4 是 c8y 异步发送在 MO.CHANNEL 上可用的最终更新

明确地说 - MO.CHANNEL 是创建和更新的对象到达 Apama 的地方。在该频道上发送不应该有效果。MO.UPDATE_CHANNEL 是您发送更新的请求通道,然后可能会在 MO.CHANNEL 上触发更多消息作为 c8y 进程。

于 2019-05-28T20:17:44.573 回答