-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcam_active_script.py
131 lines (98 loc) · 3.43 KB
/
webcam_active_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import sys
import socket
import json
import math
from dataclasses import dataclass
from typing import Dict, Tuple
#k_to_rgb and GoveeColor taken from https://github.com/wez/govee-py
def clamp(value: int, lower: int, upper: int) -> int:
"""Clamp a value to a specified range"""
return max(min(value, upper), lower)
def k_to_rgb(kelvin: int) -> Tuple[int, int, int]:
"""Compute an rgb value corresponding to a color temperature in kelvin."""
kelvin = clamp(kelvin, 1000, 40000)
temperature = kelvin / 100.0
if temperature <= 66:
red = 255.0
else:
red = 329.698727446 * math.pow(temperature - 60, -0.1332047592)
if temperature <= 66:
green = 99.4708025861 * math.log(temperature) - 161.1195681661
else:
green = 288.1221695283 * math.pow(temperature - 60, -0.0755148492)
if temperature >= 66:
blue = 255.0
elif temperature <= 19:
blue = 0.0
else:
blue = 138.5177312231 * math.log(temperature - 10) - 305.0447927307
return clamp(int(red), 0, 255), clamp(int(green), 0, 255), clamp(int(blue), 0, 255)
@dataclass
class GoveeColor:
"""Represents an sRGB color"""
red: int = 0
green: int = 0
blue: int = 0
def as_tuple(self) -> Tuple[int, int, int]:
"""Returns (r, g, b)"""
return (self.red, self.green, self.blue)
def as_json_object(self) -> Dict[str, int]:
"""Returns {"r":r, "g":b, "b":b}"""
return {"r": self.red, "g": self.green, "b": self.blue}
@staticmethod
def from_kelvin(kelvin: int):
"""Computes the rgb equivalent to a color temperature specified in kelvin"""
red, green, blue = k_to_rgb(kelvin)
print(f"{kelvin} -> {red}, {green}, {blue}")
return GoveeColor(
red=red,
green=green,
blue=blue,
)
def send_udp_json_message(ip, port, message):
# Erstellen eines UDP-Sockets
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# JSON-Nachricht erstellen
json_message = json.dumps(message).encode('utf-8')
print(json_message)
# Nachricht an die angegebene IP und Port senden
sock.sendto(json_message, (ip, port))
print(f"Nachricht gesendet an {ip}:{port}")
except Exception as e:
print(f"Fehler beim Senden der Nachricht: {e}")
finally:
# Socket schließen
sock.close()
def set_led_color(status_code):
# Define IP and port for the LED strip
ip = "192.168.50.88"
port = 4003
# Determine color based on status code
if status_code == "active":
color = GoveeColor(red=240, green=255, blue=240) # White
elif status_code == "inactive":
color = GoveeColor(red=255, green=255, blue=0) # Yellow
else:
print(f"Unbekannter Statuscode: {status_code}")
return
# Create the message to send
colormessage = {
"msg": {
"cmd": "colorwc",
"data": {
"color": color.as_json_object(),
},
}
}
# Send the color setting message to the LED strip
send_udp_json_message(ip, port, colormessage)
def main():
# Get the status code from the command line arguments
status_code = sys.argv[1]
# Print the status message
print(f"Webcam ist jetzt aktiv. Statuscode: {status_code}")
# Set the LED color based on the status code
set_led_color(status_code)
if __name__ == "__main__":
main()