ph4r05 / ph4-walkingpad

WalkingPad python controller
MIT License
72 stars 17 forks source link

Works for a few seconds only #16

Open rolandmas opened 2 months ago

rolandmas commented 2 months ago

Hi, I have a KingSmith WalkingPad A1 Pro from which I'd like to collect stats during use. The Bluetooth connection seems to work fine (as seen in bluetoothctl) but the ph4-walkingpad-ctl script only retrieves data for a few seconds. I managed to get a few tens of lines of json with a ph4-walkingpad-ctl -a 85:20:00:00:34:52 --stats 750 --json-file ~/walking.json command, then I got an exception that I didn't copy but that looked very much like the second one mentioned in #8:

[Removed after inserting the real backtrace in a comment later]

Then I can no longer get any data out of the device. I had to run bluezctl to disconnect it before ph4-walkingpad-ctl would connect again, but even then it never gets any data. status returns None, no matter how many times I run ask_stats before. (start or switch_mode don't have any effect either, but I'm really only interested in the stats.)

Running with --debug yields a huge amount of 2024-07-27 21:19:55 mammamir ph4_walkingpad.main[57264] INFO Error in ask stats: Task <Task pending name='Task-9' coro=<WalkingPadControl.stats_fetcher() running at /home/blagandzig/walkingpad/lib/python3.11/site-packages/ph4_walkingpad/main.py:171> cb=[_chain_future.<locals>._call_set_state() at /usr/lib/python3.11/asyncio/futures.py:394]> got Future <Future pending created at /usr/lib/python3.11/asyncio/base_events.py:427> attached to a different loop lines.

I'm runnning a Debian 12 system with Python 3.11, and I created a venv so that I could pip install -U ph4-walkingpad.

rolandmas commented 2 months ago

After switching the device off and on and running systemctl restart bluetooth.service, I managed to get some data again. Here's the run this time:

$ ph4-walkingpad-ctl -a 85:20:00:00:34:52 --stats 1000 --json-file ~/walking.json 
WalkingPadCurStatus(dist=0.0, time=0, steps=0, speed=0.0, state=0, mode=1, app_speed=0.23333333333333334, button=1, rest=0000)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    WalkingPad controller

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
$> 2024-07-27 21:30:56 mammamir ph4_walkingpad.pad[67002] ERROR Exception in processing msg [f8, a2, 00, 00, 01, 00, 00, 00, 00, 00, 00]: bytearray index out of range
Traceback (most recent call last):
  File "/home/blagandzig/walkingpad/lib/python3.11/site-packages/ph4_walkingpad/pad.py", line 257, in notif_handler
    m = WalkingPadCurStatus.from_data(data)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blagandzig/walkingpad/lib/python3.11/site-packages/ph4_walkingpad/pad.py", line 179, in from_data
    m.load_from(cmd)
  File "/home/blagandzig/walkingpad/lib/python3.11/site-packages/ph4_walkingpad/pad.py", line 165, in load_from
    self.steps = WalkingPad.byte2int(cmd[11:])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blagandzig/walkingpad/lib/python3.11/site-packages/ph4_walkingpad/pad.py", line 137, in byte2int
    return sum([(val[i] << (8 * (width - 1 - i))) for i in range(width)])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blagandzig/walkingpad/lib/python3.11/site-packages/ph4_walkingpad/pad.py", line 137, in <listcomp>
    return sum([(val[i] << (8 * (width - 1 - i))) for i in range(width)])
                 ~~~^^^
IndexError: bytearray index out of range

It takes about 10 seconds.

Switching the thing off and on, and restarting bluetooth again, I get another error:

$ ph4-walkingpad-ctl -a 85:20:00:00:34:52 --stats 1000 --json-file ~/walking.json 
WalkingPadCurStatus(dist=0.0, time=0, steps=0, speed=0.0, state=0, mode=1, app_speed=0.23333333333333334, button=1, rest=0000)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    WalkingPad controller

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
$> 2024-07-27 21:34:12 mammamir ph4_walkingpad.pad[68372] ERROR Exception in processing msg [f8, a2, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 00, 00, 07, 00, 00]: bytearray index out of range
Traceback (most recent call last):
  File "/home/blagandzig/walkingpad/lib/python3.11/site-packages/ph4_walkingpad/pad.py", line 263, in notif_handler
    logger_fnc('Status: %s' % (m,))
                               ^
  File "/home/blagandzig/walkingpad/lib/python3.11/site-packages/ph4_walkingpad/pad.py", line 187, in __str__
    binascii.hexlify(bytearray([self.raw[15], self.raw[17]])).decode('utf8'))
                                              ~~~~~~~~^^^^
IndexError: bytearray index out of range
rolandmas commented 2 months ago

Using --ignore-bad-packets helps somewhat: I get data for longer (about one minute), then no more. No exception is logged, but no more data is collected either.