I am trying to execute the following example:
I have created two entities in orionCB.
subservice=/subtest
{
"id":"sensor1",
"type":"sensor",
"id_accumulator":"accumulator1",
"typeEvent": 1 //can be 1 or 0
}
{
"id":"accumulator1",
"type":"accumulator",
"used":132,
"free":83,
"total":215
}
The rules should be:
1.- if typeEvent is 1, the used attribute will be plus 1 and the free attribute will be less 1
2.- if typeEvent is 0, the used attribute will be less 1 and the free attribute will be plus 1
is it possible to do with perseo rule and subscription?
More information:
when the rules have been executing, the result will be:
-----> typeEvent:1
{
"id":"accumulator1",
"type":"accumulator",
"used":133,
"free":82,
"total":215,
}
---> typeEvent:0
{
"id":"accumulator1",
"type":"accumulator",
"used":131,
"free":84,
"total":215
}
Currently the context broker does not allow to increase an attribute directly.
I think you could use some win: time rule to try to manage this case, but I see that keeping the consistency in the entities “accumulator” in real time would probably be quite complex.
To solve this question using only Perseo, perhaps the key is to use a combination of rules and subscriptions that allow to increase and decrease the attributes of the accumulator entity.
1.First of all we need to subscribe Perseo to all typeEvent attribute:
POST to OrionCB_URL/v2/subscriptions:
{
“description”: “Notify Perseo when typeEvent changes”,
“subject”: {
“entities”: [
{
“idPattern”: “.*“,
“type”: “sensor”
}
],
“condition”: {
“attrs”: [
“typeEvent”
]
}
},
“notification”: {
“http”: {
“url”: “<perseoHost>/notices”
},
“attrs”: [
“typeEvent”,
“id”,
“id_accumulator”
]
},
“expires”: “2019-06-30T14:00:00.00Z”
}
POST to PERSEO_URL/rules:
{
“name”:“changeInAcumulator”,
“text”:“select \“changeInAcumulator\” as ruleName, ev.id_accumulator? as id_accumulator, ev.typeEvent? as typeEvent from pattern [every ev=iotEvent(type=\“sensor\“)]“,
“action”:{
“type”:“update”,
“parameters”:{
“id”:“${id_accumulator}“,
“type”:“accumulator”,
“attributes”: [
{
“name”:“action”,
“value”:“${typeEvent}”
}
]
}
}
}
POST to OrionCB_URL/v2/subscriptions:
{
“description”: “Notify Perseo when accumulator changes”,
“subject”: {
“entities”: [
{
“idPattern”: “.*“,
“type”: “accumulator”
}
],
“condition”: {
“attrs”: [
“action”
]
}
},
“notification”: {
“http”: {
“url”: “http://host.docker.internal:9090/notices”
},
“attrs”: [
“id”,
“free”,
“used”,
“action”
]
},
“expires”: “2019-06-30T14:00:00.00Z”
}
POST to PERSEO_URL/rules:
{
“name”:“updateAcumulator”,
“text”:“select \“updateAcumulator\” as ruleName, ev.id? as id, case cast(cast(ev.action?,String),float) when 1 then cast(cast(ev.free?,String),float)-1 else cast(cast(ev.free?,String),float)+1 end as free, case cast(cast(ev.action?,String),float) when 1 then cast(cast(ev.used?,String),float)+1 else cast(cast(ev.used?,String),float)-1 end as used from pattern [every ev=iotEvent(type=\“accumulator\“)]“,
“action”:{
“type”:“update”,
“parameters”:{
“id”:“${id}“,
“type”:“accumulator”,
“attributes”: [
{
“name”:“free”,
“value”:“${free}”
},
{
“name”:“used”,
“value”:“${used}”
} (editado)
]
}
}
}
I hope I have helped with this response.