|
|
@@ -0,0 +1,111 @@
|
|
|
+package com.xunmei.host.alarm.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.xunmei.common.core.domain.iot.domain.IotServerInfo;
|
|
|
+import com.xunmei.common.core.domain.mediator.domain.IotAlarmRule;
|
|
|
+import com.xunmei.common.core.domain.mediator.domain.IotAlarmRuleExpress;
|
|
|
+import com.xunmei.common.core.domain.mediator.domain.IotAlarmRuleSource;
|
|
|
+import com.xunmei.host.alarm.mapper.IotAlarmRuleMapper;
|
|
|
+import com.xunmei.host.alarm.service.IotAlarmRuleExpressService;
|
|
|
+import com.xunmei.host.alarm.service.IotAlarmRuleService;
|
|
|
+import com.xunmei.host.alarm.service.IotAlarmRuleSourceService;
|
|
|
+import com.xunmei.host.websocket.constant.WebSocketConstants;
|
|
|
+import com.xunmei.host.websocket.dto.WebsocketExecuteReq;
|
|
|
+import com.xunmei.host.websocket.dto.WebsocketResult;
|
|
|
+import com.xunmei.host.websocket.enums.ProductEnums;
|
|
|
+import com.xunmei.host.websocket.enums.TopicTypeEnums;
|
|
|
+import com.xunmei.host.websocket.service.RouterService;
|
|
|
+import com.xunmei.host.websocket.utils.IotServerUtils;
|
|
|
+import com.xunmei.host.websocket.utils.WebSocketUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.StringJoiner;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class IotAlarmRuleServiceImpl extends ServiceImpl<IotAlarmRuleMapper, IotAlarmRule> implements IotAlarmRuleService, RouterService {
|
|
|
+ @Resource
|
|
|
+ private IotAlarmRuleSourceService ruleSourceService;
|
|
|
+ @Resource
|
|
|
+ private IotAlarmRuleExpressService ruleExpressService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ProductEnums product() {
|
|
|
+ return ProductEnums.DETECTION_HOST;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String routerKey() {
|
|
|
+
|
|
|
+ StringJoiner result = new StringJoiner(",");
|
|
|
+ //设备基础数据
|
|
|
+ //设备状态数据
|
|
|
+ result.add(WebSocketConstants.ALARM_RULE);
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object execute(WebsocketExecuteReq req) {
|
|
|
+ receiveAndUpdateAlarmRule(req);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void syncAlarmRuleToHost(Long ruleId) {
|
|
|
+ final IotAlarmRule iotAlarmRule = baseMapper.selectById(ruleId);
|
|
|
+ if (Objects.isNull(iotAlarmRule)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ final JSONObject jsb = JSON.parseObject(JSON.toJSONString(iotAlarmRule));
|
|
|
+ final List<IotAlarmRuleSource> ruleSourceList = ruleSourceService.selectByRuleId(ruleId);
|
|
|
+ final List<IotAlarmRuleExpress> ruleExpresses = ruleExpressService.selectByRuleId(ruleId);
|
|
|
+ jsb.put("ruleSourceList", ruleSourceList);
|
|
|
+ jsb.put("ruleExpresses", ruleExpresses);
|
|
|
+
|
|
|
+ final String topic = TopicTypeEnums.formatUrl(TopicTypeEnums.PRODUCT_SERVICE_INVOKE.getUrl(), ProductEnums.DETECTION_HOST.getProductName()[0], ProductEnums.DETECTION_HOST.getProductName()[1]);
|
|
|
+ final WebsocketResult result = IotServerUtils.invokeHostServer(topic, new com.alibaba.fastjson.JSONObject(), WebSocketConstants.ALARM_RULE, jsb);
|
|
|
+
|
|
|
+ WebSocketUtils.sendMessage(iotAlarmRule.getIotCode(), JSON.toJSONString(result));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void receiveAndUpdateAlarmRule(WebsocketExecuteReq req) {
|
|
|
+ final Object data = req.getData();
|
|
|
+ final IotServerInfo serverInfo = req.getServerInfo();
|
|
|
+ final JSONObject jsb = JSON.parseObject(data.toString(), JSONObject.class);
|
|
|
+ final IotAlarmRule alarmRule = jsb.toJavaObject(IotAlarmRule.class);
|
|
|
+
|
|
|
+ final IotAlarmRule iotAlarmRule = JSON.parseObject(JSON.toJSONString(jsb), IotAlarmRule.class);
|
|
|
+ iotAlarmRule.setIotCode(serverInfo.getIotCode());
|
|
|
+ iotAlarmRule.setOrgId(serverInfo.getOrgId());
|
|
|
+ iotAlarmRule.setOrgName(serverInfo.getOrgName());
|
|
|
+ iotAlarmRule.setServerName(serverInfo.getIotName());
|
|
|
+ final Object deleted = jsb.get("deleted");
|
|
|
+ iotAlarmRule.setIsdeleted(deleted == null ? null : Integer.parseInt(deleted.toString()));
|
|
|
+ iotAlarmRule.setType("alarm");
|
|
|
+ saveOrUpdate(alarmRule);
|
|
|
+
|
|
|
+ final Object rs = jsb.get("ruleSourceList");
|
|
|
+ final Object re = jsb.get("ruleExpresses");
|
|
|
+ if (rs != null) {
|
|
|
+ final List<JSONObject> list = JSON.parseArray(rs.toString(), JSONObject.class);
|
|
|
+ for (JSONObject j : list) {
|
|
|
+ final IotAlarmRuleSource ruleSource = j.toJavaObject(IotAlarmRuleSource.class);
|
|
|
+ ruleSource.setOrgId(serverInfo.getOrgId());
|
|
|
+ ruleSource.setIotCode(serverInfo.getIotCode());
|
|
|
+ ruleSource.setValue(serverInfo.getIotCode() + "_" + ruleSource.getProductType() + "_" + ruleSource.getDeviceCode());
|
|
|
+ ruleSource.setValueType(j.get("sourceType") == null ? "" : j.get("sourceType").toString());
|
|
|
+ ruleSourceService.saveOrUpdate(ruleSource);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (re != null) {
|
|
|
+ final List<IotAlarmRuleExpress> list = JSON.parseArray(re.toString(), IotAlarmRuleExpress.class);
|
|
|
+ ruleExpressService.saveOrUpdateBatch(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|