Browse Source

calculation functions

master
mahmoud 2 years ago
parent
commit
187dc2c31f
7 changed files with 196 additions and 0 deletions
  1. 1
    0
      server/.gitignore
  2. 100
    0
      server/app.js
  3. 18
    0
      server/cars.json
  4. 33
    0
      server/inputs.json
  5. 13
    0
      server/package-lock.json
  6. 14
    0
      server/package.json
  7. 17
    0
      server/stations.json

+ 1
- 0
server/.gitignore View File

@@ -0,0 +1 @@
node_modules

+ 100
- 0
server/app.js View File

@@ -0,0 +1,100 @@

const
inputs = require('./inputs.json'),
cars = require('./cars.json'),
stations = require('./stations.json'),
geodist = require('geodist')

const getCarLastUpdate = (carId) => {
return inputs
.filter(data => {
return data.car_id == carId
})
.sort((x, y) => {
return x.timestamp - y.timestamp
})
.pop()
}

const distanceBetweenCarAndStation = (carId, stationId) => {
let input = inputs
.filter(data => {
return data.car_id == carId
})
.sort((x, y) => {
return x.timestamp - y.timestamp
})

if ( ! input.length) return null

let lastUpdate = getCarLastUpdate(carId)

if (! lastUpdate) return null

return distanceBetweenTwoPoint(lastUpdate, stations[stationId])
}

const isTheCarInStation = (carId, stationId) => {
let distance = distanceBetweenCarAndStation(carId, stationId)

if (! distance) return null

let lastUpdate = getCarLastUpdate(carId)

return (distance < 100 && lastUpdate.speed < 1)
}

const distanceBetweenTwoPoint = (start, end) => {
return geodist(start, end, {'unit': 'meters'})
}

const calculateCarTimeToStation = (carId, stationId) => {
// Time = Distance/Speed
// Hourse = `KM` / `KM/H`
let lastUpdate = getCarLastUpdate(carId)

if (! lastUpdate) return null

let distance = distanceBetweenCarAndStation(carId, stationId) / 1000 // convert it to km
let speed = lastUpdate.speed

return distance / speed
}

const getStationTraficCondition = (stationId) => {
let stationCars = Object.values(cars)
.filter(car => {
return car.stations_ids.includes(stationId)
})
.reduce((carsIds, car) => {
carsIds.push(car.id)
return carsIds
}, [])

let speedInput = inputs
.filter(input => {
return stationCars.includes(input.car_id) && ! isTheCarInStation(input.car_id, stationId)
})

let sumSpeed = speedInput
.reduce((sumSpeed, input)=> {
sumSpeed += input.speed
return sumSpeed
}, 0)

let avg = sumSpeed / speedInput.length

return avg / stations[stationId].max_speed
}

// console.log(calculateCarTimeToStation(1, 1))
console.log(getStationTraficCondition(1))

module.exports = {
getCarLastUpdate,
distanceBetweenCarAndStation,
isTheCarInStation,
calculateCarTimeToStation,
distanceBetweenTwoPoint,
getStationTraficCondition
}

+ 18
- 0
server/cars.json View File

@@ -0,0 +1,18 @@
{
"1" :{
"id": 1,
"stations_ids": [1, 2]
},
"2": {
"id": 2,
"stations_ids": [1, 2]
},
"3": {
"id": 3,
"stations_ids": [1, 2]
},
"4": {
"id": 4,
"stations_ids": [1, 2]
}
}

+ 33
- 0
server/inputs.json View File

@@ -0,0 +1,33 @@
[
{
"car_id": 1,
"lat": 13.1835,
"lon": -54.93733,
"speed": 40,
"timestamp": 1533831400
}, {
"car_id": 1,
"lat": 13.1837,
"lon": -54.93738,
"speed": 10,
"timestamp": 1533831419
}, {
"car_id": 2,
"lat": 14.9837,
"lon": -55.93738,
"speed": 45,
"timestamp": 1533831419
}, {
"car_id": 3,
"lat": 12.9837,
"lon": -51.93738,
"speed": 5,
"timestamp": 1533831419
}, {
"car_id": 4,
"lat": 10.9837,
"lon": -45.93738,
"speed": 0,
"timestamp": 1533831419
}
]

+ 13
- 0
server/package-lock.json View File

@@ -0,0 +1,13 @@
{
"name": "server",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"geodist": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/geodist/-/geodist-0.2.1.tgz",
"integrity": "sha1-lhml2TVSkzVxb6r60yIowp3AuSA="
}
}
}

+ 14
- 0
server/package.json View File

@@ -0,0 +1,14 @@
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"geodist": "^0.2.1"
}
}

+ 17
- 0
server/stations.json View File

@@ -0,0 +1,17 @@
{
"1": {
"id:": 1,
"name": "Nablus",
"Dest": "Ramallah",
"lat": 13.9837,
"lon": -54.93738,
"max_speed": 90
},
"2": {
"id:": 2,
"name": "Ramallah,Nablus",
"lat": 15.9837,
"lon": -55.93738,
"max_speed": 90
}
}

Loading…
Cancel
Save