128 lines
3.2 KiB
TypeScript
128 lines
3.2 KiB
TypeScript
import { makeAutoObservable } from 'mobx'
|
|
|
|
import { type BleDevice } from '@/ble/core/types'
|
|
import { type DeviceInfo } from '@/ble/protocol/types'
|
|
import { storage } from '@/utils'
|
|
|
|
import { userStore } from './userStore'
|
|
|
|
interface BleState {
|
|
isScanning: boolean
|
|
isConnected: boolean
|
|
connectedDevice: BleDevice | null
|
|
deviceInfo: DeviceInfo | null
|
|
version: string
|
|
sn: string
|
|
isActivated: boolean
|
|
transferProgress: number
|
|
|
|
canBind: boolean
|
|
discoveredDevices: BleDevice[]
|
|
loading: {
|
|
connecting: boolean
|
|
querying: boolean
|
|
converting: boolean
|
|
transferring: boolean
|
|
}
|
|
error: string | null
|
|
}
|
|
|
|
interface bindDeviceItem {
|
|
id: string
|
|
name: string
|
|
userId: string
|
|
createAt: number
|
|
|
|
sn?: string
|
|
version?: string
|
|
}
|
|
|
|
class BleStore {
|
|
// State
|
|
state: BleState = {
|
|
isScanning: false,
|
|
isConnected: false,
|
|
connectedDevice: null,
|
|
deviceInfo: null,
|
|
version: '',
|
|
sn: '',
|
|
isActivated: false,
|
|
transferProgress: 0,
|
|
discoveredDevices: [],
|
|
loading: {
|
|
connecting: false,
|
|
querying: false,
|
|
converting: false,
|
|
transferring: false,
|
|
},
|
|
error: null,
|
|
}
|
|
|
|
galleryList: string[] = []
|
|
bindDeviceList: bindDeviceItem[] = []
|
|
bleSessionId = `ble-session-${Date.now().toString()}`
|
|
|
|
constructor() {
|
|
makeAutoObservable(this)
|
|
this.loadData()
|
|
}
|
|
|
|
private async loadData() {
|
|
const data = await storage.get('ble_contents')
|
|
this.setState((prev) => ({
|
|
...prev,
|
|
contents: (data as string[]) ?? [],
|
|
}))
|
|
|
|
const bindData = await storage.get('ble_bindDeviceList')
|
|
this.bindDeviceList = (bindData as typeof this.bindDeviceList) ?? []
|
|
}
|
|
|
|
setGalleryList(contents: string[]) {
|
|
this.galleryList = contents
|
|
storage.set('ble_contents', contents)
|
|
}
|
|
addGalleryItem(content: string) {
|
|
if (this.galleryList?.includes(content)) return
|
|
this.galleryList?.push(content)
|
|
storage.set('ble_contents', this.galleryList)
|
|
}
|
|
removeGalleryItem(key: string) {
|
|
this.galleryList = this.galleryList?.filter((c) => c !== key)
|
|
storage.set('ble_contents', this.galleryList)
|
|
}
|
|
|
|
addBindDeviceItem(item: bindDeviceItem) {
|
|
if (this.bindDeviceList?.find((i) => i.id === item.id)) return
|
|
this.bindDeviceList?.push({ ...item, createAt: Date.now() })
|
|
|
|
userStore.bindBluetooth({ sn: item.sn || '', version: item.version || '' })
|
|
storage.set('ble_bindDeviceList', this.bindDeviceList)
|
|
}
|
|
updateBindDeviceItem(item: bindDeviceItem) {
|
|
const index = this.bindDeviceList?.findIndex((i) => i.id === item.id)
|
|
if (index !== -1) {
|
|
this.bindDeviceList[index] = item
|
|
storage.set('ble_bindDeviceList', this.bindDeviceList)
|
|
}
|
|
}
|
|
removeBindDeviceItem(id: string) {
|
|
this.bindDeviceList = this.bindDeviceList?.filter((i) => i.id !== id)
|
|
|
|
const device = this.bindDeviceList.find((i) => i.id === id)
|
|
if (device) {
|
|
// 解绑设备id为空
|
|
userStore.bindBluetooth({ sn: device.sn || '', version: device.version || '', userDeviceId: '' })
|
|
}
|
|
storage.set('ble_bindDeviceList', this.bindDeviceList)
|
|
}
|
|
|
|
// Actions
|
|
setState(updater: (prev: BleState) => BleState) {
|
|
this.state = updater(this.state)
|
|
}
|
|
}
|
|
|
|
// Global instance
|
|
export const bleStore = new BleStore()
|