Advertisement
rickyc81

Untitled

May 1st, 2020
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.get('/pilots', function(req, res) {
  2.   let dataToSend = [];
  3.  
  4.   try {
  5.     fetch(URL_TEST_MALFORMED)
  6.       .then(response => {
  7.         //console.log(response.body._outBuffer.length)
  8.         if (response.body._outBuffer) {
  9.           return response.text();
  10.         } else {
  11.           return data = null;
  12.         }
  13.       })
  14.       .then(
  15.         data => {
  16.           //console.log(data)
  17.           if (data !== null) {
  18.             let splitted = data.split('\n');
  19.             let startIndex = splitted.indexOf("!CLIENTS") + 1;
  20.             let endIndex = splitted.indexOf("!AIRPORTS") + 2;
  21.             //console.log(startIndex, endIndex)
  22.             if (startIndex || startIndex == 0) {
  23.               res.send("Something wrong, malformed file retrived")
  24.               return
  25.             } else {
  26.               let clients = splitted.slice(startIndex, endIndex);
  27.               clients.forEach(line => {
  28.                 let fields = line.split(":");
  29.                 let temp = {}
  30.                 temp.callsign = fields[0];
  31.                 temp.vid = fields[1];
  32.                 temp.connectionTime = fields[37];
  33.                 temp.softwareName = fields[38];
  34.                 temp.softwareVersion = fields[39];
  35.                 temp.latitiude = fields[5];
  36.                 temp.longtitude = fields[6];
  37.                 temp.altitude = fields[7];
  38.                 temp.groundSpeed = fields[8];
  39.                 temp.heading = fields[45];
  40.                 temp.onGround = fields[46];
  41.                 temp.squawk = fields[17];
  42.                 temp.rating = fields[41];
  43.                 dataToSend.push(temp);
  44.               })
  45.               res.status(200).send({
  46.                 status: 'success',
  47.                 code: 200,
  48.                 response: {
  49.                   message: 'fetch data ok',
  50.                   data: dataToSend,
  51.                 },
  52.               })
  53.             }
  54.  
  55.           } else {
  56.             res.send("Response ok, but data not found!")
  57.           }
  58.         })
  59.   } catch (error) {
  60.     console.log(error)
  61.   }
  62. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement