From ec61fb6af6b4dfafe0aac7e1b29d1a65463b1dad Mon Sep 17 00:00:00 2001 From: Mirco Bauer Date: Sun, 26 May 2024 12:49:51 +0800 Subject: [PATCH 1/4] add new script that sends notificaitons to the IRC channel when the light state changes --- check_light_status_changes.py | 83 +++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 check_light_status_changes.py diff --git a/check_light_status_changes.py b/check_light_status_changes.py new file mode 100755 index 0000000..6be86c6 --- /dev/null +++ b/check_light_status_changes.py @@ -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 +# +# 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) From c627e92b1df84991c0a417bf77aa970d2a467a7f Mon Sep 17 00:00:00 2001 From: Mirco Bauer Date: Sun, 26 May 2024 12:54:43 +0800 Subject: [PATCH 2/4] clarified license DimSumLabs is a place and cannot claim ownership of any copyright, but the individual contributors of DimSumLabs may do --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 1ae3ddc..9f1a97b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 Dim Sum Labs +Copyright (c) 2020-2024 contributors of DimSumLabs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 6887a1515858d8a491b597a50db7e728d57a501f Mon Sep 17 00:00:00 2001 From: Mirco Bauer Date: Sun, 26 May 2024 13:47:58 +0800 Subject: [PATCH 3/4] flash_helios_light.py: add authors --- smuxi_hooks/on-message-received/flash_helios_light.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/smuxi_hooks/on-message-received/flash_helios_light.py b/smuxi_hooks/on-message-received/flash_helios_light.py index 9202517..0674b7f 100755 --- a/smuxi_hooks/on-message-received/flash_helios_light.py +++ b/smuxi_hooks/on-message-received/flash_helios_light.py @@ -2,6 +2,10 @@ # A Smuxi hook script. This hooks flashes the ceiling light when a new message # on the #dimsumlabs channel was received. +# +# Copyright (C) 2021 Mirco Bauer +# Copyright (C) 2021 Neil Pahl +# Copyright (C) 2021 Felix E. Klee from urllib import request from time import sleep From ef23133a23c71f3919f479eecd73c17a00fbb82f Mon Sep 17 00:00:00 2001 From: Mirco Bauer Date: Sun, 26 May 2024 13:48:41 +0800 Subject: [PATCH 4/4] flash_helios_light.py: restore previous light color after flashing The default light color (kind of white) is used to restore the previous light color. This has a side-effect that changes to the lights by users of the user interface will be lost. Instead we read the previous color state and restore it once the flash is completed. --- .../on-message-received/flash_helios_light.py | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/smuxi_hooks/on-message-received/flash_helios_light.py b/smuxi_hooks/on-message-received/flash_helios_light.py index 0674b7f..ddaf659 100755 --- a/smuxi_hooks/on-message-received/flash_helios_light.py +++ b/smuxi_hooks/on-message-received/flash_helios_light.py @@ -3,7 +3,7 @@ # A Smuxi hook script. This hooks flashes the ceiling light when a new message # on the #dimsumlabs channel was received. # -# Copyright (C) 2021 Mirco Bauer +# Copyright (C) 2021, 2024 Mirco Bauer # Copyright (C) 2021 Neil Pahl # Copyright (C) 2021 Felix E. Klee @@ -12,15 +12,34 @@ 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[0-9]+)\n" + + "(?P[0-9]+)\n" + + "(?P[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 @@ -35,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) @@ -51,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)