简体   繁体   中英

How could I do the following example with Fiware-Perseo-fe

I am trying to execute the following example:

I have created two entities in orionCB.

  • service= test
  • 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”
   }
  1. Then, we now create a rule that add an attribute in the accumulator to indicate the need to update it every time a sensor changes the value of its typeEvent attribute:

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}”
                   }
             ]
         }
      }
   }
  1. We subscribe Perseo to the changes that occur in this new attribute 'action' for all accumulator type entities.

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”
   }
  1. We create a new rule in Perseo, that manage the new accumulator notifications, and modify the accumulator entity based of the value of 'action' attribute, that contains the last 'typeEvent' value changed by the first rule.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM