msupply-foundation / msupply-cold-chain

Android application for viewing and monitoring temperatures of fridges
GNU General Public License v3.0
3 stars 3 forks source link

Run actions as a service #268

Closed mark-prins closed 7 months ago

mark-prins commented 7 months ago

Fixes #144

Creates a scheduler service which is the run as a Foreground Service. This then replaces the infinite while loop which was running in order to download logs, update battery levels and send logs to the server.

The service calls a method within the application every minute, and the method then dispatches actions to download logs (via bluetooth), update battery levels and upload logs to the server. It also updates the taskbar status text.

The taskbar shows a small cold chain icon, and the notification text gives the current status of server integration:

notification

For the battery levels, the command to fetch battery values was running at the same time as the log downloading, i.e. not running mostly because it is prevented from running simultaneously. I've added a status check at the end of the temperature log download, which dispatches the battery level action only when no active downloads are happening.

The battery updates happen every 10 minutes, to reduce load on the sensor, so those requests are time throttled.

When a foreground notification is created, the app shows a badge by default - preferably the badge would indicate a breach event, but I have not implemented or investigated that yet.

I've also noticed that the status initially says that the integration is disabled - even though it isn't. that will need to be investigated too.

Have tested by switching apps - with the Cold Chain in the background, logs are still downloaded, and server integration happens.

There's also a a BootUpReceiver to check for ACTION_BOOT_COMPLETED events and start the service and the headlessJS task which is registered with the AppRegistry - in order to start up the process when the tablet is restarted.

One other task I haven't investigated is adding a button to the notification which allows a user to stop the service. Didn't want to overcook this PR, so have left that out too.

mark-prins commented 7 months ago

Running Locally on emulator I get this?

have tried on my emulator and it worked ok 🤷 There was a version where I caused this, but I didn't commit that!

mark-prins commented 7 months ago

After a device restart, the scheduler starts up fine - but the app does need to be run for the task to be registered in the AppRegistry, so logging won't begin until the cold chain app is started. after that, closing is fine. Screen timeout is also fine - the service continues to log temperatures after the screen turns off due to a timeout