-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_loop.py
78 lines (54 loc) · 1.56 KB
/
control_loop.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
import threading
import queue
import upnpclient
class ControlLoop(threading.Thread):
def __init__(self, uri, qi, qo, *args, **kwargs):
self.uri = uri
self.qi = qi
self.qo = qo
self.status = 0
super().__init__(*args, **kwargs)
def connect(self, uri):
try:
self.device = upnpclient.Device(uri)
self.status = 1
self.log("Connected to "+ uri)
self.send("connection", "ready")
except:
self.status = 0
self.log("Unable to connect to "+ uri)
self.send("connection", "failed")
def log(self, msg):
print("[Control Loop]", msg)
def send(self, kind, data):
self.qo.put([kind, data])
def _status(self):
threading.Timer(1.0, self._status).start()
if self.status == 1:
self.send("time_info", self.device.AVTransport.GetPositionInfo(InstanceID=0))
self.log("Heartbeat")
def media(self, uri):
if self.status == 1:
self.log("New media: " + uri)
self.device.SetAVTransportURI(InstanceID=0,CurrentURI=uri,CurrentURIMetaData= '')
d.AVTransport.Play(InstanceID=0, Speed="1")
d.AVTransport.Pause(InstanceID=0)
d.AVTransport.Play(InstanceID=0, Speed="1")
def play(self):
if self.status == 1:
self.log("Play function")
self.device.AVTransport.Play(InstanceID=0, Speed='1')
def run(self):
self._status()
self.connect(self.uri)
while True:
work, args = self.qi.get()
self.log("Received command:"+ work)
if work == 'stop':
return
if work == 'play':
self.play()
if work == 'pause':
self.pause()
if work == 'seek':
self.seek(time)