andyvorld / LGSTrayBattery

A tray app used to track battery levels of wireless Logitech mouse.
GNU General Public License v3.0
369 stars 30 forks source link

[FEATURE REQUEST] Logging battery level to file on disk #107

Open propiro opened 6 months ago

propiro commented 6 months ago

Hello, I'd like to request a feature to log battery level (be it whole percentages, or fractions of percentages) to a file, with date. I'd like to monitor how long my mouse lasts on one battery charge, including how much I use it (since mine lasts 4-5 days, and friends have theirs for 2-3 weeks, ALLEGEDLY).

andyvorld commented 6 months ago

I don't think this is that core of a feature to be implemented in this program, given that there is the http server that can act as an endpoint for the logs.

For example a simple python script can just poll the endpoint and write it out to a file like the following,

import time
import urllib.request
import xml.etree.ElementTree as ET

LOG_FILE = './device.log'

SERVER = 'localhost:12321'
DEVICE_ID = 'dev00000000'

while True:
    with open(LOG_FILE, '+a') as file:
        f = urllib.request.urlopen(f'http://{SERVER}/device/{DEVICE_ID}')
        root = ET.fromstring(f.read().decode('utf-8'))

        last_update = root.find('last_update').text
        percent = root.find('battery_percent').text
        file.write(f'{last_update}, {percent}\n')
        file.flush()

    time.sleep(60)

Ill leave this open for now to see if there is any need for something like this. As I would think a customised script like above is more useful than a predetermined on by me.