Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose a high level API #28

Open
davesag opened this issue Sep 19, 2020 · 0 comments
Open

Compose a high level API #28

davesag opened this issue Sep 19, 2020 · 0 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request feature

Comments

@davesag
Copy link
Owner

davesag commented Sep 19, 2020

The low level API is problematic to say the least.

  1. potentially unstable due to being based on unofficial documentation, and trial and error,

  2. there's many endpoints that return overlapping or irrelevant, or even inaccurate data.

    For example

    • the car's id is a number that's longer than javascript's number object allows.
      The API itself recognises this and returns an idS field which is a string version of the `id.

    • It uses metric for temperature and imperial for distance (presumably because my GUI is set to Metric) but it would be nicer if distance was also reported in metric if the user's GUI settings are metric.

  3. the API does not prevent you from issuing commands to the car before it's been woken up

  4. the API does not prevent you from doing things like trying to open a sunroof on a car without a sunroof.

A better solution would be to spec out a preferred high-level API, say called 'simple-node-tesla' that uses this.

const Tesla = async (username, password) => {
  const token = password
    ? await api.oauth.({ username, password, clientId, clientSecret })
    : username

  return {
    loggedIn: Boolean(token),
    vehicles: () => {
      const { response: cars } = await api.vehicles.vehicles({ token })

      return cars.map(car => ({
        ...car,
        siblings: cars.filter(({ idS }) => idS !== car.idS),
        // then a handy set of functions
        // wrapped up with logic preventing commands when car is not awake,
        // and that manages things like refreshing tokens, and throttling api limits.
        // and which manages a cache of the state of the car
        wake: async () => api.vehicles.wake({ id: car.idS, token }),
        beep: async () => api.vehicles.beepHorn({ id: car.idS, token }),
        flash: async () => api.vehicles.flashLights({ id: car.idS, token }),
        serviceHistory: async () => api.vehicles. serviceHistory({ id: car.idS, token }),
       // etc
      }))
    }
  }
}

Then used like

const Tesla = require('simple-node-tesla')

const client = Tesla(username, password) // or token if you already have an access token
const[myCar] = await client.vehicles()
await myCar.wake()
const location = await myCar.location()
const serviceHistory = await myCar.serviceHistory()
await myCar.beep()
await myCar.flash()
@davesag davesag added documentation Improvements or additions to documentation enhancement New feature or request feature labels Sep 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request feature
Projects
None yet
Development

No branches or pull requests

1 participant