Skip to content

Commit

Permalink
[feat]: scene add time interval
Browse files Browse the repository at this point in the history
  • Loading branch information
lgt1126 committed Nov 17, 2021
1 parent 7949088 commit 2db0e15
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.zmops.iot.domain.product;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
* @author yefei
* <p>
* 触发器 时间 表达式 函数
*/

@Data
@Entity
@Table(name = "product_event_time_interval")
public class ProductEventTimeInterval {

@Id
private Long eventTimeId;

private Long eventRuleId;

private Integer startTime;

private Integer endTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import com.zmops.iot.domain.BaseEntity;
import com.zmops.iot.domain.product.ProductEvent;
import com.zmops.iot.domain.product.ProductEventRelation;
import com.zmops.iot.domain.product.query.QProductEvent;
import com.zmops.iot.domain.product.query.QProductEventExpression;
import com.zmops.iot.domain.product.query.QProductEventRelation;
import com.zmops.iot.domain.product.query.QProductEventService;
import com.zmops.iot.domain.product.query.*;
import com.zmops.iot.domain.schedule.Task;
import com.zmops.iot.domain.schedule.query.QTask;
import com.zmops.iot.enums.CommonStatus;
Expand All @@ -31,11 +28,11 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static com.zmops.iot.web.device.service.MultipleDeviceEventRuleService.TRIGGER_TYPE_CONDITION;
import static com.zmops.iot.web.device.service.MultipleDeviceEventRuleService.TRIGGER_TYPE_SCHEDULE;
import static com.zmops.iot.web.device.service.MultipleDeviceEventRuleService.*;

/**
* @author yefei
Expand All @@ -52,8 +49,6 @@ public class MultipleDeviceEventTriggerController {
@Autowired
private ZbxTrigger zbxTrigger;

private static final String EXECUTE_TAG_NAME = "__scene__";

/**
* 场景 分页列表
*
Expand Down Expand Up @@ -114,7 +109,11 @@ public ResponseData createDeviceEventRule(@RequestBody @Validated(value = BaseEn
//step 1: 先创建 zbx 触发器
String expression = eventRule.getExpList()
.stream().map(Object::toString).collect(Collectors.joining(" " + eventRule.getExpLogic() + " "));

//时间区间表达式
if (ToolUtil.isNotEmpty(eventRule.getTimeIntervals())) {
String timeExpression = eventRule.getTimeIntervals().parallelStream().map(Objects::toString).collect(Collectors.joining(" or "));
expression = "(" + expression + ") and (" + timeExpression + ")";
}
//step 2: zbx 保存触发器
String[] triggerIds = multipleDeviceEventRuleService.createZbxTrigger(eventRuleId + "", expression, eventRule.getEventLevel());

Expand Down Expand Up @@ -202,7 +201,11 @@ public ResponseData updateDeviceEventRule(@RequestBody @Validated(value = BaseEn
//step 1: 先创建 zbx 触发器
String expression = eventRule.getExpList()
.stream().map(Object::toString).collect(Collectors.joining(" " + eventRule.getExpLogic() + " "));

//时间区间表达式
if (ToolUtil.isNotEmpty(eventRule.getTimeIntervals())) {
String timeExpression = eventRule.getTimeIntervals().parallelStream().map(Objects::toString).collect(Collectors.joining(" or "));
expression = "(" + expression + ") and (" + timeExpression + ")";
}
//step 2: zbx 保存触发器
String[] triggerIds = multipleDeviceEventRuleService.createZbxTrigger(eventRule.getEventRuleId() + "", expression, eventRule.getEventLevel());

Expand Down Expand Up @@ -262,6 +265,8 @@ public ResponseData deleteProductEventRule(@RequestBody @Validated(value = BaseE
//step 2:删除 关联的表达式
new QProductEventExpression().eventRuleId.eq(eventRule.getEventRuleId()).delete();

//step 3:删除 关联的时间表达式
new QProductEventTimeInterval().eventRuleId.eq(eventRule.getEventRuleId()).delete();
} else {
//step 1:删除 定时器
new QTask().id.eq(productEvent.getTaskId()).delete();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.zmops.iot.web.device.dto;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.zmops.iot.domain.product.ProductEventExpression;
import com.zmops.iot.domain.product.ProductEventService;
import com.zmops.iot.domain.product.ProductEventTimeInterval;
import com.zmops.iot.model.cache.filter.CachedValue;
import com.zmops.iot.model.cache.filter.CachedValueFilter;
import com.zmops.iot.model.cache.filter.DicType;
import com.zmops.iot.web.product.dto.ProductEventRuleDto;
import lombok.Data;

import java.util.List;

/**
* @author yefei
**/
Expand Down Expand Up @@ -45,4 +51,14 @@ public class MultipleDeviceEventDto {

@CachedValue(value = "triggerType")
private String triggerType;

private String scheduleConf;

private List<ProductEventExpression> expList;

private List<ProductEventService> deviceServices;

private List<MultipleDeviceEventRule.Tag> tags;

private List<ProductEventTimeInterval> timeExpList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class MultipleDeviceEventRule {

private Integer taskId;

private List<TimeInterval> timeIntervals;

@Data
public static class Tag {

Expand Down Expand Up @@ -136,6 +138,18 @@ public static class DeviceService {

}

@Getter
@Setter
public static class TimeInterval {
private Integer startTime;
private Integer endTime;

@Override
public String toString() {
return "(time()>= " + startTime + " and " + " time()< " + endTime + " )";
}
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import com.zmops.iot.async.executor.Async;
import com.zmops.iot.async.wrapper.WorkerWrapper;
import com.zmops.iot.domain.product.*;
import com.zmops.iot.domain.product.query.QProductEvent;
import com.zmops.iot.domain.product.query.QProductEventExpression;
import com.zmops.iot.domain.product.query.QProductEventRelation;
import com.zmops.iot.domain.product.query.QProductEventService;
import com.zmops.iot.domain.product.query.*;
import com.zmops.iot.domain.schedule.Task;
import com.zmops.iot.domain.schedule.query.QTask;
import com.zmops.iot.enums.CommonStatus;
Expand All @@ -28,7 +25,6 @@
import com.zmops.iot.web.device.service.work.DeviceServiceLogWorker;
import com.zmops.iot.web.device.service.work.ScenesLogWorker;
import com.zmops.iot.web.exception.enums.BizExceptionEnum;
import com.zmops.iot.web.product.dto.ProductEventRuleDto;
import com.zmops.iot.web.task.dto.TaskDto;
import com.zmops.iot.web.task.service.TaskService;
import com.zmops.zeus.driver.service.ZbxTrigger;
Expand Down Expand Up @@ -64,6 +60,8 @@ public class MultipleDeviceEventRuleService {

public static final int TRIGGER_TYPE_CONDITION = 0;

public static final String EXECUTE_TAG_NAME = "__scene__";

@Autowired
TaskService taskService;

Expand Down Expand Up @@ -241,9 +239,20 @@ public void createDeviceEventRule(MultipleDeviceEventRule eventRule) {
productEventRelationList.add(productEventRelation);
});
DB.saveAll(productEventRelationList);

//step 4: 保存时间区间
List<ProductEventTimeInterval> timeExpList = new ArrayList<>();
eventRule.getTimeIntervals().forEach(i -> {
ProductEventTimeInterval timeExp = new ProductEventTimeInterval();
timeExp.setEventRuleId(eventRule.getEventRuleId());
timeExp.setStartTime(i.getStartTime());
timeExp.setEndTime(i.getEndTime());
timeExpList.add(timeExp);
});
DB.insertAll(timeExpList);
}

//step 4: 保存触发器 调用 本产品方法
//step 5: 保存触发器 调用 本产品方法
if (null != eventRule.getDeviceServices() && !eventRule.getDeviceServices().isEmpty()) {
eventRule.getDeviceServices().forEach(i -> {
DB.sqlUpdate("insert into product_event_service(event_rule_id, execute_device_id, service_id) " +
Expand Down Expand Up @@ -290,6 +299,11 @@ public void updateDeviceEventRule(MultipleDeviceEventRule eventRule) {
.setParameter("eventRuleId", eventRule.getEventRuleId())
.execute();

//step : 删除时间区间函数表达式
DB.sqlUpdate("delete from product_event_time_interval where event_rule_id = :eventRuleId")
.setParameter("eventRuleId", eventRule.getEventRuleId())
.execute();

// 删除和所有设备的关联关系
new QProductEventRelation().eventRuleId.eq(eventRule.getEventRuleId()).delete();

Expand Down Expand Up @@ -326,9 +340,20 @@ public void updateDeviceEventRule(MultipleDeviceEventRule eventRule) {
productEventRelationList.add(productEventRelation);
});
DB.saveAll(productEventRelationList);

//step 7: 保存时间区间
List<ProductEventTimeInterval> timeExpList = new ArrayList<>();
eventRule.getTimeIntervals().forEach(i -> {
ProductEventTimeInterval timeExp = new ProductEventTimeInterval();
timeExp.setEventRuleId(eventRule.getEventRuleId());
timeExp.setStartTime(i.getStartTime());
timeExp.setEndTime(i.getEndTime());
timeExpList.add(timeExp);
});
DB.insertAll(timeExpList);
}

//step 7: 保存触发器 调用 本产品方法
//step 8: 保存触发器 调用 本产品方法
if (null != eventRule.getDeviceServices() && !eventRule.getDeviceServices().isEmpty()) {
eventRule.getDeviceServices().forEach(i -> {
DB.sqlUpdate("insert into product_event_service(event_rule_id,execute_device_id, service_id) values (:eventRuleId, :executeDeviceId, :serviceId)")
Expand Down Expand Up @@ -390,35 +415,40 @@ public void updateProductEventRuleZbxId(Long triggerId, String[] zbxId) {
* @param eventRuleId
* @return
*/
public ProductEventRuleDto detail(ProductEvent productEvent, long eventRuleId) {
ProductEventRuleDto productEventRuleDto = new ProductEventRuleDto();
ToolUtil.copyProperties(productEvent, productEventRuleDto);
public MultipleDeviceEventDto detail(ProductEvent productEvent, long eventRuleId) {
MultipleDeviceEventDto multipleDeviceEventDto = new MultipleDeviceEventDto();
ToolUtil.copyProperties(productEvent, multipleDeviceEventDto);

productEventRuleDto.setDeviceServices(new QProductEventService().eventRuleId.eq(eventRuleId).findList());
multipleDeviceEventDto.setDeviceServices(new QProductEventService().eventRuleId.eq(eventRuleId).findList());

if (TRIGGER_TYPE_CONDITION == productEvent.getTriggerType()) {
List<ProductEventExpression> expList = new QProductEventExpression().eventRuleId.eq(eventRuleId).findList();
productEventRuleDto.setExpList(expList);
multipleDeviceEventDto.setExpList(expList);

List<ProductEventTimeInterval> timeExpList = new QProductEventTimeInterval().eventRuleId.eq(eventRuleId).findList();
multipleDeviceEventDto.setTimeExpList(timeExpList);

ProductEventRelation productEventRelation = new QProductEventRelation().eventRuleId.eq(eventRuleId).setMaxRows(1).findOne();
productEventRuleDto.setStatus(productEventRelation.getStatus());
productEventRuleDto.setRemark(productEventRelation.getRemark());
productEventRuleDto.setInherit(productEventRelation.getInherit());
if (null == productEventRelation) {
return multipleDeviceEventDto;
}
multipleDeviceEventDto.setStatus(productEventRelation.getStatus());
multipleDeviceEventDto.setRemark(productEventRelation.getRemark());
multipleDeviceEventDto.setInherit(productEventRelation.getInherit());

JSONArray triggerInfo = JSONObject.parseArray(zbxTrigger.triggerAndTagsGet(productEventRelation.getZbxId()));
List<ProductEventRuleDto.Tag> tagList = JSONObject.parseArray(triggerInfo.getJSONObject(0).getString("tags"), ProductEventRuleDto.Tag.class);
List<MultipleDeviceEventRule.Tag> tagList = JSONObject.parseArray(triggerInfo.getJSONObject(0).getString("tags"), MultipleDeviceEventRule.Tag.class);

productEventRuleDto.setZbxId(productEventRelation.getZbxId());
productEventRuleDto.setTags(tagList.stream()
.filter(s -> !s.getTag().equals("__execute__") && !s.getTag().equals("__alarm__") && !s.getTag().equals("__event__"))
multipleDeviceEventDto.setTags(tagList.stream()
.filter(s -> !s.getTag().equals(EXECUTE_TAG_NAME))
.collect(Collectors.toList()));
} else {
Task task = new QTask().id.eq(productEvent.getTaskId()).findOne();
productEventRuleDto.setScheduleConf(Optional.ofNullable(task).map(Task::getScheduleConf).orElse(""));
productEventRuleDto.setExpList(Collections.emptyList());
multipleDeviceEventDto.setScheduleConf(Optional.ofNullable(task).map(Task::getScheduleConf).orElse(""));
multipleDeviceEventDto.setExpList(Collections.emptyList());
}

return productEventRuleDto;
return multipleDeviceEventDto;
}

/**
Expand Down Expand Up @@ -447,16 +477,23 @@ public String[] updateZbxTrigger(String triggerId, String expression, Byte level
}

public void execute(Long eventRuleId, String type, Long userId) {
Map<String, Object> serviceLogInfo = new ConcurrentHashMap<>(3);
serviceLogInfo.put("eventRuleId", eventRuleId);
serviceLogInfo.put("triggerType", "自动".equals(type) ? "场景联动" : type);
if (null != userId) {
serviceLogInfo.put("triggerUser", userId);
}

WorkerWrapper<Map<String, Object>, Boolean> deviceServiceLogWork = new WorkerWrapper.Builder<Map<String, Object>, Boolean>().id("deviceServiceLogWorker")
.worker(deviceServiceLogWorker).param(serviceLogInfo).callback(ICallback.PRINT_EXCEPTION_STACK_TRACE)
.build();

Map<String, Object> alarmInfo = new ConcurrentHashMap<>(3);
alarmInfo.put("eventRuleId", eventRuleId);
alarmInfo.put("triggerType", type);
if (null != userId) {
alarmInfo.put("triggerUser", userId);
}

WorkerWrapper<Map<String, Object>, Boolean> deviceServiceLogWork = new WorkerWrapper.Builder<Map<String, Object>, Boolean>().id("deviceServiceLogWorker")
.worker(deviceServiceLogWorker).param(alarmInfo).callback(ICallback.PRINT_EXCEPTION_STACK_TRACE)
.build();
WorkerWrapper<Map<String, Object>, Boolean> scenesLogWork = new WorkerWrapper.Builder<Map<String, Object>, Boolean>().id("scenesLogWorker")
.worker(scenesLogWorker).param(alarmInfo).callback(ICallback.PRINT_EXCEPTION_STACK_TRACE)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ public void subscribe(String event) {
Map<String,Object> eventMap = JSONObject.parseObject(event,Map.class);
Long eventRuleId = Long.parseLong(eventMap.get("eventRuleId").toString());

Map<String, Object> alarmInfo = new ConcurrentHashMap<>(3);
alarmInfo.put("eventRuleId", eventRuleId);
alarmInfo.put("triggerType", "定时");
Map<String, Object> serviceLogInfo = new ConcurrentHashMap<>(3);
serviceLogInfo.put("eventRuleId", eventRuleId);
serviceLogInfo.put("triggerType", "场景联动");

WorkerWrapper<Map<String, Object>, Boolean> deviceServiceLogWork = new WorkerWrapper.Builder<Map<String, Object>, Boolean>().id("deviceServiceLogWorker")
.worker(deviceServiceLogWorker).param(alarmInfo)
.worker(deviceServiceLogWorker).param(serviceLogInfo)
.build();

Map<String, Object> sceneLogInfo = new ConcurrentHashMap<>(3);
sceneLogInfo.put("eventRuleId", eventRuleId);
sceneLogInfo.put("triggerType", "自动");

WorkerWrapper<Map<String, Object>, Boolean> scenesLogWork = new WorkerWrapper.Builder<Map<String, Object>, Boolean>().id("scenesLogWorker")
.worker(scenesLogWorker).param(alarmInfo)
.worker(scenesLogWorker).param(sceneLogInfo)
.build();

try {
Expand Down

0 comments on commit 2db0e15

Please sign in to comment.