initial use of eventManager using heartbeat

This commit is contained in:
Chrys 2017-06-21 11:40:10 +02:00
parent df834ca372
commit 461e9bb5b3
3 changed files with 20 additions and 10 deletions

View File

@ -4,7 +4,7 @@
# Fenrir TTY screen reader # Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers. # By Chrys, Storm Dragon, and contributers.
#from core import debug from core import debug
from queue import Empty from queue import Empty
import time import time
from enum import Enum from enum import Enum
@ -21,6 +21,7 @@ class fenrirEventType(Enum):
PlugInputDevice = 5 PlugInputDevice = 5
BrailleFlush = 6 BrailleFlush = 6
ScreenChanged = 7 ScreenChanged = 7
HeartBeat = 8 # for time based scheduling
def __int__(self): def __int__(self):
return self.value return self.value
def __str__(self): def __str__(self):
@ -35,9 +36,13 @@ class eventManager():
self.cleanEventQueue() self.cleanEventQueue()
def initialize(self, environment): def initialize(self, environment):
self.env = environment self.env = environment
self.addSimpleEventThread(fenrirEventType.HeartBeat, self.timerProcess)
def shutdown(self): def shutdown(self):
self.terminateAllProcesses() self.terminateAllProcesses()
self.cleanEventQueue() self.cleanEventQueue()
def timerProcess(self):
time.sleep(0.005)
return time.time()
def terminateAllProcesses(self): def terminateAllProcesses(self):
for proc in self._eventProcesses: for proc in self._eventProcesses:
try: try:
@ -68,6 +73,9 @@ class eventManager():
pass pass
elif event['Type'] == fenrirEventType.ScreenChanged: elif event['Type'] == fenrirEventType.ScreenChanged:
pass pass
elif event['Type'] == fenrirEventType.HeartBeat:
self.env['runtime']['fenrirManager'].handleProcess()
print('HeartBeat at ' + str(event['Type']) + ' ' +str(event['Data'] ))
def startMainEventLoop(self): def startMainEventLoop(self):
self._mainLoopRunning.value = True self._mainLoopRunning.value = True
while(self._mainLoopRunning.value): while(self._mainLoopRunning.value):
@ -83,7 +91,7 @@ class eventManager():
t.start() t.start()
def addSimpleEventThread(self, event, function): def addSimpleEventThread(self, event, function):
self._mainLoopRunning.value = True self._mainLoopRunning.value = True
t = Process(target=self.SimpleEventWorkerThread, args=(event, function)) t = Process(target=self.simpleEventWorkerThread, args=(event, function))
self._eventProcesses.append(t) self._eventProcesses.append(t)
t.start() t.start()
def cleanEventQueue(self): def cleanEventQueue(self):

View File

@ -22,7 +22,7 @@ class fenrirManager():
if not cliArgs: if not cliArgs:
return return
try: try:
self.environment = settingsManager.settingsManager().initFenrirConfig(cliArgs) self.environment = settingsManager.settingsManager().initFenrirConfig(cliArgs, self)
if not self.environment: if not self.environment:
raise RuntimeError('Cannot Initialize. Maybe the configfile is not available or not parseable') raise RuntimeError('Cannot Initialize. Maybe the configfile is not available or not parseable')
except RuntimeError: except RuntimeError:
@ -46,11 +46,8 @@ class fenrirManager():
def proceed(self): def proceed(self):
if not self.initialized: if not self.initialized:
return return
while(self.environment['general']['running']): self.environment['runtime']['eventManager'].startMainEventLoop()
try:
self.handleProcess()
except Exception as e:
self.environment['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
self.shutdown() self.shutdown()
def handleProcess(self): def handleProcess(self):
@ -118,7 +115,10 @@ class fenrirManager():
self.shutdownRequest() self.shutdownRequest()
def shutdown(self): def shutdown(self):
self.environment['eventManager'].stopMainEventLoop()
self.environment['runtime']['outputManager'].presentText(_("Quit Fenrir"), soundIcon='ScreenReaderOff', interrupt=True) self.environment['runtime']['outputManager'].presentText(_("Quit Fenrir"), soundIcon='ScreenReaderOff', interrupt=True)
self.environment['eventManager'].cleanEventQueue()
self.environment['eventManager'].stopMainEventLoop(True)
for currManager in self.environment['general']['managerList']: for currManager in self.environment['general']['managerList']:
if self.environment['runtime'][currManager]: if self.environment['runtime'][currManager]:
self.environment['runtime'][currManager].shutdown() self.environment['runtime'][currManager].shutdown()

View File

@ -242,7 +242,7 @@ class settingsManager():
optionArgDict[section] = {} optionArgDict[section] = {}
optionArgDict[section][option] = str(value) optionArgDict[section][option] = str(value)
return optionArgDict return optionArgDict
def initFenrirConfig(self, cliArgs, environment = environment.environment): def initFenrirConfig(self, cliArgs, fenrirManager = None, environment = environment.environment):
settingsRoot = '/etc/fenrir/' settingsRoot = '/etc/fenrir/'
settingsFile = cliArgs.setting settingsFile = cliArgs.setting
soundRoot = '/usr/share/sounds/fenrir/' soundRoot = '/usr/share/sounds/fenrir/'
@ -304,6 +304,8 @@ class settingsManager():
else: else:
environment['runtime']['settingsManager'].loadDicts(self.getSetting('general','punctuationProfile')) environment['runtime']['settingsManager'].loadDicts(self.getSetting('general','punctuationProfile'))
if fenrirManager:
environment['runtime']['fenrirManager'] = fenrirManager
environment['runtime']['eventManager'] = eventManager.eventManager() environment['runtime']['eventManager'] = eventManager.eventManager()
environment['runtime']['eventManager'].initialize(environment) environment['runtime']['eventManager'].initialize(environment)
environment['runtime']['inputManager'] = inputManager.inputManager() environment['runtime']['inputManager'] = inputManager.inputManager()