Search code examples
pythonandroidsharetaskerpydroid

Sharing information between Tasker and Pydroid


I've been trying to put together an app for identifiing images on my phone using pydroid and Tasker.

I use Tasker to take a screenshot every 5 seconds, save it and then display the most recent results.

In Pydroid, i open the screenshot, analize it and write the results to a text file, which is then accessed by Tasker.

Sometimes, when the file is opened by one of the apps and the other one tries to access it, my phone just crashes and reboots.

My question is, how can i detect in pydroid (or tasker) if the other app is using the file and wait until the file can be safely accessed?

Here is my Tasker task: %Enabled is controlled by other tasks

A1: Flash [
         Text: Enabled
         Continue Task Immediately: On
         Dismiss On Click: On ]
    
    A2: Variable Set [
         Name: %Enabled
         To: True
         Max Rounding Digits: 3
         Structure Output (JSON, etc): On ]
    
    A3: Take Screenshot [
         File: /storage/47BE-16F5/folder/screenshot.png ]
    
    A4: Read File [
         File: /storage/47BE-16F5/folder/text.txt
         To Var: %text
         Structure Output (JSON, etc): On ]
    
    A5: Flash [
         Text: %text
         Continue Task Immediately: On
         Dismiss On Click: On ]
    
    A6: Wait [
         MS: 0
         Seconds: 5
         Minutes: 0
         Hours: 0
         Days: 0 ]
    
    A7: Goto [
         Type: Action Number
         Number: 3 ]
        If [ %Enabled eq True ]

And here is the relevant python code:

from PIL import Image

screenshot = Image.open("/storage/47BE-16F5/folder/screenshot.png")
...
with open(TEXT_DIR, 'w', encoding="utf-8") as f:
    f.write(results)

I have tried to wrap the python code in a try-except loop, but i have no idea what the exception is exactly, because the phone just crashes every time before the error message gets printed out.

Also, when the relevant python code is replaced by constants, the phone doesn't crash at all anymore, so the issue must be related to file access.


Solution

  • Detection of modified files in Tasker doesn't work very well, after a while it doesn't work anymore, maybe because of system suspend or read/write permissions on the detect file

    A solution that would be better and with fewer flaws could perhaps be to use the 'am' command to send a broadcast/service to the tasker to execute certain tasks

    But Pydroid doesn't have this functionality

    So another alternative I found was to create an api for Tasker, send requests and get the data returned

    An example with FastAPI:

    import uvicorn
    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get('/event')
    def event(number: int):
        result = {
            'number': number
        }
        
        return result
    
    if __name__ == '__main__':
        uvicorn.run(app)
    

    and the action in Tasker to communicate with the application

    Task: HOME - .Test
    
    <Test>
    A1: Anchor
    
    A2: Variable Set [
         Name: %value
         To: 50
         Max Rounding Digits: 3
         Structure Output (JSON, etc): On ]
    
    A3: HTTP Request [
         Method: GET
         URL: http://localhost:8000/event
         Query Parameters: number:%value
         Timeout (Seconds): 30
         Structure Output (JSON, etc): On ]
    
    A4: Text/Image Dialog [
         Title: Result
         Text: Number: %http_data.number
         Close After (Seconds): 30 ]
    
    

    Although it works fine, it seems that after a while the Pydroid is disabled by the system and the API is turned off, but imagine this can be solved by messing with the battery and suspend settings