-
Notifications
You must be signed in to change notification settings - Fork 0
/
vortex.js
174 lines (165 loc) · 5.73 KB
/
vortex.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
const net = require('net')
module.exports = vortexClient
const ROW_LENGTH = 40
const RESPONSE_TYPES = {
COMMAND_RESPONSE: 1,
PAGE_REQUEST: 2,
PAGE_WITH_COMMAND_ROW: 3,
USER_ACCESS_RESPONSE: 34,
ERROR_RESPONSE: 255
}
const RESPONSE_STATUS_CODES = {
SUCCESS: 3,
ERROR: 6,
SUCCESS_TEXT: 7
}
const REQUEST_TYPES = {
COMMAND_LINE: 10,
PAGE_RESPONSE: 2
}
const CONFIG_DEFAULTS = {
port: 1025,
timeout: 5000
}
function send(client, data, type = REQUEST_TYPES.COMMAND_LINE) {
const buffer = typeof data === 'string' ? Buffer.alloc(83, 0x20) : Buffer.alloc(data.length + 3)
buffer[0] = type
Buffer.from(data).copy(buffer, 1)
buffer[buffer.length - 2] = 0xF8
buffer[buffer.length - 1] = 0x01
client.write(buffer)
}
function vortexClient(config) {
this.config = Object.assign({}, CONFIG_DEFAULTS, config)
this.client.on('data', data => this.onData(data))
this.client.on('close', () => this.onDisconnected())
}
vortexClient.prototype = {
client: new net.Socket(),
promise: Promise.resolve(),
start(action) {
this.promise = this.promise
.then(() => new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject('Timeout')
}, this.config.timeout)
Object.assign(this, { resolve, reject, timeout })
return action()
}))
return this.promise
},
end(result) {
if (this.timeout) {
clearTimeout(this.timeout)
}
if (!(this.resolve && this.reject)) {
throw new Error('end() called without pending promise')
}
if (result && result.error) {
this.reject(result.error)
}
else {
this.resolve(result && result.response || result)
}
delete this.resolve
delete this.reject
delete this.data
},
connect() {
const { host, port, username, password } = this.config
this.start(() => this.client.connect(port, host)) // no callback, as end() is triggered by welcome message
return this.login(username, password)
},
login(username, password) {
if (!username) throw new Error('Username is required')
if (!password) throw new Error('Password is required')
this.sendCommand(`log ${username}`)
return this.sendCommand(password)
},
sendCommand(cmd) {
if (typeof cmd !== 'string' || cmd.length === 0) {
return Promise.reject('Invalid command')
}
return this.start(() => {
console.log(cmd)
send(this.client, cmd)
})
},
onData(data) {
data = Array.from(data)
if (this.data) {
data = this.data.concat(data)
}
const responseComplete = data[data.length - 2] === 0xF8 && data[data.length - 1] === 1
if (responseComplete) {
data.splice(-2)
const type = data.shift()
switch (type) {
case RESPONSE_TYPES.ERROR_RESPONSE:
case RESPONSE_TYPES.COMMAND_RESPONSE: {
const [status, ...command] = data.slice(data.findIndex(i => i !== 32), 81)
const commandText = String.fromCharCode.apply(String, command.filter(i => i >= 0x20)).trim()
if (status === RESPONSE_STATUS_CODES.ERROR) {
return this.end({ error: commandText })
}
return this.end({ type, response: commandText })
}
case RESPONSE_TYPES.PAGE_WITH_COMMAND_ROW: {
const page = new Array(ROW_LENGTH * 24)
for (let row = data.shift(); row !== 255; row = data.shift()) {
page.splice(row * ROW_LENGTH, ROW_LENGTH, ...data.splice(0, ROW_LENGTH))
}
const command = data.slice(data.findIndex(i => i !== 32), 81)
return this.end({ type, page, command })
}
case RESPONSE_TYPES.PAGE_REQUEST: {
return this.end({ type })
}
case RESPONSE_TYPES.USER_ACCESS_RESPONSE: {
// explicitely don't handle this response, as the password is sent implicitely
return
}
default: {
return this.end({ error: `Unhandled response type ${type}` })
}
}
} else {
this.data = data
}
},
getPage(mag, set, page) {
let command = String(mag)
if (set || page) command += ' ' + set
if (page) command += '.' + page
return this.sendCommand(command).then(response => {
if (response.type === RESPONSE_TYPES.PAGE_WITH_COMMAND_ROW) {
const { page, command } = response
page.splice(0, 0, 0xFE, 0x01, 0x1A, 0x00, 0x00, 0x00)
page.push(...new Array(42))
return page
}
throw new Error(`Invalid response: ${response}`)
})
},
sendPage(command, data) {
return this.sendCommand(command)
.then(response => {
if (response.type !== RESPONSE_TYPES.PAGE_REQUEST) {
return Promise.reject(`Unexpected response ${response}`)
}
return this.start(() => {
send(this.client, data, REQUEST_TYPES.PAGE_RESPONSE)
})
})
},
disconnect() {
try {
this.client.end()
} catch (err) {
console.error('Failed to disconnect properly from Vortex.')
}
},
onDisconnected() {
console.log('Connection closed')
},
}