var { Client } = require('pg'); var express = require("express"); const path = require("path"); var cors = require('cors') var app = express(); const port = 3000; app.use(cors()) const static_path = path.join(__dirname, "public"); // The path to a public folder. app.use(express.static(static_path)); app.use(express.urlencoded({extended: true})); app.get('/', (req, res) => { res.sendFile('js/index.html', {root: __dirname });// When you enter the url localhost:3000/ you get the index.html file. }); app.get('/getExersises', (req,res) =>{ con = new Client({ // Establish a connection with the database called lts_ets_pro user: "", host: "", database: "lts_ets_pro", password: "" }); con.connect(function(err){ // When the connection attempt was successful this will run if(err) throw err// If there had been an error connecting to the database it will throw the error con.query("select id, name, created from exm.exercise order by created",function(err,result,fields){ if(err) throw err; var payload = []; for(var i = 0; i < result.rows.length;i++) { // This will just put the lat and lon variables in a point variable and then append the point to the payload var exersise = {exersise_id: result.rows[i].id, exersise_name: result.rows[i].name} payload.push(exersise); } res.send(payload)// Send the data to the client as a response }) }); }); app.get('/getParticipants/p', (req,res)=> { con = new Client({ // Establish a connection with the database called lts_ets_pro user: "", host: "", database: "lts_ets_pro", password: "" }); con.connect(function(err){ // When the connection attempt was successful this will run if(err) throw err// If there had been an error connecting to the database it will throw the error con.query("SELECT DISTINCT id, equipment_number, designator from ex_" + req.query.exersiseId + ".rec_participant where equipment_number IS NOT NULL order by equipment_number",function(err,result,fields){ if(err) throw err; var payload = []; for(var i = 0; i < result.rows.length;i++) { // This will just put the lat and lon variables in a point variable and then append the point to the payload var participant = {id: result.rows[i].id,equipment_number: result.rows[i].equipment_number, designator: result.rows[i].designator} payload.push(participant); } res.send(payload)// Send the data to the client as a response }) }); }); app.get("/getPositions/p", (req, res) => { // This will be run when you enter the URL localhost:3000/getData/p con = new Client({ // Establish a connection with the database called lts_ets_pro user: "", host: "", database: "lts_ets_pro", password: "" }); con.connect(function(err){ // When the connection attempt was successful this will run if(err) throw err// If there had been an error connecting to the database it will throw the error con.query("select sample_time, pos_lon*180/3.14159265359 as pos_lon, pos_lat*180/3.14159265359 as pos_lat, pos_z from ex_" + req.query.exersiseId + ".rec_participant_position where participant_id = " + req.query.parId + " order by sample_time",function(err,result,fields){ // Explenation: // ---select sample_time, pos_lon*180/3.14159265359 as pos_lon, pos_lat*180/3.14159265359 as pos_lat, pos_z // This will select what variables i want to get from the table. // ---from ex_201.rec_participant_position // This defines the table i want to get data from. // ---where participant_id = " + req.query.parId + " // I only want to take the data if it is from the participant with the participantId that has been passed from the get request // ---order by sample_time // This will just order the data by the sample_time if(err) throw err; var Payload = []; for(var i = 0; i < result.rows.length;i++) { // This will just put the lat and lon variables in a point variable and then append the point to the payload var position = []; position.push(result.rows[i].pos_lat); position.push(result.rows[i].pos_lon); var event = {event_type_id: -1,event_name: "Position", sample_time: result.rows[i].sample_time,position: position} Payload.push(event); } res.send(Payload)// Send the data to the client as a response }) }); }) app.get("/getEvents/p", (req, res) => { // This will be run when you enter the URL localhost:3000/getEvents/p con = new Client({ // Establish a connection with the database called lts_ets_pro user: "", host: "", database: "lts_ets_pro", password: "" }); con.connect(function(err){ // When the connection attempt was successful this will run. if(err) throw err; // If there had been an error connecting to the database it will throw the error con.query("select E.event_type_id, T.name, E.participant_id, E.sample_time, E.position_x*180/3.14159265359 as position_x, E.position_y*180/3.14159265359 as position_y from ex_" + req.query.exersiseId + ".eem_event E JOIN pdm.pdm_event_type T ON T.id = E.event_type_id where participant_id = " + req.query.parId + " and event_type_id IN (" + req.query.eventIds + ") order by sample_time",function(err,result,fields){ // Explenation: // ---select T.name, E.participant_id, E.sample_time, E.position_x*180/3.14159265359 as position_x, E.position_y*180/3.14159265359 as position_y // This will select what variables i want to get from the table. The E and T will be different tables that will be defined in the next step. // ---from ex_201.eem_event E JOIN pdm.pdm_event_type T // This defines the tables i want to get data from. // ---ON T.id = E.event_type_id // This will look up the correct T.name by the event_type_id of the given event // ---where participant_id = " + req.query.parId + " and event_type_id IN (" + req.query.eventIds + ") // I only want to take the data if it is from the participant with the correct participantId and event_type_id that has been passed from the get request and // ---order by sample_time // This will just order the data by the sample_time if(err) throw err; var payload = []; for(var i = 0; i < result.rows.length;i++) { // This will just put the data in a event object var position = []; position.push(result.rows[i].position_y); position.push(result.rows[i].position_x); var event = {event_type_id: result.rows[i].event_type_id,event_name: result.rows[i].name, sample_time: result.rows[i].sample_time,position: position} payload.push(event); } res.send(payload) // Send the data to the client as a response }) }); }) app.listen(port, () => { console.log(`Example app listening on port ${port}`) })