data_read #1

Merged
accidentallycompetent merged 7 commits from data_read into main 2023-11-14 22:53:03 +00:00
8 changed files with 64 additions and 63 deletions
Showing only changes of commit eb4a70a7b1 - Show all commits

5
.gitignore vendored
View File

@ -158,7 +158,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # 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 # 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. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
bluewind
bluewind/*

11
bluewind/__init__.py Normal file
View File

@ -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

View File

@ -80,6 +80,46 @@ class Headwind:
return False return False
else: else:
return False 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): async def writeOff(self):
try: try:

View File

@ -1,29 +1,22 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from flask import Flask from bluewind import app, fan
from headwind import Headwind as Headwind
app = Flask(__name__)
app.config.from_prefixed_env()
@app.route("/on", methods=["POST"]) @app.route("/on", methods=["POST"])
async def on(): async def setOn():
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.writeOn() fan_status = await fan.writeOn()
if not fan_status: if not fan_status:
return "Failed to turn headwind on", 503 return "Failed to turn headwind on", 503
return "Turning headwind on", 200 return "Turning headwind on", 200
@app.route("/sleep", methods=["POST"]) @app.route("/sleep", methods=["POST"])
async def sleep(): async def setSleep():
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.writeSleep() fan_status = await fan.writeSleep()
if not fan_status: if not fan_status:
return "Failed to put headwind to sleep", 503 return "Failed to put headwind to sleep", 503
return "Putting headwind to sleep", 200 return "Putting headwind to sleep", 200
@app.route("/speed/<int:speed>", methods=["POST"]) @app.route("/speed/<int:speed>", methods=["POST"])
async def speed(speed): async def setSpeed(speed):
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.writeSpeed(speed) fan_status = await fan.writeSpeed(speed)
if not fan_status: if not fan_status:
return f"Failed to set headwind speed to {speed}", 503 return f"Failed to set headwind speed to {speed}", 503
@ -31,21 +24,18 @@ async def speed(speed):
@app.route("/speed", methods=["GET"]) @app.route("/speed", methods=["GET"])
async def getSpeed(): async def getSpeed():
fan = Headwind(app, app.config["ADDRESS"])
speed = await fan.readSpeed() speed = await fan.readSpeed()
return f"{speed}", 200 return f"{speed}", 200
@app.route("/hr", methods=["POST"]) @app.route("/hr", methods=["POST"])
async def writeHr(): async def setHr():
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.writeHr() fan_status = await fan.writeHr()
if not fan_status: if not fan_status:
return "Failed to set headwind to HR mode", 503 return "Failed to set headwind to HR mode", 503
return "Setting headwind to HR mode", 200 return "Setting headwind to HR mode", 200
@app.route("/off", methods=["POST"]) @app.route("/off", methods=["POST"])
async def writeOff(): async def setOff():
fan = Headwind(app, app.config["ADDRESS"])
fan_status = await fan.off() fan_status = await fan.off()
if not fan_status: if not fan_status:
return "Failed to turn headwind off", 503 return "Failed to turn headwind off", 503

5
bootstrap.sh Normal file
View File

@ -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

View File

@ -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

32
main.py
View File

@ -1,32 +1,2 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import asyncio from bluewind import bluewind
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()