add different code paths for events; split up mainloop;

This commit is contained in:
chrys 2017-06-25 15:32:47 +02:00
parent 68c6ec0cc4
commit aa618cc8f5
6 changed files with 57 additions and 148 deletions

View File

@ -1,74 +0,0 @@
#!/bin/python
import time
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
from core import debug
import time
import datetime
class command():
def __init__(self):
pass
def initialize(self, environment):
self.env = environment
self.lastTime = datetime.datetime.now()
self.lastDateString = ''
self.lastTimeString = ''
def shutdown(self):
pass
def getDescription(self):
return 'No Description found'
def run(self):
if not self.env['runtime']['settingsManager'].getSettingAsBool('time', 'enabled'):
return
onMinutes = self.env['runtime']['settingsManager'].getSetting('time', 'onMinutes')
delaySec = self.env['runtime']['settingsManager'].getSettingAsInt('time', 'delaySec')
# no need
if onMinutes == '' and delaySec <= 0:
return
onMinutes = onMinutes.split(',')
now = datetime.datetime.now()
# ignore onMinutes if there is a delaySec
if delaySec > 0:
if int((now-self.lastTime).total_seconds()) < delaySec:
return
else:
# shoul announce?
if not str(now.minute) in onMinutes:
return
# already announced?
if now.hour == self.lastTime.hour:
if now.minute == self.lastTime.minute:
return
dateFormat = self.env['runtime']['settingsManager'].getSetting('general', 'dateFormat')
dateString = datetime.datetime.strftime(now, dateFormat)
presentDate = self.env['runtime']['settingsManager'].getSettingAsBool('time', 'presentDate') and \
self.lastDateString != dateString
presentTime = self.env['runtime']['settingsManager'].getSettingAsBool('time', 'presentTime')
# no changed value to announce
if not (presentDate or presentTime):
return
timeFormat = self.env['runtime']['settingsManager'].getSetting('general', 'timeFormat')
timeString = datetime.datetime.strftime(now, timeFormat)
if self.env['runtime']['settingsManager'].getSettingAsBool('time', 'interrupt'):
self.env['runtime']['outputManager'].interruptOutput()
if self.env['runtime']['settingsManager'].getSettingAsBool('time', 'announce'):
self.env['runtime']['outputManager'].playSoundIcon('announce')
if presentTime:
# present the time
self.env['runtime']['outputManager'].presentText(_('Autotime: {0}').format(timeString), soundIcon='', interrupt=False)
# and date if changes
if presentDate:
self.env['runtime']['outputManager'].presentText(dateString , soundIcon='', interrupt=False)
self.lastDateString = dateString
self.lastTime = datetime.datetime.now()
self.lastTimeString = timeString
def setCallback(self, callback):
pass

View File

@ -1,36 +0,0 @@
#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
from core import debug
class command():
def __init__(self):
pass
def initialize(self, environment):
self.env = environment
def shutdown(self):
pass
def getDescription(self):
return 'No Description found'
def run(self):
if not self.env['runtime']['settingsManager'].getSettingAsBool('speech', 'autoReadIncoming'):
return
# is there something to read?
#if not self.env['runtime']['screenManager'].isDelta():
# return
# its a cursor movement (experimental) - maybe also check current shortcut string?
if abs(self.env['screen']['newCursor']['x'] - self.env['screen']['oldCursor']['x']) >= 1:
if len(self.env['screen']['newDelta'].strip(' \n\t')) <= 2:
return
#if abs(self.env['screen']['newCursor']['y'] - self.env['screen']['oldCursor']['y']) = 1:
# return
self.env['runtime']['outputManager'].presentText(self.env['screen']['newDelta'], interrupt=False, flush=False)
def setCallback(self, callback):
pass

View File

