From eb4a70a7b157ed891a0bd9d3c7495d108af7c3fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Fr=C4=85tczak?= Date: Mon, 13 Nov 2023 23:15:12 +0100 Subject: [PATCH] refactor the application structure --- .gitignore | 5 +-- bluewind/__init__.py | 11 ++++++ {headwind => bluewind/headwind}/__init__.py | 40 +++++++++++++++++++++ {headwind => bluewind/headwind}/spec.py | 0 api.py => bluewind/views.py | 22 ++++-------- bootstrap.sh | 5 +++ config/__init__.py | 12 ------- main.py | 32 +---------------- 8 files changed, 64 insertions(+), 63 deletions(-) create mode 100644 bluewind/__init__.py rename {headwind => bluewind/headwind}/__init__.py (63%) rename {headwind => bluewind/headwind}/spec.py (100%) rename api.py => bluewind/views.py (67%) create mode 100644 bootstrap.sh delete mode 100644 config/__init__.py diff --git a/.gitignore b/.gitignore index a6b7ba4..5e49b42 100644 --- a/.gitignore +++ b/.gitignore @@ -158,7 +158,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ - -bluewind -bluewind/* \ No newline at end of file +#.idea/ \ No newline at end of file diff --git a/bluewind/__init__.py b/bluewind/__init__.py new file mode 100644 index 0000000..afe7e90 --- /dev/null +++ b/bluewind/__init__.py @@ -0,0 +1,11 @@ +# Initialise flask +from flask import Flask +app = Flask(__name__) +app.config.from_prefixed_env() + +# Initialise the bluetooth stack +from bluewind import headwind +fan = headwind.Headwind(app, app.config["ADDRESS"]) + +# Load the views +from bluewind import views \ No newline at end of file diff --git a/headwind/__init__.py b/bluewind/headwind/__init__.py similarity index 63% rename from headwind/__init__.py rename to bluewind/headwind/__init__.py index c5778df..4641273 100644 --- a/headwind/__init__.py +++ b/bluewind/headwind/__init__.py @@ -80,6 +80,46 @@ class Headwind: return False else: return False + + async def writeIncreaseSpeed(self): + try: + async with self.fanClient as client: + currentSpeed = await client.read_gatt_char(CHARACTERISTIC)[2] + newSpeed = 0 + if currentSpeed < 25: + newSpeed = 25 + elif currentSpeed < 50: + newSpeed = 50 + elif currentSpeed < 75: + newSpeed = 75 + elif currentSpeed < 100: + newSpeed = 100 + value = [0x2, newSpeed] + await client.write_gatt_char(CHARACTERISTIC, value) + return True + except Exception as e: + return False + + async def writeDecreaseSpeed(self): + try: + async with self.fanClient as client: + currentSpeed = await client.read_gatt_char(CHARACTERISTIC)[2] + newSpeed = 0 + if currentSpeed == 100: + newSpeed = 75 + elif currentSpeed >= 75: + newSpeed = 50 + elif currentSpeed >= 50: + newSpeed = 25 + elif currentSpeed > 25: + newSpeed = 25 + elif currentSpeed <= 25: + return True + value = [0x2, newSpeed] + await client.write_gatt_char(CHARACTERISTIC, value) + return True + except Exception as e: + return False async def writeOff(self): try: diff --git a/headwind/spec.py b/bluewind/headwind/spec.py similarity index 100% rename from headwind/spec.py rename to bluewind/headwind/spec.py diff --git a/api.py b/bluewind/views.py similarity index 67% rename from api.py rename to bluewind/views.py index c239878..b18263e 100755 --- a/api.py +++ b/bluewind/views.py @@ -1,29 +1,22 @@ #!/usr/bin/env python3 -from flask import Flask -from headwind import Headwind as Headwind - -app = Flask(__name__) -app.config.from_prefixed_env() +from bluewind import app, fan @app.route("/on", methods=["POST"]) -async def on(): - fan = Headwind(app, app.config["ADDRESS"]) +async def setOn(): fan_status = await fan.writeOn() if not fan_status: return "Failed to turn headwind on", 503 return "Turning headwind on", 200 @app.route("/sleep", methods=["POST"]) -async def sleep(): - fan = Headwind(app, app.config["ADDRESS"]) +async def setSleep(): fan_status = await fan.writeSleep() if not fan_status: return "Failed to put headwind to sleep", 503 return "Putting headwind to sleep", 200 @app.route("/speed/", methods=["POST"]) -async def speed(speed): - fan = Headwind(app, app.config["ADDRESS"]) +async def setSpeed(speed): fan_status = await fan.writeSpeed(speed) if not fan_status: return f"Failed to set headwind speed to {speed}", 503 @@ -31,21 +24,18 @@ async def speed(speed): @app.route("/speed", methods=["GET"]) async def getSpeed(): - fan = Headwind(app, app.config["ADDRESS"]) speed = await fan.readSpeed() return f"{speed}", 200 @app.route("/hr", methods=["POST"]) -async def writeHr(): - fan = Headwind(app, app.config["ADDRESS"]) +async def setHr(): fan_status = await fan.writeHr() if not fan_status: return "Failed to set headwind to HR mode", 503 return "Setting headwind to HR mode", 200 @app.route("/off", methods=["POST"]) -async def writeOff(): - fan = Headwind(app, app.config["ADDRESS"]) +async def setOff(): fan_status = await fan.off() if not fan_status: return "Failed to turn headwind off", 503 diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100644 index 0000000..cc759ad --- /dev/null +++ b/bootstrap.sh @@ -0,0 +1,5 @@ +#!/bin/bash +python3 -m venv .venv +source .venv/bin/activate +python3 -m pip install --upgrade pip +pip3 install -r requirements.txt \ No newline at end of file diff --git a/config/__init__.py b/config/__init__.py deleted file mode 100644 index cee1e6d..0000000 --- a/config/__init__.py +++ /dev/null @@ -1,12 +0,0 @@ -class Configurinator: - address = "" - cmd = "" - speed = "" - - def __init__(self): - pass - - def load_config(self, address, cmd, speed): - self.address = address - self.cmd = cmd - self.speed = speed diff --git a/main.py b/main.py index a0ad445..eaff4ae 100755 --- a/main.py +++ b/main.py @@ -1,32 +1,2 @@ #!/usr/bin/env python3 -import asyncio -import click -from config import Configurinator as Config -from headwind import Headwind as Headwind - -@click.command() -@click.option('--address', default=None, help='headwind mac address') -@click.option('--cmd', default=None, help='command to send') -@click.option('--speed', default=1, help='manual speed value, 1 to 100') -def main(address, cmd, speed): - conf = Config() - conf.load_config(address, cmd, speed) - asyncio.run(bluewind(conf)) - -async def bluewind(conf): - fan = Headwind(conf.address) - match conf.cmd: - case 'on': - print("turning fan on") - await fan.on() - case 'sleep': - print("putting fan to sleep") - await fan.sleep() - case 'manual': - print("setting fan speed") - await fan.speed(conf.speed) - print('stuff') - - -if __name__ == "__main__": - main() \ No newline at end of file +from bluewind import bluewind \ No newline at end of file