def __init__(self, bus_addr=1, com_port=None, baud=115200):
super(PjonPiperClient, self).__init__()
self._pipe = None
self._bus_addr = bus_addr
self._serial_baud = baud
self._piper_client_stdin_queue = Queue()
self._piper_client_stdout_queue = Queue()
self._receiver_function = self.dummy_receiver
self._error_function = self.dummy_error
self._last_watchdog_poll_ts = 0
self._piper_stdout_watchdog_timeout = 0
self._piper_stdout_last_received_ts = 0
if sys.platform == 'win32':
self._startupinfo = subprocess.STARTUPINFO()
self._startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
self._startupinfo.wShowWindow = subprocess.SW_HIDE
self._pjon_piper_path = os.path.join(self.get_self_path(), 'pjon_piper_bin', 'win', 'PJON-piper.exe')
elif sys.platform == 'linux2':
#os.setpgrp()
if(self.is_arm_platform()):
if self.is_raspberry():
self._pjon_piper_path = os.path.join(self.get_self_path(), 'pjon_piper_bin', 'rpi',
'pjon_piper')
#print(self._pjon_piper_path)
else:
NotImplementedError("Only Linux on Raspberry is supported")
else:
raise NotImplementedError("this version of Linux is not supported yet")
else:
raise NotImplementedError("platform not supported; currently provided support only for: win32")
if sys.platform == 'win32':
self._pipier_client_subproc_cmd = "%s %s %s %s\n" % (self._pjon_piper_path, com_port.strip(), baud, bus_addr)
elif sys.platform == 'linux2':
self._pipier_client_subproc_cmd = [self._pjon_piper_path, com_port.strip(), str(baud), str(bus_addr)]
if com_port is None:
raise ComPortUndefinedExc("missing com_port kwarg: serial port name is required")
available_coms = self.get_coms()
if com_port not in available_coms:
raise ComPortNotAvailableExc("com port %s is not available in this system; available ports are: %s" % (com_port, str(available_coms)))
else:
log.info("COM OK: %s" % com_port)
self._pipier_client_watchdog = WatchDog(suproc_command=self._pipier_client_subproc_cmd,
stdin_queue=self._piper_client_stdin_queue,
stdout_queue=self._piper_client_stdout_queue,
parent = self)
self._packets_processor = ReceivedPacketsProcessor(self)
atexit.register(self.stop_client)
# TODO:
# 3. implement periodic checks if com is available
# if not: restart watchdog (can be a permanent restart; no state machine required)
python类SW_HIDE的实例源码
def checkVPNCommand(addon):
# Issue the openvpn command and see if the output is a bunch of commands
if not fakeConnection():
p = getPlatform()
# Issue the openvpn command, expecting to get the options screen back
if p == platforms.RPI or p == platforms.LINUX:
# Issue Linux command
command = getOpenVPNPath() + " > " + getVPNLogFilePath() + " &"
if useSudo() : command = "sudo " + command
infoTrace("platform.py", "Testing openvpn with : " + command)
os.system(command)
elif p == platforms.WINDOWS:
# Issue Windows command
command=getOpenVPNPath()
infoTrace("platform.py", "Testing openvpn with : " + command)
args = shlex.split(command)
outfile = open(getVPNLogFilePath(),'w')
proc = subprocess.Popen(args, stdout=outfile, creationflags=subprocess.SW_HIDE, shell=True)
else:
errorTrace("platform.py", "Unsupported platform " + str(p))
# **** ADD MORE PLATFORMS HERE ****
# Waiting for the log file to appear
xbmc.sleep(1000)
i = 0
while not xbmcvfs.exists(getVPNLogFilePath()) and i < 10:
xbmc.sleep(1000)
i = i + 1
# If the log file appears, check it's what we expect
if xbmcvfs.exists(getVPNLogFilePath()):
log_file = open(getVPNLogFilePath(), 'r')
log_file_lines = log_file.readlines()
log_file.close()
# Look for a phrase we'd expect to see if the call
# worked and the list of options was displayed
for line in log_file_lines:
if "General Options" in line:
return True
# Write the log file in case there's something in it
errorTrace("platform.py", "Ran openvpn command and it failed")
writeVPNLog()
dialog_msg = "The OpenVPN executable isn't working. Check the log, then from a command line prompt type 'openvpn' and fix any problems reported."
else:
errorTrace("platform.py", "Ran openvpn command and VPN log didn't appear")
dialog_msg = "The OpenVPN executable isn't writing out a log. Try changing the Kodi log directory setting in Settings-Debug menu and retry."
# Display an error message
xbmcgui.Dialog().ok(addon.getAddonInfo("name"), dialog_msg)
return False
else: return True
def isVPNTaskRunning():
# Return True if the VPN task is still running, or the VPN connection is still active
# Return False if the VPN task is no longer running and the connection is not active
if fakeConnection(): return True
p = getPlatform()
if p == platforms.LINUX or p == platforms.RPI:
try:
command = "pidof openvpn"
if useSudo() : command = "sudo " + command
debugTrace("(Linux) Checking VPN task with " + command)
pid = os.system(command)
# This horrible call returns 0 if it finds a process, it's not returning the PID number
if xbmcaddon.Addon("service.vpn.manager").getSetting("alt_pid_check") == "true":
if pid > 0 : return True
else:
if pid == 0 : return True
debugTrace("(Linux) Didn't find a running process")
return False
except Exception as e:
errorTrace("platform.py", "VPN task list failed")
errorTrace("platform.py", str(e))
return False
if p == platforms.WINDOWS:
try:
command = 'tasklist /FI "IMAGENAME eq OPENVPN.EXE"'
debugTrace("(Windows) Checking VPN task with " + command)
args = shlex.split(command)
out = subprocess.check_output(args, creationflags=subprocess.SW_HIDE, shell=True).strip()
if "openvpn.exe" in out:
return True
else:
debugTrace("(Windows) Didn't find a running process")
return False
except Exception as e:
errorTrace("platform.py", "VPN task list failed")
errorTrace("platform.py", str(e))
return False
# **** ADD MORE PLATFORMS HERE ****
return False
def StartProcess(executable_name, clangd_log_path=None):
if not clangd_log_path or not log.logger.isEnabledFor(log.DEBUG):
clangd_log_path = os.devnull
fdClangd = open(clangd_log_path, 'w+')
# fix executable file name under windows (both cygwin and native win32)
if sys_platform == 'msys' or sys_platform == 'win32':
if not executable_name.endswith('.exe'):
executable_name += '.exe'
# apply platform-specific hacks
if sys_platform != 'win32':
# for posix or cygwin
fdInRead, fdInWrite = Pipe()
fdOutRead, fdOutWrite = Pipe()
SetCloseOnExec(fdInWrite)
SetCloseOnExec(fdOutRead)
else:
# only native win32
fdInRead, fdInWrite = Win32SocketPair()
fdOutRead, fdOutWrite = Win32SocketPair()
cwd = os.path.dirname(executable_name)
# apply native win32's hack
if sys_platform == 'win32':
# we need hide this subprocess's window under windows, or it opens a new visible window
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
clangd = Popen(
executable_name,
stdin=fdInRead,
stdout=fdOutWrite,
stderr=fdClangd,
cwd=cwd,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
startupinfo=startupinfo)
else:
clangd = Popen(
executable_name,
stdin=fdInRead,
stdout=fdOutWrite,
stderr=fdClangd,
cwd=cwd)
return clangd, fdInWrite, fdOutRead, fdClangd