@ -26,7 +26,7 @@ class eventManager():
self.cleanEventQueue()
def heartBeatTimer(self):
try:
time.sleep(8)
time.sleep(0.5)
except:
pass
#self.env['runtime']['settingsManager'].getSettingAsFloat('screen', 'screenUpdateDelay')
@ -50,15 +50,15 @@ class eventManager():
if event['Type'] == fenrirEventType.Ignore:
return
elif event['Type'] == fenrirEventType.StopMainLoop:
self._mainLoopRunning.value = 0
self.handleStopMainLoop()
print('stop')
return
elif event['Type'] == fenrirEventType.ScreenUpdate:
self.env['runtime']['fenrirManager'].handleProcess()
self.env['runtime']['fenrirManager'].handleScreenUpdate()
print(self._eventQueue.qsize())
print('ScreenUpdate')
elif event['Type'] == fenrirEventType.KeyboardInput:
self.env['runtime']['fenrirManager'].handleProcess()
self.env['runtime']['fenrirManager'].handleInput()
print(self._eventQueue.qsize())
print('KeyboardInput')
elif event['Type'] == fenrirEventType.BrailleInput:
@ -68,11 +68,12 @@ class eventManager():
elif event['Type'] == fenrirEventType.BrailleFlush:
pass
elif event['Type'] == fenrirEventType.ScreenChanged:
self.env['runtime']['fenrirManager'].handleProcess()
self.env['runtime']['fenrirManager'].handleScreenChange()
print(self._eventQueue.qsize())
print('ScreenChanged')
elif event['Type'] == fenrirEventType.HeartBeat:
self.env['runtime']['fenrirManager'].handleProcess()
# run timer actions
#self.env['runtime']['fenrirManager'].handleProcess()
print(self._eventQueue.qsize())
print('HeartBeat at {0} {1}'.format(event['Type'], event['Data'] ))
@ -84,6 +85,9 @@ class eventManager():
st = time.time()
self.proceedEventLoop()
#print('ALL loop ' + str(time.time() - st))
def handleStopMainLoop(self):
self._mainLoopRunning.value = 0
time.sleep(3.5)
def stopMainEventLoop(self, Force = False):
if Force:
self._mainLoopRunning.value = 0
@ -126,9 +130,9 @@ class eventManager():
while self.isMainEventLoopRunning():
try:
if args:
function(eventQueue, args)
function(self._mainLoopRunning, eventQueue, args)
else:
function(eventQueue)
function(self._mainLoopRunning, eventQueue)
except Exception as e:
print(e)
@ -141,7 +145,7 @@ class eventManager():
Data = None
try:
if args != None:
Data = function(args)
Data = function(self._mainLoopRunning, args)
else:
Data = function()
except Exception as e:

View File

@ -47,15 +47,10 @@ class fenrirManager():
if not self.initialized:
return
self.environment['runtime']['eventManager'].startMainEventLoop()
self.shutdown()
def handleProcess(self):
def handleInput(self):
eventReceived = self.environment['runtime']['inputManager'].getInputEvent()
startTime = time.time()
if not eventReceived:
if not self.environment['runtime']['screenManager'].isSuspendingScreen():
self.environment['runtime']['inputManager'].updateInputDevices()
startTime = time.time()
if eventReceived:
self.prepareCommand()
if not (self.wasCommand or self.environment['general']['tutorialMode']) or self.environment['runtime']['screenManager'].isSuspendingScreen():
@ -69,23 +64,39 @@ class fenrirManager():
if self.environment['input']['keyForeward'] > 0:
self.environment['input']['keyForeward'] -=1
self.environment['runtime']['screenManager'].update('onInput')
self.environment['runtime']['commandManager'].executeDefaultTrigger('onInput')
else:
self.environment['runtime']['screenManager'].update('onUpdate')
self.environment['runtime']['commandManager'].executeDefaultTrigger('onInput')
self.handleCommands()
def handleScreenChange(self):
self.environment['runtime']['screenManager'].update('onUpdate')
if self.environment['runtime']['applicationManager'].isApplicationChange():
self.environment['runtime']['commandManager'].executeDefaultTrigger('onApplicationChange')
self.environment['runtime']['commandManager'].executeSwitchTrigger('onSwitchApplicationProfile', \
self.environment['runtime']['applicationManager'].getPrevApplication(), \
self.environment['runtime']['applicationManager'].getCurrentApplication())
if self.environment['runtime']['screenManager'].isScreenChange():
if not self.environment['runtime']['screenManager'].isScreenChange():
self.environment['runtime']['commandManager'].executeDefaultTrigger('onScreenChanged')
else:
self.environment['runtime']['commandManager'].executeDefaultTrigger('onScreenUpdate')
#self.environment['runtime']['outputManager'].brailleText(flush=False)
self.handleCommands()
#print(time.time()-startTime)
def handleScreenUpdate(self):
self.environment['runtime']['screenManager'].update('onUpdate')
if self.environment['runtime']['applicationManager'].isApplicationChange():
self.environment['runtime']['commandManager'].executeDefaultTrigger('onApplicationChange')
self.environment['runtime']['commandManager'].executeSwitchTrigger('onSwitchApplicationProfile', \
self.environment['runtime']['applicationManager'].getPrevApplication(), \
self.environment['runtime']['applicationManager'].getCurrentApplication())
if not self.environment['runtime']['screenManager'].isScreenChange():
self.environment['runtime']['commandManager'].executeDefaultTrigger('onScreenUpdate')
def handlePlugInputDevice(self):
if not self.environment['runtime']['screenManager'].isSuspendingScreen(): # remove if all works
self.environment['runtime']['inputManager'].updateInputDevices()
def handleHeartBeat(self):
self.environment['runtime']['commandManager'].executeDefaultTrigger('onHeartBeat')
self.handlePlugInputDevice()
#self.environment['runtime']['outputManager'].brailleText(flush=False)
def prepareCommand(self):
if self.environment['runtime']['screenManager'].isSuspendingScreen():
self.wasCommand = False

