Hello,
I have a problem that I can’t solve.
I created an event to calculate how many devices have in each place and how many are in transit, it works correctly until a certain moment, when two or more devices trigger the event at the same time or in a very short period the calculation goes wrong.
Follow the code below:
/***********************************************************************************************************************************/
/* */
/* Every time the update is made to the Manageable Object of a property, if it is an object, it is necessary to include all the */
/* fields of the same, otherwise they are excluded */
/* */
/***********************************************************************************************************************************/
create constant variable string id_folder = "11246847";
/*get the manageable object*/
create schema MyObject(
object ManagedObject,
alarmType string
);
@Name("GetObject")
@Resilient
insert into MyObject
select
findManagedObjectById(id_folder) as object,
getString(a, "type") as alarmType
from AlarmCreated a
where (getString(a, "type") = "c8y_Geo-VW-SBC-Entering"
or getString(a, "type") = "c8y_Geo-VW-SBC-Leaving"
or getString(a, "type") = "c8y_Geo-VW-SC-Entering"
or getString(a, "type") = "c8y_Geo-VW-SC-Leaving")
and (getString(a, "status") = "ACTIVE");
/*********************************************************************************************************************************/
/******************************************** TEST *********************************************************
insert into UpdateManagedObject
select
id_folder as id,
{
"VW-São_Bernardo_do_Campo", getNumber(mo, "object.VW-São_Bernardo_do_Campo")+1,
"VW-São_Carlos", getNumber(mo, "object.VW-São_Carlos"),
"OnCarriage", getNumber(mo, "object.OnCarriage")-1
} as fragments
from MyObject mo
where getString(mo, "alarmType") = "c8y_Geo-VW-SBC-Entering";
******************************************** TEST *********************************************************/
/*********************************************************************************************************************************/
@Name("SBC-Entering")
@Resilient
insert into UpdateManagedObject
select
id_folder as id,
{
"VW-São_Bernardo_do_Campo", getNumber(mo, "object.VW-São_Bernardo_do_Campo")+1,
"OnCarriage", getNumber(mo, "object.OnCarriage")-1
} as fragments
from MyObject mo
where getString(mo, "alarmType") = "c8y_Geo-VW-SBC-Entering";
/*********************************************************************************************************************************/
@Name("SC-Entering")
@Resilient
insert into UpdateManagedObject
select
id_folder as id,
{
"VW-São_Carlos", getNumber(mo, "object.VW-São_Carlos")+1,
"OnCarriage", getNumber(mo, "object.OnCarriage")-1
} as fragments
from MyObject mo
where getString(mo, "alarmType") = "c8y_Geo-VW-SC-Entering";
/*********************************************************************************************************************************/
@Name("SBC-Leaving")
@Resilient
insert into UpdateManagedObject
select
id_folder as id,
{
"VW-São_Bernardo_do_Campo", getNumber(mo, "object.VW-São_Bernardo_do_Campo")-1,
"OnCarriage", getNumber(mo, "object.OnCarriage")+1
} as fragments
from MyObject mo
where getString(mo, "alarmType") = "c8y_Geo-VW-SBC-Leaving";
/*********************************************************************************************************************************/
@Name("SC-Leaving")
@Resilient
insert into UpdateManagedObject
select
id_folder as id,
{
"VW-São_Carlos", getNumber(mo, "object.VW-São_Carlos")-1,
"OnCarriage", getNumber(mo, "object.OnCarriage")+1
} as fragments
from MyObject mo
where getString(mo, "alarmType") = "c8y_Geo-VW-SC-Leaving";