From a8fbb578e860a36470095a213ab2990963336735 Mon Sep 17 00:00:00 2001 From: Innovation Date: Sat, 8 Feb 2025 06:14:18 +0000 Subject: [PATCH] Begin reworking vitalsd to not be crap. An abstract class is now used for general Cyberware. New Aggregator Reciever Cyberware is being worked on to be used as a general input (specifically for GadgetBridge). Infinitime needs updated. --- src/modules/vitalsd/aggregatorRecv.py | 42 ++++++++++++++++++++ src/modules/vitalsd/config.ini | 23 +++++++++++ src/modules/vitalsd/cyberwareAbs.py | 56 +++++++++++++++++++++++++++ src/modules/vitalsd/infinitime.py | 8 ++++ src/modules/vitalsd/vitalsd.py | 45 +++++++++++++++++---- 5 files changed, 166 insertions(+), 8 deletions(-) create mode 100644 src/modules/vitalsd/aggregatorRecv.py create mode 100644 src/modules/vitalsd/config.ini create mode 100644 src/modules/vitalsd/cyberwareAbs.py diff --git a/src/modules/vitalsd/aggregatorRecv.py b/src/modules/vitalsd/aggregatorRecv.py new file mode 100644 index 0000000..9b771ef --- /dev/null +++ b/src/modules/vitalsd/aggregatorRecv.py @@ -0,0 +1,42 @@ +import subprocess +import requests +import sys +from flask import jsonify +from cyberwareAbs import Cyberware + +class AggregatorRecv(Cyberware): + uuid = None + + def __init__(self): + print("GadgetBridge Aggregator Reciever started") + print("[WARN] GadgetBridge Aggregator not implemented") + +# def sendValue(self, url, jsonRequest): +# try: +# oldJson = json.loads(jsonRequest) +# uuidJson = {'uuid': self.uuid} +# oldJson.update(uuidJson) +# jsonWithUuid = json.dumps(oldJson) +# +# request = requests.post(url, json=jsonWithUuid) +# +# except: +# print(self.apiFailMsg) +# return 2 # 2 = No API contact. +# +# if request.status_code > 399: +# return 3 # 3 = API refused request. +# +# if request.status_code > 299: +# return 4 # 4 = API encountered an internal error. +# +# return 0 # 0 = Success + + def sendAll(self): + o=0 + + def connectCyberware(self): + o=0 + + def disconnectCyberware(self): + o=0 diff --git a/src/modules/vitalsd/config.ini b/src/modules/vitalsd/config.ini new file mode 100644 index 0000000..98b4f55 --- /dev/null +++ b/src/modules/vitalsd/config.ini @@ -0,0 +1,23 @@ +[enabled] +Infinitime = false +AggregatorRecv = false + +; You should not edit any capabilities variables unless you know what you're doing. +[infinitime] +capabilities = [ + "vitals/heartrate", + "vitals/steps", + "cyberware/battery" + ] +priority = 999 + +[aggregatorRecv] +capabilities = [ + "vitals/heartrate", + "vitals/steps", + "battery", + "vitals/bodytemp", + "vitals/oxygen", + "cyberware/battery" + ] +priority = 0 diff --git a/src/modules/vitalsd/cyberwareAbs.py b/src/modules/vitalsd/cyberwareAbs.py new file mode 100644 index 0000000..c278fc8 --- /dev/null +++ b/src/modules/vitalsd/cyberwareAbs.py @@ -0,0 +1,56 @@ +import subprocess +import requests +import sys +from flask import jsonify +from abc import ABC,abstractmethod + +class Cyberware(ABC): + # I'll expand this as necessary + urlBase = "http://localhost:5000/" + ENDPOINTS = { + "heartrate":urlBase+"api/vitals/heartrate", + "steps":urlBase+"api/vitals/steps", + "bodytemp":urlBase+"api/vitals/bodytemp", + "oxygen":urlBase+"api/vitals/oxygen", + "battery":urlBase+"api/vitals/battery", + "add":urlBase+"api/cyberware/add", + "remove":urlBase+"api/cyberware/remove" + } + + def sendValue(self, url, jsonRequest): + try: + oldJson = json.loads(jsonRequest) + uuidJson = {'uuid': self.uuid} + oldJson.update(uuidJson) + jsonWithUuid = json.dumps(oldJson) + + request = requests.post(url, json=jsonWithUuid) + + except: + print(self.apiFailMsg) + return 2 # 2 = No API contact. + + if request.status_code > 399: + return 3 # 3 = API refused request. + + if request.status_code > 299: + return 4 # 4 = API encountered an internal error. + + return 0 # 0 = Success + + @property + @abstractmethod + def uuid(self): + pass + + @abstractmethod + def sendAll(self): + pass + + @abstractmethod + def connectCyberware(self): + pass + + @abstractmethod + def disconnectCyberware(self): + pass diff --git a/src/modules/vitalsd/infinitime.py b/src/modules/vitalsd/infinitime.py index 4673be2..009e9d7 100644 --- a/src/modules/vitalsd/infinitime.py +++ b/src/modules/vitalsd/infinitime.py @@ -1,3 +1,6 @@ +# TODO: Rewrite this file to use the CyberwareAbs abstract class. +# works as-is but man do I have nightmares knowing this file looks like this. + import subprocess import requests import sys @@ -160,6 +163,11 @@ class Infinitime: print(self.apiFailMsg) self.uuid = None + def sendAll(self): + self.sendHeartrate() + self.sendSteps() + self.sendBattery() + # while True: # try: # heartrate = getHeartrate() diff --git a/src/modules/vitalsd/vitalsd.py b/src/modules/vitalsd/vitalsd.py index 3abaa81..434c8d3 100644 --- a/src/modules/vitalsd/vitalsd.py +++ b/src/modules/vitalsd/vitalsd.py @@ -1,25 +1,54 @@ from infinitime import Infinitime +from aggregatorRecv import AggregatorRecv import time +import configparser +import json -pinetimeEnabled = True +config = configparser.ConfigParser() +config.read('config.ini') + +pinetimeEnabled = config.getboolean('enabled','Infinitime') +aggregatorEnabled = config.getboolean('enabled', 'AggregatorRecv') + +enabled = [] if(pinetimeEnabled): pinetime = Infinitime() + capabilities = json.loads(config.get('infinitime','capabilities')) + enabled.append(pinetime) pinetime.connectCyberware() +if(aggregatorEnabled): + aggregator = AggregatorRecv() + capabilities = json.loads(config.get('aggregatorRecv','capabilities')) + enabled.append(aggregator) + aggregator.connectCyberware() + continueFlag = True +#while continueFlag: +# try: +# if(pinetimeEnabled): +# pinetime.sendHeartrate() +# pinetime.sendSteps() +# pinetime.sendHeartrate() +# +# time.sleep(1) +# except KeyboardInterrupt: +# print("Stopping vitalsd") +# continueFlag = False +# +#if(pinetimeEnabled): +# pinetime.disconnectCyberware() + while continueFlag: try: - if(pinetimeEnabled): - pinetime.sendHeartrate() - pinetime.sendSteps() - pinetime.sendHeartrate() - + for c in enabled: + c.sendAll() time.sleep(1) except KeyboardInterrupt: print("Stopping vitalsd") continueFlag = False -if(pinetimeEnabled): - pinetime.disconnectCyberware() +for c in enabled: + c.disconnectCyberware()