-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathservice.js
More file actions
81 lines (72 loc) · 2.06 KB
/
service.js
File metadata and controls
81 lines (72 loc) · 2.06 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
/** service.js
* Encapsulates a teletext service object
* Environment: node.js server side script
* @brief A service is a service name and a set of pages.
*/
'use strict'
// import logger
const LOG = require('./log.js')
/** Constructor
* @param serviceName - a Service name such as BBCONE_2007_06_19
*/
global.Service = function (serviceName) {
// member variables
this.name = serviceName
this.pages = []
// What do we want to do with a service?
// 1) Keep pages in numerical order
// 2) Load pages and save pages
// 3) Expire pages and remove them
/** Give a teletext page, inserts it into the local page list
* @todo What do we do about a duplicate page?
* We don't intend to cache pages forever so there won't ever be many pages to search through
*/
this.addPage = function (page) {
if (this.findPage(page.pageNumber) === false) {
this.pages.push(page)
}
}
/** Seek the three digit page number that we are looking for
* This is a part of a cacheing scheme so a missing page is not an errot
* @return false if the page does not exist
*/
this.findPage = function (mpp) {
LOG.fn(
['service', 'findPage'],
`Looking for page=${mpp}`,
LOG.LOG_LEVEL_VERBOSE
)
// Page out of range?
if (mpp < 0x100 || mpp > 0x7ff) {
return false
}
// For each page in the service...
for (let p = 0; p < this.pages.length; p++) {
if (this.pages[p].pageNumber === mpp) {
return this.pages[p]
}
}
return false
}
/** Switcher.
* If the message is for this service, send it to the pages
*/
this.keyMessage = function (key) {
LOG.fn(
['service', 'keyMessage'],
`Got a keymessage, name=${this.name}, data=${key.s}`,
LOG.LOG_LEVEL_INFO
)
LOG.fn(
['service', 'keyMessage'],
`key data=${JSON.stringify(key, null, 4)}`,
LOG.LOG_LEVEL_VERBOSE
)
}
/** Match the given name with the service name
* @return true if the service name matches
*/
this.matchName = function (name) {
return this.name === name
}
}