Skip to content

Commit

Permalink
Finish config API
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwohlbruck committed May 1, 2022
1 parent 074c8c3 commit dba69a2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
17 changes: 16 additions & 1 deletion micro/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def get_device_id():
default_config = {
'deviceId': get_device_id(),
'wifi': [],
'brightness': 1,
'nightMode': True,
'minimumLightLevel': 0.5,
'readingLightColorTemperature': 6000
}

def load_config():
Expand All @@ -19,8 +23,19 @@ def load_config():
f = open(config_filename, 'r')
config = json.load(f)
f.close()
return config

# If any config options are missing, add them
found_new = False
for key in default_config:
if key not in config:
config[key] = default_config[key]
found_new = True

if found_new:
write_config(config)

return config

# If file doesn't exist, create it
except OSError:
config = default_config
Expand Down
6 changes: 3 additions & 3 deletions micro/app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
from time import sleep_ms
import app.uwebsockets.client as wsclient
from app.config import get_device_id, get_config_item, load_config
from app.config import get_device_id, get_config_item, load_config, add_config
from app.wifi import disconnect_wifi

MAX_RECONNECT_ATTEMPTS = 5
Expand Down Expand Up @@ -64,8 +64,6 @@ def reconnect(self):
self.reconnect_attempts += 1
self.connect()



# Main server class to send and receive messages
# TODO: Move event names to constants
class Server():
Expand All @@ -81,6 +79,8 @@ def on_message(self, message):
name = message.get('name')
data = message.get('data')

print('Got ' + name)

if name == 'REQUEST_CONFIG':
self.send_config()

Expand Down
3 changes: 3 additions & 0 deletions micro/app/wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def connect_wifi(ssid, password):

if wifis is None:
wifis = []

# Remove the ssid if it exists already
wifis = [wifi for wifi in wifis if wifi.get('ssid', None) != ssid]

wifis.append({
'ssid': ssid,
Expand Down

0 comments on commit dba69a2

Please sign in to comment.