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

View File

@ -47,15 +47,10 @@ class fenrirManager():
if not self.initialized: if not self.initialized:
return return
self.environment['runtime']['eventManager'].startMainEventLoop() self.environment['runtime']['eventManager'].startMainEventLoop()
self.shutdown() self.shutdown()
def handleInput(self):
def handleProcess(self):
eventReceived = self.environment['runtime']['inputManager'].getInputEvent() eventReceived = self.environment['runtime']['inputManager'].getInputEvent()
startTime = time.time() startTime = time.time()
if not eventReceived:
if not self.environment['runtime']['screenManager'].isSuspendingScreen():
self.environment['runtime']['inputManager'].updateInputDevices()
if eventReceived: if eventReceived:
self.prepareCommand() self.prepareCommand()
if not (self.wasCommand or self.environment['general']['tutorialMode']) or self.environment['runtime']['screenManager'].isSuspendingScreen(): if not (self.wasCommand or self.environment['general']['tutorialMode']) or self.environment['runtime']['screenManager'].isSuspendingScreen():
@ -70,21 +65,37 @@ class fenrirManager():
self.environment['input']['keyForeward'] -=1 self.environment['input']['keyForeward'] -=1
self.environment['runtime']['screenManager'].update('onInput') self.environment['runtime']['screenManager'].update('onInput')
self.environment['runtime']['commandManager'].executeDefaultTrigger('onInput') self.environment['runtime']['commandManager'].executeDefaultTrigger('onInput')
else: self.handleCommands()
def handleScreenChange(self):
self.environment['runtime']['screenManager'].update('onUpdate') self.environment['runtime']['screenManager'].update('onUpdate')
if self.environment['runtime']['applicationManager'].isApplicationChange(): if self.environment['runtime']['applicationManager'].isApplicationChange():
self.environment['runtime']['commandManager'].executeDefaultTrigger('onApplicationChange') self.environment['runtime']['commandManager'].executeDefaultTrigger('onApplicationChange')
self.environment['runtime']['commandManager'].executeSwitchTrigger('onSwitchApplicationProfile', \ self.environment['runtime']['commandManager'].executeSwitchTrigger('onSwitchApplicationProfile', \
self.environment['runtime']['applicationManager'].getPrevApplication(), \ self.environment['runtime']['applicationManager'].getPrevApplication(), \
self.environment['runtime']['applicationManager'].getCurrentApplication()) self.environment['runtime']['applicationManager'].getCurrentApplication())
if self.environment['runtime']['screenManager'].isScreenChange(): if not self.environment['runtime']['screenManager'].isScreenChange():
self.environment['runtime']['commandManager'].executeDefaultTrigger('onScreenChanged') self.environment['runtime']['commandManager'].executeDefaultTrigger('onScreenChanged')
else:
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') 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) #self.environment['runtime']['outputManager'].brailleText(flush=False)
self.handleCommands()
#print(time.time()-startTime)
def prepareCommand(self): def prepareCommand(self):
if self.environment['runtime']['screenManager'].isSuspendingScreen(): if self.environment['runtime']['screenManager'].isSuspendingScreen():

View File

@ -46,13 +46,15 @@ class driver():
def shutdown(self): def shutdown(self):
if not self._initialized: if not self._initialized:
return return
def inputWatchdog(self, iDevicesFD): def inputWatchdog(self,active , iDevicesFD):
deviceFd = [] deviceFd = []
for fd in iDevicesFD['dev']: for fd in iDevicesFD['dev']:
deviceFd.append(fd) deviceFd.append(fd)
while self.watchDog.value == 0: while self.watchDog.value == 0:
if active.value == 0:
return
time.sleep(0.01) time.sleep(0.01)
r, w, x = select(deviceFd, [], [], 3) r, w, x = select(deviceFd, [], [], 2)
self.watchDog.value = 0 self.watchDog.value = 0
def getInputEvent(self): def getInputEvent(self):
if not self.hasIDevices(): 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['runtime']['debug'].writeDebugOut('getSessionInformation: Maybe no LoginD:' + str(e),debug.debugLevel.ERROR)
self.env['screen']['autoIgnoreScreens'] = [] self.env['screen']['autoIgnoreScreens'] = []
def updateWatchdog(self,eventQueue): def updateWatchdog(self,active , eventQueue):
print('init VCSA updateWatchdog') print('init VCSA updateWatchdog')
currScreen = '2' currScreen = '2'
vcsa = {} vcsa = {}
@ -110,10 +110,9 @@ class driver():
watchdog = select.epoll() watchdog = select.epoll()
watchdog.register(vcsa[currScreen], select.EPOLLPRI) watchdog.register(vcsa[currScreen], select.EPOLLPRI)
watchdog.register(tty, select.EPOLLPRI) watchdog.register(tty, select.EPOLLPRI)
lastChange = 0 lastScreenContent = b''
while True: while active.value == 1:
changes = watchdog.poll(2)
changes = watchdog.poll(3)
for change in changes: for change in changes:
fileno = change[0] fileno = change[0]
event = change[1] event = change[1]
@ -125,15 +124,18 @@ class driver():
watchdog.register(vcsa[ currScreen ], select.EPOLLPRI) watchdog.register(vcsa[ currScreen ], select.EPOLLPRI)
oldScreen = currScreen oldScreen = currScreen
eventQueue.put({"Type":fenrirEventType.ScreenChanged,"Data":''}) eventQueue.put({"Type":fenrirEventType.ScreenChanged,"Data":''})
vcsa[currScreen].seek(0)
lastScreenContent = vcsa[currScreen].read()
else: else:
vcsa[currScreen].seek(0) vcsa[currScreen].seek(0)
content = vcsa[currScreen].read() screenContent = vcsa[currScreen].read()
print(time.time(), lastChange) if screenContent != lastScreenContent:
if time.time() - lastChange > 0.1:
eventQueue.put({"Type":fenrirEventType.ScreenUpdate,"Data":''}) eventQueue.put({"Type":fenrirEventType.ScreenUpdate,"Data":''})
lastChange = time.time() lastScreenContent = screenContent
def update(self, trigger='onUpdate'): def update(self, trigger='onUpdate'):
if trigger == 'onInput': # no need for an update on input for VCSA
return
newContentBytes = b'' newContentBytes = b''
try: try:
# read screen # read screen