Skip to content

Commit 78fbab7

Browse files
author
marcelkb
committed
bugfix
1 parent a1996f3 commit 78fbab7

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

keep_awake.py

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,37 +76,40 @@ def keep_awake(icon):
7676
global last_state
7777
logger.info("KeepAwake service started")
7878
while not stop_event.is_set():
79-
now = datetime.datetime.now()
80-
weekday = now.weekday() # 0=Monday, 6=Sunday
79+
if working:
80+
now = datetime.datetime.now()
81+
weekday = now.weekday() # 0=Monday, 6=Sunday
8182

82-
if DISABLE_IF_WORKSTATION_LOCKED and is_workstation_locked():
83-
desired_state = "normal" # monitor locked → do nothing
84-
logger.info("Workstation locked → allowing normal standby")
85-
else:
86-
if working and weekday < 5 and START_HOUR <= now.hour < END_HOUR:
87-
desired_state = "awake"
88-
else:
89-
desired_state = "normal"
90-
91-
# Apply change only if state has changed
92-
if desired_state != last_state:
93-
if desired_state == "awake":
94-
success = disable_sleep()
83+
if DISABLE_IF_WORKSTATION_LOCKED and is_workstation_locked():
84+
desired_state = "normal" # monitor locked → do nothing
85+
logger.info("Workstation locked → allowing normal standby")
9586
else:
96-
success = enable_sleep()
97-
if success:
98-
last_state = desired_state
99-
update_status(icon)
100-
101-
# optical if its in working time and enabled
102-
if desired_state == "awake" and weekday < 5 and START_HOUR <= now.hour < END_HOUR and icon.icon != ICON_WORKTIME:
103-
icon.icon = ICON_WORKTIME
104-
elif desired_state == "normal" or not (weekday < 5 and START_HOUR <= now.hour < END_HOUR):
105-
icon.icon = ICON_ACTIVE
87+
if working and weekday < 5 and START_HOUR <= now.hour < END_HOUR:
88+
desired_state = "awake"
89+
else:
90+
desired_state = "normal"
91+
92+
# Apply change only if state has changed
93+
if desired_state != last_state:
94+
if desired_state == "awake":
95+
success = disable_sleep()
96+
else:
97+
success = enable_sleep()
98+
if success:
99+
last_state = desired_state
100+
update_status(icon)
101+
102+
# optical if its in working time and enabled
103+
if desired_state == "awake" and weekday < 5 and START_HOUR <= now.hour < END_HOUR and icon.icon != ICON_WORKTIME:
104+
icon.icon = ICON_WORKTIME
105+
elif desired_state == "normal" or not (weekday < 5 and START_HOUR <= now.hour < END_HOUR):
106+
icon.icon = ICON_ACTIVE
107+
else:
108+
logger.error("no state change, sleep change call was unsuccessful")
106109
else:
107-
logger.error("no state change, sleep change call was unsuccessful")
110+
logger.info(f"No state change, still '{last_state}' with working: {working} and stop event not set.")
108111
else:
109-
logger.info(f"No state change, still '{last_state}' with working: {working} and stop event not set.")
112+
logger.info(f"working: {working} and stop event not set.")
110113

111114
# If icon object exists but tray is gone → restart it
112115
if icon and not icon.visible:
@@ -154,6 +157,7 @@ def is_workstation_locked():
154157
def on_start(icon, item):
155158
global working
156159
global last_state
160+
logger.info("on_start")
157161
working = True
158162
now = datetime.datetime.now()
159163
weekday = now.weekday() # 0=Monday, 6=Sunday
@@ -171,6 +175,7 @@ def on_start(icon, item):
171175
def on_force(icon, item):
172176
global working
173177
global last_state
178+
logger.info("on_force")
174179
if working is True:
175180
working = False
176181
disable_sleep()
@@ -181,13 +186,15 @@ def on_force(icon, item):
181186

182187
def on_stop(icon, item):
183188
global working
189+
logger.info("on_stop")
184190
working = False
185191
icon.icon = ICON_INACTIVE
186192
enable_sleep()
187193
update_status(icon)
188194
logger.info("KeepAwake deactivated")
189195

190196
def on_exit(icon, item):
197+
logger.info("on_exit")
191198
icon.stop()
192199
stop_event.set() # signal thread to stop immediately
193200
update_status(icon)

0 commit comments

Comments
 (0)