MTrab / pyworxcloud

PyPI module for integrating with Worx Cloud devices
GNU General Public License v3.0
22 stars 20 forks source link

Question - how can i write current api data to a file #175

Closed MarkResting closed 9 months ago

MarkResting commented 1 year ago

Hi, I try to use the youre testfile with the setCallback function and it prints the new status very well. But I'm not able to revere to the variables within the registered function.

Can you give me a tip how i can do that?

Here is the function part of whot I'm trying to do:

def receive_data(
    name: str, device: DeviceHandler  # pylint: disable=unused-argument
) -> None:
    """Callback function when the MQTT broker sends new data."""

    print("Got data on MQTT from " + name)

    #logTimestamp = str(datetime.now())
    #logText = str(vars(cloud))
    landroidLog = open('landroidLog.txt','a')
    landroidLog.write(vars(cloud))

A big thx up front and kind regards, Mark

MTrab commented 12 months ago

What is not working for you? Without having tested, it looks about right to me.

MarkResting commented 11 months ago

I'm getting the error:

NameError: name 'cloud' is not defined

i think it's not difined in that registered function anymore,... so i don'tknow how to bring the data into a variable,...

sorry for these beginner questions.

Kind regards, Sebastian

MTrab commented 11 months ago

Guess you haven't logged in to the cloud then or haven't initialized the object. Hard to say with that small code snip

MarkResting commented 11 months ago

Sorry, here's the full code:

import asyncio
import datetime
import json
import time
from os import environ

from pyworxcloud import WorxCloud
from pyworxcloud.events import LandroidEvent
from pyworxcloud.utils import DeviceHandler

tz = datetime.datetime.now().astimezone().tzinfo.tzname(None)

async def main():
    loop = asyncio.get_running_loop()
    await async_worx()

async def async_worx():
    # Clear the screen for better visibility when debugging

    # Initialize the class and connect
    cloud = WorxCloud("mail", "Password", "worx")
    cloud.authenticate()
    cloud.connect()
    cloud.set_callback(LandroidEvent.DATA_RECEIVED, receive_data)

    ## Force an data update
    # device = cloud.devices[environ["DEV_NAME"]]
    # cloud.update(device.serial_number)

    print("Listening for new data")

    while 1:
        pass

    #logTimestamp = str(datetime.now())
    #logText = str(vars(cloud))
    landroidLog = open('landroidLog.txt','a')
    landroidLog.write(vars(cloud))
   landroidLog.write(vars(cloud))

    # Self explanatory - disconnect from the cloud
    cloud.disconnect()

def receive_data(
    name: str, device: DeviceHandler  # pylint: disable=unused-argument
) -> None:
    """Callback function when the MQTT broker sends new data."""

    print("Got data on MQTT from " + name)

    #logTimestamp = str(datetime.now())
    #logText = str(vars(cloud))
    landroidLog = open('landroidLog.txt','a')
    landroidLog.write(vars(cloud))

asyncio.run(main())
french162 commented 11 months ago

@MarkResting Please delete the email address and password from your comment and reset your password. Now it is possible for anyone to lock or control your Mower. And you haven't initialized the object cloud in the method receive_data. Probably you can use device.json_data, but i haven't test it...

MarkResting commented 10 months ago

d you haven't initialized the object cloud in the method receive_data. Probably you can use device.json_data, but i haven't test it...

OMG that was really stupid! THX for that remark. I will try youre hint.

MarkResting commented 5 months ago

Sorry to bother you again with this, but I'm still to stupid to get it done :-(

How does the "receive_data" function needs to look like? how can i reviere there to my "cloud" object.

i tried to define it global, but that doesn't help. or did i a mistake in the assignment:

"cloud.set_callback(LandroidEvent.DATA_RECEIVED, receive_data)"

Any help would be much appreciated

MTrab commented 5 months ago

Did you look at https://github.com/MTrab/pyworxcloud/blob/master/test_async.py ??

MarkResting commented 5 months ago

Yes (copied from there) The only missing part is to print the received data there,...

MTrab commented 5 months ago

That's plain normal Python

MarkResting commented 5 months ago

yeah,.... i an really beginner,...

Sorry,...

just need to know how to iterate through "device"