Skip to content

Update app.py #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import configparser
import sys
import json
import time

_config = configparser.ConfigParser()
DEFAULT_CONFIG_FILES = []
Expand All @@ -25,6 +26,21 @@

app = Flask(__name__)

# Signal decoder, exactly the same as the example in C
def decode(c):
c = c ^ 0x55
m = c & 0x0f
e = (c & 0x70) >> 4
if e > 0:
m |= 0x10
m <<= 4
m |= 0x08
if e > 1:
m <<= e-1
if c < 0x80:
m = -m
return m

@app.route("/")
def get_code():
"""
Expand Down Expand Up @@ -90,4 +106,44 @@ def get_token():

#DEBUG: print(r_getdevice,file=sys.stderr)

return r_getdevice
#return r_getdevice

# List all samples of stethoscope
#startdate=2024/2/29
#enddate=1 minute ago
offset = '00' #x next available rows
payload = 'action=list'+"&"+ \
'startdate=1709136000'+"&"+ \
'enddate='+str(int(time.time())-60)+"&"+ \
'offset='+offset

r_listdevice = requests.post('https://wbsapi.withings.net/v2/stetho',
headers=headers,
params=payload).json()

#return r_listdevice

# Get list of all signal id
#sample_list = json.loads(r_listdevice)
signalid_list = [series['signalid'] for series in r_listdevice['body']['series']]

# Download all samples
for signalid in signalid_list:
payload = 'action=get'+"&"+ \
'signalid='+str(signalid)

r_getsample = requests.post('https://wbsapi.withings.net/v2/stetho',
headers=headers,
params=payload).json()
# Decode samples
signal = r_getsample['body']['signal']
for i in range(len(signal)):
if signal[i] <0:
signal[i] += 256
signal[i] = decode(signal[i])

filename = f"{signalid}.json"
with open(filename, 'w') as file:
file.write(json.dumps(r_getsample))

return "Done!"