fenrir/src/fenrirscreenreader/core/remoteManager.py

160 lines
6.2 KiB
Python
Raw Normal View History

2018-09-04 16:32:03 -04:00
#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
2018-09-05 17:11:42 -04:00
'''
Remote controll:
start delimiter = #<=>#
category=settings,command
setting actions:
- set (Parameter = settings)
- reset
command actions:
- exec (Parameter1 = Command, Parameter2 = Command Parameters)
- cancel
structure:
#<=>#category##action[##Parameter1##Parameter2]
settings:
#<=>#settings##set##section#setting=value[,section#setting=value]
#<=>#settings##set##speech#voice=de
#<=>#settings##reset
execute command:
#<=>#command##exec#say##this is a test
#<=>#command##cancel##say
'''
2018-09-04 16:32:03 -04:00
from fenrirscreenreader.core import debug
2018-09-05 13:13:20 -04:00
from fenrirscreenreader.core.eventData import fenrirEventType
2018-09-05 16:27:56 -04:00
import time
import select
2018-09-05 17:03:07 -04:00
import socket
import os, os.path
2018-09-04 16:32:03 -04:00
class remoteManager():
def __init__(self):
pass
def initialize(self, environment):
2018-09-05 16:27:56 -04:00
self.env = environment
2018-09-05 17:03:07 -04:00
2018-09-05 16:27:56 -04:00
if self.env['runtime']['settingsManager'].getSettingAsBool('remote', 'enabled'):
if self.env['runtime']['settingsManager'].getSetting('remote', 'method') == 'unix':
self.env['runtime']['processManager'].addCustomEventThread(self.unixSocketWatchDog, multiprocess=True)
elif self.env['runtime']['settingsManager'].getSetting('remote', 'method') == 'tcp':
self.env['runtime']['processManager'].addCustomEventThread(self.tcpWatchDog, multiprocess=True)
2018-09-04 16:32:03 -04:00
def shutdown(self):
2018-09-05 16:27:56 -04:00
if self.sock:
self.sock.close()
self.sock = None
def unixSocketWatchDog(self, active, eventQueue):
2018-09-05 17:03:07 -04:00
# echo "#<=>#command##exec#say##this is a test" | socat - UNIX-CLIENT:/tmp/fenrir-deamon.sock
2018-09-06 17:54:56 -04:00
# socket daemon
# /run/user/<uid>/fenrirscreenreader/daemon
# socket pty
# /run/user/<uid>/fenrirscreenreader/ptyX
socketpath = '/tmp/fenrir-deamon.sock'
if os.path.exists(socketpath):
os.remove(socketpath)
2018-09-05 17:03:07 -04:00
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
2018-09-06 17:54:56 -04:00
self.sock.bind(socketpath)
2018-09-05 17:03:07 -04:00
self.sock.listen(1)
2018-09-05 16:27:56 -04:00
while active.value == 1:
2018-09-05 17:03:07 -04:00
client_sock, client_addr = self.sock.accept()
if client_sock:
# Check if the client is still connected and if data is available:
try:
r, w, e = select.select([client_sock,], [], [])
except select.error:
return
if len(r) > 0:
rawdata = client_sock.recv(8129)
try:
2018-09-05 17:11:42 -04:00
data = rawdata.decode("utf-8").rstrip().lstrip()
2018-09-05 17:03:07 -04:00
if data.startswith('#<=>#'):
eventQueue.put({"Type":fenrirEventType.RemoteIncomming,
"Data": data
})
except:
pass
client_sock.close()
if self.sock:
self.sock.close()
self.sock = None
2018-09-05 16:27:56 -04:00
def tcpWatchDog(self, active, eventQueue):
2018-09-05 17:03:07 -04:00
# echo "#<=>#command##exec#say##this is a test" | nc localhost 22447
2018-09-06 17:54:56 -04:00
# port should be configureable
2018-09-05 16:27:56 -04:00
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.host = '0.0.0.0'
self.port = 22447
self.sock.bind((self.host, self.port))
self.sock.listen(1)
while active.value == 1:
client_sock, client_addr = self.sock.accept()
if client_sock:
# Check if the client is still connected and if data is available:
try:
r, w, e = select.select([client_sock,], [], [])
except select.error:
return
if len(r) > 0:
rawdata = client_sock.recv(8129)
try:
2018-09-05 17:11:42 -04:00
data = rawdata.decode("utf-8").rstrip().lstrip()
2018-09-05 17:03:07 -04:00
if data.startswith('#<=>#'):
2018-09-05 16:27:56 -04:00
eventQueue.put({"Type":fenrirEventType.RemoteIncomming,
"Data": data
})
except:
pass
client_sock.close()
if self.sock:
self.sock.close()
self.sock = None
def handleSettingsChange(self, settingsText):
if not self.env['runtime']['settingsManager'].getSettingAsBool('remote', 'enableSettingsRemote'):
return
if settingsText.startswith('set##'):
parameterText = settingsText[len('set##'):]
self.setSettings(parameterText)
if settingsText.startswith('reset'):
self.resetSettings()
def handleCommandExecution(self, commandText):
if not self.env['runtime']['settingsManager'].getSettingAsBool('remote', 'enableCommandRemote'):
return
if commandText.startswith('exec#say##'):
parameterText = commandText[len('exec#say##'):]
self.execSay(parameterText)
if commandText.startswith('cancel##say'):
self.execInterruptSpeech()
def execSay(self, text):
self.env['runtime']['outputManager'].speakText(text)
def execInterruptSpeech(self):
self.env['runtime']['outputManager'].interruptOutput()
def resetSettings(self):
self.env['runtime']['settingsManager'].resetSettingArgDict()
def setSettings(self, settingsArgs):
self.env['runtime']['settingsManager'].parseSettingArgs(settingsArgs)
2018-09-05 13:13:20 -04:00
def handleRemoteIncomming(self, eventData):
if not eventData:
return
2018-09-05 17:11:42 -04:00
# examples
2018-09-05 16:27:56 -04:00
# settings:
2018-09-05 17:03:07 -04:00
# #<=>#settings##set##section#setting=value[,section#setting=value]
# #<=>#settings##set##speech#voice=de
# #<=>#settings##reset
2018-09-05 16:27:56 -04:00
# execute command:
2018-09-05 17:03:07 -04:00
# #<=>#command##exec#say##this is a test
# #<=>#command##cancel##say
if not eventData.startswith('#<=>#'):
2018-09-05 16:27:56 -04:00
return
2018-09-05 17:03:07 -04:00
if eventData.startswith('#<=>#settings##'):
settingsText = eventData[len('#<=>#settings##'):]
2018-09-05 16:27:56 -04:00
self.handleSettingsChange(settingsText)
2018-09-05 17:03:07 -04:00
elif eventData.startswith('#<=>#command##'):
commandText = eventData[len('#<=>#command##'):]
2018-09-05 16:27:56 -04:00
self.handleCommandExecution(commandText)