add dot env
This commit is contained in:
parent
462afbe4da
commit
b2896ef825
6
.env
Normal file
6
.env
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
BROKER = ""
|
||||||
|
PORT = 1883
|
||||||
|
SUB_TOPIC = ""
|
||||||
|
PUB_TOPIC = ""
|
||||||
|
USERNAME = ""
|
||||||
|
PASSWORD = ""
|
18
app.py
18
app.py
@ -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()
|
|
||||||
|
|
18
app_off.py
18
app_off.py
@ -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()
|
|
||||||
|
|
@ -1,48 +1,28 @@
|
|||||||
from flask import Flask, request
|
import subprocess
|
||||||
import ast
|
from tenacity import retry, stop_after_attempt
|
||||||
import subprocess
|
from colormap import rgb2hex
|
||||||
from colormap import rgb2hex
|
|
||||||
from tenacity import retry, stop_after_attempt
|
|
||||||
|
@retry(stop=stop_after_attempt(5))
|
||||||
app = Flask(__name__)
|
def run_bulb_action(value):
|
||||||
|
result = subprocess.run(
|
||||||
|
[
|
||||||
@retry(stop=stop_after_attempt(5))
|
f"gatttool -i hci0 -b b2:3b:03:00:14:d6 --char-write-req -a 0x0009 -n {value}"
|
||||||
def run_bulb_action(value):
|
],
|
||||||
result = subprocess.run(
|
stdout=subprocess.PIPE,
|
||||||
[
|
shell=True,
|
||||||
f"gatttool -i hci0 -b b2:3b:03:00:14:d6 --char-write-req -a 0x0009 -n {value}"
|
).stdout.decode("utf-8")
|
||||||
],
|
print(result)
|
||||||
stdout=subprocess.PIPE,
|
if "Characteristic value was written successfully" not in result:
|
||||||
shell=True,
|
raise Exception
|
||||||
).stdout.decode("utf-8")
|
else:
|
||||||
print(result)
|
return True
|
||||||
if "Characteristic value was written successfully" not in result:
|
|
||||||
raise Exception
|
|
||||||
else:
|
def convert_to_bulb_format(colour_map):
|
||||||
return True
|
hex = rgb2hex(
|
||||||
|
colour_map["color"]["r"], colour_map["color"]["g"], colour_map["color"]["b"]
|
||||||
|
)
|
||||||
@app.route("/on")
|
hex = hex.replace("#", "")
|
||||||
def on():
|
value = "56" + hex + "00f0aa"
|
||||||
res = run_bulb_action("cc2333")
|
return value
|
||||||
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}
|
|
27
mqtt.py
27
mqtt.py
@ -1,27 +1,20 @@
|
|||||||
import random
|
import random
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
from paho.mqtt import client as mqtt_client
|
from paho.mqtt import client as mqtt_client
|
||||||
from server import run_bulb_action
|
from lib.shared import run_bulb_action, convert_to_bulb_format
|
||||||
from colormap import rgb2hex
|
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
broker = "vps.k-world.me.uk"
|
broker = os.getenv("BROKER")
|
||||||
port = 1883
|
port = os.getenv("PORT")
|
||||||
topic = "ledLight/set"
|
topic = os.getenv("SUB_TOPIC")
|
||||||
# generate client ID with pub prefix randomly
|
# generate client ID with pub prefix randomly
|
||||||
client_id = f"python-mqtt-{random.randint(0, 100)}"
|
client_id = f"python-mqtt-{random.randint(0, 100)}"
|
||||||
username = "karl"
|
username = os.getenv("USERNAME")
|
||||||
password = "karlmax"
|
password = os.getenv("PASSWORD")
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def connect_mqtt() -> mqtt_client:
|
def connect_mqtt() -> mqtt_client:
|
||||||
@ -39,7 +32,7 @@ def connect_mqtt() -> mqtt_client:
|
|||||||
|
|
||||||
|
|
||||||
def publish(client):
|
def publish(client):
|
||||||
result = client.publish("ledLight/state", "ON")
|
result = client.publish(os.getenv('PUB_TOPIC'), "ON")
|
||||||
status = result[0]
|
status = result[0]
|
||||||
if status == 0:
|
if status == 0:
|
||||||
print(f"Send `ON` to topic `{topic}`")
|
print(f"Send `ON` to topic `{topic}`")
|
||||||
|
@ -1,24 +1,75 @@
|
|||||||
black==21.9b0
|
aioesphomeapi==9.1.4
|
||||||
click==8.0.3
|
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
|
colorama==0.4.4
|
||||||
|
coloredlogs==15.0.1
|
||||||
colorlog==6.5.0
|
colorlog==6.5.0
|
||||||
colormap==1.0.4
|
colormap==1.0.4
|
||||||
|
cryptography==35.0.0
|
||||||
easydev==0.12.0
|
easydev==0.12.0
|
||||||
|
ecdsa==0.17.0
|
||||||
|
esphome==2021.9.3
|
||||||
|
esphome-dashboard==20210908.0
|
||||||
|
esptool==3.1
|
||||||
Flask==2.0.2
|
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
|
importlib-metadata==4.8.1
|
||||||
itsdangerous==2.0.1
|
itsdangerous==2.0.1
|
||||||
Jinja2==3.0.2
|
Jinja2==3.0.2
|
||||||
MarkupSafe==2.0.1
|
MarkupSafe==2.0.1
|
||||||
mypy-extensions==0.4.3
|
marshmallow==3.14.0
|
||||||
paho-mqtt==1.6.1
|
noiseprotocol==0.3.1
|
||||||
pathspec==0.9.0
|
paho-mqtt==1.5.1
|
||||||
pexpect==4.8.0
|
pexpect==4.8.0
|
||||||
platformdirs==2.4.0
|
Pillow==8.4.0
|
||||||
|
platformio==5.2.0
|
||||||
|
protobuf==3.18.1
|
||||||
ptyprocess==0.7.0
|
ptyprocess==0.7.0
|
||||||
regex==2021.10.23
|
pycparser==2.20
|
||||||
tenacity==8.0.1
|
pycryptodome==3.11.0
|
||||||
tomli==1.2.2
|
pyelftools==0.27
|
||||||
typed-ast==1.4.3
|
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
|
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
|
Werkzeug==2.0.2
|
||||||
|
wsproto==1.0.0
|
||||||
|
zeroconf==0.36.8
|
||||||
zipp==3.6.0
|
zipp==3.6.0
|
||||||
|
32
rest.py
Normal file
32
rest.py
Normal file
@ -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}
|
Loading…
x
Reference in New Issue
Block a user