Browse Source

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

master
klonfish 10 years ago
parent
commit
762d3b4775
  1. 31
      fetapdtest.py
  2. 10
      phoneinterface.py

31
fetapdtest.py

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

10
phoneinterface.py

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

Loading…
Cancel
Save