From 461e9bb5b36d04ffe92bac3b2248a8e22de4ec3d Mon Sep 17 00:00:00 2001 From: Chrys Date: Wed, 21 Jun 2017 11:40:10 +0200 Subject: [PATCH] initial use of eventManager using heartbeat --- src/fenrir/core/eventManager.py | 14 +++++++++++--- src/fenrir/core/fenrirManager.py | 12 ++++++------ src/fenrir/core/settingsManager.py | 4 +++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/fenrir/core/eventManager.py b/src/fenrir/core/eventManager.py index 0f926bbb..e7bb728e 100644 --- a/src/fenrir/core/eventManager.py +++ b/src/fenrir/core/eventManager.py @@ -4,7 +4,7 @@ # Fenrir TTY screen reader # By Chrys, Storm Dragon, and contributers. -#from core import debug +from core import debug from queue import Empty import time from enum import Enum @@ -21,6 +21,7 @@ class fenrirEventType(Enum): PlugInputDevice = 5 BrailleFlush = 6 ScreenChanged = 7 + HeartBeat = 8 # for time based scheduling def __int__(self): return self.value def __str__(self): @@ -34,10 +35,14 @@ class eventManager(): self._eventQueue = Queue() # multiprocessing.Queue() self.cleanEventQueue() def initialize(self, environment): - self.env = environment + self.env = environment + self.addSimpleEventThread(fenrirEventType.HeartBeat, self.timerProcess) def shutdown(self): self.terminateAllProcesses() self.cleanEventQueue() + def timerProcess(self): + time.sleep(0.005) + return time.time() def terminateAllProcesses(self): for proc in self._eventProcesses: try: @@ -68,6 +73,9 @@ class eventManager(): pass elif event['Type'] == fenrirEventType.ScreenChanged: pass + elif event['Type'] == fenrirEventType.HeartBeat: + self.env['runtime']['fenrirManager'].handleProcess() + print('HeartBeat at ' + str(event['Type']) + ' ' +str(event['Data'] )) def startMainEventLoop(self): self._mainLoopRunning.value = True while(self._mainLoopRunning.value): @@ -83,7 +91,7 @@ class eventManager(): t.start() def addSimpleEventThread(self, event, function): self._mainLoopRunning.value = True - t = Process(target=self.SimpleEventWorkerThread, args=(event, function)) + t = Process(target=self.simpleEventWorkerThread, args=(event, function)) self._eventProcesses.append(t) t.start() def cleanEventQueue(self): diff --git a/src/fenrir/core/fenrirManager.py b/src/fenrir/core/fenrirManager.py index cb8ffda8..d8bfff1a 100644 --- a/src/fenrir/core/fenrirManager.py +++ b/src/fenrir/core/fenrirManager.py @@ -22,7 +22,7 @@ class fenrirManager(): if not cliArgs: return try: - self.environment = settingsManager.settingsManager().initFenrirConfig(cliArgs) + self.environment = settingsManager.settingsManager().initFenrirConfig(cliArgs, self) if not self.environment: raise RuntimeError('Cannot Initialize. Maybe the configfile is not available or not parseable') except RuntimeError: @@ -46,11 +46,8 @@ class fenrirManager(): def proceed(self): if not self.initialized: return - while(self.environment['general']['running']): - try: - self.handleProcess() - except Exception as e: - self.environment['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR) + self.environment['runtime']['eventManager'].startMainEventLoop() + self.shutdown() def handleProcess(self): @@ -118,7 +115,10 @@ class fenrirManager(): self.shutdownRequest() def shutdown(self): + self.environment['eventManager'].stopMainEventLoop() 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']: if self.environment['runtime'][currManager]: self.environment['runtime'][currManager].shutdown() diff --git a/src/fenrir/core/settingsManager.py b/src/fenrir/core/settingsManager.py index 5d11e80b..aad79688 100644 --- a/src/fenrir/core/settingsManager.py +++ b/src/fenrir/core/settingsManager.py @@ -242,7 +242,7 @@ class settingsManager(): optionArgDict[section] = {} optionArgDict[section][option] = str(value) return optionArgDict - def initFenrirConfig(self, cliArgs, environment = environment.environment): + def initFenrirConfig(self, cliArgs, fenrirManager = None, environment = environment.environment): settingsRoot = '/etc/fenrir/' settingsFile = cliArgs.setting soundRoot = '/usr/share/sounds/fenrir/' @@ -304,6 +304,8 @@ class settingsManager(): else: environment['runtime']['settingsManager'].loadDicts(self.getSetting('general','punctuationProfile')) + if fenrirManager: + environment['runtime']['fenrirManager'] = fenrirManager environment['runtime']['eventManager'] = eventManager.eventManager() environment['runtime']['eventManager'].initialize(environment) environment['runtime']['inputManager'] = inputManager.inputManager()