File size: 5,433 Bytes
b450976
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import {useEffect, useRef, useState} from "react"

import BluetoothSdk from "../_private/BluetoothSdkModule"
import {
  createDisconnectedGlassesStatus,
  isConnectedGlassesConnectionStatus,
  isReadyGlassesConnectionStatus,
} from "../BluetoothSdk.types"
import type {
  BatteryStatusEvent,
  HotspotStatus,
  HotspotStatusChangeEvent,
  PublicBluetoothStatus,
  PublicGlassesStatus,
  WifiStatus,
  WifiStatusChangeEvent,
} from "../BluetoothSdk.types"

export type UseBluetoothStatusOptions = {
  enabled?: boolean
  onError?: (error: unknown) => void
}

export type BluetoothStatusHookResult = {
  bluetoothStatus: Partial<PublicBluetoothStatus>
  connected: boolean
  error: unknown | null
  glassesStatus: Partial<PublicGlassesStatus>
  loading: boolean
  ready: boolean
  refresh: () => Promise<void>
}

function wifiStatusFromEvent(event: WifiStatusChangeEvent): WifiStatus {
  switch (event.state) {
    case "connected":
      return {state: "connected", ssid: event.ssid, localIp: event.localIp}
    case "disconnected":
      return {state: "disconnected"}
  }
}

function hotspotStatusFromEvent(event: HotspotStatusChangeEvent): HotspotStatus {
  if (event.state === "enabled") {
    return {
      state: "enabled",
      ssid: event.ssid,
      password: event.password,
      localIp: event.localIp,
    }
  }
  return {state: event.state}
}

function mergeGlassesStatus(

  current: Partial<PublicGlassesStatus>,

  changed: Partial<PublicGlassesStatus>,

): Partial<PublicGlassesStatus> {
  if (changed.connection?.state === "disconnected") {
    return {...createDisconnectedGlassesStatus(), ...changed}
  }
  return {...current, ...changed}
}

export function useBluetoothStatus(options: UseBluetoothStatusOptions = {}): BluetoothStatusHookResult {
  const enabled = options.enabled ?? true
  const onErrorRef = useRef(options.onError)
  const [bluetoothStatus, setBluetoothStatus] = useState<Partial<PublicBluetoothStatus>>({})
  const [error, setError] = useState<unknown | null>(null)
  const [glassesStatus, setGlassesStatus] = useState<Partial<PublicGlassesStatus>>(() =>
    createDisconnectedGlassesStatus(),
  )
  const [loading, setLoading] = useState(enabled)

  useEffect(() => {
    onErrorRef.current = options.onError
  }, [options.onError])

  async function refresh() {
    setLoading(true)
    try {
      const [nextGlassesStatus, nextBluetoothStatus] = await Promise.all([
        BluetoothSdk.getGlassesStatus(),
        BluetoothSdk.getBluetoothStatus(),
      ])
      setGlassesStatus(nextGlassesStatus)
      setBluetoothStatus(nextBluetoothStatus)
      setError(null)
    } catch (nextError) {
      setError(nextError)
      onErrorRef.current?.(nextError)
    } finally {
      setLoading(false)
    }
  }

  useEffect(() => {
    if (!enabled) {
      setLoading(false)
      return undefined
    }

    let mounted = true

    const loadInitialStatus = async () => {
      setLoading(true)
      try {
        const [nextGlassesStatus, nextBluetoothStatus] = await Promise.all([
          BluetoothSdk.getGlassesStatus(),
          BluetoothSdk.getBluetoothStatus(),
        ])
        if (!mounted) {
          return
        }
        setGlassesStatus(nextGlassesStatus)
        setBluetoothStatus(nextBluetoothStatus)
        setError(null)
      } catch (nextError) {
        if (!mounted) {
          return
        }
        setError(nextError)
        onErrorRef.current?.(nextError)
      } finally {
        if (mounted) {
          setLoading(false)
        }
      }
    }

    void loadInitialStatus()

    const removeGlasses = BluetoothSdk.onGlassesStatus((changed) => {
      setGlassesStatus((current) => mergeGlassesStatus(current, changed))
    })
    const removeBluetooth = BluetoothSdk.onBluetoothStatus((changed) => {
      setBluetoothStatus((current) => ({...current, ...changed}))
    })
    const batterySubscription = BluetoothSdk.addListener("battery_status", (event: BatteryStatusEvent) => {
      setGlassesStatus((current) => ({
        ...current,
        batteryLevel: event.level,
        charging: event.charging,
      }))
    })
    const wifiSubscription = BluetoothSdk.addListener("wifi_status_change", (event) => {
      setGlassesStatus((current) => ({...current, wifi: wifiStatusFromEvent(event)}))
    })
    const hotspotSubscription = BluetoothSdk.addListener("hotspot_status_change", (event) => {
      setGlassesStatus((current) => ({...current, hotspot: hotspotStatusFromEvent(event)}))
    })
    const hotspotErrorSubscription = BluetoothSdk.addListener("hotspot_error", () => {
      setGlassesStatus((current) => ({...current, hotspot: {state: "disabled"}}))
    })

    return () => {
      mounted = false
      removeGlasses()
      removeBluetooth()
      batterySubscription.remove()
      wifiSubscription.remove()
      hotspotSubscription.remove()
      hotspotErrorSubscription.remove()
    }
  }, [enabled])

  const connection = glassesStatus.connection
  const connected = connection ? isConnectedGlassesConnectionStatus(connection) : false
  const ready = connection ? isReadyGlassesConnectionStatus(connection) : false

  return {
    bluetoothStatus,
    connected,
    error,
    glassesStatus,
    loading,
    ready,
    refresh,
  }
}