API REST y WebSocket que permite registrar y obtener datos de consumo eléctrico y estado de maquinarias
Main technology: Node.js
fetch('/api/admin', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'your_api_key'
},
body: JSON.stringify({
username: 'admin1',
password: 'password123'
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
fetch('/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: 'admin1',
password: 'password123'
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
fetch('/api/clients', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'your_api_key'
},
body: JSON.stringify({
name: 'Client1',
email: '[email protected]',
status: 'active'
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
fetch('/api/machines', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'your_api_key'
},
body: JSON.stringify({
client_id: 1,
type: 'Type1',
location: 'Location1',
serial_number: 'SN123456',
last_maintenance_date: '2023-07-08'
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
fetch('/api/consults/machineclient/1', {
method: 'GET',
headers: {
'Authorization': 'your_api_key'
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
fetch('/api/consults/execute', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'your_api_key'
},
body: JSON.stringify({
query: 'SELECT * FROM logs WHERE machine_id = 1 LIMIT 10'
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
fetch('/api/logs', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'your_api_key'
},
body: JSON.stringify({
machine_id: 1,
local_timestamp: '2023-07-08T10:00:00',
unit: 'Celsius',
value: 25.5,
status: 'ok'
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
fetch('/api/logs/multiple', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'your_api_key'
},
body: JSON.stringify({
logs: [
{
machine_id: 1,
local_timestamp: '2023-07-08T10:00:00',
unit: 'Celsius',
value: 25.5,
status: 'ok'
},
{
machine_id: 2,
local_timestamp: '2023-07-08T10:01:00',
unit: 'Fahrenheit',
value: 77.0,
status: 'ok'
}
]
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
The WebSocket server runs on ws://localhost:3001
const socket = new WebSocket('ws://localhost:3001');
socket.onopen = () => {
console.log('Connected to WebSocket');
};
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Received:', data);
};
socket.onclose = () => {
console.log('Disconnected from WebSocket');
};
socket.onerror = (error) => {
console.error('WebSocket Error:', error);
};
const logData = {
type: 'newLog',
log: {
machine_id: 1,
local_timestamp: new Date().toISOString(),
unit: 'Celsius',
value: 25.5,
status: 'ok'
}
};
socket.send(JSON.stringify(logData));
The WebSocket server sends two types of messages:
{
type: 'allLogs',
logs: [
{
id: 1,
machine_id: 1,
local_timestamp: '2023-07-09 10:00:00',
unit: 'Celsius',
value: 25.5,
status: 'ok'
},
// ... more logs
]
}
{
type: 'newLog',
log: {
id: 2,
machine_id: 1,
local_timestamp: '2023-07-09 10:05:00',
unit: 'Celsius',
value: 26.0,
status: 'ok'
}
}
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type === 'allLogs') {
console.log('Received all logs:', data.logs);
// Handle initial logs data
} else if (data.type === 'newLog') {
console.log('Received new log:', data.log);
// Handle new log data
}
};