File size: 3,991 Bytes
f234112
 
32b6488
 
 
f234112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// services/api.js — REST + WebSocket data layer

const APP_BASE = location.pathname.startsWith('/dashboard') ? '/dashboard' : ''
const BASE = `${APP_BASE}/api`
const WS_URL = `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}${APP_BASE}/ws/live`

export async function fetchStatus() {
  const r = await fetch(`${BASE}/status`)
  return r.json()
}

export async function fetchJunctions() {
  const r = await fetch(`${BASE}/junctions`)
  return r.json()
}

export async function fetchJunction(id) {
  const r = await fetch(`${BASE}/junctions/${id}`)
  if (!r.ok) return null
  return r.json()
}

export async function fetchDecisions() {
  const r = await fetch(`${BASE}/decisions`)
  return r.json()
}

export async function fetchSafety() {
  const r = await fetch(`${BASE}/safety`)
  return r.json()
}

export async function fetchEmergencies() {
  const r = await fetch(`${BASE}/emergencies`)
  return r.json()
}

export async function fetchMetrics() {
  const r = await fetch(`${BASE}/metrics`)
  return r.json()
}

export async function fetchLogs() {
  const r = await fetch(`${BASE}/logs`)
  return r.json()
}

export async function fetchComparison() {
  const r = await fetch(`${BASE}/comparison`)
  return r.json()
}

export async function fetchPerception(frame = null) {
  const url = frame != null ? `${BASE}/perception?frame=${frame}` : `${BASE}/perception`
  const r = await fetch(url)
  if (!r.ok) return null
  return r.json()
}

export async function startSimulation(options = {}) {
  const r = await fetch(`${BASE}/simulation/start`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(options),
  })
  return r.json()
}

export async function stopSimulation() {
  const r = await fetch(`${BASE}/simulation/stop`, {
    method: 'POST',
  })
  return r.json()
}

export async function fetchVideoSamples() {
  const r = await fetch(`${BASE}/video/samples`)
  if (!r.ok) return []
  return r.json()
}

export async function fetchLatestVideoAnalysis() {
  const r = await fetch(`${BASE}/video/latest-analysis`)
  if (!r.ok) return null
  return r.json()
}

export async function analyzeSampleVideo(samplePath) {
  const r = await fetch(`${BASE}/video/analyze-sample`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ sample_path: samplePath }),
  })
  if (!r.ok) {
    const err = await r.json().catch(() => ({}))
    throw new Error(err.detail || 'Video analysis failed')
  }
  return r.json()
}

export async function uploadAndAnalyzeVideo(file) {
  const formData = new FormData()
  formData.append('file', file)
  const r = await fetch(`${BASE}/video/upload-and-analyze`, {
    method: 'POST',
    body: formData,
  })
  if (!r.ok) {
    const err = await r.json().catch(() => ({}))
    throw new Error(err.detail || 'Video processing failed')
  }
  return r.json()
}

export function createWebSocket(onMessage, onOpen, onClose) {
  let ws = null
  let reconnectTimer = null
  let isClosed = false

  function connect() {
    if (isClosed) return
    try {
      ws = new WebSocket(WS_URL)
    } catch {
      reconnectTimer = setTimeout(connect, 2500)
      return
    }

    ws.onopen = () => {
      clearTimeout(reconnectTimer)
      onOpen?.()
    }

    ws.onmessage = (e) => {
      try {
        onMessage(JSON.parse(e.data))
      } catch (_) {}
    }

    ws.onclose = () => {
      onClose?.()
      if (!isClosed) {
        clearTimeout(reconnectTimer)
        reconnectTimer = setTimeout(connect, 2500)
      }
    }

    ws.onerror = () => {
      // Allow browser onclose handler to fire naturally and handle reconnect
    }
  }

  connect()

  return {
    close() {
      isClosed = true
      clearTimeout(reconnectTimer)
      try {
        ws?.close()
      } catch (_) {}
    },
    send(data) {
      if (ws?.readyState === WebSocket.OPEN) {
        ws.send(typeof data === 'string' ? data : JSON.stringify(data))
      }
    },
  }
}