-
Notifications
You must be signed in to change notification settings - Fork 8
/
PacketBuilder.js
180 lines (149 loc) · 4.19 KB
/
PacketBuilder.js
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
const crc32 = require('buffer-crc32');
const crypto = require('crypto');
let Packet = {
encryptionKey : '',
magic: new Buffer("6864", 'hex'),
build: function () {
let packetId = new Buffer(this.id, 'ascii');
let payload = encodePayload(JSON.stringify(this.json), this.encryptionKey);
let crc = crc32(payload);
let length = getLength([this.magic, this.packetType, packetId, crc, payload], 2); // Extra 2 for the length field itself
return Buffer.concat([this.magic, length, this.packetType, crc, packetId, payload]);
}
};
let PKPacket = Object.assign({}, Packet, {
packetType: new Buffer('pk', 'ascii'),
});
let DKPacket = Object.assign({}, Packet, {
packetType: new Buffer('dk', 'ascii'),
});
let helloPacket = function({ serial, encryptionKey, id, orviboKey }) {
let json = {
cmd: 0,
status: 0,
serial: serial,
key: encryptionKey
};
let pkt = Object.assign(Object.create(PKPacket), {
json: json,
id: id,
encryptionKey: orviboKey,
});
return pkt.build();
};
let handshakePacket = function({ serial, encryptionKey, id }) {
let json = {
cmd: 6,
status:0,
serial: serial
};
let pkt = Object.assign(Object.create(DKPacket), {
json: json,
id: id,
encryptionKey: encryptionKey
});
return pkt.build();
};
let heartbeatPacket = function({serial, uid, encryptionKey, id}) {
let json = {
cmd: 32,
status:0,
serial: serial,
uid: uid,
utc: new Date().getTime()
};
let pkt = Object.assign(Object.create(DKPacket), {
json: json,
id: id,
encryptionKey,
});
return pkt.build();
};
let comfirmStatePacket = function({serial, uid, state, encryptionKey, id}) {
let json = {
uid: uid,
cmd: 42,
statusType: 0,
value3: 0,
alarmType: 1,
serial: serial,
value4: 0,
deviceId: 0,
value1: state,
value2: 0,
updateTimeSec: new Date().getTime(),
status: 0
};
let pkt = Object.assign(Object.create(DKPacket), {
json: json,
id: id,
encryptionKey,
});
return pkt.build();
};
let defaultPacket = function({serial, uid, cmd, id, encryptionKey}) {
let json = {
uid: uid,
cmd: cmd,
serial: serial,
status: 0
};
let pkt = Object.assign(Object.create(DKPacket), {
json: json,
id: id,
encryptionKey,
});
return pkt.build();
};
let updatePacket = function({ uid, state, serial, id, clientSessionId , deviceId, encryptionKey}) {
let json = {
uid: uid,
delayTime: 0,
cmd: 15,
order: state === 0 ? "on" : "off",
userName: "[email protected]",
ver: "3.0.0",
value3: 0,
serial: serial,
value4: 0,
deviceId: deviceId,
value1: state,
value2: 0,
clientSessionId: clientSessionId
};
let pkt = Object.assign(Object.create(DKPacket), {
json: json,
id: id,
encryptionKey,
});
return pkt.build();
};
let encodePayload = function(json, key) {
let cipher = crypto.createCipheriv('aes-128-ecb', key, '');
cipher.setAutoPadding(true);
let crypted = cipher.update(json,'utf8','hex');
crypted += cipher.final('hex');
return new Buffer(crypted, 'hex');
};
let getLength = function(items, extra) {
let length = extra || 0;
for (let i = 0; i < items.length; i++) {
length += items[i].length;
}
return getHexLengthPadded(length);
};
let getHexLengthPadded = function(lengthDecimal) {
let lengthHex = lengthDecimal.toString(16);
let paddingLength = 4 - lengthHex.length;
let padding = '';
for (let i = 0; i < paddingLength; i++) {
padding +=0;
}
return new Buffer(padding + lengthHex, 'hex');
};
module.exports.helloPacket = helloPacket;
module.exports.handshakePacket = handshakePacket;
module.exports.heartbeatPacket = heartbeatPacket;
module.exports.comfirmStatePacket = comfirmStatePacket;
module.exports.defaultPacket = defaultPacket;
module.exports.updatePacket = updatePacket;