-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinphone.py
53 lines (47 loc) · 1.91 KB
/
linphone.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
import subprocess
from select import select
import re
from threading import Thread
class Linphone(Thread):
def __init__(self, queue):
Thread.__init__(self)
subprocess.call(['killall', '-9', 'linphonec'])
self.pipe = subprocess.Popen(["linphonec", "-c", "/home/pi/.linphonerc"],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.queue = queue
self.daemon = True
def sendDTMF(self, number):
self.pipe.stdin.write(number)
self.pipe.stdin.write("\n")
def answer(self):
self.pipe.stdin.write("answer\n")
def hangup(self):
self.pipe.stdin.write("terminate\n")
def call(self, number):
def run(self):
while 1:
status = select([self.pipe.stdout, self.pipe.stderr], [],
[self.pipe.stdout, self.pipe.stderr])
if status[2] != []:
# error condition, better restart linphonec
self.pipe.close
self.pipe = subprocess.Popen("linphonec",
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
line = status[0][0].readline()
# print(line)
m = re.search('Receiving new incoming call from (.*),', line)
if m:
self.queue.put(['incoming', m.group(1)])
m = re.search('Call .* with (.*) ended', line)
if m:
self.queue.put(['disconnected', m.group(1)])
m = re.search('Call .* with (.*) connected.', line)
if m:
self.queue.put(['connected', m.group(1)])
m = re.search('User is busy.', line)
if m:
self.queue.put(['busy'])
m = re.search('Call .* with (.*) error\\.', line)
if m:
self.queue.put(['error', m.group(1)])