我在 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.
有没有办法直接过滤创建事件?为什么更新后我收到了两份打印报表?
谢谢马蒂亚斯