nightui/src/modules/vitalsd/vitalsd.py

75 lines
2 KiB
Python

import subprocess
import time
import requests
import sys
from flask import jsonify
heartrateCmd = ['itctl', 'get', 'heart']
stepsCmd = ['itctl', 'get', 'steps']
batteryCmd = ['itctl', 'get', 'battery']
def getHeartrate():
proc = subprocess.Popen(heartrateCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
o, e = proc.communicate()
if(proc.returncode != 0):
return -1 # Inform that cyberware is inoperative
o = o.decode("utf-8")
bpm = o.split(' ')
return bpm[0]
def getSteps():
proc = subprocess.Popen(stepsCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
o, e = proc.communicate()
if(proc.returncode != 0):
return -1 # Inform that cyberware is inoperative
o = o.decode("utf-8")
steps = o.split(' ')
return steps[0]
def getBattery():
proc = subprocess.Popen(batteryCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
o, e = proc.communicate()
if(proc.returncode != 0):
return -1 # Inform that cyberware is inoperative
o = o.decode("utf-8")
battery = o.split('%')
return battery[0]
heartrate = 0
steps = 0
battery = 0
urlBase = 'http://localhost:5000'
urlHeartrate = urlBase + '/api/vitals/heartrate'
urlSteps = urlBase + '/api/fitness/steps'
urlAddCyberware = urlBase + '/api/cyberware/add'
#urlBattery = urlBase + # Cyberware management not yet implemented
# Add to Cyberware and get UUID
uuidRequest = requests.post(urlAddCyberware, json={ 'name': "PineTime", 'hotpluggable': True, 'canSet': [ '/api/vitals/heartrate', '/api/fitness/steps' ] })
if(uuidRequest.status_code == 200):
uuid = uuidRequest.json()[0]['uuid']
print(uuid)
else:
sys.exit("Failed to get UUID")
while True:
try:
heartrate = getHeartrate()
steps = getSteps()
#battery = getBattery()
heartrateRequest = requests.post(urlHeartrate, json={ 'heartrate': heartrate, 'uuid': uuid } )
stepsRequest = requests.post(urlSteps, json={ 'steps': steps, 'uuid': uuid })
except:
print("An exception occured. TODO: Exception report to frontend.")
time.sleep(1)
# Disconnect Cyberware
requests.post(urlRemoveCyberware, json={ 'uuid': uuid })