-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathkeystroke.js
More file actions
374 lines (317 loc) · 9.24 KB
/
keystroke.js
File metadata and controls
374 lines (317 loc) · 9.24 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/** keystroke.js
* Part of the Muttlee system
* Copyright Peter Kwan (c) 2018 - 2025
* MIT license blah blah.
* Records keystrokes received from connected clients
* Used to keep a record of edits to pages
*
* addEvent(data) - Adds a key event to the list
* replayEvents() - Replays the events to newly connected clients
* saveEvents() - Saves the events to file
* matchPage(event) - Returns a matching event if there is one in the list. (page, subpage, service)
*
& @todo Move all the file stuff out of here!
*/
/* global Page, fs */ // Keeps this happy: $ standard keystroke.js --fix
'use strict'
const path = require('path')
// import constants and config for use server-side
const CONST = require('./constants.js')
const CONFIG = require('./config.js')
require('./page.js')
// import logger
const LOG = require('./log.js')
const page = new Page()
global.KeyStroke = function () {
const that = this // Make the parent available in the nested functions. Probably 100 reasons why you shouldn't do this.
this.sourceFile = ''
this.destFile = ''
this.event = undefined // Used to talk to inner function
this.outfile = undefined
this.eventList = []
// Not sophisticated. Just set all the characters to space
this.clearPage = function (data) {
LOG.fn(
['keystroke', 'clearPage'],
`Clearing page data.S=${data.S}, data.p=${data.p}, data.s=${data.s}`,
LOG.LOG_LEVEL_INFO
)
data.k = ' '
for (let row = 0; row < 24; row++) {
data.y = row
for (let col = 0; col < 40; col++) {
const d = {
S: data.S, // service number
p: data.p,
s: data.s,
k: data.k,
x: col,
y: row,
id: data.id
}
this.addEvent(d)
}
}
}
/** Add a keystroke event to the list */
this.addEvent = function (data) {
// Unfortunately, we need to check that we don't already have a character at that location
// @todo Search through the list and if the character location matches
// then replace the key entry for that location
// otherwise push the event
let overwrite = false
// Packet X28 skips this check.
if (data.y === 28) {
// Don't check for an existing x28
}
else
{
for (let i = 0; i < this.eventList.length; i++) {
if (this.sameChar(data, this.eventList[i])) {
this.eventList[i].k = data.k // replace the key as this overwrites the original character
overwrite = true
LOG.fn(
['keystroke', 'addEvent'],
'Overwriting character',
LOG.LOG_LEVEL_VERBOSE
)
break
}
}
}
if (!overwrite) {
this.eventList.push(data)
}
LOG.fn(
['keystroke', 'addEvent'],
`queue length=${this.eventList.length}`,
LOG.LOG_LEVEL_INFO
)
}
/** <return true if the character location is the same in both key events
*/
this.sameChar = function (a, b) {
if (a === undefined) return false
if (b === undefined) return false
if (a.x !== b.x) return false // Column
// Check each value for not matching
if (a.p !== b.p) return false // Page
if (a.s !== b.s) return false // Subpage
if (a.y !== b.y) return false // Row
if (a.S !== b.S) return false // Service
return true
}
/** replayEvents to the specified client */
this.replayEvents = function (client) {
LOG.fn(
['keystroke', 'replay'],
'',
LOG.LOG_LEVEL_VERBOSE
)
for (let i = 0; i < this.eventList.length; i++) {
client.emit('keystroke', this.eventList[i])
}
}
this.matchPage = function (event) {
LOG.fn(
['keystroke', 'matchPage'],
'',
LOG.LOG_LEVEL_VERBOSE
)
return event // @todo
}
/* ( Helper for saveEdits() */
this.savePage = function () {
LOG.fn(
['keystroke', 'savePage'],
'enter',
LOG.LOG_LEVEL_VERBOSE
)
// page.filename='/dev/shm/mypage.tti'
page.savePage(
'dummy',
function () {
LOG.fn(
['keystroke', 'savePage'],
'Write completed',
LOG.LOG_LEVEL_INFO
)
},
function (err) {
LOG.fn(
['keystroke', 'savePage'],
`Write failed: ${err}`,
LOG.LOG_LEVEL_ERROR
)
}
)
}
/** Write the edits back to file
* @return true if there are edits that were done
*/
this.saveEdits = function () {
// Are there any edits to save?
if (this.eventList.length === 0) {
return false
}
LOG.fn(
['keystroke', 'saveEdit events to save = ' + this.eventList.length],
'',
LOG.LOG_LEVEL_INFO
)
// Sort the event list by S(service name) p(page 100..8ff) s(subpage 0..99) y(row 0..24)
this.eventList.sort(
function (a, b) {
// the main service is never defined, so set it to the configured CONFIG.DEFAULT_SERVICE
if (a.S === undefined) a.S = CONFIG[CONST.CONFIG.DEFAULT_SERVICE]
if (b.S === undefined) a.S = CONFIG[CONST.CONFIG.DEFAULT_SERVICE]
// Service sort
if (a.S < b.S) return -1
if (a.S > b.S) return 1
// page sort
if (a.p < b.p) return -1
if (a.p > b.p) return 1
// subpage sort
if (a.s < b.s) return -1
if (a.s > b.s) return 1
// row sort
if (a.y < b.y) return -1
if (a.y > b.y) return 1
return 0 // same
}
)
// Now that we are sorted we can apply the edits
// However, due to the async nature, we only do one file at a time
if (this.eventList.length > 0) {
let event = this.eventList[0]
// Get the filename
let service = event.S
if (!service) {
service = CONFIG[CONST.CONFIG.DEFAULT_SERVICE]
}
const pageNumber = event.p
const filename = path.join(
CONFIG[CONST.CONFIG.SERVICE_PAGES_SERVE_DIR],
service,
`p${pageNumber.toString(16)}${CONST.PAGE_EXT_TTI}` // The filename of the original page
)
const that = this
page.loadPage(
filename,
function () {
LOG.fn(
['keystroke', 'saveEdits'],
'Loaded. Now edit...',
LOG.LOG_LEVEL_VERBOSE
)
// apply all the edits that refer to this page
for (; ((that.eventList.length > 0) && (pageNumber === event.p) && (service === event.S)); event = that.eventList[0]) {
/*
// Send the message to the page. X28 is a whole row, so send it differently
if (event.y === 28) {
console.log("[KeyStroke:saveEdits] Packet 28 not implemented")
// TODO: Probably page.keyMessage(event) is the right way to go as this is the routine that parses the page file
}
else {
page.keyMessage(event)
}
*/
page.keyMessage(event)
that.eventList.shift() // Remove the event we just processed
}
page.validatePage() // Check for badly formed teletext page
page.print()
// At this point we trigger off a timer
setTimeout(this.savePage, 500)
},
function (err) {
LOG.fn(
['keystroke', 'saveEdits'],
`Edit failed: ${err}`,
LOG.LOG_LEVEL_ERROR
)
}
)
}
return true // we have done edits
}
this.copyback = function () {
copyFile(
that.sourceFile,
that.destFile,
function (err) {
if (err !== undefined) {
LOG.fn(
['keystroke', 'copyback'],
`Failed: ${err}`,
LOG.LOG_LEVEL_ERROR
)
}
}
)
}
/** Dump the summary of the contents of the key events list */
this.dump = function () {
console.log('Dump ' + this.eventList.length + ' items')
for (let i = 0; i < this.eventList.length; i++) {
console.log(
'p:' + this.eventList[i].p.toString(16) +
' s:' + this.eventList[i].s +
' k:' + this.eventList[i].k +
' x:' + this.eventList[i].x +
' y:' + this.eventList[i].y
)
}
}
}
/** Utility */
function setCharAt (str, index, chr) {
if (index > str.length - 1) return str
return str.substr(0, index) + chr + str.substr(index + 1)
}
/** copyFile - Make a copy of a file
* @param source - Source file
* @param target - Destination file
* @param cb - Callback when completed, with an error message
*/
function copyFile (source, target, cb) {
LOG.fn(
['keystroke', 'copyFile'],
`Copying ${source} to ${target}`,
LOG.LOG_LEVEL_INFO
)
let cbCalled = false
let rd = fs.createReadStream(source)
rd.on('error', function (err) {
done(err)
})
const wr = fs.createWriteStream(target)
wr.on('error', function (err) {
done(err)
})
wr.on('end', function (ex) {
LOG.fn(
['keystroke', 'copyFile'],
'Closing files... (end)',
LOG.LOG_LEVEL_INFO
)
done()
})
wr.on('close', function (ex) {
LOG.fn(
['keystroke', 'copyFile'],
'Closing files... (close)',
LOG.LOG_LEVEL_INFO
)
done()
})
rd.pipe(wr)
function done (err) {
if (!cbCalled) {
cb(err)
cbCalled = true
}
rd.close()
rd = null
}
}