File size: 1,828 Bytes
808ab9a
c6c1596
808ab9a
 
 
c6c1596
 
 
 
808ab9a
 
c6c1596
808ab9a
 
c6c1596
 
 
808ab9a
c6c1596
 
 
 
808ab9a
 
c6c1596
 
808ab9a
 
c6c1596
 
808ab9a
 
c6c1596
808ab9a
c6c1596
 
 
7f237a3
 
 
 
 
 
808ab9a
 
c6c1596
808ab9a
c6c1596
 
 
808ab9a
 
c6c1596
808ab9a
c6c1596
 
 
808ab9a
 
c6c1596
 
808ab9a
 
c6c1596
 
808ab9a
 
c6c1596
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
// API client for the Decidron network simulator.
//
// Defaults to relative URLs so a production build served by the FastAPI
// container hits the same origin. For CRA dev (`npm start` on :3000),
// set REACT_APP_API_URL=http://localhost:7860 in frontend/.env.development.
const API_BASE = process.env.REACT_APP_API_URL !== undefined
  ? process.env.REACT_APP_API_URL
  : '';

async function jsonFetch(path, options = {}) {
  const resp = await fetch(`${API_BASE}${path}`, {
    headers: { 'Content-Type': 'application/json' },
    cache: 'no-store',
    ...options,
  });
  if (!resp.ok) {
    const err = await resp.json().catch(() => ({ detail: resp.statusText }));
    throw new Error(err.detail || `Request failed: ${resp.status}`);
  }
  return resp.json();
}

export function getNetwork() {
  return jsonFetch('/api/decidron/network');
}

export function getCommands() {
  return jsonFetch('/api/decidron/commands');
}

export function createCommand(command) {
  return jsonFetch('/api/decidron/commands', {
    method: 'POST',
    body: JSON.stringify(command),
  });
}

export function deleteCommand(name) {
  return jsonFetch(`/api/decidron/commands/${encodeURIComponent(name)}`, {
    method: 'DELETE',
  });
}

export function submitSensorInput(channel, value) {
  return jsonFetch('/api/decidron/sensors/input', {
    method: 'POST',
    body: JSON.stringify({ channel, value }),
  });
}

export function runSimulation(inputs = null) {
  return jsonFetch('/api/decidron/run', {
    method: 'POST',
    body: JSON.stringify(inputs ? { inputs } : {}),
  });
}

export function getStats() {
  return jsonFetch('/api/decidron/stats');
}

export function getHistory() {
  return jsonFetch('/api/decidron/history');
}

export function resetSimulation() {
  return jsonFetch('/api/decidron/reset', { method: 'POST' });
}