传感器的逻辑模式
Logical Pattern With Sensors
我有两个控制灯的传感器。当其中一个检测到有人时,它会打开灯,当两个传感器都检测到没有人时,它不会显示任何灯。有两种特殊情况。如果有人去了第一个传感器然后没有去第二个传感器就离开了,它会关掉灯。如果有人从第一个传感器转到第二个传感器。不走
我尝试使用 Siddhi 的逻辑模式,但没有得到预期的结果。
@App:name("ControllerRightApp")
define stream inputStream(ternateId string,deviceId int,data string);
@sink(type='log',prefix='LOGGER')
define stream OutputStream(action string);
@info(name='CargoWeightQuery')
from inputStream[ternateId =='demo']
select ternateId,deviceId, data,json:getString(data,'$.eventName') as eventName
insert into tempStream;
from every ((e1=tempStream)->(e2=tempStream[deviceId==1 and eventName=='out'] and e3=tempStream[deviceId==2 and eventName=='out']))
select "{'deviceId':'3','action':'close'}" as action
insert into OutputStream;
我建议以下解决方案,因为从一个传感器转移到另一个传感器区域不重叠是有点棘手的。在这里,我们使用周期性触发器来检查传感器状态并每 5 秒更新一次灯泡状态。唯一的妥协是打开 on/off 灯的最大延迟为 n 秒。
@App:name("ControllerRightApp")
@App:description("Description of the plan")
define stream inputStream(ternateId string,deviceId int,data string);
@sink(type='log',prefix='LOGGER')
define stream OutputStream(action string);
@PrimaryKey('deviceId')
define table sensorStateTable(deviceId int, state string);
define trigger actionTrigger at every 5 sec;
define trigger populateTrigger at 'start';
from inputStream[ternateId =='demo']
select ternateId,deviceId, json:getString(data,'$.eventName') as eventName
insert into tempStream;
from tempStream[deviceId == 1 or deviceId == 2]
select deviceId, eventName as state
update or insert into sensorStateTable on deviceId == sensorStateTable.deviceId;
--If at least one sensor is in IN state open the switch
from actionTrigger left outer join sensorStateTable as t1 on (t1.state == 'in')
select ifThenElse(state is null, "{'deviceId':'3','action':'close'}","{'deviceId':'3','action':'open'}") as action
insert into OutputStream;
--Queries to add initial state
from populateTrigger
select 1 as deviceId, 'out' as state
insert into sensorStateTable;
from populateTrigger
select 2 as deviceId, 'out' as state
insert into sensorStateTable;
我有两个控制灯的传感器。当其中一个检测到有人时,它会打开灯,当两个传感器都检测到没有人时,它不会显示任何灯。有两种特殊情况。如果有人去了第一个传感器然后没有去第二个传感器就离开了,它会关掉灯。如果有人从第一个传感器转到第二个传感器。不走
我尝试使用 Siddhi 的逻辑模式,但没有得到预期的结果。
@App:name("ControllerRightApp")
define stream inputStream(ternateId string,deviceId int,data string);
@sink(type='log',prefix='LOGGER')
define stream OutputStream(action string);
@info(name='CargoWeightQuery')
from inputStream[ternateId =='demo']
select ternateId,deviceId, data,json:getString(data,'$.eventName') as eventName
insert into tempStream;
from every ((e1=tempStream)->(e2=tempStream[deviceId==1 and eventName=='out'] and e3=tempStream[deviceId==2 and eventName=='out']))
select "{'deviceId':'3','action':'close'}" as action
insert into OutputStream;
我建议以下解决方案,因为从一个传感器转移到另一个传感器区域不重叠是有点棘手的。在这里,我们使用周期性触发器来检查传感器状态并每 5 秒更新一次灯泡状态。唯一的妥协是打开 on/off 灯的最大延迟为 n 秒。
@App:name("ControllerRightApp")
@App:description("Description of the plan")
define stream inputStream(ternateId string,deviceId int,data string);
@sink(type='log',prefix='LOGGER')
define stream OutputStream(action string);
@PrimaryKey('deviceId')
define table sensorStateTable(deviceId int, state string);
define trigger actionTrigger at every 5 sec;
define trigger populateTrigger at 'start';
from inputStream[ternateId =='demo']
select ternateId,deviceId, json:getString(data,'$.eventName') as eventName
insert into tempStream;
from tempStream[deviceId == 1 or deviceId == 2]
select deviceId, eventName as state
update or insert into sensorStateTable on deviceId == sensorStateTable.deviceId;
--If at least one sensor is in IN state open the switch
from actionTrigger left outer join sensorStateTable as t1 on (t1.state == 'in')
select ifThenElse(state is null, "{'deviceId':'3','action':'close'}","{'deviceId':'3','action':'open'}") as action
insert into OutputStream;
--Queries to add initial state
from populateTrigger
select 1 as deviceId, 'out' as state
insert into sensorStateTable;
from populateTrigger
select 2 as deviceId, 'out' as state
insert into sensorStateTable;