-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:dimsumlabs/dsl-wall
- Loading branch information
Showing
3 changed files
with
122 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#!/usr/bin/python3 | ||
|
||
# A cronjob script to check the current light status (on or off). If the status changes, we | ||
# send a message to the #dimsumlabs IRC channel via Smuxi. | ||
# | ||
# Add this script as crontab entry via "crontab -e" with the same user that smuxi-frontend-gnome is running as: | ||
# * * * * * /home/pi/check_light_status_changes.py > /home/pi/check_light_status_changes.out 2>&1 | ||
# | ||
# For the message sending to to work, it requires Smuxi 1.3 or a git build > May 18th of May 2023 to support --execute-command='/say something' | ||
# https://github.com/meebey/smuxi/commit/0fcced4fe95edc2740a379a3fc6ec0f06153a62b | ||
# | ||
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | ||
# Version 2, December 2004 | ||
# | ||
# Copyright (C) 2024 Mirco Bauer <[email protected]> | ||
# | ||
# Everyone is permitted to copy and distribute verbatim or modified | ||
# copies of this license document, and changing it is allowed as long | ||
# as the name is changed. | ||
# | ||
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | ||
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | ||
# | ||
# 0. You just DO WHAT THE FUCK YOU WANT TO. | ||
|
||
from urllib import request | ||
import os | ||
import time | ||
|
||
helios_rest_service = 'http://helios.in.dimsumlabs.com' | ||
previous_light_state_file_path = '$HOME/.helios_light_state' | ||
|
||
def is_light_on(helios_host): | ||
service_url = '{}/rgb'.format(helios_host); | ||
print(service_url); | ||
# attempt to fetch the state for 3 times as the REST call fails | ||
# sometimes because of unreliable comms/API | ||
for i in range(3): | ||
req = request.Request(service_url, method='GET') | ||
try: | ||
# timeout after 1 second as the light might be turned off | ||
res = request.urlopen(req, data=None, timeout=2) | ||
body = res.read() | ||
return True | ||
except: | ||
print('attempt has failed') | ||
time.sleep(2) | ||
continue | ||
|
||
# all state fetch attempts have failed, we determine with high convidence: lights are off -> False | ||
return False | ||
|
||
def get_previous_light_status(): | ||
path = os.path.expandvars(previous_light_state_file_path); | ||
try: | ||
f = open(path, "r") | ||
state_content = f.read() | ||
f.close() | ||
return state_content.lower() == 'true'; | ||
except: | ||
return False; | ||
|
||
def set_previous_light_status(status): | ||
path = os.path.expandvars(previous_light_state_file_path); | ||
f = open(path, "w+") | ||
f.write(str(status)) | ||
f.close() | ||
|
||
def execute_smuxi_command(command): | ||
os.system("mono /opt/smuxi*/smuxi-frontend-gnome.exe --execute-command='{}'".format(command)); | ||
|
||
if is_light_on(helios_rest_service): | ||
print('light is on') | ||
if get_previous_light_status() == False: | ||
print('light state has changed') | ||
execute_smuxi_command('/me senses lights are on'); | ||
set_previous_light_status(True) | ||
else: | ||
print('light is off') | ||
if get_previous_light_status() == True: | ||
print('light state has changed') | ||
execute_smuxi_command('/me senses lights are off'); | ||
set_previous_light_status(False) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,21 +2,44 @@ | |
|
||
# A Smuxi hook script. This hooks flashes the ceiling light when a new message | ||
# on the #dimsumlabs channel was received. | ||
# | ||
# Copyright (C) 2021, 2024 Mirco Bauer <[email protected]> | ||
# Copyright (C) 2021 Neil Pahl <[email protected]> | ||
# Copyright (C) 2021 Felix E. Klee <[email protected]> | ||
|
||
from urllib import request | ||
from time import sleep | ||
import os | ||
import sys | ||
import time | ||
import re | ||
from os.path import exists | ||
|
||
DND_TIME_FILENAME="/home/pi/dsl-wall/smuxi_scripting_fun/dnd_expire_epoch.txt" | ||
DND_TIME_FILENAME="$HOME/dsl-wall/smuxi_scripting_fun/dnd_expire_epoch.txt" | ||
if exists(DND_TIME_FILENAME) == True: | ||
dnd_file = open(DND_TIME_FILENAME, "r") | ||
path = os.path.expandvars(DND_TIME_FILENAME); | ||
dnd_file = open(path, "r") | ||
dnd_on = int(dnd_file.read().strip()) > time.time() | ||
else: | ||
dnd_on = False | ||
|
||
def get_ceiling_color(light_host): | ||
request_url = light_host + 'rgb' | ||
req = request.Request(request_url) | ||
# timeout after 1 second as the light might be turned off | ||
res = request.urlopen(req, data=None, timeout=1) | ||
body = res.read().decode("ascii") | ||
print("body: '{}'".format(body)) | ||
match = re.search("(?P<red>[0-9]+)\n" + | ||
"(?P<green>[0-9]+)\n" + | ||
"(?P<blue>[0-9]+).*", | ||
body) | ||
if not match: | ||
return None | ||
return ( match.group('red'), | ||
match.group('green'), | ||
match.group('blue') ) | ||
|
||
def change_ceiling_color(light_host, red, green, blue): | ||
color_url = 'rgb.lua?r={}&g={}&b={}'.format(red, green, blue) | ||
request_url = light_host + color_url | ||
|
@@ -31,12 +54,20 @@ def change_ceiling_color(light_host, red, green, blue): | |
if os.environ['SMUXI_MSG_TYPE'] != "Normal" or os.environ['SMUXI_CHAT_ID'] != "#dimsumlabs" or dnd_on: | ||
sys.exit(0) | ||
|
||
helios_service = 'http://helios/' | ||
#helios2_service = 'http://helios2/' | ||
helios_service = 'http://helios.in.dimsumlabs.com/' | ||
#helios2_service = 'http://helios2.in.dimsumlabs.com/' | ||
# HACK: disable helios2 as it is broken and needs repair. The HTTP/REST call | ||
# just hang forever... | ||
helios2_service = None | ||
|
||
# WTF: 0 is brightest value, lulz | ||
white = (0, 0, 0) | ||
|
||
current_color = get_ceiling_color(helios_service) | ||
if not current_color: | ||
print('could not determine current color, assuming white...') | ||
current_color = white | ||
|
||
# color value range 0 to 1024 | ||
light_pink = (7, 751, 130) | ||
light_green = (783, 7, 652) | ||
|
@@ -47,9 +78,7 @@ def change_ceiling_color(light_host, red, green, blue): | |
# sleep for 1 second to give time for the color transition to complete | ||
sleep(1) | ||
|
||
# WTF: 0 is brightest value, lulz | ||
white = (0, 0, 0) | ||
change_ceiling_color(helios_service, *white) | ||
# restore to previous color | ||
change_ceiling_color(helios_service, *current_color) | ||
if helios2_service: | ||
change_ceiling_color(helios2_service, *white) | ||
|
||
change_ceiling_color(helios2_service, *current_color) |