View File

@ -46,13 +46,15 @@ class driver():
def shutdown(self):
if not self._initialized:
return
def inputWatchdog(self, iDevicesFD):
def inputWatchdog(self,active , iDevicesFD):
deviceFd = []
for fd in iDevicesFD['dev']:
deviceFd.append(fd)
while self.watchDog.value == 0:
if active.value == 0:
return
time.sleep(0.01)
r, w, x = select(deviceFd, [], [], 3)
r, w, x = select(deviceFd, [], [], 2)
self.watchDog.value = 0
def getInputEvent(self):
if not self.hasIDevices():

View File

@ -97,7 +97,7 @@ class driver():
self.env['runtime']['debug'].writeDebugOut('getSessionInformation: Maybe no LoginD:' + str(e),debug.debugLevel.ERROR)
self.env['screen']['autoIgnoreScreens'] = []
def updateWatchdog(self,eventQueue):
def updateWatchdog(self,active , eventQueue):
print('init VCSA updateWatchdog')
currScreen = '2'
vcsa = {}
@ -110,10 +110,9 @@ class driver():
watchdog = select.epoll()
watchdog.register(vcsa[currScreen], select.EPOLLPRI)
watchdog.register(tty, select.EPOLLPRI)
lastChange = 0
while True:
changes = watchdog.poll(3)
lastScreenContent = b''
while active.value == 1:
changes = watchdog.poll(2)
for change in changes:
fileno = change[0]
event = change[1]
@ -124,16 +123,19 @@ class driver():
watchdog.unregister(vcsa[ oldScreen ])
watchdog.register(vcsa[ currScreen ], select.EPOLLPRI)
oldScreen = currScreen
eventQueue.put({"Type":fenrirEventType.ScreenChanged,"Data":''})
eventQueue.put({"Type":fenrirEventType.ScreenChanged,"Data":''})
vcsa[currScreen].seek(0)
lastScreenContent = vcsa[currScreen].read()
else:
vcsa[currScreen].seek(0)
content = vcsa[currScreen].read()
print(time.time(), lastChange)
if time.time() - lastChange > 0.1:
screenContent = vcsa[currScreen].read()
if screenContent != lastScreenContent:
eventQueue.put({"Type":fenrirEventType.ScreenUpdate,"Data":''})
lastChange = time.time()
lastScreenContent = screenContent
def update(self, trigger='onUpdate'):
if trigger == 'onInput': # no need for an update on input for VCSA
return
newContentBytes = b''
try:
# read screen