pismotek-remote/pismotek-remote.py

146 lines
4.6 KiB
Python
Raw Normal View History

2024-05-26 14:47:45 -06:00
#!/bin/env python
2024-05-26 16:58:18 -06:00
import os
2024-05-26 20:49:53 -06:00
import psutil
2024-05-26 14:47:45 -06:00
import PySimpleGUI as sg
2024-05-26 20:49:53 -06:00
import subprocess
import sys
2024-05-26 20:49:53 -06:00
import time
2024-05-26 14:47:45 -06:00
"""
Pismotek-Remote - Remote Desktop Application
To "swap out" a portion of a window, use a Column element for that portion. Add multiple Columns
on the same row and make only 1 of them active at a time
Will determine if tvnserver is a running service or application
If it is not running it will be run in application mode
2024-05-26 14:47:45 -06:00
"""
# ----------- Utility functions -----------
2024-05-26 16:58:18 -06:00
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def get_service(name):
""" return information about services """
service = None
try:
service = psutil.win_service_get(name)
service = service.as_dict()
except Exception as ex:
pass
return service
def build_layout():
logo = resource_path("logo.png")
# ----------- Create the 3 layouts this Window will display -----------
layout1 = [
[sg.Push(), sg.Image(logo), sg.Push()],
[sg.Text('This program allows Will B. to remotely control your')],
[sg.Text('computer through the internet')],
[sg.Text('Is Will waiting for you - *right now* - to run this program?')],
]
layout2 = [
[sg.Text('Remote connection started')],
[sg.Text('Allow connection through firewall if asked.')],
[sg.Text('')],
[sg.Text('Click Exit when finished')],
]
layout3 = [
[sg.Text('Please run this program ONLY when Will is waiting for you')],
[sg.Text('Click Exit to continue')],
]
# ----------- Create actual layout using Columns and a row of Buttons
layout = [
[
sg.Column(layout1, key='-COL1-'),
sg.Column(layout2, visible=False, key='-COL2-'),
sg.Column(layout3, visible=False, key='-COL3-')
],
[sg.VPush()],
2024-05-27 02:30:04 -06:00
[sg.Push(), sg.Button('No'), sg.Button('Yes', focus=True), sg.Button('Exit', visible=False)]
]
return layout
2024-05-26 14:47:45 -06:00
def tab_no(window):
""" handle displaying the no tab/column """
window['-COL1-'].update(visible=False)
window['-COL3-'].update(visible=True)
window['No'].update(visible=False)
window['Yes'].update(visible=False)
window['Exit'].update(visible=True)
2024-05-27 02:30:04 -06:00
def tab_yes(window, tvn_mode, tvn_state):
""" handle displaying the yes tab/column"""
window['-COL1-'].update(visible=False)
window['-COL2-'].update(visible=True)
window['No'].update(visible=False)
window['Yes'].update(visible=False)
window['Exit'].update(visible=True)
2024-05-27 02:30:04 -06:00
window.perform_long_operation(lambda : vncconnect(tvn_mode, tvn_state), '-VNCCONNECT-')
def tvn_get_state():
""" get current state of tvnserver - retrun stopped if does not exist """
state = "stopped"
service = get_service('tvnserver')
if service:
state = service["status"]
return state
def get_tvnserver_info():
"""
determine the state of tvnserver.exe
service running - can use as is
application running - can use as is
application stopped - need to start application
"""
2024-05-27 02:30:04 -06:00
mode = "-controlapp"
state = tvn_get_state()
if state == "running":
2024-05-27 02:30:04 -06:00
mode = "-controlservice"
elif "tvnserver.exe" in (p.name() for p in psutil.process_iter()):
state = "running"
return mode, state
2024-05-27 02:30:04 -06:00
def vncconnect(tvn_mode, tvn_state):
""" start tvnserver if needed, kill any connection and attempt to connect """
app = resource_path("tvnserver.exe")
# print(tvn_mode, tvn_state, app)
if tvn_state == "stopped":
print("Starting Service")
# subprocess.call([app, "-run"], shell=True)
sg.execute_command_subprocess(app, *["-run"], wait=False)
time.sleep(3)
# print("Disconnectiong")
sg.execute_command_subprocess(app, *[tvn_mode, "-disconnectall"], wait=False)
time.sleep(3)
# print("Connecting")
sg.execute_command_subprocess(app, *[tvn_mode, "-connect 192.168.0.94"], wait=False)
def main():
tvn_mode, tvn_state = get_tvnserver_info()
2024-05-27 02:30:04 -06:00
icon_file=resource_path('remotedesktop.ico')
layout = build_layout()
2024-05-27 02:30:04 -06:00
window = sg.Window('Pismotek', layout, size=(380, 350), icon=icon_file)
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event == 'No':
tab_no(window)
elif event == 'Yes':
2024-05-27 02:30:04 -06:00
tab_yes(window, tvn_mode, tvn_state)
# window.close()
if __name__ == "__main__":
main()