File size: 3,736 Bytes
fba4209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios from 'axios';
import { API_BASE_URL, ENDPOINTS } from '../constants/api';

// Create axios instance with base configuration
const apiClient = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// Error handling interceptor
apiClient.interceptors.response.use(
  response => response,
  error => {
    console.error('API Error:', {
      status: error.response?.status,
      data: error.response?.data,
      headers: error.response?.headers,
      message: error.message,
      config: error.config
    });
    return Promise.reject(error);
  }
);


// API service functions
export const apiService = {
  // Get hello message
  getHelloMessage: async () => {
    try {
      const response = await apiClient.get(ENDPOINTS.HELLO);
      return response.data;
    } catch (error) {
      throw error;
    }
  },
  
  // Set index
  setIndex: async (index) => {
    try {
      const response = await apiClient.post(ENDPOINTS.SET_INDEX, { index });
      return response.data;
    } catch (error) {
      throw error;
    }
  },

  getHistoricalScan: async (startDate, endDate) => {
    try {
      const response = await apiClient.get(ENDPOINTS.HISTORY, {
        params: {
          start_date: startDate,
          end_date: endDate
        }
      });
      return response.data;
    } catch (error) {
      throw error;
    }
  },

  // Start or stop scan
  controlScan: async (action) => {
    try {
      const message = action.message;
      const alt_button = action.alt_button || false;
      const ignored_index_list = action.ignored_index_list ? action.ignored_index_list : null;
      const response = await apiClient.post(ENDPOINTS.SCAN, { message,ignored_index_list,alt_button});
      return response;
    } catch (error) {
      throw error;
    }
  },
  
  getLatestScan: async (fileName = 'scan_output.json') => {
    try {
      const response = await apiClient.get(ENDPOINTS.LATEST_SCAN, {
        params: { file: fileName }
      });
      return response.data;
    } catch (error) {
      throw error;
    }
  },

  // Get robot status
  getRobotStatus: async () => {
    try {
      const response = await apiClient.get(ENDPOINTS.ROBOT_STATUS);
      return response.data;
    } catch (error) {
      throw error;
    }
  },
  
  // Get scan log
  getScanLog: async () => {
    try {
      const response = await apiClient.get(ENDPOINTS.SCAN_LOG);
      return response.data;
    } catch (error) {
      throw error;
    }
  },
  
  // Send air signal
  sendAirSignal: async () => {
    try {
      const response = await apiClient.get(ENDPOINTS.AIR);
      return response.data;
    } catch (error) {
      throw error;
    }
  },

  getColors: async () => {
    try {
      const response = await apiClient.get(ENDPOINTS.COLORS);
      return response.data;
    } catch (error) {
      throw error;
    }
  },

  downloadExcel: async (fileName = 'scan_output.json') => {
    try {
      const response = await apiClient.get(ENDPOINTS.DOWNLOAD_EXCEL, { responseType: 'blob', params: { file: fileName } });
      return response;
    } catch (error) {
      throw error;
    }
  },

  GetConfig: async () => {
    try {
      const response = await apiClient.get(ENDPOINTS.CONFIG); 
      return response.data;
    } catch (error) {
      console.error("getConfig error:", error);
      throw error;
    }
  },

  SetConfig: async (config) => {
    try {
      const response = await apiClient.post(
        ENDPOINTS.CONFIG,
        config,
        {
          headers: {
            'Content-Type': 'application/json'
          }
        }
      );
      return response.data;
    } catch (error) {
      throw error;
    }
  }
};