Skip to content

Commit

Permalink
activity timer
Browse files Browse the repository at this point in the history
  • Loading branch information
awalford16 committed Dec 6, 2024
1 parent feac20c commit 381937c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
repo ?= adamwalford
app ?= office-lights
version ?= $$(cat ./src/${app}/VERSION)

.PHONY: build
build:
docker build ./src/${app} -t ${repo}/${app}:${version}

.PHONY: push
push:
docker push ${repo}/${app}:${version}
1 change: 1 addition & 0 deletions src/office-lights/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.0
37 changes: 27 additions & 10 deletions src/office-lights/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
from phillips import PhillipsHue, Groups, States
from mqtt import MQTT
import time
from timer import DeviceTimer

hue = PhillipsHue()
MQTT_SUBSCRIPTION = "office/lights"
DEFAULT_LIGHT_STATE = "FOCUS"
IS_DISABLED = False

hue = PhillipsHue()
activity_timer = DeviceTimer()


# Callback when timer completes
def turn_off_light_after_timeout():
print("Activity Timer Expired")
hue.change_light_state(Groups.OFFICE, False)


# Callback when a message is received from the broker
def on_message(client, userdata, message):
global IS_DISABLED
global IS_DISABLED, TIMEOUT, activity_timer
state = message.payload.decode()

# Start a new timer
activity_timer.new_timer(lambda: turn_off_light_after_timeout())

# Disable/enable office lights
if state == "DISABLE" or state == "ENABLE":
hue.change_light_state(Groups.OFFICE, False)
Expand All @@ -21,22 +34,24 @@ def on_message(client, userdata, message):
if not IS_DISABLED:
print(f"Setting state to {state}")

if state == "OFF":
hue.change_light_state(Groups.OFFICE, False)
return

# If state is not supported, return with no action
if not hasattr(States, state):
print("Invalid State, will default to FOCUS")
state = "FOCUS"
print(f"Invalid State, nothing to do")
return

# Update the light state
hue.change_light_state(Groups.OFFICE, True, States[state])
print(f"Starting light timeout of: {activity_timer.timeout}")

# Start the timer
activity_timer.start()


if __name__ == "__main__":
try:
mqtt = MQTT(on_message)

mqtt.subscribe("office/lights")
mqtt.subscribe(MQTT_SUBSCRIPTION)

# Start the network loop to process incoming and outgoing messages
mqtt.client.loop_start()
Expand All @@ -50,5 +65,7 @@ def on_message(client, userdata, message):
except KeyboardInterrupt:
print("Exiting...")
finally:
# Cancel any active timers and disconnect from MQTT
activity_timer.cancel()
mqtt.client.disconnect()
mqtt.client.loop_stop()
5 changes: 3 additions & 2 deletions src/office-lights/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import paho.mqtt.client as mqtt
from timer import DeviceTimer
from constants import MQTT_SERVER
import os
import sys
Expand Down Expand Up @@ -47,7 +48,7 @@ def test_on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))


TEST_TOPIC = "office/lights"
TEST_TOPIC = "office/test"

if __name__ == "__main__":
# Arg parser to run tester
Expand All @@ -71,7 +72,7 @@ def test_on_message(client, userdata, msg):
client.loop_start()

# Publish a message to a topic
message = "OFF" # Your message here
message = "TEST" # Your message here
# client.publish(topic, message)
# client.publish(topic, "ALERT")
# client.publish(topic, message)
Expand Down
21 changes: 21 additions & 0 deletions src/office-lights/timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import time
import threading


class DeviceTimer:
def __init__(self):
self.timeout = 300
self.active_timer = None

def new_timer(self, callback):
print("Resetting activity timer")
# Cancel any active timers
self.cancel()
self.active_timer = threading.Timer(self.timeout, lambda: callback())

def start(self):
self.active_timer.start()

def cancel(self):
if self.active_timer:
self.active_timer.cancel()

0 comments on commit 381937c

Please sign in to comment.