diff --git a/.env b/.env new file mode 100644 index 0000000..9bb55d5 --- /dev/null +++ b/.env @@ -0,0 +1,6 @@ +BROKER = "" +PORT = 1883 +SUB_TOPIC = "" +PUB_TOPIC = "" +USERNAME = "" +PASSWORD = "" \ No newline at end of file diff --git a/app.py b/app.py deleted file mode 100644 index e7faa3f..0000000 --- a/app.py +++ /dev/null @@ -1,18 +0,0 @@ - -#import pygatt -import os - -#adapter = pygatt.GATTToolBackend() -on = 'cc2333' -off = 'cc2433' - -try: -# adapter.start() -# device = adapter.connect("b2:3b:03:00:14:d6") -# device.char_write_handle(0x0009, bytearray.fromhex(off)) - os.system('gatttool -i hci0 -b b2:3b:03:00:14:d6 --char-write-req -a 0x0009 -n cc2333') -except error as ex: - print(ex) -#finally: -# adapter.stop() - diff --git a/app_off.py b/app_off.py deleted file mode 100644 index 0b064c3..0000000 --- a/app_off.py +++ /dev/null @@ -1,18 +0,0 @@ - -#import pygatt -import os - -#adapter = pygatt.GATTToolBackend() -on = 'cc2333' -off = 'cc2433' - -try: -# adapter.start() -# device = adapter.connect("b2:3b:03:00:14:d6") -# device.char_write_handle(0x0009, bytearray.fromhex(off)) - os.system('gatttool -i hci0 -b b2:3b:03:00:14:d6 --char-write-req -a 0x0009 -n cc2433') -except error as ex: - print(ex) -#finally: -# adapter.stop() - diff --git a/server.py b/lib/shared.py similarity index 55% rename from server.py rename to lib/shared.py index e46bef4..44a2f91 100644 --- a/server.py +++ b/lib/shared.py @@ -1,48 +1,28 @@ -from flask import Flask, request -import ast -import subprocess -from colormap import rgb2hex -from tenacity import retry, stop_after_attempt - -app = Flask(__name__) - - -@retry(stop=stop_after_attempt(5)) -def run_bulb_action(value): - result = subprocess.run( - [ - f"gatttool -i hci0 -b b2:3b:03:00:14:d6 --char-write-req -a 0x0009 -n {value}" - ], - stdout=subprocess.PIPE, - shell=True, - ).stdout.decode("utf-8") - print(result) - if "Characteristic value was written successfully" not in result: - raise Exception - else: - return True - - -@app.route("/on") -def on(): - res = run_bulb_action("cc2333") - return {"result": res} - - -@app.route("/off") -def off(): - res = run_bulb_action("cc2433") - return {"result": res} - - -@app.route("/colour") -def colour(): - rgb = list(ast.literal_eval(request.args.get("rgb"))) - r = rgb[0] - g = rgb[1] - b = rgb[2] - hex = rgb2hex(r, g, b) - hex = hex.replace("#", "") - value = "56" + hex + "00f0aa" - res = run_bulb_action(value) - return {"result": res} +import subprocess +from tenacity import retry, stop_after_attempt +from colormap import rgb2hex + + +@retry(stop=stop_after_attempt(5)) +def run_bulb_action(value): + result = subprocess.run( + [ + f"gatttool -i hci0 -b b2:3b:03:00:14:d6 --char-write-req -a 0x0009 -n {value}" + ], + stdout=subprocess.PIPE, + shell=True, + ).stdout.decode("utf-8") + print(result) + if "Characteristic value was written successfully" not in result: + raise Exception + else: + return True + + +def convert_to_bulb_format(colour_map): + hex = rgb2hex( + colour_map["color"]["r"], colour_map["color"]["g"], colour_map["color"]["b"] + ) + hex = hex.replace("#", "") + value = "56" + hex + "00f0aa" + return value diff --git a/mqtt.py b/mqtt.py index 04ed5df..71b1327 100644 --- a/mqtt.py +++ b/mqtt.py @@ -1,27 +1,20 @@ import random import json +import os +from dotenv import load_dotenv from paho.mqtt import client as mqtt_client -from server import run_bulb_action -from colormap import rgb2hex +from lib.shared import run_bulb_action, convert_to_bulb_format +load_dotenv() -broker = "vps.k-world.me.uk" -port = 1883 -topic = "ledLight/set" +broker = os.getenv("BROKER") +port = os.getenv("PORT") +topic = os.getenv("SUB_TOPIC") # generate client ID with pub prefix randomly client_id = f"python-mqtt-{random.randint(0, 100)}" -username = "karl" -password = "karlmax" - - -def convert_to_bulb_format(colour_map): - hex = rgb2hex( - colour_map["color"]["r"], colour_map["color"]["g"], colour_map["color"]["b"] - ) - hex = hex.replace("#", "") - value = "56" + hex + "00f0aa" - return value +username = os.getenv("USERNAME") +password = os.getenv("PASSWORD") def connect_mqtt() -> mqtt_client: @@ -39,7 +32,7 @@ def connect_mqtt() -> mqtt_client: def publish(client): - result = client.publish("ledLight/state", "ON") + result = client.publish(os.getenv('PUB_TOPIC'), "ON") status = result[0] if status == 0: print(f"Send `ON` to topic `{topic}`") diff --git a/requirements.txt b/requirements.txt index 4fea0ff..b81e3b9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,24 +1,75 @@ -black==21.9b0 -click==8.0.3 +aioesphomeapi==9.1.4 +aiofiles==0.7.0 +ajsonrpc==1.2.0 +anime-downloader==5.0.15 +anyio==3.3.4 +appdirs==1.4.4 +asgiref==3.4.1 +attrs==21.2.0 +beautifulsoup4==4.10.0 +bitstring==3.1.9 +bottle==0.12.19 +cattrs==1.8.0 +certifi==2021.5.30 +cffi==1.14.6 +cfscrape==2.1.1 +charset-normalizer==2.0.6 +click==7.1.2 colorama==0.4.4 +coloredlogs==15.0.1 colorlog==6.5.0 colormap==1.0.4 +cryptography==35.0.0 easydev==0.12.0 +ecdsa==0.17.0 +esphome==2021.9.3 +esphome-dashboard==20210908.0 +esptool==3.1 Flask==2.0.2 +fuzzywuzzy==0.18.0 +h11==0.12.0 +humanfriendly==10.0 +idna==3.2 +ifaddr==0.1.7 importlib-metadata==4.8.1 itsdangerous==2.0.1 Jinja2==3.0.2 MarkupSafe==2.0.1 -mypy-extensions==0.4.3 -paho-mqtt==1.6.1 -pathspec==0.9.0 +marshmallow==3.14.0 +noiseprotocol==0.3.1 +paho-mqtt==1.5.1 pexpect==4.8.0 -platformdirs==2.4.0 +Pillow==8.4.0 +platformio==5.2.0 +protobuf==3.18.1 ptyprocess==0.7.0 -regex==2021.10.23 -tenacity==8.0.1 -tomli==1.2.2 -typed-ast==1.4.3 +pycparser==2.20 +pycryptodome==3.11.0 +pyelftools==0.27 +pyreadline==2.1 +pyserial==3.5 +pySmartDL==1.3.4 +python-dateutil==2.8.2 +python-dotenv==0.19.1 +pytz==2021.1 +PyYAML==5.4.1 +reedsolo==1.5.4 +requests==2.26.0 +requests-cache==0.8.1 +semantic-version==2.8.5 +six==1.16.0 +sniffio==1.2.0 +soupsieve==2.2.1 +starlette==0.16.0 +tabulate==0.8.9 +tornado==6.1 typing-extensions==3.10.0.2 +tzlocal==2.1 +url-normalize==1.4.3 +urllib3==1.26.7 +uvicorn==0.15.0 +voluptuous==0.12.1 Werkzeug==2.0.2 +wsproto==1.0.0 +zeroconf==0.36.8 zipp==3.6.0 diff --git a/rest.py b/rest.py new file mode 100644 index 0000000..95a46a3 --- /dev/null +++ b/rest.py @@ -0,0 +1,32 @@ +from flask import Flask, request +import ast + +from colormap import rgb2hex +from lib.shared import run_bulb_action + +app = Flask(__name__) + + +@app.route("/on") +def on(): + res = run_bulb_action("cc2333") + return {"result": res} + + +@app.route("/off") +def off(): + res = run_bulb_action("cc2433") + return {"result": res} + + +@app.route("/colour") +def colour(): + rgb = list(ast.literal_eval(request.args.get("rgb"))) + r = rgb[0] + g = rgb[1] + b = rgb[2] + hex = rgb2hex(r, g, b) + hex = hex.replace("#", "") + value = "56" + hex + "00f0aa" + res = run_bulb_action(value) + return {"result": res}