add first trigger

This commit is contained in:
chrys
2016-07-12 23:09:11 +02:00
parent 0fb493e391
commit 6f84710b6f
11 changed files with 144 additions and 35 deletions

View File

@ -7,17 +7,26 @@ import time
import re
#import fenrir.utils.debug
class screenManager():
class screen():
def __init__(self, device='/dev/vcsa'):
self.vcsaDevicePath = device
self.textWrapper = textwrap.TextWrapper()
self.textWrapper.drop_whitespace = False
def analyzeScreen(self, environment):
# set new "old" values
environment['screenData']['oldContentBytes'] = environment['screenData']['newContentBytes']
environment['screenData']['oldContentText'] = environment['screenData']['newContentText']
environment['screenData']['oldContentTextAttrib'] = environment['screenData']['newContentAttrib']
environment['screenData']['oldCursor']['x'] = environment['screenData']['newCursor']['x']
environment['screenData']['oldCursor']['y'] = environment['screenData']['newCursor']['y']
environment['screenData']['oldTTY'] = environment['screenData']['newTTY']
environment['screenData']['oldDelta'] = environment['screenData']['newDelta']
# read screen
currTTY = open('/sys/devices/virtual/tty/tty0/active','r')
environment['screenData']['newTTY'] = currTTY.read()[3:-1]
currTTY.close()
try:
vcsa = open(self.vcsaDevicePath + environment['screenData']['newTTY'] ,'rb',0)
environment['screenData']['newContentBytes'] = vcsa.read()
@ -35,10 +44,7 @@ class screenManager():
# analyze content
environment['screenData']['newContentText'] = str(environment['screenData']['newContentBytes'][4:][::2].decode('WINDOWS-1250'))
#environment['screenData']['newContentText'] = str(environment['screenData']['newContentBytes'][4:][::2].decode('cp1252')).encode('utf-8')[2:]
environment['screenData']['newContentAttrib'] = environment['screenData']['newContentBytes'][5:][::2]
# environment['screenData']['newContentText'] = '\n'.join(textwrap.wrap(environment['screenData']['newContentText'], environment['screenData']['columns']))[:-2]
#environment['screenData']['newContentText'] = re.sub("(.{"+ str(environment['screenData']['columns'])+"})", "\\1\n", str(environment['screenData']['newContentText']), 0, re.DOTALL)
environment['screenData']['newContentText'] = '\n'.join(self.textWrapper.wrap(environment['screenData']['newContentText'], ))[:-2]
if environment['screenData']['newTTY'] != environment['screenData']['oldTTY']:
@ -48,20 +54,13 @@ class screenManager():
environment['screenData']['oldContentText'] = ''
environment['screenData']['oldCursor']['x'] = 0
environment['screenData']['oldCursor']['y'] = 0
environment['runtime']['speechDriver'].cancel()
environment['screenData']['oldDelta'] = ''
environment['screenData']['newDelta'] = ''
# changes on the screen
if (environment['screenData']['oldContentText'] != environment['screenData']['newContentText']) and \
(len(environment['screenData']['newContentText']) > 0):
diff = difflib.ndiff(" ".join(environment['screenData']['oldContentText'].split()), " ".join(environment['screenData']['newContentText'].split()))
environment['screenData']['delta'] = ''.join(x[2:] for x in diff if x.startswith('+ '))
if ((len(environment['screenData']['delta']) == 1)):
environment['runtime']['speechDriver'].cancel()
environment['runtime']['speechDriver'].speak(environment['screenData']['delta'])
# set new "old" values
environment['screenData']['oldContentBytes'] = environment['screenData']['newContentBytes']
environment['screenData']['oldContentText'] = environment['screenData']['newContentText']
environment['screenData']['oldContentTextAttrib'] = environment['screenData']['newContentAttrib']
environment['screenData']['oldCursor']['x'] = environment['screenData']['newCursor']['x']
environment['screenData']['oldCursor']['y'] = environment['screenData']['newCursor']['y']
environment['screenData']['oldTTY'] = environment['screenData']['newTTY']
environment['screenData']['newDelta'] = ''.join(x[2:] for x in diff if x.startswith('+ '))
return environment