Simon Zeyer 65f3a5c1da refactoring as gui application
:# Please enter the commit message for your changes. Lines starting
2021-12-29 15:14:33 +01:00

148 lines
5.0 KiB
Python

# Import the required libraries
import logging
from tkinter import *
from pystray import MenuItem as item
import pystray
from PIL import Image, ImageTk
import threading
from functools import partial
import time
from mediacontrol import MediaControl
from ledcontroll import BLACK, LedControl, RED1, GREEN
from swyxcontrol import SwyxControl, LineState
from workstationcontrol import LockscreenMonitor, SessionEvent
class Gui():
win: Tk
t_icon: threading.Thread
strvar_swyx_connected_status: StringVar
strvar_screen_locked: StringVar
strvar_line_inactive: StringVar
strvar_play_state: StringVar
on_quit = None
def set_swyx_status(self, string):
self.strvar_swyx_connected_status.set(string)
# Define a function for quit the window
def quit_window(self, icon, item = None):
self.sc.stop()
self.wm.stop()
self.mc.stop()
icon.stop()
self.win.deiconify()
self.win.quit()
# Define a function to show the window again
def show_window(self, icon, item):
self.win.deiconify()
# Hide the window and show on the system taskbar
def hide_window(self):
self.win.withdraw()
def handle_lockscreen(self, event: SessionEvent):
if event == SessionEvent.SESSION_LOCK:
logging.debug("locked, pause")
self.mc.pause()
self.strvar_screen_locked.set("Locked")
if event == SessionEvent.SESSION_UNLOCK:
if self.sc.all_lines_inactive:
logging.debug("unlocked, global inactive, play")
time.sleep(3)
self.mc.play()
self.strvar_screen_locked.set("Unlocked")
def handle_line_state_changed(self, line):
if not line.DispState == LineState.Inactive:
if line.DispState == LineState.Ringing:
self.lc.show_all(GREEN)
else:
self.lc.show_all(RED1)
self.mc.pause()
self.strvar_play_state.set("try pause")
self.strvar_line_inactive.set("False")
if self.sc.all_lines_inactive:
self.strvar_line_inactive.set("True")
self.lc.show_all(BLACK)
if not self.wm.screen_locked():
self.mc.play()
self.strvar_play_state.set("try play")
def handle_phone_mgr_connected(self):
if self.sc.all_lines_inactive:
self.strvar_line_inactive.set("True")
self.lc.show_all(BLACK)
return
self.strvar_line_inactive.set("False")
def handle_media_control_init(self):
playback_info = self.mc.get_playback_info()
if playback_info is None:
print(playback_info)
self.strvar_play_state.set("no session active")
def __init__(self):
self.load_gui()
self.sc = SwyxControl()
self.sc.name = "SwyxControl"
self.sc.add_phone_mgr_connected_handler(self.handle_phone_mgr_connected)
self.sc.add_line_state_changed_handler(self.handle_line_state_changed)
self.sc.start()
self.lc = LedControl('COM3')
self.mc = MediaControl()
self.mc.name = "MediaControl"
self.mc.add_init_handler(self.handle_media_control_init)
self.mc.start()
self.wm = LockscreenMonitor()
self.wm.name = "LockscreenMonitor"
self.wm.register_handler(self.handle_lockscreen)
self.strvar_screen_locked.set("Locked" if self.wm.screen_locked() else "Unlocked")
self.wm.start()
self.win.protocol('WM_DELETE_WINDOW', self.hide_window)
image=Image.open("favicon.ico")
menu=(item('Beenden', self.quit_window), item('Anzeigen', self.show_window))
self.icon=pystray.Icon("name", image, "Swyx Media Controll", menu)
self.t_icon = threading.Thread(target=self.icon.run)
self.t_icon.name = "icon_thread"
self.t_icon.start()
self.win.mainloop()
def load_gui(self):
self.win=Tk()
# Create an instance of tkinter frame or window
self.win.title("Swyx Media Controll")
# Set the size of the window
self.win.geometry("350x150")
self.win.resizable(False, False)
## Screen Lock
Label(self.win, text="Screen Lock: ", font=("Helvetica", 10)).place(x=10, y=10)
self.strvar_screen_locked = StringVar(self.win)
Label(self.win, textvariable=self.strvar_screen_locked, font=("Helvetica", 10)).place(x=100, y=10)
## Line Inactive
Label(self.win, text="Line Inactive: ", font=("Helvetica", 10)).place(x=10, y=30)
self.strvar_line_inactive = StringVar(self.win)
Label(self.win, textvariable=self.strvar_line_inactive, font=("Helvetica", 10)).place(x=100, y=30)
## Play State
Label(self.win, text="Play State: ", font=("Helvetica", 10)).place(x=10, y=50)
self.strvar_play_state = StringVar(self.win)
Label(self.win, textvariable=self.strvar_play_state, font=("Helvetica", 10)).place(x=100, y=50)
if __name__ == "__main__":
gui = Gui()