From 9194bf5c5fabd0d258bc54a2b43f346a84cf4f4c Mon Sep 17 00:00:00 2001 From: RoyceDa Date: Sat, 31 Jan 2026 03:01:25 +0200 Subject: [PATCH] update protocol for devices system --- .../protocol/packets/packet.device.list.ts | 75 +++++++++++++++++++ .../protocol/packets/packet.device.new.ts | 64 ++++++++++++++++ .../protocol/packets/packet.handshake.ts | 8 +- 3 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 app/providers/ProtocolProvider/protocol/packets/packet.device.list.ts create mode 100644 app/providers/ProtocolProvider/protocol/packets/packet.device.new.ts diff --git a/app/providers/ProtocolProvider/protocol/packets/packet.device.list.ts b/app/providers/ProtocolProvider/protocol/packets/packet.device.list.ts new file mode 100644 index 0000000..a40b2f5 --- /dev/null +++ b/app/providers/ProtocolProvider/protocol/packets/packet.device.list.ts @@ -0,0 +1,75 @@ +import Packet from "../packet"; +import Stream from "../stream"; + +export enum DeviceState { + ONLINE = 0, + OFFLINE = 1 +} + +export enum DeviceVerifyState { + VERIFIED = 0, + /** + * Устройство не подтверждено пользователем + * через другой девайс + */ + NOT_VERIFIED = 1 +} + +export interface DeviceEntry { + deviceId: string; + deviceName: string; + deviceOs: string; + deviceStatus: DeviceState; + deviceVerify: DeviceVerifyState; +} + +/** + * Отправляется с сервера клиенту для передачи списка + * подключенных в данный момент к аккаунту устройств + */ +export class PacketDeviceList extends Packet { + + private devices: DeviceEntry[] = []; + + public getPacketId(): number { + return 0x17; + } + + public _receive(stream: Stream): void { + const deviceCount = stream.readInt16(); + this.devices = []; + for (let i = 0; i < deviceCount; i++) { + const device: DeviceEntry = { + deviceId: stream.readString(), + deviceName: stream.readString(), + deviceOs: stream.readString(), + deviceStatus: stream.readInt8(), + deviceVerify: stream.readInt8() + }; + this.devices.push(device); + } + } + + public _send(): Promise | Stream { + const stream = new Stream(); + stream.writeInt16(this.getPacketId()); + stream.writeInt16(this.devices.length); + for (const device of this.devices) { + stream.writeString(device.deviceId); + stream.writeString(device.deviceName); + stream.writeString(device.deviceOs); + stream.writeInt8(device.deviceStatus); + stream.writeInt8(device.deviceVerify); + } + return stream; + } + + public getDevices(): DeviceEntry[] { + return this.devices; + } + + public setDevices(devices: DeviceEntry[]): void { + this.devices = devices; + } + +} \ No newline at end of file diff --git a/app/providers/ProtocolProvider/protocol/packets/packet.device.new.ts b/app/providers/ProtocolProvider/protocol/packets/packet.device.new.ts new file mode 100644 index 0000000..fcf7d1b --- /dev/null +++ b/app/providers/ProtocolProvider/protocol/packets/packet.device.new.ts @@ -0,0 +1,64 @@ +import Packet from "../packet"; +import Stream from "../stream"; + +export interface Device { + deviceId: string; + deviceName: string; + deviceOs: string; +} + +/** + * Приходит всегда с сервера, + * когда к аккаунту пользователя подключается новое устройство. + * Приходит на все девайсы. + */ +export class PacketDeviceNew extends Packet { + + private ipAddress: string = ""; + + private device: Device = { + deviceId: "", + deviceName: "", + deviceOs: "" + } + + public getPacketId(): number { + return 0x09; + } + + public _receive(stream: Stream): void { + this.ipAddress = stream.readString(); + this.device = { + deviceId: stream.readString(), + deviceName: stream.readString(), + deviceOs: stream.readString() + }; + } + + public _send(): Promise | Stream { + const stream = new Stream(); + stream.writeInt16(this.getPacketId()); + stream.writeString(this.ipAddress); + stream.writeString(this.device.deviceId); + stream.writeString(this.device.deviceName); + stream.writeString(this.device.deviceOs); + return stream; + } + + public getIpAddress(): string { + return this.ipAddress; + } + + public setIpAddress(ipAddress: string): void { + this.ipAddress = ipAddress; + } + + public getDevice(): Device { + return this.device; + } + + public setDevice(device: Device): void { + this.device = device; + } + +} \ No newline at end of file diff --git a/app/providers/ProtocolProvider/protocol/packets/packet.handshake.ts b/app/providers/ProtocolProvider/protocol/packets/packet.handshake.ts index 5b26075..7c84f11 100644 --- a/app/providers/ProtocolProvider/protocol/packets/packet.handshake.ts +++ b/app/providers/ProtocolProvider/protocol/packets/packet.handshake.ts @@ -9,6 +9,7 @@ export enum HandshakeState { export interface Device { deviceId: string; deviceName: string; + deviceOs: string; } /** @@ -28,7 +29,8 @@ export default class PacketHandshake extends Packet { private heartbeatInterval : number = 15; private device: Device = { deviceId: "", - deviceName: "" + deviceName: "", + deviceOs: "" }; private handshakeState: HandshakeState = HandshakeState.NEED_DEVICE_VERIFICATION; @@ -44,7 +46,8 @@ export default class PacketHandshake extends Packet { this.heartbeatInterval = stream.readInt8(); this.device = { deviceId: stream.readString(), - deviceName: stream.readString() + deviceName: stream.readString(), + deviceOs: stream.readString() } this.handshakeState = stream.readInt8(); } @@ -58,6 +61,7 @@ export default class PacketHandshake extends Packet { stream.writeInt8(this.heartbeatInterval); stream.writeString(this.device.deviceId); stream.writeString(this.device.deviceName); + stream.writeString(this.device.deviceOs); stream.writeInt8(this.handshakeState); return stream; }