removed obsolete test file, cosmetics

This commit is contained in:
Frederic 2017-04-28 18:57:43 +02:00
parent c21b6f9e96
commit 2bdf0f7485
5 changed files with 54 additions and 235 deletions

View file

@ -25,6 +25,7 @@ class FeApPinConfiguration(object):
self.pin_wecker_b = pin_wecker_b self.pin_wecker_b = pin_wecker_b
self.invert_gs = invert_gs self.invert_gs = invert_gs
class FeApUserInterface(object): class FeApUserInterface(object):
def __init__(self, pinconfig): def __init__(self, pinconfig):
self.__pinconfig = pinconfig self.__pinconfig = pinconfig
@ -115,6 +116,7 @@ class FeApUserInterface(object):
def set_schauzeichen(self, enabled): def set_schauzeichen(self, enabled):
gpio.output(self.__pinconfig.pin_schauzeichen, 1 if enabled else 0) gpio.output(self.__pinconfig.pin_schauzeichen, 1 if enabled else 0)
if __name__ == '__main__': if __name__ == '__main__':
gpio.setmode(gpio.BOARD) gpio.setmode(gpio.BOARD)
pinconfig = FeApPinConfiguration() pinconfig = FeApPinConfiguration()

View file

@ -19,33 +19,27 @@ class ConfigurationReader(object):
'default_proxy': '' 'default_proxy': ''
} }
def __init__(self): def __init__(self):
self.__cp = ConfigParser.ConfigParser(defaults=ConfigurationReader.DEFAULTS) self.__cp = ConfigParser.ConfigParser(defaults=ConfigurationReader.DEFAULTS)
self.pinconfig = None self.pinconfig = None
self.dialconfig = None self.dialconfig = None
self.phoneconfig = None self.phoneconfig = None
def __get_global_val(self, option): def __get_global_val(self, option):
return self.__cp.get('fetapd', option) return self.__cp.get('fetapd', option)
def __get_global_val_int(self, option): def __get_global_val_int(self, option):
return int(self.__cp.get('fetapd', option)) return int(self.__cp.get('fetapd', option))
def __get_global_val_bool(self, option): def __get_global_val_bool(self, option):
return self.__cp.get('fetapd', option).lower() in ['true', 'yes', '1'] return self.__cp.get('fetapd', option).lower() in ['true', 'yes', '1']
def __get_proxy_val(self, proxyname, option): def __get_proxy_val(self, proxyname, option):
return self.__cp.get('proxy_'+proxyname, option) return self.__cp.get('proxy_'+proxyname, option)
def __get_proxy_val_int(self, proxyname, option): def __get_proxy_val_int(self, proxyname, option):
return self.__cp.getint('proxy_'+proxyname, option) return self.__cp.getint('proxy_'+proxyname, option)
def __read_shortcuts(self): def __read_shortcuts(self):
fname = self.__get_global_val('shortcuts_file') fname = self.__get_global_val('shortcuts_file')
shortcuts = {} shortcuts = {}

View file

