update protocol for devices system
This commit is contained in:
@@ -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> | 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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> | 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user