-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add initial onboarding config
- Loading branch information
Showing
5 changed files
with
259 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import os | ||
|
||
class ConfigManager: | ||
def __init__(self, file_path: str): | ||
self.file_path = file_path | ||
if not os.path.exists(self.file_path): | ||
with open(self.file_path, 'w') as f: | ||
pass | ||
|
||
def __getattr__(self, key): | ||
return self.get(key) | ||
|
||
def __setattr__(self, key, value): | ||
if key in ['file_path']: | ||
super().__setattr__(key, value) | ||
else: | ||
self.set(key, value) | ||
|
||
def __iter__(self): | ||
return iter(self.to_dict().items()) | ||
|
||
def to_dict(self): | ||
result = {} | ||
with open(self.file_path, 'r') as f: | ||
for line in f: | ||
if line.startswith('#'): | ||
continue | ||
if '=' in line: | ||
key, value = line.strip().split('=', 1) | ||
result[key] = value | ||
return result | ||
|
||
def get(self, key): | ||
with open(self.file_path, 'r') as f: | ||
for line in f: | ||
if line.startswith('#'): | ||
continue | ||
if line.startswith(key + '='): | ||
return line[len(key) + 1:].strip() | ||
return None | ||
|
||
def set(self, key, value): | ||
lines = [] | ||
found = False | ||
new_value = f'{key}={value}\n' | ||
with open(self.file_path, 'r') as f: | ||
for line in f: | ||
if line.startswith(key + '=') and not line.startswith('#'): | ||
if line == new_value: | ||
return True | ||
# update value instead of adding current line | ||
lines.append(new_value) | ||
found = True | ||
else: | ||
lines.append(line) | ||
if not found: | ||
lines.append(new_value) | ||
with open(self.file_path, 'w') as f: | ||
f.writelines(lines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services_dir = '/etc/init.d' | ||
services_ignored = ['boot', 'coredump', 'done', 'led', 'silentboot'] | ||
config_listener = '/data/listener' | ||
|
||
lx06_infrared = '/sys/ir_tx_gpio/ir_data' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Xiaomi Smart Speaker</title> | ||
<!-- Import Materialize CSS --> | ||
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet"> | ||
</head> | ||
<body> | ||
<nav> | ||
<div class="nav-wrapper" style="background-color: #5AF;"> | ||
<a href="#" class="brand-logo" style="padding-left: 15px;">Xiaoai</a> | ||
</div> | ||
</nav> | ||
|
||
<div class="container"> | ||
<p class="flow-text">Home Assistant instances found:</p> | ||
<div class="row"></div> | ||
</div> | ||
|
||
<script> | ||
document.addEventListener('DOMContentLoaded', function() { | ||
fetch('/discover') | ||
.then(response => response.json()) | ||
.then(data => { | ||
const hostname = data.hostname; | ||
document.querySelector('.brand-logo').textContent = hostname; | ||
data.instances.forEach(instance => { | ||
const card = ` | ||
<div class="col s12 m12 l6"> | ||
<div class="card"> | ||
<div class="card-content"> | ||
<span class="card-title">${instance.location_name}</span> | ||
<p>${instance.base_url} - ${instance.version}</p> | ||
</div> | ||
<div class="card-action"> | ||
<form action="/auth" method="post"> | ||
<input type="hidden" name="url" value="${instance.base_url}"> | ||
<button type="submit" class="btn">Authenticate</button> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
document.querySelector('.container .row').insertAdjacentHTML('beforeend', card); | ||
}); | ||
}) | ||
.catch(error => { | ||
console.error('Error fetching data:', error); | ||
}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import socket | ||
import fcntl | ||
import struct | ||
|
||
def get_ip_address(ifname): | ||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
return socket.inet_ntoa(fcntl.ioctl( | ||
s.fileno(), | ||
0x8915, # SIOCGIFADDR | ||
struct.pack('256s', ifname[:15].encode('utf-8')) | ||
)[20:24]) |