feat: Enhance device registration to include name and active slot, supporting remote configuration updates and shutdown.
This commit is contained in:
@@ -2,5 +2,6 @@
|
|||||||
"serial_number": "CB1FCDC3",
|
"serial_number": "CB1FCDC3",
|
||||||
"mode": "DEV",
|
"mode": "DEV",
|
||||||
"active_slot": "A",
|
"active_slot": "A",
|
||||||
"status": "OFFLINE"
|
"status": "OFFLINE",
|
||||||
|
"name": "Unnamed Device"
|
||||||
}
|
}
|
||||||
@@ -33,26 +33,45 @@ class HeadUnit:
|
|||||||
"serial_number": self.serial_number,
|
"serial_number": self.serial_number,
|
||||||
"mode": self.mode,
|
"mode": self.mode,
|
||||||
"active_slot": self.active_slot,
|
"active_slot": self.active_slot,
|
||||||
"status": self.status
|
"status": self.status,
|
||||||
|
"name": self.config.get("name", "Unnamed Device")
|
||||||
}
|
}
|
||||||
with open(self.config_path, 'w') as f:
|
with open(self.config_path, 'w') as f:
|
||||||
json.dump(self.config, f, indent=4)
|
json.dump(self.config, f, indent=4)
|
||||||
|
|
||||||
def register(self):
|
def register(self):
|
||||||
print(f"[*] Registering device {self.serial_number} ({self.mac_address})...")
|
print(f"[*] Synchronising with mesh orchestration... ({self.serial_number})")
|
||||||
try:
|
try:
|
||||||
resp = requests.post(f"{self.server_url}/api/devices/register", json={
|
resp = requests.post(f"{self.server_url}/api/devices/register", json={
|
||||||
"serialNumber": self.serial_number,
|
"serialNumber": self.serial_number,
|
||||||
"macAddress": self.mac_address
|
"macAddress": self.mac_address,
|
||||||
|
"activeSlot": self.active_slot,
|
||||||
|
"name": self.config.get("name", "Unnamed Device")
|
||||||
})
|
})
|
||||||
if resp.status_code == 200:
|
if resp.status_code == 200:
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
self.status = data.get("status")
|
self.status = data.get("status")
|
||||||
|
|
||||||
|
# Adopt remote configuration
|
||||||
|
remote_slot = data.get("activeSlot")
|
||||||
|
if remote_slot and remote_slot != self.active_slot:
|
||||||
|
print(f"[!] Remote slot override: Switching {self.active_slot} -> {remote_slot}")
|
||||||
|
self.active_slot = remote_slot
|
||||||
|
|
||||||
|
remote_name = data.get("name")
|
||||||
|
if remote_name:
|
||||||
|
self.config["name"] = remote_name
|
||||||
|
|
||||||
self.save_config()
|
self.save_config()
|
||||||
print(f"[+] Registration successful. Status: {self.status}")
|
print(f"[+] Sync successful. Slot: {self.active_slot} | Status: {self.status}")
|
||||||
|
|
||||||
|
if self.status == "OFFLINE":
|
||||||
|
print("[!] Remote instruction: System decommissioned. Shutting down...")
|
||||||
|
return "SHUTDOWN"
|
||||||
|
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
print(f"[-] Registration failed: {resp.text}")
|
print(f"[-] Synchronisation failed: {resp.text}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[-] Connection error: {e}")
|
print(f"[-] Connection error: {e}")
|
||||||
return False
|
return False
|
||||||
@@ -145,10 +164,17 @@ if __name__ == "__main__":
|
|||||||
print("\n[*] Entering automated telemetry loop. Press Ctrl+C to return to menu.")
|
print("\n[*] Entering automated telemetry loop. Press Ctrl+C to return to menu.")
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
hu.register()
|
result = hu.register()
|
||||||
|
if result == "SHUTDOWN":
|
||||||
|
print("[!] System shutdown confirmed. Exiting loop.")
|
||||||
|
break
|
||||||
|
|
||||||
if hu.status == "ENROLLED":
|
if hu.status == "ENROLLED":
|
||||||
hu.report_test(generate_mock_hdmi_test())
|
hu.report_test(generate_mock_hdmi_test())
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
|
if hu.status == "OFFLINE":
|
||||||
|
break
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\n[!] Loop interrupted by user.")
|
print("\n[!] Loop interrupted by user.")
|
||||||
elif choice == "0":
|
elif choice == "0":
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { prisma } from "@/lib/prisma";
|
|||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
try {
|
try {
|
||||||
const { serialNumber, macAddress } = await req.json();
|
const { serialNumber, macAddress, activeSlot, name } = await req.json();
|
||||||
|
|
||||||
if (!serialNumber || !macAddress) {
|
if (!serialNumber || !macAddress) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@@ -28,6 +28,8 @@ export async function POST(req: Request) {
|
|||||||
data: {
|
data: {
|
||||||
serialNumber,
|
serialNumber,
|
||||||
macAddress,
|
macAddress,
|
||||||
|
activeSlot: activeSlot || "A",
|
||||||
|
name: name || "Unnamed Device",
|
||||||
status: "PENDING",
|
status: "PENDING",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user