blc2 - Python library and frontend for running theatrical lighting and SFX. Written for the English 2041F fall 2019 theatre production of "The Cenci".

git clone https://benconnors.ca/git-repos/blc2

Log | Files | Refs

dummyserver.py (1973B) - raw


      1 #!/usr/bin/env python3
      2 
      3 import array
      4 import socket
      5 import threading
      6 import time
      7 
      8 from tkinter import *
      9 from tkinter.ttk import *
     10 
     11 CHANNEL_COUNT = 64
     12 
     13 class Main(Frame):
     14     def _update_display(self):
     15         with self.lock:
     16             for i, s in zip(self.channels, self.sliders):
     17                 s.config(state=NORMAL)
     18                 s.set(i)
     19                 s.config(state=DISABLED)
     20 
     21         self.master.after(16, self._update_display)
     22 
     23     
     24     def update(self, b: bytes):
     25         with self.lock:
     26             self.channels = array.array('B', b)
     27 
     28     def __init__(self, root):
     29         super().__init__(root)
     30 
     31         self.sliders = []
     32         self.rowconfigure(0, weight=1)
     33         for i in range(CHANNEL_COUNT):
     34             self.columnconfigure(i, weight=1)
     35             self.sliders.append(Scale(self, from_=255, to=0, orient=VERTICAL, state=DISABLED))
     36             self.sliders[-1].grid(row=0, column=i, sticky=N+E+S+W)
     37             Label(self, text=str(i+1)).grid(row=1, column=i, sticky=N+E+S+W)
     38 
     39         self.channels = array.array('B', (0 for i in range(CHANNEL_COUNT)))
     40         self.lock = threading.RLock()
     41 
     42         self.master.after(0, self._update_display)
     43 
     44 def handle_conn(conn, m):
     45     while True:
     46         a = conn.recv(1024)
     47         if not a:
     48             break
     49         m.update(a)
     50 
     51 def socket_main(m):
     52     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
     53         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,  1)
     54         s.bind(("", 6969))
     55         s.listen()
     56         print("Listening")
     57         while True:
     58             conn, addr = s.accept()
     59             threading.Thread(target=handle_conn, args=(conn, m,)).start()
     60 
     61 if __name__ == "__main__":
     62     root = Tk()
     63     root.rowconfigure(0, weight=1)
     64     root.columnconfigure(0, weight=1)
     65 
     66     root.wm_title("Lighting Output")
     67 
     68     main = Main(root)
     69     main.grid(row=0, column=0, sticky=N+E+S+W)
     70 
     71     root.after(0, threading.Thread(target=socket_main, args=(main,)).start)
     72 
     73     main.mainloop()