|
|
@@ -0,0 +1,159 @@
|
|
|
+package com.xunmei.mediator.api.protection.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.xunmei.common.core.domain.iot.domain.IotAlarmSubsystem;
|
|
|
+import com.xunmei.common.core.domain.iot.domain.IotAlarmSubsystemLog;
|
|
|
+import com.xunmei.common.core.util.BeanHelper;
|
|
|
+import com.xunmei.mediator.api.protection.mapper.ProtectionLogMapper;
|
|
|
+import com.xunmei.mediator.api.protection.service.IIotAlarmHostService;
|
|
|
+import com.xunmei.mediator.api.protection.service.IotAlarmSubsystemService;
|
|
|
+import com.xunmei.mediator.iot.service.IIotDeviceInfoService;
|
|
|
+import com.xunmei.mediator.websocket.constant.WebSocketConstants;
|
|
|
+import com.xunmei.mediator.websocket.dto.WebsocketExecuteReq;
|
|
|
+import com.xunmei.mediator.websocket.enums.ProductEnums;
|
|
|
+import com.xunmei.mediator.websocket.service.RouterService;
|
|
|
+import com.xunmei.system.api.domain.iot.IotDeviceInfo;
|
|
|
+import com.xunmei.system.api.enums.ProtectionStatus;
|
|
|
+import com.xunmei.system.api.util.LogUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.StringJoiner;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class IotAlarmHostServiceImpl implements IIotAlarmHostService, RouterService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private IIotDeviceInfoService iIotDeviceInfoService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private IotAlarmSubsystemService subsystemService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ ProtectionLogMapper protectionLogMapper;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void changeAlarmHostStatus(WebsocketExecuteReq req){
|
|
|
+ try {
|
|
|
+ LogUtils.STATUS_INFO_DEFENCEAREA.info("【iot服务报警主机设备状态变更事件】【iotToken:{}】【msgId:{}】【接收参数:{}】", req.getToken(), req.getId(),JSON.toJSONString(req));
|
|
|
+ if (req.getData() != null){
|
|
|
+ JSONObject object = (JSONObject) req.getData();
|
|
|
+ String status = object.getString("status");
|
|
|
+ String updateTime = object.getString("time");
|
|
|
+
|
|
|
+ QueryWrapper<IotDeviceInfo> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.lambda()
|
|
|
+ .eq(IotDeviceInfo::getDeviceProduct,req.getProductName())
|
|
|
+ .eq(IotDeviceInfo::getDeviceCode,req.getDeviceName())
|
|
|
+ .eq(IotDeviceInfo::getIotToken,req.getToken())
|
|
|
+ .eq(IotDeviceInfo::getDeleted,0);
|
|
|
+ IotDeviceInfo deviceInfo = iIotDeviceInfoService.getOne(wrapper);
|
|
|
+ if (deviceInfo != null){
|
|
|
+ if ("Online".equals(status)){
|
|
|
+ deviceInfo.setNetStatus("1");
|
|
|
+ }else if ("Offline".equals(status)){
|
|
|
+ deviceInfo.setNetStatus("2");
|
|
|
+ }else {
|
|
|
+ deviceInfo.setNetStatus("0");
|
|
|
+ }
|
|
|
+ iIotDeviceInfoService.updateById(deviceInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ LogUtils.STATUS_INFO_DEFENCEAREA.error("处理报警主机设备状态改变事件出错", e);
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void changeSubSystemStatus(WebsocketExecuteReq req){
|
|
|
+ try {
|
|
|
+ LogUtils.STATUS_INFO_DEFENCEAREA.info("【iot服务报警主机布撤防状态变更事件】【iotToken:{}】【msgId:{}】【接收参数:{}】", req.getToken(), req.getId(),JSON.toJSONString(req));
|
|
|
+ if (req.getData() != null){
|
|
|
+ JSONObject object = (JSONObject) req.getData();
|
|
|
+ Integer subSystemId = object.getInteger("id");
|
|
|
+ String status = object.getString("status");
|
|
|
+
|
|
|
+ QueryWrapper<IotAlarmSubsystem> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.lambda()
|
|
|
+ .eq(IotAlarmSubsystem::getAlarmHostCode,req.getDeviceName())
|
|
|
+ .eq(IotAlarmSubsystem::getCode,String.valueOf(subSystemId))
|
|
|
+ .eq(IotAlarmSubsystem::getIotToken,req.getToken())
|
|
|
+ .eq(IotAlarmSubsystem::getDeleted,0);
|
|
|
+ IotAlarmSubsystem subsystem = subsystemService.getOne(wrapper);
|
|
|
+
|
|
|
+ if (subsystem != null){
|
|
|
+ Integer dbStatus = subsystem.getStatus();
|
|
|
+ //Arm:布防 DisArm:撤防 Unknown:未知
|
|
|
+ //0:撤防,1:布防,2:未知
|
|
|
+ if ("Arm".equals(status)){
|
|
|
+ subsystem.setStatus(ProtectionStatus.PROTECTION.ordinal());
|
|
|
+ }else if ("DisArm".equals(status)){
|
|
|
+ subsystem.setStatus(ProtectionStatus.REMOVAL.ordinal());
|
|
|
+ }else {
|
|
|
+ subsystem.setStatus(ProtectionStatus.UNKNOWN.ordinal());
|
|
|
+ }
|
|
|
+ subsystem.setStatusUpdateTime(LocalDateTime.now());
|
|
|
+ if (ObjectUtil.notEqual(subsystem.getStatus(), dbStatus)) {
|
|
|
+ subsystem.setStatusChangeTime(LocalDateTime.now());
|
|
|
+ }
|
|
|
+ subsystemService.updateById(subsystem);
|
|
|
+
|
|
|
+ this.saveProtectionLog(subsystem);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ LogUtils.STATUS_INFO_DEFENCEAREA.error("处理报警主机布撤防状态改变事件出错", e);
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveProtectionLog(IotAlarmSubsystem protection) {
|
|
|
+ IotAlarmSubsystemLog protectionLog = new IotAlarmSubsystemLog();
|
|
|
+ BeanHelper.copyProperties(protectionLog, protection);
|
|
|
+ protectionLog.setId(null);
|
|
|
+ protectionLog.setCreateTime(LocalDateTime.now());
|
|
|
+ protectionLog.setUpdateTime(protection.getUpdateTime());
|
|
|
+ protectionLog.setProtectionId(protection.getId());
|
|
|
+ protectionLogMapper.insert(protectionLog);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ProductEnums product() {
|
|
|
+ return ProductEnums.ALARM_HOST;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String routerKey() {
|
|
|
+ StringJoiner result = new StringJoiner(",");
|
|
|
+ result.add(WebSocketConstants.ON_LINE_STATUS);
|
|
|
+ result.add(WebSocketConstants.SUB_SYSTEM_STATUS);
|
|
|
+ //报警防区状态暂时不接
|
|
|
+ //result.add(WebSocketConstants.INPUT_STATUS);
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Object execute(WebsocketExecuteReq req) {
|
|
|
+ try {
|
|
|
+ if (WebSocketConstants.ON_LINE_STATUS.equals(req.getEvent())){
|
|
|
+ changeAlarmHostStatus(req);
|
|
|
+ }else if(WebSocketConstants.SUB_SYSTEM_STATUS.equals(req.getEvent())){
|
|
|
+ changeSubSystemStatus(req);
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|