API Documentation

Project Description

API REST y WebSocket que permite registrar y obtener datos de consumo eléctrico y estado de maquinarias

Main technology: Node.js

Endpoints

/api/admin

  • GET /api/admin
  • POST /api/admin

Example Request:

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));
                    

/api/auth

  • POST /api/auth/login

Example Request:

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));
                    

/api/clients

  • GET /api/clients
  • POST /api/clients
  • GET /api/clients/:id
  • PUT /api/clients/:id
  • DELETE /api/clients/:id

Example Request:

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));
                    

/api/machines

  • GET /api/machines
  • POST /api/machines
  • GET /api/machines/:id
  • PUT /api/machines/:id
  • DELETE /api/machines/:id

Example Request:

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));
                    

/api/consults

  • GET /api/consults/machineclient/:clientId
  • GET /api/consults/machinelogs/:machineId
  • GET /api/consults/machinelogsdate/:machineId/daterange
  • POST /api/consults/execute

Example Request (Machines by Client):

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));
                    

Example Request (Execute SQL):

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));
                    

/api/logs

  • GET /api/logs
  • POST /api/logs
  • GET /api/logs/:id
  • POST /api/logs/multiple

Example Request (Single Log):

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));
                    

Example Request (Multiple Logs):

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));
                    

WebSocket API

The WebSocket server runs on ws://localhost:3001

Connecting to the WebSocket

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);
};
                

Sending a Log

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));
                

Receiving Data

The WebSocket server sends two types of messages:

1. All Logs (on connection)

{
    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
    ]
}
                

2. New Log (when a new log is added)

{
    type: 'newLog',
    log: {
        id: 2,
        machine_id: 1,
        local_timestamp: '2023-07-09 10:05:00',
        unit: 'Celsius',
        value: 26.0,
        status: 'ok'
    }
}
                

Handling Received Data

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
    }
};