@ -3,6 +3,7 @@ import time
import threading import threading
import subprocess import subprocess
class PhoneProxyConfiguration(object): class PhoneProxyConfiguration(object):
def __init__(self, name, proxy, identity, username, password, realm, def __init__(self, name, proxy, identity, username, password, realm,
prefix): prefix):
@ -14,6 +15,7 @@ class PhoneProxyConfiguration(object):
self.realm = realm self.realm = realm
self.prefix = prefix self.prefix = prefix
class PhoneConfiguration(object): class PhoneConfiguration(object):
def __init__(self, sound_device, incoming_timeout, linphone_config, def __init__(self, sound_device, incoming_timeout, linphone_config,
default_proxy, proxies, stun_server): default_proxy, proxies, stun_server):
@ -24,6 +26,7 @@ class PhoneConfiguration(object):
self.proxies = proxies self.proxies = proxies
self.stun_server = stun_server self.stun_server = stun_server
class PhoneEvent(object): class PhoneEvent(object):
RegInProgress,\ RegInProgress,\
RegSuccessfull,\ RegSuccessfull,\
@ -41,6 +44,7 @@ class PhoneEvent(object):
if v == val: if v == val:
return k return k
class PhoneInterface(object): class PhoneInterface(object):
def __init__(self, config): def __init__(self, config):
cbs = { cbs = {
@ -56,7 +60,9 @@ class PhoneInterface(object):
# Create and add all proxy configs # Create and add all proxy configs
for p in config.proxies: for p in config.proxies:
ainfo = self.__core.create_auth_info(p.username, p.username, p.password, None, p.realm, None) ainfo = self.__core.create_auth_info(p.username, p.username,
p.password, None, p.realm,
None)
self.__core.add_auth_info(ainfo) self.__core.add_auth_info(ainfo)
pconf = self.__core.create_proxy_config() pconf = self.__core.create_proxy_config()
@ -76,8 +82,11 @@ class PhoneInterface(object):
self.__core.default_proxy_config = pconf self.__core.default_proxy_config = pconf
self.__audioproc = None self.__audioproc = None
aplay = subprocess.Popen(['aplay', '-qD%s' % config.sound_device], stdin=subprocess.PIPE) aplay = subprocess.Popen(['aplay', '-qD%s' % config.sound_device],
self.__ttsproc = subprocess.Popen(['espeak', '-p10', '--stdout'], stdin=subprocess.PIPE, stdout=aplay.stdin) stdin=subprocess.PIPE)
self.__ttsproc = subprocess.Popen(['espeak', '-p10', '--stdout'],
stdin=subprocess.PIPE,
stdout=aplay.stdin)
# Set default parameters overriding the ones from the given config file # Set default parameters overriding the ones from the given config file
self.__core.set_user_agent('FeTAp 615', '0.1') self.__core.set_user_agent('FeTAp 615', '0.1')
@ -178,21 +187,26 @@ class PhoneInterface(object):
def play_dial_tone(self): def play_dial_tone(self):
self.stop_playing() self.stop_playing()
self.__audioproc = subprocess.Popen(['play', '-nq', 'synth', 'sine', '425'], self.__audioproc = subprocess.Popen(
env = {'AUDIODRIVER': 'alsa', ['play', '-nq', 'synth', 'sine', '425'],
'AUDIODEV': self.__config.sound_device}) env={'AUDIODRIVER': 'alsa', 'AUDIODEV': self.__config.sound_device}
)
def play_ringback_tone(self): def play_ringback_tone(self):
self.stop_playing() self.stop_playing()
self.__audioproc = subprocess.Popen(['play', '-nq', 'synth', '1', 'sine', '425', 'pad', '4@1', 'repeat', '1000'], self.__audioproc = subprocess.Popen(
env = {'AUDIODRIVER': 'alsa', ['play', '-nq', 'synth', '1', 'sine', '425', 'pad', '4@1',
'AUDIODEV': self.__config.sound_device}) 'repeat', '1000'],
env={'AUDIODRIVER': 'alsa', 'AUDIODEV': self.__config.sound_device}
)
def play_busy_tone(self): def play_busy_tone(self):
self.stop_playing() self.stop_playing()
self.__audioproc = subprocess.Popen(['play', '-nq', 'synth', '0.48', 'sine', '425', 'pad', '0.48@0.48', 'repeat', '1000'], self.__audioproc = subprocess.Popen(
env = {'AUDIODRIVER': 'alsa', ['play', '-nq', 'synth', '0.48', 'sine', '425', 'pad', '0.48@0.48',
'AUDIODEV': self.__config.sound_device}) 'repeat', '1000'],
env={'AUDIODRIVER': 'alsa', 'AUDIODEV': self.__config.sound_device}
)
def stop_playing(self): def stop_playing(self):
if self.__audioproc is not None: if self.__audioproc is not None:

View file

@ -1,21 +1,23 @@
import threading import threading
import Queue as queue import Queue as queue
class DialConfiguration(object): class DialConfiguration(object):
def __init__(self, dial_timeout, shortcuts, blacklist): def __init__(self, dial_timeout, shortcuts, blacklist):
self.dial_timeout = dial_timeout self.dial_timeout = dial_timeout
self.shortcuts = shortcuts self.shortcuts = shortcuts
self.blacklist = blacklist self.blacklist = blacklist
class IllegalEventError(Exception): class IllegalEventError(Exception):
pass pass
"""
An abstract state, needed to define all possible events.
"""
class AbstractState(object): class AbstractState(object):
"""
An abstract state, needed to define all possible events.
"""
def on_registration_in_progress(self): def on_registration_in_progress(self):
raise IllegalEventError() raise IllegalEventError()
@ -59,12 +61,12 @@ class AbstractState(object):
return None return None
"""
The basic state that every other state inherits from. It defines default
behaviour for some events (overriden if necessary).
"""
class BaseState(AbstractState): class BaseState(AbstractState):
"""
The basic state that every other state inherits from. It defines default
behaviour for some events (overriden if necessary).
"""
def __init__(self, controller): def __init__(self, controller):
self._controller = controller self._controller = controller
@ -101,6 +103,7 @@ class InitState(BaseState):
print('registration in progress') print('registration in progress')
return RegisteringState return RegisteringState
class RegisteringState(BaseState): class RegisteringState(BaseState):
def __init__(self, controller): def __init__(self, controller):
super(RegisteringState, self).__init__(controller) super(RegisteringState, self).__init__(controller)
@ -110,6 +113,7 @@ class RegisteringState(BaseState):
self._controller.feap.set_schauzeichen(False) self._controller.feap.set_schauzeichen(False)
return IdleState return IdleState
class IdleState(BaseState): class IdleState(BaseState):
def on_incoming_call(self): def on_incoming_call(self):
print('incomfing call') print('incomfing call')
@ -126,6 +130,7 @@ class IdleState(BaseState):
print('gabel up') print('gabel up')
return DialingState return DialingState
class SchelltState(BaseState): class SchelltState(BaseState):
def __init__(self, controller): def __init__(self, controller):
super(SchelltState, self).__init__(controller) super(SchelltState, self).__init__(controller)
@ -140,6 +145,7 @@ class SchelltState(BaseState):
def on_call_ended(self): def on_call_ended(self):
return IdleState return IdleState
class AcceptingState(BaseState): class AcceptingState(BaseState):
def __init__(self, controller): def __init__(self, controller):
super(AcceptingState, self).__init__(controller) super(AcceptingState, self).__init__(controller)
@ -148,6 +154,7 @@ class AcceptingState(BaseState):
def on_call_accepted(self): def on_call_accepted(self):
return CallRunningState return CallRunningState
class CallTerminatingState(BaseState): class CallTerminatingState(BaseState):
def __init__(self, controller): def __init__(self, controller):
super(CallTerminatingState, self).__init__(controller) super(CallTerminatingState, self).__init__(controller)
@ -159,10 +166,12 @@ class CallTerminatingState(BaseState):
def on_call_accepted(self): def on_call_accepted(self):
return None return None
class ForgottenState(BaseState): class ForgottenState(BaseState):
def on_gabelschalter_down(self): def on_gabelschalter_down(self):
return IdleState return IdleState
class BusyBeepingState(BaseState): class BusyBeepingState(BaseState):
def __init__(self, controller): def __init__(self, controller):
super(BusyBeepingState, self).__init__(controller) super(BusyBeepingState, self).__init__(controller)
@ -177,6 +186,7 @@ class BusyBeepingState(BaseState):
def on_gabelschalter_down(self): def on_gabelschalter_down(self):
return IdleState return IdleState
class CallRunningState(BaseState): class CallRunningState(BaseState):
def on_gabelschalter_down(self): def on_gabelschalter_down(self):
return CallTerminatingState return CallTerminatingState
@ -184,6 +194,7 @@ class CallRunningState(BaseState):
def on_call_ended(self): def on_call_ended(self):
return BusyBeepingState return BusyBeepingState
class WecktState(BaseState): class WecktState(BaseState):
def __init__(self, controller): def __init__(self, controller):
super(WecktState, self).__init__(controller) super(WecktState, self).__init__(controller)
@ -201,6 +212,7 @@ class WecktState(BaseState):
def on_call_accepted(self): def on_call_accepted(self):
return CallRunningState return CallRunningState
class ConnectingState(BaseState): class ConnectingState(BaseState):
def on_gabelschalter_down(self): def on_gabelschalter_down(self):
return CallTerminatingState return CallTerminatingState
@ -218,6 +230,7 @@ class ConnectingState(BaseState):
def on_call_ended(self): def on_call_ended(self):
return BusyBeepingState return BusyBeepingState
class DialingState(BaseState): class DialingState(BaseState):
def __init__(self, controller): def __init__(self, controller):
super(DialingState, self).__init__(controller) super(DialingState, self).__init__(controller)
@ -244,7 +257,8 @@ class DialingState(BaseState):
self._controller.phone.stop_playing() self._controller.phone.stop_playing()
self.__number += str(num) self.__number += str(num)
self._controller.abort_timeout() self._controller.abort_timeout()
self._controller.set_timeout(self._controller.dialconfig.dial_timeout * 1000) self._controller.set_timeout(self._controller.dialconfig.dial_timeout
* 1000)
self._controller.phone.read_text(str(num)) self._controller.phone.read_text(str(num))
def on_timeout(self): def on_timeout(self):
@ -300,7 +314,8 @@ class StateMachineController(object):
self.__evqueue.put((evname, evargs, evkwargs)) self.__evqueue.put((evname, evargs, evkwargs))
def set_timeout(self, timeout): def set_timeout(self, timeout):
self.__timeout = threading.Timer(timeout/1000, self.queue_event, args=['timeout']) self.__timeout = threading.Timer(timeout/1000,
self.queue_event, args=['timeout'])
self.__timeout.start() self.__timeout.start()
def abort_timeout(self): def abort_timeout(self):

View file

@ -1,206 +0,0 @@
import threading
import Queue as queue
class IllegalEventError(Exception):
pass
class AbstractState(object):
def __init__(self, controller):
pass
def on_registration_in_progress(self):
raise IllegalEventError()
def on_registration_successful(self):
raise IllegalEventError()
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_nummernschalter_input(self, num):
raise IllegalEventError()
def on_timeout(self):
raise IllegalEventError()
class InitState(AbstractState):
def on_registration_in_progress(self):
print('registration in progress')
return RegisteringState
class RegisteringState(AbstractState):
def on_registration_successful(self):
print('registration successfull')
return IdleState
class IdleState(AbstractState):
def on_incoming_call(self):
print('incomfing call')
def on_gabelschalter_up(self):
print('gabel up')
return DialingState
def on_gabelschalter_down(self):
pass
def on_nummernschalter_input(self, x):
pass
class SchelltState(AbstractState):
def on_gabelschalter_up(self):
return AcceptingState
def on_call_ended(self):
return IdleState
class AcceptingState(AbstractState):
def on_call_accepted(self):
return CallRunningState
class CallTerminatingState(AbstractState):
def on_call_ended(self):
return IdleState
def on_call_accepted(self):
return None
class ForgottenState(AbstractState):
def on_gabelschalter_down(self):
return IdleState
class BusyBeepingState(AbstractState):
def on_timeout(self):
return ForgottenState
def on_gabelschalter_down(self):
return IdleState
class CallRunningState(AbstractState):
def on_gabelschalter_down(self):
return CallTerminatingState
def on_call_ended(self):
return BusyBeepingState
class WecktState(AbstractState):
def on_gabelschalter_down(self):
return CallTerminatingState
def on_call_ended(self):
return BusyBeepingState
def on_call_accepted(self):
return CallRunningState
class ConnectingState(AbstractState):
def on_gabelschalter_down(self):
return CallTerminatingState
def on_call_ringing(self):
return WecktState
class DialingState(AbstractState):
def on_gabelschalter_down(self):
return IdleState
def on_nummernschalter_input(self, num):
print('nummernschalter: %d' % (num))
def on_timeout(self):
return ConnectingState
class TelefonapparatUserInterface(object):
def __init__(self):
pass
def add_gabelschalter_callback(self, cb):
pass
def add_nummernschalter_callback(self, cb):
pass
def set_wecker(self, enabled):
pass
def set_schauzeichen(self, enabled):
pass
class StateMachineController(object):
def __init__(self):
self.__state = InitState(self)
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!!!!!!!!!!!!!!!!!!!!')
if not newstate:
continue
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 stop(self, hard=False):
if hard:
self.__running = False
self.__evqueue.put((None, None, None))
if __name__ == '__main__':
c = StateMachineController()
c.queue_event('registration_in_progress')
c.queue_event('registration_successful')
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')
c.stop()