-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiService.js
More file actions
38 lines (33 loc) · 1.11 KB
/
apiService.js
File metadata and controls
38 lines (33 loc) · 1.11 KB
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
import axios from 'axios';
/**
* A centralized Axios instance.
*/
const api = axios.create();
/**
* Configures the default settings for the centralized API instance.
* This should be called once when the app loads and when settings change.
* @param {string} baseUrl The base URL for the API.
* @param {string} apiKey The API key for the 'x-api-key' header.
*/
export const configureApi = (baseUrl, apiKey) => {
api.defaults.baseURL = baseUrl;
api.defaults.headers.common['x-api-key'] = apiKey;
};
/**
* Fetches the current sensor values from the API.
*/
export const getSensorValues = async () => {
const response = await api.get('/sensor-values');
return response.data;
};
/**
* Sends an update command for a specific relay.
* @param {string} relayName - The name of the relay to update (e.g., 'RELAY1').
* @param {number} value - The value to set for the relay (1 or 2).
*/
export const updateRelay = async (relayName, value) => {
const response = await api.post('/update-relay', { relayName, value });
return response.data;
};
// We can also export the instance itself if needed elsewhere
export default api;