Skip to content

Commit

Permalink
code cleanup and added requirements.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
NivEz committed Jul 28, 2023
1 parent aeb74ec commit 2b9bc20
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 76 deletions.
8 changes: 4 additions & 4 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import './App.css'
import {Controller} from "./components/controller/Controller.jsx";
import './App.css';
import { Controller } from "./components/controller/Controller.jsx";

function App() {
return (
<>
<h1>Smart bulb</h1>
<h1>Super Bulb</h1>
<Controller />
</>
)
}

export default App
export default App;
5 changes: 1 addition & 4 deletions client/src/components/controller/Controller.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import { Credentials } from "../credentials/Credentials.jsx";
import { useLocalStorage } from "../../hooks/useLocalStorage.js";
import { SettingsButton } from "./SettingsButton.jsx";

// const host = process.env.IP || 'localhost';
// const host = 'localhost';
const port = 6543;
const reConnectTimeout = 5000;

const defaultCredentials = JSON.stringify({
wsHost: 'localhost',
Expand All @@ -30,7 +27,7 @@ export const Controller = () => {
const [credentials, setCredentials] = useLocalStorage("credentials", defaultCredentials);
const [isSettingsOpen, setIsSettingsOpen] = useState(false);

const { ws, sendMessage, reConnectToWebsocket } = useWebsocket({ host: credentials.wsHost, port, reConnectTimeout });
const { ws, sendMessage, reConnectToWebsocket } = useWebsocket({ host: credentials.wsHost, port });

useEffect(() => {
ws.onopen = () => {
Expand Down
34 changes: 0 additions & 34 deletions client/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@
-webkit-text-size-adjust: 100%;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
Expand All @@ -36,34 +27,9 @@ h1 {
line-height: 1.1;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #f5f5f5;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
29 changes: 0 additions & 29 deletions server/bulb.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import broadlink
from broadlink.exceptions import NetworkTimeoutError
import socket
from time import sleep
import random

TIMEOUT = 3
SSID = "" # TODO add env os.environ
WIFI_PASSWORD = ""


def initialize_connection(bulb_ip, ssid, wifi_password, timeout=TIMEOUT):
Expand Down Expand Up @@ -37,28 +33,3 @@ def initialize_connection(bulb_ip, ssid, wifi_password, timeout=TIMEOUT):

found_device.auth()
return found_device


def waves_poc(bulb, iterations=100, freq=1.0):
for i in range(iterations):
brightness = random.randrange(1, 101)
red, blue, green = [random.randrange(0, 256) for r in range(3)]
bulb.set_state(brightness=brightness, red=red, blue=blue, green=green)
sleep(0.25)
bulb.set_state(brightness=1, transitionduration=1500)
print(f"Brightness: {brightness}. Change number: {i + 1}...")
sleep(1 / freq)


if __name__ == '__main__':
bulb = initialize_connection()
print("State before: ", bulb.get_state())
# bulb.set_state(brightness=50, red=0, blue=0, green=0, hue=0, saturation=0, bulb_colormode=0, transitionduration=0)

# bulb.set_state(brightness=1)
waves_poc(bulb, freq=4, iterations=15)

# bulb.set_state(brightness=100)
# bulb.set_state(brightness=100, transitionduration=1, bulb_colormode=1, red=255, green=255, blue=255)

print("State after: ", bulb.get_state())
5 changes: 5 additions & 0 deletions server/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
broadlink==0.18.3
cffi==1.15.1
cryptography==40.0.2
pycparser==2.21
websockets==11.0.3
Empty file removed server/utils.py
Empty file.
6 changes: 1 addition & 5 deletions server/websocket.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import asyncio
import websockets
import os
import json
# from dotenv import load_dotenv
from bulb import initialize_connection

# load_dotenv()

PORT = 6543
BULB_IP = os.environ.get("BULB_IP")
RELEVANT_STATE_FIELDS = ["pwr", "brightness", "bulb_colormode", "red", "green", "blue", "transitionduration"]


async def handler(websocket):
bulb = None
# maintaining brightness state to prevent glitch with color command
# (color commands will change brightness from some reason)
brightness_state = 0
async for message in websocket:
splitted_message = message.split("!")
Expand Down

0 comments on commit 2b9bc20

Please sign in to comment.