mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
解绑先验归属,通讯录别名不再落空
解绑接口原来不核对设备归属,谁知道对方的设备 id 就能把那台设备的通讯录清空,设备本身倒是因为带了用户条件没删掉。现在先看归属,不是自己的直接不动。 设备登记了但用户没起名时,别名会落成空,缓存刷新把整条跳过,对方再也呼不过来。改成和呼叫那处一样,两级判空都取不到才退回尾号称呼。顺带加了一条回填,把写进去的空值补上。 呼叫那条测试原来会真发请求也没验到东西,改成桩掉网关直接看发出去的称呼。
This commit is contained in:
parent
5f2e331648
commit
a8b2e5f9e9
6 changed files with 139 additions and 6 deletions
|
|
@ -165,9 +165,15 @@ public class DeviceAddressBookServiceImpl implements DeviceAddressBookService {
|
||||||
// 如果 alias 为空,就默认使用设备名称;对方设备尚未登记时退回尾号称呼
|
// 如果 alias 为空,就默认使用设备名称;对方设备尚未登记时退回尾号称呼
|
||||||
if (StringUtils.isBlank(alias)) {
|
if (StringUtils.isBlank(alias)) {
|
||||||
DeviceEntity targetDevice = deviceService.getDeviceByMacAddress(targetMac);
|
DeviceEntity targetDevice = deviceService.getDeviceByMacAddress(targetMac);
|
||||||
alias = targetDevice == null
|
if (targetDevice != null) {
|
||||||
? formatMacAsDeviceName(targetMac)
|
alias = targetDevice.getAlias();
|
||||||
: targetDevice.getAlias();
|
}
|
||||||
|
// 设备登记了但用户没起过名,alias 建表就是 null,这是默认状态
|
||||||
|
// 不是异常。这时候落个 null 进去,缓存刷新会把整条跳过,
|
||||||
|
// 接口照样返回成功,对方却永远呼不过来
|
||||||
|
if (StringUtils.isBlank(alias)) {
|
||||||
|
alias = formatMacAsDeviceName(targetMac);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// 检查重名
|
// 检查重名
|
||||||
alias = generateUniqueAlias(macAddress, targetMac, alias);
|
alias = generateUniqueAlias(macAddress, targetMac, alias);
|
||||||
|
|
|
||||||
|
|
@ -316,6 +316,12 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
|
||||||
if (device == null) {
|
if (device == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// 后面删通讯录那步是按 mac_address 删的,不看 user_id。
|
||||||
|
// 设备本身因为带了 user_id 条件删不掉,通讯录却会照删不误,
|
||||||
|
// 任意登录用户知道对方的设备 id 就能把对方整本通讯录清空
|
||||||
|
if (userId == null || device.getUserId() == null || !device.getUserId().equals(userId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
String macAddress = device.getMacAddress();
|
String macAddress = device.getMacAddress();
|
||||||
if (StringUtils.isNotBlank(device.getAgentId())) {
|
if (StringUtils.isNotBlank(device.getAgentId())) {
|
||||||
// 清除智能体设备数量缓存
|
// 清除智能体设备数量缓存
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
-- 通讯录别名回填:设备登记了但用户没起名时,alias 落的是 NULL,
|
||||||
|
-- 缓存刷新会整条跳过,对方永远呼不过来,接口却返回成功
|
||||||
|
--
|
||||||
|
-- 名字取法和 DeviceAddressBookServiceImpl.formatMacAsDeviceName 一致:
|
||||||
|
-- 设备在智控台起过名就用那个名,没起过才退回「尾号为xx的设备」。
|
||||||
|
-- target_mac 为空或短于两位的行维持原样,Java 那边也是原样返回,算不出名字就不硬造
|
||||||
|
|
||||||
|
UPDATE `ai_device_address_book` b
|
||||||
|
LEFT JOIN `ai_device` d
|
||||||
|
ON d.`mac_address` = b.`target_mac`
|
||||||
|
SET b.`alias` = COALESCE(
|
||||||
|
NULLIF(TRIM(d.`alias`), ''),
|
||||||
|
CONCAT('尾号为', RIGHT(b.`target_mac`, 2), '的设备')
|
||||||
|
)
|
||||||
|
WHERE (b.`alias` IS NULL OR TRIM(b.`alias`) = '')
|
||||||
|
AND b.`target_mac` IS NOT NULL
|
||||||
|
AND CHAR_LENGTH(b.`target_mac`) >= 2;
|
||||||
|
|
@ -753,3 +753,10 @@ databaseChangeLog:
|
||||||
- sqlFile:
|
- sqlFile:
|
||||||
encoding: utf8
|
encoding: utf8
|
||||||
path: classpath:db/changelog/202609172100.sql
|
path: classpath:db/changelog/202609172100.sql
|
||||||
|
- changeSet:
|
||||||
|
id: 202610231400
|
||||||
|
author: wk01
|
||||||
|
changes:
|
||||||
|
- sqlFile:
|
||||||
|
encoding: utf8
|
||||||
|
path: classpath:db/changelog/202610231400.sql
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.mockStatic;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
@ -12,9 +15,11 @@ import static org.mockito.Mockito.when;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
|
||||||
import xiaozhi.common.constant.Constant;
|
import xiaozhi.common.constant.Constant;
|
||||||
import xiaozhi.common.redis.RedisKeys;
|
import xiaozhi.common.redis.RedisKeys;
|
||||||
|
|
@ -76,7 +81,30 @@ class DeviceAddressBookServiceImplTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void callByNicknameToleratesCallerMacThatIsNotRegistered() {
|
void blankAliasOnRegisteredTargetWithoutNicknameFallsBackToMacSuffix() {
|
||||||
|
DeviceAddressBookDao addressBookDao = mock(DeviceAddressBookDao.class);
|
||||||
|
RedisUtils redisUtils = mock(RedisUtils.class);
|
||||||
|
DeviceService deviceService = mock(DeviceService.class);
|
||||||
|
SysParamsService sysParamsService = mock(SysParamsService.class);
|
||||||
|
DeviceAddressBookServiceImpl service = new DeviceAddressBookServiceImpl(
|
||||||
|
addressBookDao, redisUtils, deviceService, sysParamsService);
|
||||||
|
when(addressBookDao.selectOne(any())).thenReturn(null);
|
||||||
|
when(addressBookDao.selectList(null)).thenReturn(List.of());
|
||||||
|
DeviceEntity registered = new DeviceEntity();
|
||||||
|
registered.setMacAddress(OTHER_MAC);
|
||||||
|
registered.setAlias(null);
|
||||||
|
when(deviceService.getDeviceByMacAddress(OTHER_MAC)).thenReturn(registered);
|
||||||
|
|
||||||
|
service.saveOrUpdate(MAC, OTHER_MAC, null, true);
|
||||||
|
|
||||||
|
ArgumentCaptor<DeviceAddressBookEntity> entityCaptor = ArgumentCaptor.forClass(DeviceAddressBookEntity.class);
|
||||||
|
verify(addressBookDao).insertAddressBook(entityCaptor.capture());
|
||||||
|
assertEquals("尾号为66的设备", entityCaptor.getValue().getAlias(),
|
||||||
|
"登记了但没起名的设备落个空名,缓存刷新会跳过这条,对方永远呼不过来");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void callByNicknameFallsBackToMacSuffixWhenCallerHasNoAlias() {
|
||||||
DeviceAddressBookDao addressBookDao = mock(DeviceAddressBookDao.class);
|
DeviceAddressBookDao addressBookDao = mock(DeviceAddressBookDao.class);
|
||||||
RedisUtils redisUtils = mock(RedisUtils.class);
|
RedisUtils redisUtils = mock(RedisUtils.class);
|
||||||
DeviceService deviceService = mock(DeviceService.class);
|
DeviceService deviceService = mock(DeviceService.class);
|
||||||
|
|
@ -93,8 +121,24 @@ class DeviceAddressBookServiceImplTest {
|
||||||
when(sysParamsService.getValue("server.mqtt_manager_api", true)).thenReturn("127.0.0.1:1883");
|
when(sysParamsService.getValue("server.mqtt_manager_api", true)).thenReturn("127.0.0.1:1883");
|
||||||
when(sysParamsService.getValue(Constant.SERVER_MQTT_SECRET, true)).thenReturn("gateway-key");
|
when(sysParamsService.getValue(Constant.SERVER_MQTT_SECRET, true)).thenReturn("gateway-key");
|
||||||
|
|
||||||
Map<String, Object> result = service.callByNickname(MAC, "office", false);
|
// 网关是真的 http 调用,桩掉它,否则测试会往 127.0.0.1:1883 真发请求
|
||||||
|
AtomicReference<String> sentUrl = new AtomicReference<>();
|
||||||
|
AtomicReference<String> sentBody = new AtomicReference<>();
|
||||||
|
Map<String, Object> result;
|
||||||
|
try (MockedStatic<MqttGatewayAuthorization> gateway = mockStatic(MqttGatewayAuthorization.class)) {
|
||||||
|
gateway.when(() -> MqttGatewayAuthorization.postJson(
|
||||||
|
anyString(), anyString(), anyString(), any(), anyInt()))
|
||||||
|
.thenAnswer(invocation -> {
|
||||||
|
sentUrl.set(invocation.getArgument(0));
|
||||||
|
sentBody.set(invocation.getArgument(1));
|
||||||
|
return "{\"status\":\"success\",\"message\":\"ok\"}";
|
||||||
|
});
|
||||||
|
result = service.callByNickname(MAC, "office", false);
|
||||||
|
}
|
||||||
|
|
||||||
assertNotNull(result);
|
assertEquals("success", result.get("status"));
|
||||||
|
assertEquals("http://127.0.0.1:1883/api/call/request", sentUrl.get());
|
||||||
|
assertTrue(sentBody.get().contains("尾号为55的设备"),
|
||||||
|
"主叫方没登记也没起名时该退回尾号称呼:" + sentBody.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,15 +2,24 @@ package xiaozhi.modules.device.service.impl;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyList;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.mockStatic;
|
import static org.mockito.Mockito.mockStatic;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.MockedStatic;
|
import org.mockito.MockedStatic;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||||
|
|
||||||
import xiaozhi.common.redis.RedisUtils;
|
import xiaozhi.common.redis.RedisUtils;
|
||||||
import xiaozhi.common.user.UserDetail;
|
import xiaozhi.common.user.UserDetail;
|
||||||
import xiaozhi.modules.device.dao.DeviceDao;
|
import xiaozhi.modules.device.dao.DeviceDao;
|
||||||
|
|
@ -95,6 +104,45 @@ class DeviceServiceImplTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("解绑自己的设备时,通讯录跟着一起清")
|
||||||
|
void unbindOwnDeviceClearsItsAddressBook() {
|
||||||
|
DeviceDao dao = mock(DeviceDao.class);
|
||||||
|
when(dao.selectById("device-1")).thenReturn(device(USER_ID, "device-1"));
|
||||||
|
DeviceAddressBookService addressBookService = mock(DeviceAddressBookService.class);
|
||||||
|
DeviceServiceImpl service = serviceWithDao(dao, addressBookService);
|
||||||
|
|
||||||
|
service.unbindDevice(USER_ID, "device-1");
|
||||||
|
|
||||||
|
verify(dao).delete(any(UpdateWrapper.class));
|
||||||
|
verify(addressBookService).deleteByMacAddresses(List.of(MAC));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("解绑别人的设备时什么都不删,通讯录不能跟着没")
|
||||||
|
void unbindAnotherUsersDeviceDeletesNothing() {
|
||||||
|
DeviceDao dao = mock(DeviceDao.class);
|
||||||
|
when(dao.selectById("device-1")).thenReturn(device(OTHER_USER_ID, "device-1"));
|
||||||
|
DeviceAddressBookService addressBookService = mock(DeviceAddressBookService.class);
|
||||||
|
DeviceServiceImpl service = serviceWithDao(dao, addressBookService);
|
||||||
|
|
||||||
|
service.unbindDevice(USER_ID, "device-1");
|
||||||
|
|
||||||
|
verify(dao, never()).delete(any(UpdateWrapper.class));
|
||||||
|
verify(addressBookService, never()).deleteByMacAddresses(anyList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private DeviceServiceImpl serviceWithDao(DeviceDao dao, DeviceAddressBookService addressBookService) {
|
||||||
|
// baseDao 由父类持有,子类内部赋给 this,测试类自己碰不到它
|
||||||
|
return new DeviceServiceImpl(
|
||||||
|
dao, mock(SysUserUtilService.class), mock(SysParamsService.class),
|
||||||
|
mock(RedisUtils.class), mock(OtaService.class), addressBookService) {
|
||||||
|
{
|
||||||
|
this.baseDao = dao;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private DeviceServiceImpl service() {
|
private DeviceServiceImpl service() {
|
||||||
return spy(new DeviceServiceImpl(
|
return spy(new DeviceServiceImpl(
|
||||||
mock(DeviceDao.class),
|
mock(DeviceDao.class),
|
||||||
|
|
@ -106,7 +154,12 @@ class DeviceServiceImplTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private DeviceEntity device(Long userId) {
|
private DeviceEntity device(Long userId) {
|
||||||
|
return device(userId, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DeviceEntity device(Long userId, String id) {
|
||||||
DeviceEntity entity = new DeviceEntity();
|
DeviceEntity entity = new DeviceEntity();
|
||||||
|
entity.setId(id);
|
||||||
entity.setMacAddress(MAC);
|
entity.setMacAddress(MAC);
|
||||||
entity.setUserId(userId);
|
entity.setUserId(userId);
|
||||||
return entity;
|
return entity;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue