-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
159 lines (119 loc) · 4.38 KB
/
main.py
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
# PyUPnP - Simple Python UPnP device library built in Twisted
# Copyright (C) 2013 Dean Gardiner <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
from threading import Thread
import time
from twisted.internet import reactor
from pyupnp.device import Device, DeviceIcon
from pyupnp.logr import Logr
from pyupnp.services import register_action
from pyupnp.services.connection_manager import ConnectionManagerService
from pyupnp.services.content_directory import ContentDirectoryService
from pyupnp.services.microsoft.media_receiver_registrar import MediaReceiverRegistrarService
from pyupnp.ssdp import SSDP
from pyupnp.upnp import UPnP
class MediaServerDevice(Device):
deviceType = 'urn:schemas-upnp-org:device:MediaServer:1'
friendlyName = "PyUPnP-MediaServer"
def __init__(self):
Device.__init__(self)
self.uuid = '2fac1234-31f8-11b4-a222-08002b34c003'
self.connectionManager = MSConnectionManager()
self.contentDirectory = MSContentDirectory()
self.mediaReceiverRegistrar = MSMediaReceiverRegistrar()
self.services = [
self.connectionManager,
self.contentDirectory,
self.mediaReceiverRegistrar
]
self.icons = [
DeviceIcon('image/png', 32, 32, 24,
'http://172.25.3.103:52323/MediaRenderer_32x32.png')
]
self.namespaces['dlna'] = 'urn:schemas-dlna-org:device-1-0'
self.extras['dlna:X_DLNADOC'] = 'DMS-1.50'
class MSConnectionManager(ConnectionManagerService):
def __init__(self):
ConnectionManagerService.__init__(self)
self.source_protocol_info = 'http-get:*:*:*'
self.current_connection_ids = '0'
class MSContentDirectory(ContentDirectoryService):
def __init__(self):
ContentDirectoryService.__init__(self)
self.system_update_id = 0
@register_action('X_GetRemoteSharingStatus')
def getRemoteSharingStatus(self):
return {
'Status': 1
}
class MSMediaReceiverRegistrar(MediaReceiverRegistrarService):
def __init__(self):
MediaReceiverRegistrarService.__init__(self)
@register_action('IsAuthorized')
def isAuthorized(self, device_id):
return {
'Result': 1
}
@register_action('RegisterDevice')
def registerDevice(self, request):
Logr.warning("RegisterDevice not implemented")
return {
'RegistrationRespMsg': None
}
@register_action('IsValidated')
def isValidated(self, device_id):
return {
'Result': None
}
class CommandThread(Thread):
def __init__(self, device, upnp, ssdp):
"""
:type device: Device
:type upnp: UPnP
:type ssdp: SSDP
"""
Thread.__init__(self)
self.device = device
self.upnp = upnp
self.ssdp = ssdp
self.running = True
def run(self):
while self.running:
try:
command = 'command_' + raw_input('')
if hasattr(self, command):
getattr(self, command)()
except EOFError:
self.command_stop()
def command_stop(self):
# Send 'byebye' NOTIFY
self.ssdp.clients.sendall_NOTIFY(None, 'ssdp:byebye', True)
# Stop everything
self.upnp.stop()
self.ssdp.stop()
reactor.stop()
self.running = False
if __name__ == '__main__':
Logr.configure(logging.DEBUG)
device = MediaServerDevice()
upnp = UPnP(device)
ssdp = SSDP(device)
upnp.listen()
ssdp.listen()
def event_test():
device.contentDirectory.system_update_id = time.time()
reactor.callLater(5, event_test)
event_test()
r = CommandThread(device, upnp, ssdp)
r.start()
reactor.run()