stevieb has asked for the wisdom of the Perl Monks concerning the following question:
Greetings and good day my fellow esteemed Monks.
I come today with a Dancer2 and simultaneous asynchronous request question(s). I have a microcontroller (Wemos D1 mini with an ESP8266 chip) in my garage that presents data about my Tesla vehicle state on an OLED screen and a strip of LEDs. Here's how it works:
- Microcontroller calls REST API on a Raspberry Pi to get vehicle data every two seconds while there's motion in garage
- Raspberry Pi fetches vehicle data from Tesla API
- Raspberry Pi sends updated data back to microcontroller
- Microcontroller presents status to user
What I want to have happen, is something like this:
- Microcontroller requests data from Raspberry Pi every two seconds if there is motion in garage
- Raspberry Pi sends an async request to Tesla API for updated data, but while waiting, sends back last known data (I'll set an expiry)
- Microcontroller presents status to user
Essentially, I want the Pi to asynchronously request data from Tesla's API about my vehicle, respond with the last (global) poll data before the current (Tesla API) request is done (effectively terminating the HTTP connection), then update the global variable that contains the vehicle data when the async Tesla API call is complete for the next microcontroller call.
How can I respond back to the microcontroller with previous data before the current Tesla API call is completed?
Also, how can I ensure that if the first async Tesla API call is done before spawning off a second one (ie. if two seconds have elapsed and a new microcontroller check comes in and the async process isn't finished from last time), respond with the existing data without respawning a new async process?