new devices system prototype
This commit is contained in:
@@ -1,76 +1,31 @@
|
||||
import { decodeWithPassword, encodeWithPassword } from "@/app/crypto/crypto";
|
||||
import { useFileStorage } from "@/app/hooks/useFileStorage";
|
||||
import { generateRandomKey } from "@/app/utils/utils";
|
||||
import { createContext, useEffect, useState } from "react";
|
||||
import { createContext, useState } from "react";
|
||||
import { DeviceEntry, PacketDeviceList } from "../ProtocolProvider/protocol/packets/packet.device.list";
|
||||
import { usePacket } from "../ProtocolProvider/usePacket";
|
||||
|
||||
interface DeviceProviderContextValue {
|
||||
deviceId: string;
|
||||
devices: DeviceEntry[];
|
||||
}
|
||||
|
||||
export const DeviceProviderContext = createContext<DeviceProviderContextValue|null>(null);
|
||||
export const DeviceProviderContext = createContext<DeviceProviderContextValue|null>(null);
|
||||
|
||||
interface DeviceProviderProps {
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
|
||||
export function DeviceProvider(props: DeviceProviderProps) {
|
||||
const [deviceId, setDeviceId] = useState<string>("");
|
||||
const {writeFile, readFile} = useFileStorage();
|
||||
/**
|
||||
* Подключенные устройства
|
||||
*/
|
||||
const [devices, setDevices] = useState<DeviceEntry[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchDeviceId();
|
||||
usePacket(0x17, (packet : PacketDeviceList) => {
|
||||
console.info("Device list update", packet.getDevices());
|
||||
setDevices(packet.getDevices());
|
||||
}, []);
|
||||
|
||||
const fetchDeviceId = async () => {
|
||||
const device = await readFile("device");
|
||||
if(device){
|
||||
const decoded = await decodeDevice(Buffer.from(device).toString('utf-8'));
|
||||
if(decoded){
|
||||
setDeviceId(decoded);
|
||||
return;
|
||||
}
|
||||
}
|
||||
await createDeviceId();
|
||||
}
|
||||
|
||||
const createDeviceId = async () => {
|
||||
const newDevice = generateRandomKey(128);
|
||||
const encoded = await encodeDevice(newDevice);
|
||||
await writeFile("device", encoded);
|
||||
setDeviceId(newDevice);
|
||||
}
|
||||
|
||||
const decodeDevice = async (data: string) => {
|
||||
const hwid = window.deviceId;
|
||||
const platform = window.deviceName;
|
||||
const salt = "rosetta-device-salt";
|
||||
|
||||
try {
|
||||
const decoded = await decodeWithPassword(hwid + platform + salt, data);
|
||||
return decoded;
|
||||
} catch (e) {
|
||||
console.error("Failed to decode device data:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const encodeDevice = async (data: string) => {
|
||||
const hwid = window.deviceId;
|
||||
const platform = window.deviceName;
|
||||
const salt = "rosetta-device-salt";
|
||||
|
||||
try {
|
||||
const encoded = await encodeWithPassword(hwid + platform + salt, data);
|
||||
return encoded;
|
||||
} catch (e) {
|
||||
console.error("Failed to encode device data:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<DeviceProviderContext.Provider value={{
|
||||
deviceId: deviceId
|
||||
devices: devices
|
||||
}}>
|
||||
{props.children}
|
||||
</DeviceProviderContext.Provider>
|
||||
|
||||
Reference in New Issue
Block a user