-
Notifications
You must be signed in to change notification settings - Fork 24
/
dvr.js
178 lines (165 loc) · 5.37 KB
/
dvr.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
175
176
177
178
const _ = require('lodash')
const SSDP = require('node-ssdp').Server
const Logger = new (require('./logger'))()
class DVR {
constructor (server) {
// Declares
this.version = {
major: 0,
minor: 1
}
this.server = server
this.express = this.server.express
this.scanPossible = 0
this.scanInProgress = 0
this.scanFound = 0
this.scanProgress = 0
this.friendlyName = this.server.settings.serverName
this.manufacturer = 'Silicondust'
this.modelName = 'HDHR - Plex - IPTV'
this.modelNumber = 'HDHR-PLEX-IPTV'
this.serialNumber = this.server.settings.serialNumber
this.deviceId = this.server.settings.deviceId
this.firmwareVersion = `${this.version.major}.${this.version.minor}`
this.firmwareName = `plex-iptv-${this.firmwareVersion}`
this.tunerCount = this.server.settings.tunerCount
this.deviceAuth = 'user123'
this.lineupUrl = '/lineup.json'
this.scanUrl = '/lineup.post'
this.lineStatusUrl = '/lineup_status.json'
this.discoverUrl = '/discover.json'
this.deviceUrl = '/device.xml'
// Bindings
this.init = this.init.bind(this)
this.channels = this.channels.bind(this)
this.lineup = this.lineup.bind(this)
this.scan = this.scan.bind(this)
this.lineupStatus = this.lineupStatus.bind(this)
this.discover = this.discover.bind(this)
this.device = this.device.bind(this)
}
init () {
this.express.use(this.lineupUrl, this.lineup)
this.express.use(this.scanUrl, this.scan)
this.express.use(this.lineStatusUrl, this.lineupStatus)
this.express.use(this.discoverUrl, this.discover)
this.express.use(this.deviceUrl, this.device)
this.ssdpServer = new SSDP({
location: {
port: this.express.serverPort,
path: '/device.xml'
},
udn: `f10c2345-7329-40b7-8b04-27${this.serialNumber}`,
allowWildcards: true,
adInterval: 5000,
ssdpSig: `${this.friendlyName}/${this.firmwareVersion} UPnP/1.0`
})
this.ssdpServer.addUSN('upnp:rootdevice')
this.ssdpServer.addUSN('urn:schemas-upnp-org:device:MediassdpServer:1')
this.ssdpServer.addUSN('urn:schemas-upnp-org:service:ContentDirectory:1')
this.ssdpServer.addUSN('urn:schemas-upnp-org:service:ConnectionManager:1')
this.ssdpServer.start()
Logger.verbose('DVR is now initiated.')
}
channels (req) {
const hostname = req.protocol + '://' + req.get('host')
const lines = []
_.forEach(this.server.channels, (line) => {
lines.push({
GuideNumber: line.channel,
GuideName: line.name,
URL: `${hostname}/channel/${line.channel}`
})
})
Logger.verbose(`Return ${lines.length} channels.`)
return lines
}
lineup (req, res, next) {
Logger.verbose(`Received a lineup request.`)
res.json(this.channels(req))
}
scan (req, res, next) {
Logger.verbose(`Received a scan request.`)
process.nextTick(() => {
res.json({})
})
this.scanPossible = 0
this.scanInProgress = 1
const delay = 10
let progressDelay = delay
let counter = 1
_.forEach(this.server.channels, (item) => {
setTimeout(() => {
this.scanFound = counter
this.scanProgress = Math.floor(counter / this.server.channels.length)
counter = counter + 1
}, progressDelay)
progressDelay = delay + progressDelay
})
setTimeout(() => {
this.scanPossible = 1
this.scanInProgress = 0
}, progressDelay)
}
status () {
let status = {
ScanInProgress: this.scanInProgress,
ScanPossible: this.scanPossible,
Source: 'Cable',
SourceList: ['Cable']
}
if (this.scanInProgress) {
status = {
ScanInProgress: this.scanInProgress,
Progress: this.scanProgress,
Found: this.scanFound
}
}
return status
}
lineupStatus (req, res, next) {
Logger.verbose(`Received a lineup status request.`)
res.json(this.status())
}
discover (req, res, next) {
Logger.verbose(`Received a discover request.`)
var baseUrl = req.protocol + '://' + req.get('host')
const status = {
FriendlyName: this.friendlyName,
Manufacturer: this.manufacturer,
ModelNumber: this.modelName,
FirmwareName: this.firmwareName,
TunerCount: this.tunerCount,
FirmwareVersion: this.firmwareVersion,
DeviceID: this.deviceId,
DeviceAuth: this.deviceAuth,
BaseURL: baseUrl,
LineupURL: `${baseUrl}${this.lineupUrl}`
}
res.json(status)
}
device (req, res, next) {
Logger.verbose(`Received a device identify request.`)
var baseUrl = req.protocol + '://' + req.get('host')
const xmlContent =
`<root xmlns="urn:schemas-upnp-org:device-1-0">
<specVersion>
<major>${this.version.major}</major>
<minor>${this.version.minor}</minor>
</specVersion>
<URLBase>${baseUrl}</URLBase>
<device>
<deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>
<friendlyName>${this.friendlyName}</friendlyName>
<manufacturer>${this.manufacturer}</manufacturer>
<modelName>${this.modelName}</modelName>
<modelNumber>${this.modelNumber}</modelNumber>
<serialNumber>${this.serialNumber}</serialNumber>
<UDN>uuid:${this.deviceId}</UDN>
</device>
</root>`
res.set('Content-Type', 'text/xml')
res.send(xmlContent)
}
}
module.exports = DVR