Implemented event for invalid number, replaced own timeout thread by better builtin threading.Timer

This commit is contained in:
klonfish 2015-05-18 21:39:53 +02:00
parent 20ed8681a1
commit 762d3b4775
2 changed files with 25 additions and 16 deletions

View File

@ -34,6 +34,9 @@ class AbstractState(object):
def on_call_ringing(self):
raise IllegalEventError()
def on_invalid_number(self):
raise IllegalEventError()
def on_nummernschalter_input(self, num):
raise IllegalEventError()
@ -125,6 +128,7 @@ class BusyBeepingState(AbstractState):
return ForgottenState
def on_gabelschalter_down(self):
self.__on_leave()
return IdleState
class CallRunningState(AbstractState):
@ -154,6 +158,10 @@ class ConnectingState(AbstractState):
def on_call_accepted(self):
return CallRunningState
def on_invalid_number(self):
# TODO: play sound
return BusyBeepingState
class DialingState(AbstractState):
def __init__(self, controller):
super(DialingState, self).__init__(controller)
@ -186,6 +194,8 @@ class StateMachineController(object):
self.__phone = phone
self.__feap = feap
self.__timeout = None
self.__running = True
self.__evqueue = queue.Queue()
self.__evthread = threading.Thread(target=self.__event_dispatcher)
@ -210,27 +220,19 @@ class StateMachineController(object):
print('%s -> %s' % (oldstate.__name__, newstate.__name__))
self.__state = newstate(self)
def __timeout_thread(self, timeout):
i = 0
while self.__do_timeout and self.__running:
time.sleep(0.001)
i += 1
if i == timeout and self.__do_timeout:
self.queue_event('timeout')
return
def queue_event(self, evname, *evargs, **evkwargs):
if not hasattr(AbstractState, 'on_%s' % (evname)):
raise ValueError('Illegal event name: %s' % (evname))
self.__evqueue.put((evname, evargs, evkwargs))
def set_timeout(self, timeout):
self.__do_timeout = True
t = threading.Thread(target=self.__timeout_thread, args=(timeout,))
t.start()
self.__timeout = threading.Timer(timeout/1000, lambda: self.queue_event('timeout'))
self.__timeout.start()
def abort_timeout(self):
self.__do_timeout = False
if self.__timeout:
self.__timeout.cancel()
self.__timeout = None
def get_phone(self):
return phone
@ -243,7 +245,6 @@ class StateMachineController(object):
self.__running = False
self.__evqueue.put((None, None, None))
c = None
def gabelschalter_cb(state):
global c
@ -269,6 +270,8 @@ def phone_cb(event):
c.queue_event('call_ended')
elif event == PhoneEvent.CallRinging:
c.queue_event('call_ringing')
elif event == PhoneEvent.CallInvalidNumber:
c.queue_event('invalid_number')
if __name__ == '__main__':
phone = PhoneInterface(None, '.linphonerc')

View File

@ -15,7 +15,8 @@ class PhoneEvent(object):
CallRinging,\
CallAccepted,\
CallEnded,\
CallBusy= range(7)
CallBusy,\
CallInvalidNumber = range(8)
@classmethod
def string(cls, val):
@ -76,8 +77,13 @@ class PhoneInterface(object):
elif state == linphone.CallState.End:
evt = PhoneEvent.CallEnded
elif state == linphone.CallState.Error:
if call.error_info.reason == linphone.Reason.Busy:
error = call.error_info.reason
if error == linphone.Reason.Busy:
evt = PhoneEvent.CallBusy
elif error == linphone.Reason.NotFound:
evt = PhoneEvent.CallInvalidNumber
else:
evt = PhoneEvent.CallEnded
if evt is not None:
for cb in self.__event_cbs: