dudanov / pyftms

PyFTMS - Python Fitness Machine Service client library.
Apache License 2.0
6 stars 1 forks source link

Example code? #4

Closed cpwardell closed 3 months ago

cpwardell commented 3 months ago

Thanks for writing this library, I think it's going to be incredibly useful. I'm really interested in using this package to communicate with my treadmill, but I'm having trouble getting started.

Is there any chance you could supply a few bits of example code? The examples in the pycycling repo you forked are great, but I'd have to do a lot of rewriting to make pycycling work with a treadmill. I've also looked at the code in your hassio-ftms repo, but it's difficult to untangle the pyftms code from the HACS code.

A simple script that connects to a device with a specified UUID and then reports the various values (speed/distance/etc), and a primer on how to set some values would be incredibly useful to users like me.

dudanov commented 3 months ago

Hi.

I was thinking about writing a couple of examples, but since the library is very simple, I decided at that time that automatic generation of API documentation using pdoc would be enough.

I will write a couple of examples and place them in the repository.

For now, a simple example can be found in the main .py file.

P.S.: I looked at your scientific works and research. It is impressive. Thanks.

cpwardell commented 3 months ago

I really appreciate both the example and the compliment. I'm a researcher, so I write a lot of code but I'm definitely not a software engineer like yourself, so I'm equally impressed by your work.

I will either close this issue or add another comment once I've had some time to look at this in the next couple of days at home.

Thanks again for your help and your open-source code!

dudanov commented 3 months ago

Thank you. It's always nice to see your work useful to people.

Let's close this issue after you find time and try the example.

cpwardell commented 3 months ago

I tested the example code you wrote and it works great once I hacked my way around a bug that might be specific to the machine I have. It apparently doesn't produce any AdvertisementData. In the get_machine_type_from_service_data() function, when the line below is called, the result is "None" which raises a "NotFitnessMachineError".

data = adv_data.service_data.get(FITNESS_MACHINE_SERVICE_UUID)

This is strange, because when I run bleakscanner I find my device with advertisementdata that has a UUID that matches your variable (FITNESS_MACHINE_SERVICE_UUID = "00001826-0000-1000-8000-00805f9b34fb")

Bleakscanner results:

BLEDevice(A0:BB:3E:40:8B:96, MX-TM 8B96) AdvertisementData(local_name='MX-TM 8B96', manufacturer_data={13: b'\xa0\xbb>@\x8b\x96'}, service_data={'00001826-0000-1000-8000-00805f9b34fb': b'\x01\x01\x00'}, service_uuids=['00001826-0000-1000-8000-00805f9b34fb', '00001816-0000-1000-8000-00805f9b34fb'], rssi=-59)

I also got a list of the services and it's clearly there:

00001800-0000-1000-8000-00805f9b34fb (Handle: 1): Generic Access Profile 00001801-0000-1000-8000-00805f9b34fb (Handle: 8): Generic Attribute Profile 0000180a-0000-1000-8000-00805f9b34fb (Handle: 12): Device Information 0000181c-0000-1000-8000-00805f9b34fb (Handle: 25): User Data 00001826-0000-1000-8000-00805f9b34fb (Handle: 40): Fitness Machine 00010203-0405-0607-0809-0a0b0c0d1912 (Handle: 91): Unknown 0000fff0-0000-1000-8000-00805f9b34fb (Handle: 95): Vendor specific 00001816-0000-1000-8000-00805f9b34fb (Handle: 112): Cycling Speed and Cadence

For reference, the treadmill is a Matrix T50 with an XR display, the simplest one.

I got around this by commenting out some code and forcing the get_machine() function to always return Treadmill. Once I did that, I can both get properties when it's running and send commands, so I'm happy.

dudanov commented 3 months ago

It seems that your device can send advertising messages without service data. I have made a fix, please test version v0.2.23. Now the scanner will ignore advertising packets without FTMS service data.

cpwardell commented 3 months ago

The new version works perfectly with no further edits from me. I'll close this issue. Thanks again both for your code and your help, I really appreciate it.