fetapi/fetapdtest.py

391 lines
9.2 KiB
Python
Raw Normal View History

2015-05-18 19:25:48 +02:00
import time
import threading
import Queue as queue
from phoneinterface import PhoneInterface, PhoneEvent
from tuitest import FeApPinConfiguration, FeApUserInterface
2015-06-04 18:42:08 +02:00
import configreader
class DialConfiguration(object):
def __init__(self, dial_timeout, shortcuts):
self.dial_timeout = dial_timeout
self.shortcuts = shortcuts
2015-05-18 19:25:48 +02:00
class IllegalEventError(Exception):
pass
"""
An abstract state, needed to define all possible events.
"""
2015-05-18 19:25:48 +02:00
class AbstractState(object):
def on_registration_in_progress(self):
raise IllegalEventError()
def on_registration_successful(self):
raise IllegalEventError()
2015-05-20 01:11:00 +02:00
def on_registration_lost(self):
raise IllegalEventError()
2015-05-18 19:25:48 +02:00
def on_gabelschalter_up(self):
raise IllegalEventError()
def on_gabelschalter_down(self):
raise IllegalEventError()
def on_incoming_call(self):
raise IllegalEventError()
def on_call_ended(self):
raise IllegalEventError()
def on_call_accepted(self):
raise IllegalEventError()
def on_call_ringing(self):
raise IllegalEventError()
def on_invalid_number(self):
raise IllegalEventError()
2015-05-18 19:25:48 +02:00
def on_nummernschalter_active(self):
raise IllegalEventError()
2015-05-18 19:25:48 +02:00
def on_nummernschalter_input(self, num):
raise IllegalEventError()
def on_timeout(self):
raise IllegalEventError()
2015-05-20 00:20:50 +02:00
def leave(self):
return None
"""
The basic state that every other state inherits from. It defines default
behaviour for some events (overriden if necessary).
"""
class BaseState(AbstractState):
def __init__(self, controller):
self._controller = controller
2015-05-20 01:11:00 +02:00
def on_registration_lost(self):
return InitState
def on_gabelschalter_up(self):
return None
def on_gabelschalter_down(self):
return None
def on_incoming_call(self):
self._controller.phone.decline_call()
def on_call_ended(self):
# When an incoming call is declined, a call_ended event occurs, which
# needs to be ignored, here.
return None
def on_nummernschalter_active(self):
return None
def on_nummernschalter_input(self, num):
return None
class InitState(BaseState):
2015-05-18 19:25:48 +02:00
def __init__(self, controller):
super(InitState, self).__init__(controller)
self._controller.feap.set_schauzeichen(True)
2015-05-18 19:25:48 +02:00
def on_registration_in_progress(self):
print('registration in progress')
return RegisteringState
class RegisteringState(BaseState):
2015-05-18 19:25:48 +02:00
def __init__(self, controller):
super(RegisteringState, self).__init__(controller)
def on_registration_successful(self):
print('registration successful')
self._controller.feap.set_schauzeichen(False)
2015-05-18 19:25:48 +02:00
return IdleState
class IdleState(BaseState):
2015-05-18 19:25:48 +02:00
def on_incoming_call(self):
print('incomfing call')
return SchelltState
def on_gabelschalter_up(self):
print('gabel up')
return DialingState
class SchelltState(BaseState):
2015-05-18 19:25:48 +02:00
def __init__(self, controller):
super(SchelltState, self).__init__(controller)
self._controller.feap.set_wecker(True)
2015-05-18 19:25:48 +02:00
2015-05-20 00:20:50 +02:00
def leave(self):
self._controller.feap.set_wecker(False)
2015-05-18 19:25:48 +02:00
def on_gabelschalter_up(self):
return AcceptingState
def on_call_ended(self):
return IdleState
class AcceptingState(BaseState):
2015-05-18 19:25:48 +02:00
def __init__(self, controller):
super(AcceptingState, self).__init__(controller)
self._controller.phone.accept_call()
2015-05-18 19:25:48 +02:00
def on_call_accepted(self):
return CallRunningState
class CallTerminatingState(BaseState):
2015-05-18 19:25:48 +02:00
def __init__(self, controller):
super(CallTerminatingState, self).__init__(controller)
self._controller.phone.end_call()
2015-05-18 19:25:48 +02:00
def on_call_ended(self):
return IdleState
def on_call_accepted(self):
return None
class ForgottenState(BaseState):
2015-05-18 19:25:48 +02:00
def on_gabelschalter_down(self):
return IdleState
class BusyBeepingState(BaseState):
2015-05-18 19:25:48 +02:00
def __init__(self, controller):
super(BusyBeepingState, self).__init__(controller)
self._controller.phone.play_busy_tone()
2015-05-18 19:25:48 +02:00
2015-05-20 00:20:50 +02:00
def leave(self):
self._controller.phone.stop_playing()
2015-05-18 19:25:48 +02:00
def on_timeout(self):
return ForgottenState
def on_gabelschalter_down(self):
return IdleState
class CallRunningState(BaseState):
2015-05-18 19:25:48 +02:00
def on_gabelschalter_down(self):
return CallTerminatingState
def on_call_ended(self):
return BusyBeepingState
class WecktState(BaseState):
def __init__(self, controller):
super(WecktState, self).__init__(controller)
self._controller.phone.play_ringback_tone()
def leave(self):
self._controller.phone.stop_playing()
2015-05-18 19:25:48 +02:00
def on_gabelschalter_down(self):
return CallTerminatingState
def on_call_ended(self):
return BusyBeepingState
def on_call_accepted(self):
return CallRunningState
class ConnectingState(BaseState):
2015-05-18 19:25:48 +02:00
def on_gabelschalter_down(self):
return CallTerminatingState
def on_call_ringing(self):
return WecktState
def on_call_accepted(self):
return CallRunningState
def on_invalid_number(self):
# TODO: play sound
return BusyBeepingState
def on_call_ended(self):
return BusyBeepingState
class DialingState(BaseState):
2015-05-18 19:25:48 +02:00
def __init__(self, controller):
super(DialingState, self).__init__(controller)
self._controller.phone.play_dial_tone()
self.__dial_tone = True
2015-05-18 19:25:48 +02:00
self.__number = ''
2015-05-20 00:20:50 +02:00
def leave(self):
if self.__dial_tone:
self._controller.phone.stop_playing()
2015-05-18 19:25:48 +02:00
self._controller.abort_timeout()
def on_gabelschalter_down(self):
return IdleState
def on_nummernschalter_active(self):
self._controller.abort_timeout()
if self.__dial_tone:
self._controller.phone.stop_playing()
2015-05-18 19:25:48 +02:00
def on_nummernschalter_input(self, num):
print('nummernschalter: %d' % (num))
if self.__dial_tone:
self._controller.phone.stop_playing()
2015-05-18 19:25:48 +02:00
self.__number += str(num)
self._controller.abort_timeout()
self._controller.set_timeout(5000)
2015-06-04 18:48:40 +02:00
self._controller.phone.read_text(str(num))
2015-05-18 19:25:48 +02:00
def on_timeout(self):
print 'Dialing number:', self.__number
self._controller.phone.call(self.__number)
2015-05-18 19:25:48 +02:00
return ConnectingState
2015-05-18 19:25:48 +02:00
class StateMachineController(object):
def __init__(self, phone, feap):
self.__phone = phone
self.__feap = feap
self.__state = InitState(self)
self.__timeout = None
2015-05-18 19:25:48 +02:00
self.__running = True
self.__evqueue = queue.Queue()
self.__evthread = threading.Thread(target=self.__event_dispatcher)
self.__evthread.start()
def __event_dispatcher(self):
while self.__running:
(evname, evargs, evkwargs) = self.__evqueue.get()
if not evname:
return
print('!!! event: %s' % (evname))
handler = getattr(self.__state, 'on_%s' % (evname))
try:
newstate = handler(*evargs, **evkwargs)
except IllegalEventError:
print('illegal event occured!!!!!!!!!!!!!!!!!!!!', self.__state.__class__.__name__)
if not newstate:
continue
2015-05-20 00:20:50 +02:00
self.__state.leave()
self.abort_timeout()
2015-05-18 19:25:48 +02:00
oldstate = self.__state.__class__
print('%s -> %s' % (oldstate.__name__, newstate.__name__))
self.__state = newstate(self)
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):
2015-05-18 21:50:51 +02:00
self.__timeout = threading.Timer(timeout/1000, self.queue_event, args=['timeout'])
self.__timeout.start()
2015-05-18 19:25:48 +02:00
def abort_timeout(self):
if self.__timeout:
self.__timeout.cancel()
self.__timeout = None
2015-05-18 19:25:48 +02:00
@property
def phone(self):
return self.__phone
2015-05-18 19:25:48 +02:00
@property
def feap(self):
return self.__feap
2015-05-18 19:25:48 +02:00
def stop(self, hard=False):
if hard:
self.__running = False
self.__evqueue.put((None, None, None))
c = None
def gabelschalter_cb(state):
global c
if state == 1:
c.queue_event('gabelschalter_up')
else:
c.queue_event('gabelschalter_down')
def nummernschalter_active_cb():
global c
c.queue_event('nummernschalter_active')
def nummernschalter_done_cb(digit):
2015-05-18 19:25:48 +02:00
global c
c.queue_event('nummernschalter_input', digit)
def phone_cb(event):
if event == PhoneEvent.RegInProgress:
c.queue_event('registration_in_progress')
elif event == PhoneEvent.RegSuccessfull:
c.queue_event('registration_successful')
2015-05-20 01:11:00 +02:00
elif event == PhoneEvent.RegLost:
c.queue_event('registration_lost')
2015-05-18 19:25:48 +02:00
elif event == PhoneEvent.CallIncoming:
c.queue_event('incoming_call')
elif event == PhoneEvent.CallAccepted:
c.queue_event('call_accepted')
elif event == PhoneEvent.CallEnded:
c.queue_event('call_ended')
elif event == PhoneEvent.CallRinging:
c.queue_event('call_ringing')
elif event == PhoneEvent.CallBusy:
c.queue_event('call_ended')
elif event == PhoneEvent.CallInvalidNumber:
c.queue_event('invalid_number')
2015-05-18 19:25:48 +02:00
if __name__ == '__main__':
2015-06-04 18:42:08 +02:00
cfg = configreader.ConfigurationReader()
cfg.read('fetap.ini')
phone = PhoneInterface(cfg.phoneconfig)
feap = FeApUserInterface(cfg.pinconfig)
2015-05-18 19:25:48 +02:00
c = StateMachineController(phone, feap)
feap.add_gabelschalter_callback(gabelschalter_cb)
feap.add_nummernschalter_active_callback(nummernschalter_active_cb)
feap.add_nummernschalter_done_callback(nummernschalter_done_cb)
2015-05-18 19:25:48 +02:00
phone.add_event_cb(phone_cb)
phone.start()
try:
while True:
time.sleep(1)
'''
c.queue_event('gabelschalter_up')
c.queue_event('nummernschalter_input', 4)
c.queue_event('nummernschalter_input', 2)
#c.queue_event('gabelschalter_down')
#c.queue_event('call_accepted')
c.queue_event('timeout')
c.queue_event('call_ringing')
#c.queue_event('gabelschalter_down')
c.queue_event('call_accepted')
c.queue_event('call_ended')
c.queue_event('timeout')
c.queue_event('gabelschalter_down')
'''
except KeyboardInterrupt:
phone.stop()
feap.set_wecker(False)
c.stop()