我有两个控制灯光的传感器。当任何一个检测到有人时,它都打开灯,当两个传感器都检测不到任何人时,它不显示任何灯。有两种特殊情况。如果有人去了第一个传感器,然后又不去第二个传感器,就离开了,它会关灯。如果有人从第一个传感器转到第二个传感器。不离开
我尝试使用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;
答案 0 :(得分:0)
我建议采用以下解决方案,因为从一个传感器移动到另一个传感器是传感器区域不重叠,这有点棘手。在这里,我们使用定期触发来检查传感器状态并每5秒更新一次灯泡状态。唯一的妥协是打开/关闭灯的最大延迟时间为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;