diff --git a/src/app/_components/.CommandManager.tsx.swp b/src/app/_components/.CommandManager.tsx.swp new file mode 100644 index 0000000..6fe9b29 Binary files /dev/null and b/src/app/_components/.CommandManager.tsx.swp differ diff --git a/src/app/_components/CommandManager.tsx b/src/app/_components/CommandManager.tsx index ada7a3b..619a5cf 100644 --- a/src/app/_components/CommandManager.tsx +++ b/src/app/_components/CommandManager.tsx @@ -1,9 +1,21 @@ // In file: src/app/_components/CommandManager.tsx "use client"; -import { useState } from "react"; +import { useState, useEffect } from "react"; import { api } from "~/trpc/react"; -import { postCommand, type DeviceCommandType } from "~/lib/api"; // Your Laravel API helper +import { postCommand, type DeviceCommandType } from "~/lib/api"; + +const predefinedPayloads: Record = { + lights: { on: true }, + camera: { on: true }, + sleep: { interval_sec: 300 }, + telemetry_sec: { interval: 60 }, + poll_sec: { interval: 120 }, + wifi: { target: "home", ssid: "YourSSID", password: "YourPassword" }, + ring_fence: { enabled: true, points: [{ lat: 0, lng: 0 }] }, + reboot: {}, + ota: { url: "http://example.com/firmware.bin" }, +}; interface CommandManagerProps { selectedImei: string; @@ -11,23 +23,28 @@ interface CommandManagerProps { export function CommandManager({ selectedImei }: CommandManagerProps) { const [name, setName] = useState(""); - const [type, setType] = useState("reboot"); - const [payload, setPayload] = useState(""); // Stored as a string for the textarea + const [type, setType] = useState("lights"); + const [payload, setPayload] = useState(""); + + // This useEffect hook updates the payload whenever the command type changes + useEffect(() => { + const newPayload = predefinedPayloads[type]; + setPayload(JSON.stringify(newPayload, null, 2)); // Pretty-print the JSON + }, [type]); // Dependency array: this runs only when 'type' changes const utils = api.useUtils(); const templatesQuery = api.commandTemplate.getAll.useQuery(); const createTemplateMutation = api.commandTemplate.create.useMutation({ onSuccess: () => { - utils.commandTemplate.getAll.invalidate(); // Invalidate cache to refetch list + utils.commandTemplate.getAll.invalidate(); setName(""); - setPayload(""); }, }); const deleteTemplateMutation = api.commandTemplate.delete.useMutation({ onSuccess: () => { - utils.commandTemplate.getAll.invalidate(); // Refetch list after deleting + utils.commandTemplate.getAll.invalidate(); }, }); @@ -40,23 +57,23 @@ export function CommandManager({ selectedImei }: CommandManagerProps) { } }; - const handleSendCommand = async (template: { type: string; payload: any }) => { - try { - await postCommand(selectedImei, { - type: template.type as DeviceCommandType, - payload: template.payload, - }); - alert(`Command "${template.type}" sent to ${selectedImei}!`); - } catch (e) { - alert(`Failed to send command: ${e instanceof Error ? e.message : "Unknown error"}`); - } + const handleSendCommand = async (template: { name: string, type: string; payload: any }) => { + try { + await postCommand(selectedImei, { + type: template.type as DeviceCommandType, + payload: template.payload, + }); + alert(`Command "${template.name}" sent to ${selectedImei}!`); + } catch (e) { + alert(`Failed to send command: ${e instanceof Error ? e.message : "Unknown error"}`); + } }; return (
{/* Form to add new templates */}
-

Create New Command Template

+

Create/Edit Command

setName(e.target.value)} className="p-2 text-white rounded-md bg-gray-800 border border-gray-600" /> - - -