initial watchdog for VCSA driver

This commit is contained in:
chrys 2017-06-22 15:08:15 +02:00
parent b0604a9907
commit a476e730c4
3 changed files with 84 additions and 46 deletions

23
src/fenrir/core/eventData.py Executable file
View File

@ -0,0 +1,23 @@
#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
from core import debug
from enum import Enum
class fenrirEventType(Enum):
Ignore = 0
StopMainLoop = 1
ScreenUpdate = 2
KeyboardInput = 3
BrailleInput = 4
PlugInputDevice = 5
BrailleFlush = 6
ScreenChanged = 7
HeartBeat = 8 # for time based scheduling
def __int__(self):
return self.value
def __str__(self):
return self.name

View File

@ -5,29 +5,13 @@
# By Chrys, Storm Dragon, and contributers.
from core import debug
from core.eventData import fenrirEventType
from queue import Empty
import time
from enum import Enum
from multiprocessing import Process, Queue
from multiprocessing.sharedctypes import Value
from ctypes import c_bool
class fenrirEventType(Enum):
Ignore = 0
StopMainLoop = 1
ScreenUpdate = 2
KeyboardInput = 3
BrailleInput = 4
PlugInputDevice = 5
BrailleFlush = 6
ScreenChanged = 7
HeartBeat = 8 # for time based scheduling
def __int__(self):
return self.value
def __str__(self):
return self.name
class eventManager():
def __init__(self):
self._mainLoopRunning = Value(c_bool, True)
@ -42,7 +26,7 @@ class eventManager():
self.cleanEventQueue()
def timerProcess(self):
try:
time.sleep(0.03)
time.sleep(1.3)
except:
pass
#self.env['runtime']['settingsManager'].getSettingAsFloat('screen', 'screenUpdateDelay')
@ -69,10 +53,13 @@ class eventManager():
print('stop')
return
elif event['Type'] == fenrirEventType.ScreenUpdate:
print('do an update')
pass
self.env['runtime']['fenrirManager'].handleProcess()
print(self._eventQueue.qsize())
print('ScreenUpdate')
elif event['Type'] == fenrirEventType.KeyboardInput:
pass
self.env['runtime']['fenrirManager'].handleProcess()
print(self._eventQueue.qsize())
print('KeyboardInput')
elif event['Type'] == fenrirEventType.BrailleInput:
pass
elif event['Type'] == fenrirEventType.PlugInputDevice:
@ -80,14 +67,19 @@ class eventManager():
elif event['Type'] == fenrirEventType.BrailleFlush:
pass
elif event['Type'] == fenrirEventType.ScreenChanged:
pass
self.env['runtime']['fenrirManager'].handleProcess()
print(self._eventQueue.qsize())
print('ScreenChanged')
elif event['Type'] == fenrirEventType.HeartBeat:
self.env['runtime']['fenrirManager'].handleProcess()
print(self._eventQueue.qsize())
print('HeartBeat at ' + str(event['Type']) + ' ' +str(event['Data'] ))
print('HeartBeat at {0} {1}'.format(event['Type'], event['Data'] ))
def isMainEventLoopRunning(self):
return self._mainLoopRunning.value == 1
def startMainEventLoop(self):
self._mainLoopRunning.value = 1
while(self._mainLoopRunning.value == 1):
while( self.isMainEventLoopRunning()):
st = time.time()
self.proceedEventLoop()
print('ALL loop ' + str(time.time() - st))
@ -97,7 +89,7 @@ class eventManager():
self._eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None})
def addCustomEventThread(self, function):
self._mainLoopRunning.value = 1
t = Process(target=self.eventWorkerThread, args=(q, function))
t = Process(target=self.customEventWorkerThread, args=(self._eventQueue, function))
self._eventProcesses.append(t)
t.start()
def addSimpleEventThread(self, event, function):
@ -119,11 +111,11 @@ class eventManager():
self._eventQueue.put({"Type":event,"Data":data})
return True
def customEventWorkerThread(self, eventQueue, function):
if not isinstance(eventQueue, Queue):
return
#if not isinstance(eventQueue, Queue):
# return
if not callable(function):
return
while self._mainLoopRunning.value:
while self.isMainEventLoopRunning():
try:
function(eventQueue)
except Exception as e:
@ -134,7 +126,7 @@ class eventManager():
return
if not callable(function):
return
while self._mainLoopRunning.value == 1:
while self.isMainEventLoopRunning():
Data = None
try:
Data = function()
@ -144,20 +136,3 @@ class eventManager():
self.putToEventQueue(event, Data)
if runOnce:
break
'''
def p():
time.sleep(0.02)
return("p")
i = 1
e = eventManager()
e.addEventThread(fenrirEventType.ScreenUpdate,p)
e.addEventThread(fenrirEventType.BrailleInput,p)
e.addEventThread(fenrirEventType.PlugInputDevice,p)
e.addEventThread(fenrirEventType.ScreenChanged,p)
time.sleep(1.5)
e.addEventThread(fenrirEventType.StopMainLoop,e.stopMainEventLoop)
s = time.time()
e.startMainEventLoop()
print(time.time() - s )
'''

View File

@ -10,8 +10,10 @@ import subprocess
import fcntl
import termios
import time
import select
import dbus
from core import debug
from core.eventData import fenrirEventType
from utils import screen_utils
class driver():
@ -20,6 +22,7 @@ class driver():
self.ListSessions = None
def initialize(self, environment):
self.env = environment
self.env['runtime']['eventManager'].addCustomEventThread(self.updateWatchdog)
def shutdown(self):
pass
def getCurrScreen(self):
@ -93,7 +96,44 @@ class driver():
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('getSessionInformation: Maybe no LoginD:' + str(e),debug.debugLevel.ERROR)
self.env['screen']['autoIgnoreScreens'] = []
def updateWatchdog(self,eventQueue):
print('init updateWatchdog')
currScreen = '2'
vcsa = {}
for i in range(1,7):
vcsa[str(i)] = open('/dev/vcsa'+str(i),'rb')
tty = open('/sys/devices/virtual/tty/tty0/active','r')
currScreen = str(tty.read()[3:-1])
oldScreen = currScreen
watchdog = select.epoll()
watchdog.register(vcsa[currScreen], select.EPOLLPRI)
watchdog.register(tty, select.EPOLLPRI)
while True:
changes = watchdog.poll()
print('-----------------------------')
print(changes)
for change in changes:
fileno = change[0]
event = change[1]
print(change,fileno, tty.fileno())
if fileno == tty.fileno():
tty.seek(0)
currScreen = str(tty.read()[3:-1])
if currScreen != oldScreen:
watchdog.unregister(vcsa[ oldScreen ])
watchdog.register(vcsa[ currScreen ], select.EPOLLPRI)
oldScreen = currScreen
eventQueue.put({"Type":fenrirEventType.ScreenChanged,"Data":''})
print('new screen '+ currScreen)
else:
vcsa[currScreen].seek(0)
content = vcsa[currScreen].read()
eventQueue.put({"Type":fenrirEventType.ScreenUpdate,"Data":''})
print('update '+ str(time.time()))
def update(self, trigger='onUpdate'):
newContentBytes = b''
try: