feat: 添加设备绑定状态管理,优化设备连接逻辑
This commit is contained in:
parent
5465c4f2dd
commit
f6cf8037ac
|
|
@ -517,6 +517,8 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
||||||
const [nameShow, setNameShow] = useState(bindDevice ? `${bindDevice?.name}` : name)
|
const [nameShow, setNameShow] = useState(bindDevice ? `${bindDevice?.name}` : name)
|
||||||
const tempName = useRef(nameShow)
|
const tempName = useRef(nameShow)
|
||||||
|
|
||||||
|
const connectFinal = isConnected && bindDevice
|
||||||
|
|
||||||
const canEdit = !!bindDevice && isConnected
|
const canEdit = !!bindDevice && isConnected
|
||||||
|
|
||||||
// 绑定过的设备
|
// 绑定过的设备
|
||||||
|
|
@ -537,13 +539,19 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log('设备连接成功')
|
console.log('设备连接成功')
|
||||||
if (userId) {
|
if (userId) {
|
||||||
bleManager.bindDevice(userId).then((res) => {
|
bleManager
|
||||||
// console.log('Bind device response------------:', res)
|
.bindDevice(userId)
|
||||||
if (res.success !== 1) {
|
.then((res) => {
|
||||||
|
console.log('Bind device response------------:', res)
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
console.log('Bind device error------------:', e)
|
||||||
Toast.show({ title: '设备已经被其他用户绑定过了' })
|
Toast.show({ title: '设备已经被其他用户绑定过了' })
|
||||||
bleManager.disconnectDevice()
|
bleManager.disconnectDevice()
|
||||||
}
|
})
|
||||||
})
|
.finally(() => {
|
||||||
|
Toast.hideLoading()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
|
|
@ -581,7 +589,6 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
||||||
try {
|
try {
|
||||||
await bleManager.unBindDevice(userId!)
|
await bleManager.unBindDevice(userId!)
|
||||||
bleManager.disconnectDevice()
|
bleManager.disconnectDevice()
|
||||||
bleStore.removeBindDeviceItem(device.id)
|
|
||||||
setNameShow(device.name || 'Unknown Device')
|
setNameShow(device.name || 'Unknown Device')
|
||||||
Toast.show({ title: '解绑成功' })
|
Toast.show({ title: '解绑成功' })
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|
@ -622,7 +629,7 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Block className="relative mb-[16px] flex-row items-center overflow-hidden border-[3px] border-black bg-white p-[16px] shadow-large-black">
|
<Block className="relative mb-[16px] flex-row items-center overflow-hidden border-[3px] border-black bg-white p-[16px] shadow-large-black">
|
||||||
{isConnected && <Block className="absolute inset-y-0 left-0 w-[16px] border-r-[3px] border-black bg-accent" />}
|
{connectFinal && <Block className="absolute inset-y-0 left-0 w-[16px] border-r-[3px] border-black bg-accent" />}
|
||||||
|
|
||||||
<Block className="size-[48px] items-center justify-center border-[3px] border-black bg-gray-100 shadow-soft-black-10">
|
<Block className="size-[48px] items-center justify-center border-[3px] border-black bg-gray-100 shadow-soft-black-10">
|
||||||
<Ionicons color="black" name="phone-portrait-outline" size={24} />
|
<Ionicons color="black" name="phone-portrait-outline" size={24} />
|
||||||
|
|
@ -641,16 +648,16 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
||||||
<Block className="flex-row">
|
<Block className="flex-row">
|
||||||
<Block className="">
|
<Block className="">
|
||||||
<Text
|
<Text
|
||||||
className={`border-2 border-black px-[6px] text-[10px] font-[900] ${isConnected ? 'bg-black text-accent' : 'bg-gray-200 text-gray-500'}`}
|
className={`border-2 border-black px-[6px] text-[10px] font-[900] ${connectFinal ? 'bg-black text-accent' : 'bg-gray-200 text-gray-500'}`}
|
||||||
>
|
>
|
||||||
{isConnected ? '已连接' : '未连接'}
|
{connectFinal ? '已连接' : '未连接'}
|
||||||
</Text>
|
</Text>
|
||||||
</Block>
|
</Block>
|
||||||
|
|
||||||
{hasBind && (
|
{hasBind && (
|
||||||
<Block className="ml-[8px]">
|
<Block className="ml-[8px]">
|
||||||
<Text
|
<Text
|
||||||
className={`border-2 border-black px-[6px] text-[10px] font-[900] ${isConnected ? 'bg-black text-accent' : 'bg-gray-200 text-gray-500'}`}
|
className={`border-2 border-black px-[6px] text-[10px] font-[900] ${connectFinal ? 'bg-black text-accent' : 'bg-gray-200 text-gray-500'}`}
|
||||||
>
|
>
|
||||||
已绑定
|
已绑定
|
||||||
</Text>
|
</Text>
|
||||||
|
|
@ -662,10 +669,10 @@ const DeviceItem = observer(({ device }: { device: any }) => {
|
||||||
|
|
||||||
<Block className="relative z-10 flex-col items-end space-x-[8px]">
|
<Block className="relative z-10 flex-col items-end space-x-[8px]">
|
||||||
<Block
|
<Block
|
||||||
className={`h-[40px] items-center justify-center border-[3px] border-black px-[16px] text-[12px] font-[900] ${isConnected ? 'bg-accent text-black shadow-medium-black' : 'bg-white text-black shadow-medium-gray'}`}
|
className={`h-[40px] items-center justify-center border-[3px] border-black px-[16px] text-[12px] font-[900] ${connectFinal ? 'bg-accent text-black shadow-medium-black' : 'bg-white text-black shadow-medium-gray'}`}
|
||||||
onClick={() => onConnectToggle(device)}
|
onClick={() => onConnectToggle(device)}
|
||||||
>
|
>
|
||||||
<Text className="text-[12px] font-[900]">{isConnected ? '已连接' : '连接'}</Text>
|
<Text className="text-[12px] font-[900]">{connectFinal ? '已连接' : '连接'}</Text>
|
||||||
</Block>
|
</Block>
|
||||||
{canEdit && (
|
{canEdit && (
|
||||||
<Block
|
<Block
|
||||||
|
|
|
||||||
|
|
@ -117,6 +117,7 @@ class BleManager {
|
||||||
this.resolvePendingOperation('bindDevice', status)
|
this.resolvePendingOperation('bindDevice', status)
|
||||||
} else {
|
} else {
|
||||||
this.rejectPendingOperation('bindDevice', '绑定失败')
|
this.rejectPendingOperation('bindDevice', '绑定失败')
|
||||||
|
bleStore.removeBindDeviceItem(bleStore?.state?.connectedDevice?.id!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -641,7 +642,7 @@ class BleManager {
|
||||||
try {
|
try {
|
||||||
const resultPromise = this.createWaitableOperation<UnBindResponse>('unbindDevice')
|
const resultPromise = this.createWaitableOperation<UnBindResponse>('unbindDevice')
|
||||||
await this.deviceInfoService.unbindDevice(state.connectedDevice.id, userId)
|
await this.deviceInfoService.unbindDevice(state.connectedDevice.id, userId)
|
||||||
|
bleStore.removeBindDeviceItem(state.connectedDevice.id)
|
||||||
return await resultPromise
|
return await resultPromise
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
this.setError(`Request failed: ${e.message}`)
|
this.setError(`Request failed: ${e.message}`)
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,8 @@ interface BleState {
|
||||||
sn: string
|
sn: string
|
||||||
isActivated: boolean
|
isActivated: boolean
|
||||||
transferProgress: number
|
transferProgress: number
|
||||||
|
|
||||||
|
canBind: boolean
|
||||||
discoveredDevices: BleDevice[]
|
discoveredDevices: BleDevice[]
|
||||||
loading: {
|
loading: {
|
||||||
connecting: boolean
|
connecting: boolean
|
||||||
|
|
@ -43,7 +45,6 @@ class BleStore {
|
||||||
connectedDevice: null,
|
connectedDevice: null,
|
||||||
deviceInfo: null,
|
deviceInfo: null,
|
||||||
version: '',
|
version: '',
|
||||||
|
|
||||||
sn: '',
|
sn: '',
|
||||||
isActivated: false,
|
isActivated: false,
|
||||||
transferProgress: 0,
|
transferProgress: 0,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue