🌈 style(eslint): 新增eslint规则
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import React, { useRef, useState } from 'react'
|
||||
import Taro from '@tarojs/taro'
|
||||
import React, { useRef, useState } from 'react'
|
||||
import { Command } from '@/common/bluetooth/command'
|
||||
import { uint8ArrayToFloat32, uint8ArrayToHex, waitFor } from '@/common/bluetooth/utils'
|
||||
|
||||
@@ -33,13 +33,13 @@ interface stateStype {
|
||||
|
||||
/** 是否显示蓝牙调试信息 */
|
||||
debug: any
|
||||
//搜索到的设备
|
||||
// 搜索到的设备
|
||||
devices: any
|
||||
//取色仪主动返回的数据
|
||||
// 取色仪主动返回的数据
|
||||
deviceLab: any
|
||||
}
|
||||
|
||||
let stateObj: stateStype = {
|
||||
const stateObj: stateStype = {
|
||||
/** 事件监听器 */
|
||||
listeners: new Set(),
|
||||
/** 正在扫描设备 */
|
||||
@@ -64,25 +64,26 @@ let stateObj: stateStype = {
|
||||
|
||||
/** 是否显示蓝牙调试信息 */
|
||||
debug: true,
|
||||
//搜索到的设备
|
||||
// 搜索到的设备
|
||||
devices: [],
|
||||
//取色仪主动返回的数据
|
||||
// 取色仪主动返回的数据
|
||||
deviceLab: null,
|
||||
}
|
||||
|
||||
export default (props) => {
|
||||
let refStatus = useRef(stateObj)
|
||||
let [state, setState] = useState(refStatus.current)
|
||||
const refStatus = useRef(stateObj)
|
||||
const [state, setState] = useState(refStatus.current)
|
||||
|
||||
const changeStatus = (obj: Object): void => {
|
||||
refStatus.current = { ...refStatus.current, ...obj }
|
||||
setState({ ...refStatus.current })
|
||||
}
|
||||
|
||||
const init = async () => {
|
||||
const init = async() => {
|
||||
try {
|
||||
await openAdapter()
|
||||
} catch (e) {
|
||||
}
|
||||
catch (e) {
|
||||
changeStatus({ available: false })
|
||||
}
|
||||
|
||||
@@ -94,11 +95,12 @@ export default (props) => {
|
||||
emit({ type: res.connected ? 'connected' : 'disconnect', detail: res })
|
||||
})
|
||||
Taro.onBLECharacteristicValueChange(({ value }) => notifySubscriber(value))
|
||||
subscribe(async (ev) => {
|
||||
subscribe(async(ev) => {
|
||||
if (ev.type === 'stateUpdate') {
|
||||
// 蓝牙状态发生的变化
|
||||
changeStatus({ discovering: ev.detail.discovering, available: ev.detail.available })
|
||||
} else if (ev.type === 'disconnect' && refStatus.current.connected && refStatus.current.connected.deviceId === ev.detail.deviceId) {
|
||||
}
|
||||
else if (ev.type === 'disconnect' && refStatus.current.connected && refStatus.current.connected.deviceId === ev.detail.deviceId) {
|
||||
// 断开连接
|
||||
changeStatus({
|
||||
connected: null,
|
||||
@@ -108,12 +110,14 @@ export default (props) => {
|
||||
devices: [],
|
||||
})
|
||||
Taro.showToast({ icon: 'none', title: '蓝牙连接已断开' })
|
||||
} else if (ev.type === 'connected' && refStatus.current.connecting) {
|
||||
}
|
||||
else if (ev.type === 'connected' && refStatus.current.connecting) {
|
||||
// 连接成功
|
||||
changeStatus({ connected: refStatus.current.connecting, connecting: null })
|
||||
Taro.showToast({ title: '蓝牙已连接' })
|
||||
} else if (ev.type === 'measure') {
|
||||
//监听取色仪主动推送lab
|
||||
}
|
||||
else if (ev.type === 'measure') {
|
||||
// 监听取色仪主动推送lab
|
||||
await measureAndGetLab()
|
||||
}
|
||||
})
|
||||
@@ -186,11 +190,11 @@ export default (props) => {
|
||||
})
|
||||
}
|
||||
|
||||
//获取搜索到的设备
|
||||
// 获取搜索到的设备
|
||||
const getDevices = (res) => {
|
||||
res.devices.forEach((device) => {
|
||||
// 排除掉已搜索到的设备和名称不合法的设备, 将新发现的设备添加到列表中
|
||||
if (/^CM/.test(device.name) && !refStatus.current.devices.find((i) => i.deviceId === device.deviceId)) {
|
||||
if (/^CM/.test(device.name) && !refStatus.current.devices.find(i => i.deviceId === device.deviceId)) {
|
||||
changeStatus({ devices: [...refStatus.current.devices, device] })
|
||||
}
|
||||
})
|
||||
@@ -200,7 +204,7 @@ export default (props) => {
|
||||
* 连接设备
|
||||
* @param {{ name: string, deviceId: string, RSSI: number }} device
|
||||
*/
|
||||
const connect = async (device) => {
|
||||
const connect = async(device) => {
|
||||
try {
|
||||
changeStatus({ connecting: device })
|
||||
console.log('connecting::', device)
|
||||
@@ -208,7 +212,8 @@ export default (props) => {
|
||||
await discoverService(device.deviceId)
|
||||
await discoverCharacteristic(device.deviceId)
|
||||
await notifyCharacteristicValueChange(device.deviceId)
|
||||
} catch (e) {
|
||||
}
|
||||
catch (e) {
|
||||
changeStatus({ connecting: null })
|
||||
Taro.showToast({ icon: 'none', title: '蓝牙连接失败' })
|
||||
throw e
|
||||
@@ -216,8 +221,8 @@ export default (props) => {
|
||||
}
|
||||
|
||||
/** 断开当前连接的设备 */
|
||||
const disconnect = async () => {
|
||||
if (!refStatus.current.connected && !refStatus.current.connecting) return
|
||||
const disconnect = async() => {
|
||||
if (!refStatus.current.connected && !refStatus.current.connecting) { return }
|
||||
if (refStatus.current.connected) {
|
||||
await closeConnection(refStatus.current.connected.deviceId)
|
||||
resetCommand()
|
||||
@@ -265,10 +270,11 @@ export default (props) => {
|
||||
Taro.getBLEDeviceServices({
|
||||
deviceId,
|
||||
success: ({ services }) => {
|
||||
const service = services.find((i) => refStatus.current.serviceRule.test(i.uuid))
|
||||
const service = services.find(i => refStatus.current.serviceRule.test(i.uuid))
|
||||
if (!service) {
|
||||
reject(new Error('服务不可用'))
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
changeStatus({ serviceId: service.uuid })
|
||||
resolve(service)
|
||||
}
|
||||
@@ -285,10 +291,11 @@ export default (props) => {
|
||||
deviceId,
|
||||
serviceId: refStatus.current.serviceId,
|
||||
success: ({ characteristics }) => {
|
||||
const characteristic = characteristics.find((i) => refStatus.current.characteristicRule.test(i.uuid))
|
||||
const characteristic = characteristics.find(i => refStatus.current.characteristicRule.test(i.uuid))
|
||||
if (!characteristic) {
|
||||
reject(new Error('特征不可用'))
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
changeStatus({ characteristicId: characteristic.uuid })
|
||||
resolve(characteristic)
|
||||
}
|
||||
@@ -325,14 +332,16 @@ export default (props) => {
|
||||
if (refStatus.current.command.isComplete) {
|
||||
if (refStatus.current.command.isValid && refStatus.current.responseResolve) {
|
||||
refStatus.current.responseResolve(refStatus.current.command.response)
|
||||
} else if (!refStatus.current.command.isValid) {
|
||||
}
|
||||
else if (!refStatus.current.command.isValid) {
|
||||
refStatus.current.responseReject(new Error('无效数据'))
|
||||
}
|
||||
resetCommand()
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
const uint8Array = new Uint8Array(buffer)
|
||||
if (uint8Array[0] === 0xbb && uint8Array[1] === 1 && uint8Array[3] === 0) {
|
||||
if (uint8Array[0] === 0xBB && uint8Array[1] === 1 && uint8Array[3] === 0) {
|
||||
const ev = { type: 'measure', detail: { mode: uint8Array[2] } }
|
||||
emit(ev)
|
||||
}
|
||||
@@ -345,10 +354,11 @@ export default (props) => {
|
||||
* @returns {Promise<Uint8Array>}
|
||||
*/
|
||||
function exec(command) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
return new Promise(async(resolve, reject) => {
|
||||
if (refStatus.current.command) {
|
||||
reject(new Error('正在执行其他命令'))
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
try {
|
||||
refStatus.current.command = command
|
||||
const data = command.data
|
||||
@@ -359,7 +369,8 @@ export default (props) => {
|
||||
if (command.responseSize <= 0) {
|
||||
resolve(true)
|
||||
resetCommand()
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
refStatus.current.responseReject = reject
|
||||
refStatus.current.responseResolve = resolve
|
||||
refStatus.current.responseTimer = setTimeout(() => {
|
||||
@@ -367,7 +378,8 @@ export default (props) => {
|
||||
resetCommand()
|
||||
}, command.timeout)
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
catch (e) {
|
||||
reject(e)
|
||||
}
|
||||
}
|
||||
@@ -471,7 +483,8 @@ export const useBluetooth = () => {
|
||||
const res = React.useContext<any>(Context)
|
||||
if (res) {
|
||||
return { ...res }
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user