forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketMessage.ts
More file actions
53 lines (52 loc) · 1.88 KB
/
Copy pathSocketMessage.ts
File metadata and controls
53 lines (52 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { NoderedUtil } from "@openiap/openflow-api";
import { Message } from "./Messages/Message";
function isNumber(value: string | number): boolean {
return ((value != null) && !isNaN(Number(value.toString())));
}
export class SocketMessage {
public id: string;
public replyto: string;
public command: string;
public data: string;
public count: number;
public index: number;
public priority: number = 1;
public static fromjson(json: string): SocketMessage {
let result: SocketMessage = new SocketMessage();
let obj: any = JSON.parse(json);
result.command = obj.command;
result.id = obj.id;
result.replyto = obj.replyto;
result.count = 1;
result.index = 0;
if (!NoderedUtil.IsNullEmpty(obj.priority)) result.priority = obj.priority;
result.data = obj.data;
if (isNumber(obj.count)) { result.count = obj.count; }
if (isNumber(obj.index)) { result.index = obj.index; }
if (result.id === null || result.id === undefined || result.id === "") {
result.id = NoderedUtil.GetUniqueIdentifier();
}
return result;
}
public static fromcommand(command: string): SocketMessage {
const result: SocketMessage = new SocketMessage();
result.command = command;
result.count = 1;
result.index = 0;
result.id = NoderedUtil.GetUniqueIdentifier();
return result;
}
public static frommessage(msg: Message, data: string, count: number, index: number): SocketMessage {
const result: SocketMessage = new SocketMessage();
result.id = msg.id;
result.replyto = msg.replyto;
result.command = msg.command;
result.count = count;
result.index = index;
result.data = data;
return result;
}
public tojson(): string {
return JSON.stringify(this);
}
}