Skip to content

Commit

Permalink
Add motion detection toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwohlbruck committed May 1, 2022
1 parent cd51316 commit 426ae65
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 20 deletions.
1 change: 1 addition & 0 deletions client/src/services/lamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const getLampConfig = async(id: string) => {
export type LampConfig = {
brightness?: number
nightMode?: boolean
motionDetection?: boolean
minimumLightLevel?: number
readingLightColorTemperature?: number
}
Expand Down
52 changes: 35 additions & 17 deletions client/src/views/LampSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
v-container
v-card.pa-6.d-flex.flex-column(flat outlined style='gap: 2rem')
v-fade-transition
v-overlay(absolute v-if='loadingConfig')
v-overlay(absolute v-if='loadingConfigf')
v-progress-circular(indeterminate)

.d-flex
Expand Down Expand Up @@ -40,6 +40,23 @@ v-container
@change='updateBrightness'
)

//- Reading light color temp
div
h6.text-body-1 Reading light color temperature
.d-flex
div(:style='`width: 12px; height: 12px; background: ${colorTempRGB}; border-radius: 50%; margin: 10px 8px 0 0`')
v-slider(
v-model='config.readingLightColorTemperature'
label='Reading light color temperature'
min='2100'
max='10000'
unit='K'
:color='colorTempRGB'
:label='`${config.readingLightColorTemperature} K`'
@change='updateReadingLightColorTemperature'
hide-details
)

//- Night mode
div
v-checkbox(
Expand All @@ -50,10 +67,10 @@ v-container
hide-details
@change='updateNightMode'
)
p.text-caption.mt-2 When enabled, the lamp will turn off when the room is dark.
p.text-caption.mt-2.mb-0 When enabled, the lamp will turn off when the room is dark.

v-slide-y-transition
div(v-show='config.nightMode')
div.mt-4(v-show='config.nightMode')
h6.text-body-1 Minimum ambient light level (night mode)
v-slider(
v-model='config.minimumLightLevel'
Expand All @@ -67,21 +84,18 @@ v-container
@change='updateMinimumLightLevel'
)

//- Reading light color temp
//- Motion detection
div
h6.text-body-1 Reading light color temperature
.d-flex
div(:style='`width: 12px; height: 12px; background: ${colorTempRGB}; border-radius: 50%; margin: 10px 8px 0 0`')
v-slider(
v-model='config.readingLightColorTemperature'
label='Reading light color temperature'
min='2100'
max='10000'
unit='K'
:color='colorTempRGB'
:label='`${config.readingLightColorTemperature} K`'
@change='updateReadingLightColorTemperature'
)
v-checkbox(
v-model='config.motionDetection'
label='Motion detection'
color='primary'
dense
hide-details
@change='updateMotionDetection'
)
p.text-caption.mt-2 When enabled, the lamp will only activate when motion is dected.


//- Delete lamp
v-dialog(v-model='deleteLampDialog' max-width='500')
Expand Down Expand Up @@ -169,6 +183,10 @@ export default class LampSettings extends Vue {
this.updateConfig({ nightMode })
}
async updateMotionDetection(motionDetection: boolean) {
this.updateConfig({ motionDetection })
}
async updateMinimumLightLevel(minimumLightLevel: number) {
this.updateConfig({ minimumLightLevel })
}
Expand Down
32 changes: 29 additions & 3 deletions micro/app/commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

BRIGHTNESS = 1
NIGHT_MODE = True
MOTION_DETECTION = True
MINIMUM_LIGHT_LEVEL = 0.5
READING_LIGHT_COLOR_TEMPERATURE = 6000

server = None
last_active = False
last_colors = [(255,255,255)]
reading_light_on = False

def on_message(name, data):
Expand All @@ -28,13 +31,15 @@ def on_message(name, data):
def start_commander():
global BRIGHTNESS
global NIGHT_MODE
global MOTION_DETECTION
global MINIMUM_LIGHT_LEVEL
global READING_LIGHT_COLOR_TEMPERATURE

config = load_config()
if config:
BRIGHTNESS = config.get('brightness', BRIGHTNESS)
NIGHT_MODE = config.get('nightMode', NIGHT_MODE)
MOTION_DETECTION = config.get('motionDetection', MOTION_DETECTION)
MINIMUM_LIGHT_LEVEL = config.get('minimumLightLevel', MINIMUM_LIGHT_LEVEL)
READING_LIGHT_COLOR_TEMPERATURE = config.get('readingLightColorTemperature', READING_LIGHT_COLOR_TEMPERATURE)

Expand All @@ -45,10 +50,12 @@ def start_commander():

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

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

def turn_on_reading_light():
Expand Down Expand Up @@ -78,7 +85,6 @@ def factory_reset():
reset()

def state_from_color_list(colors, brightness=None):

if len(colors) == 1:
colors = get_color_gradient(rgb_to_hue(*hex_to_rgb(colors[0])))
else:
Expand All @@ -93,10 +99,15 @@ def pulse_received(data):
if state == None and active == None:
return

global last_active
global last_colors
last_active = active
colors = state.get('colors')
last_colors = colors

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

else:
Expand All @@ -105,27 +116,42 @@ def pulse_received(data):
if reading_light_on:
turn_on_reading_light(colors)
else:
state = state_from_color_list(colors, .03)
state = state_from_color_list(colors, BRIGHTNESS * .05)
set(state)

def update_config(config):
global BRIGHTNESS
global NIGHT_MODE
global MOTION_DETECTION
global MINIMUM_LIGHT_LEVEL
global READING_LIGHT_COLOR_TEMPERATURE

brightness = config.get('brightness', None)
night_mode = config.get('nightMode', None)
motion_detection = config.get('motionDetection', None)
minimum_light_level = config.get('minimumLightLevel', None)
reading_light_color_temperature = config.get('readingLightColorTemperature', None)

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)
else:
state = state_from_color_list(last_colors, brightness=BRIGHTNESS * .05)
set(state)

if night_mode is not None:
add_config('nightMode', night_mode)
NIGHT_MODE = night_mode

if motion_detection is not None:
add_config('motionDetection', motion_detection)
MOTION_DETECTION = motion_detection

if minimum_light_level is not None:
add_config('minimumLightLevel', minimum_light_level)
Expand Down
1 change: 1 addition & 0 deletions micro/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def get_device_id():
'wifi': [],
'brightness': 1,
'nightMode': True,
'motionDetection': True,
'minimumLightLevel': 0.5,
'readingLightColorTemperature': 6000
}
Expand Down

0 comments on commit 426ae65

Please sign in to comment.