-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodec.js
132 lines (121 loc) · 4.42 KB
/
codec.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
const { Buffer } = require('node:buffer')
const opcodes = require('../nodes/opcodes.js')
const encoder = require('../nodes/encoder.js')
const decoder = require('../nodes/decoder.js')
/**
* Lookup table that maps op codes to the equivalent encoder function.
*/
const enc = new Map([
[opcodes.GetDevice, encoder.GetDevice],
[opcodes.SetIP, encoder.SetIP],
[opcodes.GetListener, encoder.GetListener],
[opcodes.SetListener, encoder.SetListener],
[opcodes.GetTime, encoder.GetTime],
[opcodes.SetTime, encoder.SetTime],
[opcodes.GetDoorControl, encoder.GetDoorControl],
[opcodes.SetDoorControl, encoder.SetDoorControl],
[opcodes.RecordSpecialEvents, encoder.RecordSpecialEvents],
[opcodes.GetStatus, encoder.GetStatus],
[opcodes.OpenDoor, encoder.OpenDoor],
[opcodes.GetCards, encoder.GetCards],
[opcodes.GetCardByID, encoder.GetCardByID],
[opcodes.GetCardByIndex, encoder.GetCardByIndex],
[opcodes.PutCard, encoder.PutCard],
[opcodes.DeleteCard, encoder.DeleteCard],
[opcodes.DeleteCards, encoder.DeleteCards],
[opcodes.GetTimeProfile, encoder.GetTimeProfile],
[opcodes.SetTimeProfile, encoder.SetTimeProfile],
[opcodes.ClearTimeProfiles, encoder.ClearTimeProfiles],
[opcodes.ClearTaskList, encoder.ClearTaskList],
[opcodes.AddTask, encoder.AddTask],
[opcodes.RefreshTaskList, encoder.RefreshTaskList],
[opcodes.GetEventIndex, encoder.GetEventIndex],
[opcodes.SetEventIndex, encoder.SetEventIndex],
[opcodes.GetEvent, encoder.GetEvent],
[opcodes.SetPCControl, encoder.SetPCControl],
[opcodes.SetInterlock, encoder.SetInterlock],
[opcodes.ActivateKeypads, encoder.ActivateKeypads],
[opcodes.SetDoorPasscodes, encoder.SetDoorPasscodes],
[opcodes.GetAntiPassback, encoder.GetAntiPassback],
[opcodes.SetAntiPassback, encoder.SetAntiPassback],
[opcodes.RestoreDefaultParameters, encoder.RestoreDefaultParameters],
])
/**
* Lookup table that maps message codes to the equivalent decoder function.
*/
const dec = new Map([
[0x20, decoder.GetStatus],
[0x30, decoder.SetTime],
[0x32, decoder.GetTime],
[0x40, decoder.OpenDoor],
[0x50, decoder.PutCard],
[0x52, decoder.DeleteCard],
[0x54, decoder.DeleteCards],
[0x58, decoder.GetCards],
[0x5a, decoder.GetCardByID],
[0x5c, decoder.GetCardByIndex],
[0x80, decoder.SetDoorControl],
[0x82, decoder.GetDoorControl],
[opcodes.SetAntiPassback, decoder.SetAntiPassback],
[opcodes.GetAntiPassback, decoder.GetAntiPassback],
[0x90, decoder.SetListener],
[0x92, decoder.GetListener],
[0x94, decoder.GetDevice],
[0x98, decoder.GetTimeProfile],
[0x88, decoder.SetTimeProfile],
[0x8a, decoder.ClearTimeProfiles],
[opcodes.SetDoorPasscodes, decoder.SetDoorPasscodes],
[0x8e, decoder.RecordSpecialEvents],
[0xa0, decoder.SetPCControl],
[0xa2, decoder.SetInterlock],
[0xa4, decoder.ActivateKeypads],
[0xa6, decoder.ClearTaskList],
[0xa8, decoder.AddTask],
[0xac, decoder.RefreshTaskList],
[0xb0, decoder.GetEvent],
[0xb2, decoder.SetEventIndex],
[0xb4, decoder.GetEventIndex],
[0xc8, decoder.RestoreDefaultParameters],
])
module.exports = {
/**
* Encodes a request as a 64 byte UDP message.
*
* @param {opcode} code Function opcode, translated into message function byte
* @param {number} deviceId The serial number for the target access controller
* @param {object} object Additional request specific information.
*
* @param {buffer} 64 byte NodeJS Buffer
*
* @exports
*/
encode: function (code, deviceId, object) {
const request = Buffer.alloc(64)
request.writeUInt8(0x17, 0)
if (enc.has(code)) {
const f = enc.get(code)
return f(deviceId, object)
}
throw new Error(`invalid protocol function code ${code}`)
},
/**
* Decodes a 64 byte received message into the corresponding object.
*
* @param {buffer} buffer 64 byte NodeJS buffer
* @param {function} translator (optional) function to internationalise the text in a decoded object
*
* @param {object} Decoded object (or null)
*
* @exports
*/
decode: function (buffer, translator) {
// NOTE: v6.62 firmware sends events with SOM code 0x19
// Ref. https://github.com/uhppoted/node-red-contrib-uhppoted/issues/3
if (buffer.length === 64 && (buffer[0] === 0x17 || (buffer[0] === 0x19 && buffer[1] === 0x20)) && dec.has(buffer[1])) {
const f = dec.get(buffer[1])
const bytes = new DataView(buffer.buffer)
return f(bytes, translator)
}
return null
},
}