Created
October 29, 2023 17:55
-
-
Save justynroberts/9c814ee2edb61c2a8550a2cedf3982c7 to your computer and use it in GitHub Desktop.
Incident to lights -
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import json | |
import time | |
from pycololight import PyCololight | |
from pdpyras import APISession | |
# Initialize API session | |
api_key = "ENTERKEY" | |
session = APISession(api_key) | |
# Setup hexagon device | |
light = PyCololight(device="hexagon", host="192.168.3.10") | |
# Turn on at 100% brightness | |
light.on = 100 | |
light.colour = (0, 0, 0) | |
# Initialize variables to hold last known status and message | |
last_status = None | |
last_message = None | |
def messagetxt(message, color): | |
"""Sends a message to a specified server with background color.""" | |
# Server address and port | |
server_address = ('192.168.3.184', 80) | |
# Create a socket object | |
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# Connect to the server | |
client_socket.connect(server_address) | |
# Create the payload | |
payload = json.dumps({ | |
"message": message, | |
"background": color, | |
"foreground": "white", | |
"outline": "black" | |
}).encode('utf-8') | |
# Create the HTTP request | |
http_request = f"""POST / HTTP/1.1\r\nHost: {server_address[0]}\r\nUser-Agent: Custom-Python-Client\r\nAccept: */*\r\nContent-Type: application/octet-stream\r\nContent-Length: {len(payload)}\r\n\r\n""" | |
# Encode to bytes and concatenate with payload | |
http_request = http_request.encode('utf-8') + payload | |
# Send the HTTP request | |
client_socket.sendall(http_request) | |
# Receive the response | |
response = client_socket.recv(1024) | |
print("Received:", response.decode('utf-8')) | |
# Close the socket | |
client_socket.close() | |
# Main loop to check incidents and update light and message only if status changes | |
while True: | |
time.sleep(10) | |
incidents = session.list_all( | |
'incidents', | |
params={'statuses[]': ['triggered', 'acknowledged']} | |
) | |
# Determine the current status and message | |
current_status = None | |
current_message = None | |
if len(incidents) == 0: | |
current_status = "green" | |
current_message = "All Clear :)" | |
else: | |
for i in incidents: | |
if i["status"] == "triggered": | |
current_status = "red" | |
current_message = f"Incident {len(incidents)}" | |
elif i["status"] == "acknowledged": | |
current_status = "yellow" | |
current_message = f"Working {len(incidents)}" | |
# Check if the status or message has changed | |
if current_status != last_status or current_message != last_message: | |
# Update light color and message | |
if current_status == "green": | |
light.colour = (0, 255, 0) | |
elif current_status == "red": | |
light.colour = (255, 0, 0) | |
elif current_status == "yellow": | |
light.colour = (255, 255, 0) | |
messagetxt(current_message, current_status) | |
print(current_status) | |
# Update the last known status and message | |
last_status = current_status | |
last_message = current_message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment