This commit is contained in:
yumoqing 2021-01-24 11:17:29 +08:00
parent b9778f226a
commit ae0194c62b

View File

@ -1,4 +1,5 @@
from functools import partial
import json import json
from pythonosc import dispatcher, osc_server, udp_client from pythonosc import dispatcher, osc_server, udp_client
from appPublic.sockPackage import get_free_local_addr from appPublic.sockPackage import get_free_local_addr
@ -7,22 +8,22 @@ from kivy.event import EventDispatcher
class PlayerOSCServer(EventDispatcher): class PlayerOSCServer(EventDispatcher):
def __init__(self,playerid): def __init__(self,playerid, cmds=[]):
EventDispatcher.__init__(self) EventDispatcher.__init__(self)
self.playerid = playerid self.playerid = playerid
dispatch = dispatcher.Dispatcher() dispatch = dispatcher.Dispatcher()
self.ip,self.port = get_free_local_addr() self.ip,self.port = get_free_local_addr()
# self.server = osc_server.ThreadingOSCUDPServer( (self.ip, self.port), dispatch)
self.server = osc_server.BlockingOSCUDPServer( (self.ip, self.port), dispatch) self.server = osc_server.BlockingOSCUDPServer( (self.ip, self.port), dispatch)
self.osc_dispatch = dispatch self.osc_dispatch = dispatch
self.commands = [] self.commands = cmds
self.register_event_type('on_osc_event') for cmd in self.commands:
self.map(cmd)
def on_osc_event(self,*args): def on_osc_event(self, cmd, *args):
print('PlayerOSCServer():on_osc_event():args=',args) print('PlayerOSCServer():on_osc_event():cmd=', cmd, 'args=',args)
def action_event(self,*args): def action_event(self,cmd,*args):
self.dispatch('on_osc_event',*args) self.dispatch('on_%s' % cmd, *args)
def start(self): def start(self):
self.thread = Background(self.server.serve_forever) self.thread = Background(self.server.serve_forever)
@ -38,14 +39,23 @@ class PlayerOSCServer(EventDispatcher):
} }
def map(self,cmd): def map(self,cmd):
self.commands.append(cmd) event_name = 'on_%s' % cmd
self.osc_dispatch.map(cmd,self.action_event) on_f = partial(self.on_osc_event, cmd)
setattr(self,event_name, on_f)
self.register_event_type(event_name)
self.osc_dispatch.map( '/%s' % cmd,partial(self.action_event, cmd))
def stop(self): def stop(self):
self.server.shutdown() self.server.shutdown()
self.server.server_close() self.server.server_close()
self.thread.join(5) self.thread.join(5)
def send_message(self, api, data, ip, port):
client = udp_client.SimpleUDPClient(ip, port) # Create client
t = json.dumps(data)
client.send_message('/%s' % api, t)
class PlayerOSCClient: class PlayerOSCClient:
def __init__(self, ip,port): def __init__(self, ip,port):
self.client = udp_client.SimpleUDPClient(ip, port) # Create client self.client = udp_client.SimpleUDPClient(ip, port) # Create client