dispatcher = Dispatcher() dispatcher.map("/obs/scene/*", scene_handler) dispatcher.map("/obs/audio/*", audio_handler) dispatcher.map("/obs/osc/*", osc_handler) # +======== ASYNC/THREADED EVENT LOOP ========+ loop = asyncio.get_event_loop() loop.stop() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) def ticker_loop(delta_time): loop.stop() loop.run_forever() return True def script_tick(delta_time): ticker_loop(delta_time) server = AsyncIOOSCUDPServer((IP, PORT), dispatcher, asyncio.get_event_loop()) server.serve() # Finally refresh scenes refresh_scenes()
def init_osc(loop): dispatcher = Dispatcher() dispatcher.map("/output_*", say_handler) osc_server = AsyncIOOSCUDPServer(('127.0.0.1', 12000), dispatcher, loop) osc_server.serve() # Calls 'run_until_complete' internally
def init_osc(loop): dispatcher = Dispatcher() dispatcher.map("/output_*", say_handler) osc_server = AsyncIOOSCUDPServer(('127.0.0.1', 12000), dispatcher, loop) osc_server.serve() # Calls 'run_until_complete' internally
class Network: # def __init__(self, listen_port, ip, port): # self.client = -1 self.server = -1 # printer reference self.printer = None # self.InitNetwork(ip, port, listen_port) # network binds the printer to update nozzle poz def Bind(self, p: Printer): self.printer = p # def InitNetwork(self, client_ip, client_port, listen_port): # client self.client = udp_client.SimpleUDPClient(client_ip, client_port) PrintManager("Connecting client on port {}.".format(client_port), 4) # server callbacks dispatch = Dispatcher() dispatch.map("/move*", self.MoveNozzle) # /move/extrude and /move dispatch.map("/extrude", self.ExtractMaterial) dispatch.map("/retract", self.RetractMaterial) dispatch.map("/req/nozzle_pos", self.RequestNozzlePosition) dispatch.set_default_handler( lambda _: PrintManager("Received message.", 1)) # TODO: Add other functions # server loop = asyncio.get_event_loop() self.server = AsyncIOOSCUDPServer(("127.0.0.1", listen_port), dispatch, asyncio.get_event_loop()) PrintManager("Serving on {}:{}".format("127.0.0.1", listen_port), 4) self.server.serve() # start the event loop for server self.osc_thread = threading.Thread(target=loop.run_forever) self.osc_thread.daemon = True self.osc_thread.start() # # Send message to processing def SendMessage(self, name, message): self.client.send_message(name, message) # client.send_message("/filter1", [1., 2.]) # # Processing Callbacks # def isPrinterConnected(self): return (self.printer is not None and self.printer.isconnected) def MoveNozzle(self, identifier, *args): if self.isPrinterConnected(): speed = self.printer.print_speed try: speed = float(args[3]) except: pass self.printer.print_speed = speed if identifier == "/move/extrude": self.printer.UpdatePosition(args[0], args[1], args[2], args[3], True) # extrude=True else: self.printer.UpdatePosition(args[0], args[1], args[2], args[3]) # PrintManager("{} x={}, y={}, z={}".format(identifier, args[0], args[1], args[2]), 1) # def ExtractMaterial(self, identifier, *args): if self.isPrinterConnected(): self.printer.Extract() # def RetractMaterial(self, identifier, *args): if self.isPrinterConnected(): self.printer.Retract() # def RequestNozzlePosition(self, identifier, *args): if self.isPrinterConnected(): self.printer.UpdateNozzlePosition()