-
Notifications
You must be signed in to change notification settings - Fork 8
/
OrviboPacket.js
64 lines (51 loc) · 1.74 KB
/
OrviboPacket.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
const crc32 = require('buffer-crc32');
const crypto = require('crypto');
let pkt = function(packetBuffer) {
this.magic = packetBuffer.slice(0,2);
this.packetLength = packetBuffer.slice(2,4);
this.packetType = packetBuffer.slice(4,6);
this.crc32 = packetBuffer.slice(6,10);
this.packetId = packetBuffer.slice(10,42);
this.payload = packetBuffer.slice(42, packetBuffer.length);
};
pkt.prototype.logPacket = function(type) {
console.log(type, JSON.stringify(this.payloadJSON));
};
pkt.prototype.getCommand = function() {
return this.payloadJSON.cmd;
};
pkt.prototype.getSerial = function() {
return this.payloadJSON.serial;
};
pkt.prototype.getUid = function() {
return this.payloadJSON.uid;
};
pkt.prototype.getValue1 = function() {
return this.payloadJSON.value1;
};
pkt.prototype.getModelId = function() {
return this.payloadJSON.modelId;
};
pkt.prototype.processPacket = function(key) {
this.payloadJSON = this.decodeJSON(key);
this.orviboKey = key;
};
pkt.prototype.getOrviboKey = function() {
return this.orviboKey;
};
pkt.prototype.validCRC = function() {
return crc32(this.payload).toString('hex') === this.crc32.toString('hex');
};
pkt.prototype.packetTypeText = function() {
return this.packetType.toString('ascii');
};
pkt.prototype.decodeJSON = function(key) {
let decipher = crypto.createDecipheriv('aes-128-ecb', key, '');
decipher.setAutoPadding(true);
let decrypted = decipher.update(this.payload.toString('hex'), 'hex', 'utf8');
decrypted += decipher.final('utf8');
// Sometimes there are bad chars on the end of the JSON so check here
decrypted = decrypted.substring(0, decrypted.indexOf('}') + 1);
return JSON.parse(decrypted);
};
module.exports = pkt;