#init
This commit is contained in:
@@ -0,0 +1,386 @@
|
||||
package com.fastbee.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fastbee.common.core.domain.AjaxResult;
|
||||
import com.fastbee.common.core.domain.entity.UserClient;
|
||||
import com.fastbee.common.core.domain.model.LoginUser;
|
||||
import com.fastbee.common.exception.ServiceException;
|
||||
import com.fastbee.common.utils.MessageUtils;
|
||||
import com.fastbee.dto.*;
|
||||
import com.fastbee.enums.ConnectorStatus;
|
||||
import com.fastbee.iot.domain.Device;
|
||||
import com.fastbee.iot.mapper.DeviceMapper;
|
||||
import com.fastbee.iot.service.IDeviceService;
|
||||
import com.fastbee.manager.DeviceSessionManager;
|
||||
import com.fastbee.system.mapper.SysUserClientMapper;
|
||||
import com.fastbee.system.service.ISysUserClientService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class TransferDeviceService {
|
||||
|
||||
@Autowired
|
||||
private IDeviceService iDeviceService;
|
||||
|
||||
@Autowired
|
||||
private ISysUserClientService sysUserClientService;
|
||||
|
||||
@Autowired
|
||||
private DeviceSessionManager sessionManager;
|
||||
|
||||
@Autowired
|
||||
private SysUserClientMapper userClientMapper;
|
||||
|
||||
@Autowired
|
||||
private DeviceMapper deviceMapper;
|
||||
|
||||
|
||||
public AjaxResult bind(DeviceBindDTO bindDTO, LoginUser user) {
|
||||
try {
|
||||
String deviceId = bindDTO.getDeviceId();
|
||||
String deviceAlias = bindDTO.getDeviceAlias();
|
||||
//判断当前设备是否存
|
||||
Device device = iDeviceService.selectDeviceBySerialNumber(deviceId);
|
||||
if (device == null) {
|
||||
return AjaxResult.error("设备不存在");
|
||||
} else {
|
||||
Long tenantId = device.getTenantId();
|
||||
if (tenantId != -1) {
|
||||
if (!tenantId.equals(user.getUserId())) {
|
||||
return AjaxResult.error("设备有其他用户绑定");
|
||||
}
|
||||
}
|
||||
device.setTenantId(user.getUserId());
|
||||
device.setTenantName(user.getUsername());
|
||||
device.setUpdateTime(new Date());
|
||||
device.setDeviceAlias(deviceAlias);
|
||||
device.setActiveTime(new Date());
|
||||
iDeviceService.updateDeviceBySelf(device);
|
||||
// deviceMapper.updateById(device);
|
||||
return AjaxResult.success(true);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("设备绑定失败: {}", e.getMessage(), e);
|
||||
return AjaxResult.error("设备绑定失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public AjaxResult unbind(DeviceBindDTO bindDTO, LoginUser user) {
|
||||
try {
|
||||
String deviceId = bindDTO.getDeviceId();
|
||||
//判断当前设备是否存
|
||||
Device device = iDeviceService.selectDeviceBySerialNumber(deviceId);
|
||||
if (device == null) {
|
||||
return AjaxResult.error("设备不存在");
|
||||
} else {
|
||||
Long tenantId = device.getTenantId();
|
||||
if (tenantId == -1) {
|
||||
return AjaxResult.error("设备未绑定");
|
||||
}
|
||||
if (!tenantId.equals(user.getUserId())) {
|
||||
return AjaxResult.error("无解绑权限");
|
||||
}
|
||||
device.setTenantId(-1L);
|
||||
device.setTenantName("");
|
||||
device.setUpdateTime(new Date());
|
||||
device.setDeviceAlias("");
|
||||
// deviceMapper.updateById(device);
|
||||
iDeviceService.updateDeviceBySelf(device);
|
||||
//判断是否正在控制 如果正在控制,删除控制状态
|
||||
LambdaQueryWrapper<UserClient> lambdaWrapper = new LambdaQueryWrapper<>();
|
||||
lambdaWrapper.eq(UserClient::getDeviceName, deviceId);
|
||||
List<UserClient> users = userClientMapper.selectList(lambdaWrapper);
|
||||
log.info("users:{}", users);
|
||||
if (!CollectionUtils.isEmpty(users)) {
|
||||
users.forEach(userClient -> {
|
||||
log.info("userClient:{}", userClient);
|
||||
if (userClient != null) {
|
||||
String masterId = userClient.getClientName();
|
||||
NettyDevice deviceMaster = sessionManager.getDevice(masterId);
|
||||
if (deviceMaster != null) {
|
||||
String oldSlave = userClient.getDeviceName();
|
||||
//删除主的连接关系
|
||||
deviceMaster.getConnectedConnectors().removeIf(x -> x.getConnectorId().equals(oldSlave));
|
||||
//删除从的连接关系
|
||||
NettyDevice oldSlaveDevice = sessionManager.getDevice(oldSlave);
|
||||
if (oldSlaveDevice != null && !CollectionUtils.isEmpty(oldSlaveDevice.getConnectedConnectors())) {
|
||||
Set<Connector> connectedDevices = oldSlaveDevice.getConnectedConnectors();
|
||||
connectedDevices.removeIf(x -> x.getConnectorId().equals(masterId));
|
||||
}
|
||||
}
|
||||
sysUserClientService.updateAliveStatus(masterId, "", 0);
|
||||
log.info("updateById:{}", userClient.getClientName());
|
||||
}
|
||||
});
|
||||
}
|
||||
return AjaxResult.success(true);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("设备解绑失败: {}", e.getMessage(), e);
|
||||
return AjaxResult.error("设备解绑失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public AjaxResult updateDeviceAlias(DeviceBindDTO bindDTO, LoginUser user) {
|
||||
try {
|
||||
String deviceId = bindDTO.getDeviceId();
|
||||
//判断当前设备是否存
|
||||
Device device = iDeviceService.selectDeviceBySerialNumber(deviceId);
|
||||
if (device == null) {
|
||||
return AjaxResult.error("设备不存在");
|
||||
} else {
|
||||
Long tenantId = device.getTenantId();
|
||||
if (tenantId == -1) {
|
||||
return AjaxResult.error("设备未绑定");
|
||||
}
|
||||
if (!tenantId.equals(user.getUserId())) {
|
||||
return AjaxResult.error("无操作权限");
|
||||
}
|
||||
device.setDeviceAlias(bindDTO.getDeviceAlias());
|
||||
device.setUpdateTime(new Date());
|
||||
device.setCreateBy(user.getUsername());
|
||||
iDeviceService.updateDeviceBySelf(device);
|
||||
return AjaxResult.success(true);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("设备更新失败: {}", e.getMessage(), e);
|
||||
return AjaxResult.error("设备更新失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized boolean switchDevice(DeviceBindDTO bindDTO, LoginUser user) {
|
||||
try {
|
||||
String slaveId = bindDTO.getDeviceId();
|
||||
String platform = bindDTO.getPlatform();
|
||||
String userName = user.getUsername();
|
||||
String masterId = userName + ":" + platform;
|
||||
//todo 判断是否有权限 正在控制才能切
|
||||
//todo 离线没有控制权的怎么切
|
||||
log.info("switchDevice:{}", masterId);
|
||||
NettyDevice deviceMaster = sessionManager.getDevice(masterId);
|
||||
log.info("deviceMaster:{}", deviceMaster);
|
||||
if (deviceMaster != null) {
|
||||
log.info("ConnectorStatus:{}", deviceMaster.getConnectorStatus());
|
||||
if (ConnectorStatus.RECEIVER.equals(deviceMaster.getConnectorStatus())) {
|
||||
//当前账号没有控制则可以切换为控制?
|
||||
NettyDevice activeDevice = sessionManager.getUserControl(userName);
|
||||
deviceMaster.getConnectedConnectors().clear();
|
||||
if (activeDevice == null) {
|
||||
log.info("bindConnector ACTIVE :{}", masterId);
|
||||
sessionManager.bindConnector(masterId, slaveId);
|
||||
deviceMaster.setConnectorStatus(ConnectorStatus.ACTIVE);
|
||||
deviceMaster.setCurrentSlaveId(slaveId);
|
||||
sysUserClientService.updateAliveStatus(masterId, slaveId, 1);
|
||||
} else {
|
||||
log.info("bindConnector RECEIVER :{}", masterId);
|
||||
sessionManager.bindConnector(masterId, slaveId);
|
||||
deviceMaster.setConnectorStatus(ConnectorStatus.RECEIVER);
|
||||
deviceMaster.setCurrentSlaveId(slaveId);
|
||||
sysUserClientService.updateAliveStatus(masterId, slaveId, 2);
|
||||
}
|
||||
} else {
|
||||
//从关联其他设备切换到 当前禁言 假如有控制且不是当前更新为2
|
||||
List<NettyDevice> controls = sessionManager.getSlaveControl(slaveId);
|
||||
if (CollectionUtils.isEmpty(controls)) {
|
||||
deviceMaster.getConnectedConnectors().clear();
|
||||
log.info("bindConnector 1:{}", masterId);
|
||||
sessionManager.bindConnector(masterId, slaveId);
|
||||
deviceMaster.setConnectorStatus(ConnectorStatus.ACTIVE);
|
||||
deviceMaster.setCurrentSlaveId(slaveId);
|
||||
sysUserClientService.updateAliveStatus(masterId, slaveId, 1);
|
||||
} else {
|
||||
NettyDevice controlDevice = controls.get(0);
|
||||
if (!masterId.equals(controlDevice.getConnectorId())) {
|
||||
//当前的设备有控制 的主机
|
||||
deviceMaster.getConnectedConnectors().clear();
|
||||
log.info("bindConnector RECEIVER 2:{}", masterId);
|
||||
sessionManager.bindConnector(masterId, slaveId);
|
||||
deviceMaster.setConnectorStatus(ConnectorStatus.RECEIVER);
|
||||
deviceMaster.setCurrentSlaveId(slaveId);
|
||||
sysUserClientService.updateAliveStatus(masterId, slaveId, 2);
|
||||
}
|
||||
//当前 切当前不做操作
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("切换设备失败: {}", e.getMessage(), e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// public DeviceControlDTO remoteControl(DeviceBindDTO bindDTO, LoginUser user) {
|
||||
// try {
|
||||
// String slaveId = bindDTO.getDeviceId();
|
||||
// String platform = bindDTO.getPlatform();
|
||||
// String userName = user.getUsername();
|
||||
// String masterId = userName + ":" + platform;
|
||||
// DeviceControlDTO dto = new DeviceControlDTO();
|
||||
// NettyDevice deviceMaster = sessionManager.getDevice(masterId);
|
||||
// log.info("remoteControl deviceMaster:{}", masterId);
|
||||
// if (deviceMaster != null) {
|
||||
// if (ConnectorStatus.ACTIVE.equals(deviceMaster.getConnectorStatus())) {
|
||||
// if (!StringUtils.isEmpty(deviceMaster.getCurrentSlaveId())) {
|
||||
// if (deviceMaster.getCurrentSlaveId().equalsIgnoreCase(slaveId)) {
|
||||
// dto.setRemoteControl(true);
|
||||
// dto.setOwner(platform);
|
||||
// } else {
|
||||
// dto.setRemoteControl(false);
|
||||
// dto.setReason("occupied");
|
||||
// List<NettyDevice> list = sessionManager.getSlaveControl(slaveId);
|
||||
// if (!CollectionUtils.isEmpty(list)) {
|
||||
// dto.setOwner(list.get(0).getConnectorId());
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// dto.setRemoteControl(false);
|
||||
// dto.setReason("no control");
|
||||
// List<NettyDevice> list = sessionManager.getSlaveControl(slaveId);
|
||||
// if (!CollectionUtils.isEmpty(list)) {
|
||||
// dto.setOwner(list.get(0).getConnectorId());
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// dto.setRemoteControl(false);
|
||||
// dto.setReason("not active");
|
||||
// List<NettyDevice> list = sessionManager.getSlaveControl(slaveId);
|
||||
// if (!CollectionUtils.isEmpty(list)) {
|
||||
// dto.setOwner(list.get(0).getConnectorId());
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// dto.setRemoteControl(false);
|
||||
// dto.setReason("not exist");
|
||||
// List<NettyDevice> list = sessionManager.getSlaveControl(slaveId);
|
||||
// if (!CollectionUtils.isEmpty(list)) {
|
||||
// dto.setOwner(list.get(0).getConnectorId());
|
||||
// }
|
||||
// }
|
||||
// return dto;
|
||||
// } catch (Exception e) {
|
||||
// log.error(e.getMessage());
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
|
||||
public DeviceControlDTO remoteControl(DeviceBindDTO bindDTO, LoginUser user) {
|
||||
try {
|
||||
// String slaveId = bindDTO.getDeviceId();
|
||||
String platform = bindDTO.getPlatform();
|
||||
String userName = user.getUsername();
|
||||
String masterId = userName + ":" + platform;
|
||||
DeviceControlDTO dto = new DeviceControlDTO();
|
||||
NettyDevice deviceMaster = sessionManager.getDevice(masterId);
|
||||
log.info("remoteControl deviceMaster:{}", masterId);
|
||||
if (deviceMaster != null) {
|
||||
if (ConnectorStatus.ACTIVE.equals(deviceMaster.getConnectorStatus())) {
|
||||
dto.setRemoteControl(true);
|
||||
dto.setOwner(platform);
|
||||
} else {
|
||||
dto.setRemoteControl(false);
|
||||
dto.setReason("occupied");
|
||||
NettyDevice activeDevice = sessionManager.getUserControl(userName);
|
||||
if (activeDevice != null) {
|
||||
String[] l = activeDevice.getConnectorId().split(":");
|
||||
dto.setOwner(l[1]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
dto.setRemoteControl(false);
|
||||
dto.setReason("not exist");
|
||||
NettyDevice activeDevice = sessionManager.getUserControl(userName);
|
||||
if (activeDevice != null) {
|
||||
String[] l = activeDevice.getConnectorId().split(":");
|
||||
dto.setOwner(l[1]);
|
||||
}
|
||||
}
|
||||
return dto;
|
||||
} catch (Exception e) {
|
||||
log.error("获取设备权限失败", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public DeviceSwitchControlDTO switchControl(DeviceBindDTO bindDTO, LoginUser user) {
|
||||
try {
|
||||
String slaveId = bindDTO.getDeviceId();
|
||||
String platform = bindDTO.getPlatform();
|
||||
String userName = user.getUsername();
|
||||
String masterId = userName + ":" + platform;
|
||||
|
||||
DeviceSwitchControlDTO dto = new DeviceSwitchControlDTO();
|
||||
//收到明确的切换设备请求了 从哪边切换到另一边
|
||||
//判断当前是否有控制的设备
|
||||
NettyDevice masterDevice = sessionManager.getDevice(masterId);
|
||||
if (masterDevice == null || !masterDevice.getChannel().isActive()) {
|
||||
//主机已离线
|
||||
dto.setSwitchControl(false);
|
||||
dto.setReason("主机已离线");
|
||||
return dto;
|
||||
}
|
||||
|
||||
NettyDevice slaveDevice = sessionManager.getDevice(slaveId);
|
||||
if (slaveDevice == null || !masterDevice.getChannel().isActive()) {
|
||||
//从机已离线
|
||||
//主机已离线
|
||||
dto.setSwitchControl(false);
|
||||
dto.setReason("从机已离线");
|
||||
return dto;
|
||||
}
|
||||
|
||||
List<NettyDevice> controlList = sessionManager.getSlaveControl(slaveId);
|
||||
|
||||
if (!CollectionUtils.isEmpty(controlList)) {
|
||||
//如果当前设备有控制
|
||||
controlList.forEach(c -> {
|
||||
if (!c.getConnectorId().equals(masterId)) {
|
||||
c.setCurrentSlaveId(null);
|
||||
c.removeConnectedDevice(slaveDevice);
|
||||
c.setConnectorStatus(ConnectorStatus.RECEIVER);
|
||||
sysUserClientService.updateAliveStatus(c.getConnectorId(), slaveId, 2);
|
||||
}
|
||||
});
|
||||
}
|
||||
masterDevice.setCurrentSlaveId(slaveId);
|
||||
masterDevice.setConnectorStatus(ConnectorStatus.ACTIVE);
|
||||
sysUserClientService.updateAliveStatus(masterDevice.getConnectorId(), slaveId, 1);
|
||||
|
||||
dto.setSwitchControl(true);
|
||||
dto.setHolder(platform);
|
||||
return dto;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
return new DeviceSwitchControlDTO(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void releaseControl(DeviceBindDTO bindDTO, LoginUser user) {
|
||||
try {
|
||||
String platform = bindDTO.getPlatform();
|
||||
String userName = user.getUsername();
|
||||
String masterId = userName + ":" + platform;
|
||||
NettyDevice deviceMaster = sessionManager.getDevice(masterId);
|
||||
if (deviceMaster != null) {
|
||||
sysUserClientService.updateAliveStatus(masterId, "", 0);
|
||||
deviceMaster.setCurrentSlaveId(null);
|
||||
deviceMaster.getConnectedConnectors().clear();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user