-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.ts
188 lines (146 loc) · 5.69 KB
/
message.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
namespace sphero {
export interface IApiMessage {
readonly flags: number;
readonly sequenceNumber: number;
readonly targetId: number;
readonly sourceId: number;
readonly deviceId: number;
readonly commandId: number;
readonly dataRawBytes: Array<number> | null;
readonly messageRawBytes: Array<number>;
generateMessageRawBytes(): void;
}
export class ApiMessage implements IApiMessage {
protected _flags: number;
public get flags(): number {
return this._flags;
}
protected _sequenceNumber: number;
public get sequenceNumber(): number {
return this._sequenceNumber;
}
protected _targetId: number;
public get targetId(): number {
return this._targetId;
}
protected _sourceId: number;
public get sourceId(): number {
return this._sourceId;
}
protected _deviceId: number;
public get deviceId(): number {
return this._deviceId;
}
protected _commandId: number;
public get commandId(): number {
return this._commandId;
}
protected _dataRawBytes: Array<number>;
public get dataRawBytes(): Array<number> {
return this._dataRawBytes;
}
private _messageRawBytes: Array<number>;
public get messageRawBytes(): Array<number> {
return this._messageRawBytes;
}
public constructor(
flags: number, sequenceNumber: number,
targetId: number, sourceId: number,
deviceId: number, commandId: number,
dataRawBytes: Array<number> | null = null) {
this._flags = flags;
this._sequenceNumber = sequenceNumber;
this._targetId = targetId;
this._sourceId = sourceId;
this._deviceId = deviceId;
this._commandId = commandId;
this._dataRawBytes = !dataRawBytes ? [] : dataRawBytes;
}
public generateMessageRawBytes(): void {
this.generateMessageRawBytesInternal();
}
protected generateMessageRawBytesInternal(): void {
let checksum = 0;
this._messageRawBytes = [];
this._messageRawBytes.push(ApiParserFlags.startOfPacket);
this.encodeByteInBytes(this._messageRawBytes, this.flags);
checksum += this.flags;
if ((this.flags & ApiFlags.packetHasTargetId) > 0x00) {
this.encodeByteInBytes(this._messageRawBytes, this.targetId);
checksum += this.targetId;
}
if ((this.flags & ApiFlags.packetHasSourceId) > 0x00) {
this.encodeByteInBytes(this._messageRawBytes, this.sourceId);
checksum += this.sourceId;
}
this.encodeByteInBytes(this._messageRawBytes, this.deviceId);
checksum += this.deviceId;
this.encodeByteInBytes(this._messageRawBytes, this.commandId);
checksum += this.commandId;
this.encodeByteInBytes(this._messageRawBytes, this.sequenceNumber);
checksum += this.sequenceNumber;
for (let i: number = 0; i < this.dataRawBytes.length; i++) {
let dataByte = this.dataRawBytes[i];
this.encodeByteInBytes(this._messageRawBytes, dataByte);
checksum += dataByte;
}
checksum = ~(checksum % 256);
if (checksum < 0) {
checksum = 256 + checksum;
}
this.encodeByteInBytes(this._messageRawBytes, checksum);
this._messageRawBytes.push(ApiParserFlags.endOfPacket);
}
private encodeByteInBytes(bytes: Array<number>, byte: number): void {
switch (byte) {
case ApiParserFlags.startOfPacket:
bytes.push(ApiParserFlags.escape);
bytes.push(ApiParserFlags.escapedStartOfPacket);
break;
case ApiParserFlags.endOfPacket:
bytes.push(ApiParserFlags.escape);
bytes.push(ApiParserFlags.escapedEndOfPacket);
break;
case ApiParserFlags.escape:
bytes.push(ApiParserFlags.escape);
bytes.push(ApiParserFlags.escapedEscape);
break;
default:
bytes.push(byte);
break;
}
}
}
let _sequenceNumber: number = 0;
function getNextSequenceNumber(): number {
return _sequenceNumber++ % 256;
}
function buildApiCommandMessage(
flags: number,
targetId: number, sourceId: number,
deviceId: number, commandId: number,
dataRawBytes: Array<number> | null = null): IApiMessage {
let sequenceNumber: number = getNextSequenceNumber();
let apiMessage: IApiMessage = new ApiMessage(
flags, sequenceNumber,
targetId, sourceId,
deviceId, commandId,
dataRawBytes
);
apiMessage.generateMessageRawBytes();
return apiMessage;
}
export function buildApiCommandMessageWithDefaultFlags(
targetId: number, sourceId: number,
deviceId: number, commandId: number,
dataRawBytes: Array<number> | null = null): IApiMessage {
let messageFlags: number = ApiFlags.defaultRequestWithResponseFlags;
let apiMessage: IApiMessage = buildApiCommandMessage(
messageFlags,
targetId, sourceId,
deviceId, commandId,
dataRawBytes
);
return apiMessage;
}
}