Skip to content

Commit

Permalink
Implement message queue
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwohlbruck committed May 2, 2022
1 parent b6806c8 commit 4386b17
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 33 deletions.
8 changes: 5 additions & 3 deletions client/src/views/LampSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ v-container.d-flex.flex-column.align-center
| {{ name && name.length ? name : (lamp ? lamp.name : 'Lamp') }} settings
v-spacer
transition(name='bounce')
.d-flex(v-if='saving')
.d-flex.mt-2(v-hide='!saving')
span.mr-2 Saving
v-progress-circular(indeterminate size='20')
v-progress-circular.mt-1.ml-1(indeterminate size='20')

//- Lamp name
v-text-field(
Expand Down Expand Up @@ -98,7 +98,9 @@ v-container.d-flex.flex-column.align-center


v-sheet.d-flex.flex-column(style='background-color: rgba(255,0,0,.1)' rounded outlined)
v-subheader ⚠️ Danger zone!
v-subheader.white--text
span(style='margin: 0 8px 3px 0') ⚠️
| Danger zone!
v-divider(label='asdf')


Expand Down
98 changes: 76 additions & 22 deletions micro/app/commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from machine import reset
from app.led import set_color, turn_off, set, get_color_gradient, rgb_to_hue, hex_to_rgb, polylinear_gradient, rgb_to_hex, kelvin_to_rgb
from app.server import start_server
from time import sleep

BRIGHTNESS = 1
NIGHT_MODE = True
Expand All @@ -14,6 +15,11 @@
last_colors = [(255,255,255)]
reading_light_on = False

last_room_is_lit = True
last_motion_detected = True

message_queue = []

def on_message(name, data):
# TODO: Move this switch to a dict outside
if name == 'GROUP_STATE_CHANGED':
Expand Down Expand Up @@ -48,24 +54,41 @@ def start_commander():
server.subscribe(on_message)


# Send activate command to server
def activate(color):
if (server):
active = True
server.send_lamp_command(rgb_to_hex(*color), True)

# Send deactivate command to server
def deactivate(color=(0,0,0)):
if (server):
active = False
server.send_lamp_command(rgb_to_hex(*color), False)

# Activate LEDs from last stored colors
def activate_local():
global last_colors
state = state_from_color_list(last_colors, brightness=BRIGHTNESS)
set(state)

# Deactivate LEDs from last stored colors
def deactivate_local():
global last_colors
global reading_light_on
if reading_light_on:
turn_on_reading_light(last_colors)
else:
state = state_from_color_list(last_colors, BRIGHTNESS * .05)
set(state)

def turn_on_reading_light():
global READING_LIGHT_COLOR_TEMPERATURE
print('on')
global reading_light_on
reading_light_on = True
print(READING_LIGHT_COLOR_TEMPERATURE)
print(kelvin_to_rgb(READING_LIGHT_COLOR_TEMPERATURE))
set_color(kelvin_to_rgb(READING_LIGHT_COLOR_TEMPERATURE))
rgb = kelvin_to_rgb(READING_LIGHT_COLOR_TEMPERATURE)
set_color(rgb, brightness=BRIGHTNESS)

def turn_off_reading_light():
print('off')
Expand All @@ -80,6 +103,35 @@ def toggle_reading_light():
else:
turn_on_reading_light()

# Triggered by input.py when the room becomes bright
def room_is_lit(val):
print('room is lit' + str(val))
global last_room_is_lit
last_room_is_lit = val
dequeue()

# Triggered by input.py when motion is detected
def motion_detected(val):
print('motion detected' + str(val))
global last_motion_detected
last_motion_detected = val
dequeue()

# If room is lid and motion detected, dequeue pending messages
def dequeue():
global last_room_is_lit
global last_motion_detected
if last_room_is_lit and last_motion_detected:
# Wait 1 second between dequeuing
while len(message_queue) > 0:
message = message_queue.pop(0)
pulse_received(message)
sleep(1)

# Deactivate after dequeuing
deactivate_local()


def factory_reset():
reset_config()
reset()
Expand All @@ -98,6 +150,15 @@ def pulse_received(data):

if state == None and active == None:
return

global last_room_is_lit
global last_motion_detected

# Queue if the room is not lit or no motion detected
if not last_room_is_lit or not last_motion_detected:
if active:
message_queue.append(data)
return

global last_active
global last_colors
Expand All @@ -106,18 +167,9 @@ def pulse_received(data):
last_colors = colors

if active:
# effect = start_effect('rotate', colors=state.get('colors'))
state = state_from_color_list(colors, brightness=BRIGHTNESS)
set(state)

activate_local()
else:
# stop_effect(effect)
global reading_light_on
if reading_light_on:
turn_on_reading_light(colors)
else:
state = state_from_color_list(colors, BRIGHTNESS * .05)
set(state)
deactivate_local()

def update_config(config):
global BRIGHTNESS
Expand All @@ -135,15 +187,17 @@ def update_config(config):
if brightness is not None:
add_config('brightness', brightness)
BRIGHTNESS = brightness

global last_active
global last_colors
if last_active:
state = state_from_color_list(last_colors, brightness=BRIGHTNESS)
set(state)

global reading_light_on
if reading_light_on:
turn_on_reading_light()
else:
state = state_from_color_list(last_colors, brightness=BRIGHTNESS * .05)
set(state)
global last_active
global last_colors
if last_active:
activate_local()
else:
deactivate_local()

if night_mode is not None:
add_config('nightMode', night_mode)
Expand Down
12 changes: 5 additions & 7 deletions micro/app/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from machine import Pin, ADC
from app.rotary.rotary_irq_esp import RotaryIRQ
from app.led import set_color, rgb_to_hex, hsl_to_rgb, turn_off
from app.commander import activate, toggle_reading_light, deactivate, factory_reset
from app.commander import activate, toggle_reading_light, deactivate, factory_reset, room_is_lit, motion_detected

ROTARY_STEPS = 32
LIGHT_SENSITIVITY = 3400
Expand Down Expand Up @@ -108,12 +108,13 @@ def input_watcher():
val = int((rotary_new / ROTARY_STEPS) * 360)
last_color = hsl_to_rgb(val, 1, 0.5)
set_color(last_color, top=True)
print('result =', rotary_new)
# print('result =', rotary_new)

# Motion sensor
if motion_old != motion_new:
print('motion =', motion_new)
# print('motion =', motion_new)
motion_old = motion_new
motion_detected(motion_new)

# Light sensor
light_values.append(light_new)
Expand Down Expand Up @@ -141,10 +142,7 @@ def input_watcher():
if now - light_time > LIGHT_WAIT:
if reads_dark != is_dark:
is_dark = reads_dark
if is_dark:
turn_off()
else:
set_color((0,0,255), brightness=.05)
room_is_lit(not is_dark)

past_threshold = False

Expand Down
2 changes: 1 addition & 1 deletion micro/app/uwebsockets/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def recv(self):
continue
elif opcode == OP_PING:
# We need to send a pong frame
if __debug__: print("Sending PONG")
# if __debug__: print("Sending PONG")
self.write_frame(OP_PONG, data)
# And then wait to receive
continue
Expand Down

0 comments on commit 4386b17

Please sign in to comment.