Compare commits

...

4 Commits

Author SHA1 Message Date
Tomasz Frątczak c04197e7ae remove obsolete prints 2023-11-18 01:11:53 +01:00
Tomasz Frątczak d6a111ede7 debugging 503 errors after idle time 2023-11-16 23:11:02 +01:00
Tomasz Frątczak 28a8c01a5a fix host flag and simplify the headwind constructor 2023-11-15 22:16:16 +01:00
Tomasz Frątczak 71ab300339 add basic Dockerfile 2023-11-14 23:56:07 +01:00
6 changed files with 29 additions and 26 deletions

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM --platform=$BUILDPLATFORM python:3.10-alpine
WORKDIR /app
COPY requirements.txt /app
RUN pip3 install -r requirements.txt
COPY . /app
CMD ["python", "main.py"]

View File

@ -1,12 +1,11 @@
# Initialise flask
from flask import Flask
app = Flask(__name__)
app.config['SERVER_NAME'] = '0.0.0.0:5000'
app.config.from_prefixed_env()
# Initialise the bluetooth stack
from bluewind import headwind
fan = headwind.Headwind(app, app.config["ADDRESS"])
fan = headwind.Headwind(app.config["ADDRESS"])
# Load the views
from bluewind import views

View File

@ -15,15 +15,12 @@ CHARACTERISTIC = "a026e038-0a7d-4ab3-97fa-f1500f9feb8b"
# > bytearray(b'\xfd\x01\x00\x01')
class Headwind:
fanClient = None
flaskApp = None
def __init__(self, flaskApp, address):
self.flaskApp = flaskApp
self.fanClient = BleakClient(address)
def __init__(self, address):
self.address = address
async def readSpeed(self):
try:
async with self.fanClient as client:
async with BleakClient(self.address) as client:
result = await client.read_gatt_char(CHARACTERISTIC)
return result[2]
except Exception:
@ -31,7 +28,7 @@ class Headwind:
async def readMode(self):
try:
async with self.fanClient as client:
async with BleakClient(self.address) as client:
result = await client.read_gatt_char(CHARACTERISTIC)
return result[3] # Return state code, needs to figure out what each code means
except Exception:
@ -39,32 +36,32 @@ class Headwind:
async def writeSleep(self):
try:
async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, SLEEP)
async with BleakClient(self.address) as client:
await client.write_gatt_char(CHARACTERISTIC, SLEEP, response=False)
return True
except Exception as e:
return False
async def writeOn(self):
try:
async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, ON)
async with BleakClient(self.address) as client:
await client.write_gatt_char(CHARACTERISTIC, ON, response=False)
return True
except Exception as e:
return False
async def writeSpeedMode(self):
try:
async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, SPD)
async with BleakClient(self.address) as client:
await client.write_gatt_char(CHARACTERISTIC, SPD, response=False)
return True
except Exception as e:
return False
async def writeHrMode(self):
try:
async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, HR)
async with BleakClient(self.address) as client:
await client.write_gatt_char(CHARACTERISTIC, HR, response=False)
return True
except Exception as e:
return False
@ -73,8 +70,8 @@ class Headwind:
if speed > 0:
value = [0x2, speed]
try:
async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, value)
async with BleakClient(self.address) as client:
await client.write_gatt_char(CHARACTERISTIC, value, response=False)
return True
except Exception as e:
return False
@ -82,8 +79,8 @@ class Headwind:
async def writeOff(self):
try:
async with self.fanClient as client:
await client.write_gatt_char(CHARACTERISTIC, OFF)
async with BleakClient(self.address) as client:
await client.write_gatt_char(CHARACTERISTIC, OFF, response=False)
return True
except Exception as e:
return False

View File

@ -24,7 +24,6 @@ async def setSpeed(speed):
@app.route("/speed/increase", methods=["POST"])
async def setIncreaseSpeed():
print("increase speed")
currentSpeed = await fan.readSpeed()
newSpeed = 0
if currentSpeed < 25:
@ -42,7 +41,6 @@ async def setIncreaseSpeed():
@app.route("/speed/decrease", methods=["POST"])
async def setDecreaseSpeed():
print("decrease speed")
currentSpeed = await fan.readSpeed()
newSpeed = 0
if currentSpeed == 100:

View File

@ -1,2 +1 @@
FLASK_ADDRESS='F2:B3:F7:6A:24:48'
FLASK_URL='127.0.0.1:5000'
FLASK_ADDRESS='F2:B3:F7:6A:24:48'

View File

@ -2,4 +2,4 @@
from bluewind import app
if __name__ == '__main__':
app.run(debug=False)
app.run(debug=False, host='0.0.0.0')