Add support for protocol v1 (JD 2016-2019)
This commit is contained in:
		
							parent
							
								
									d84ad34434
								
							
						
					
					
						commit
						38badd22b1
					
				
					 4 changed files with 51 additions and 16 deletions
				
			
		
							
								
								
									
										15
									
								
								dance.py
									
										
									
									
									
								
							
							
						
						
									
										15
									
								
								dance.py
									
										
									
									
									
								
							|  | @ -13,7 +13,8 @@ from pyjoycon import ButtonEventJoyCon, JoyCon | ||||||
| from pyjoycon.constants import JOYCON_PRODUCT_IDS, JOYCON_VENDOR_ID | from pyjoycon.constants import JOYCON_PRODUCT_IDS, JOYCON_VENDOR_ID | ||||||
| 
 | 
 | ||||||
| from joydance import JoyDance, PairingState | from joydance import JoyDance, PairingState | ||||||
| from joydance.constants import DEFAULT_CONFIG, JOYDANCE_VERSION | from joydance.constants import (DEFAULT_CONFIG, JOYDANCE_VERSION, | ||||||
|  |                                 WsSubprotocolVersion) | ||||||
| 
 | 
 | ||||||
| logging.getLogger('asyncio').setLevel(logging.WARNING) | logging.getLogger('asyncio').setLevel(logging.WARNING) | ||||||
| 
 | 
 | ||||||
|  | @ -138,14 +139,24 @@ async def connect_joycon(app, ws, data): | ||||||
|     config_parser['joydance'] = config |     config_parser['joydance'] = config | ||||||
|     save_config(config_parser) |     save_config(config_parser) | ||||||
| 
 | 
 | ||||||
|  |     if pairing_method == PairingMethod.DEFAULT.value: | ||||||
|         app['joycons_info'][serial]['pairing_code'] = pairing_code |         app['joycons_info'][serial]['pairing_code'] = pairing_code | ||||||
|  |     else: | ||||||
|  |         app['joycons_info'][serial]['pairing_code'] = '' | ||||||
|  | 
 | ||||||
|     joycon = ButtonEventJoyCon(vendor_id, product_id, serial) |     joycon = ButtonEventJoyCon(vendor_id, product_id, serial) | ||||||
| 
 | 
 | ||||||
|     if pairing_method == PairingMethod.DEFAULT.value: |     if pairing_method == PairingMethod.DEFAULT.value: | ||||||
|         console_ip_addr = None |         console_ip_addr = None | ||||||
| 
 | 
 | ||||||
|  |     if pairing_method == PairingMethod.OLD.value: | ||||||
|  |         protocol_version = WsSubprotocolVersion.V1 | ||||||
|  |     else: | ||||||
|  |         protocol_version = WsSubprotocolVersion.V2 | ||||||
|  | 
 | ||||||
|     joydance = JoyDance( |     joydance = JoyDance( | ||||||
|             joycon, |             joycon, | ||||||
|  |             protocol_version=protocol_version, | ||||||
|             pairing_code=pairing_code, |             pairing_code=pairing_code, | ||||||
|             host_ip_addr=host_ip_addr, |             host_ip_addr=host_ip_addr, | ||||||
|             console_ip_addr=console_ip_addr, |             console_ip_addr=console_ip_addr, | ||||||
|  | @ -163,7 +174,7 @@ async def disconnect_joycon(app, ws, data): | ||||||
|     print(data) |     print(data) | ||||||
|     serial = data['joycon_serial'] |     serial = data['joycon_serial'] | ||||||
|     joydance = app['joydance_connections'][serial] |     joydance = app['joydance_connections'][serial] | ||||||
|     app['joycons_info'][serial]['state'] = PairingState.IDLE |     app['joycons_info'][serial]['state'] = PairingState.IDLE.value | ||||||
| 
 | 
 | ||||||
|     await joydance.disconnect() |     await joydance.disconnect() | ||||||
|     try: |     try: | ||||||
|  |  | ||||||
|  | @ -12,7 +12,8 @@ import websockets | ||||||
| from .constants import (ACCEL_ACQUISITION_FREQ_HZ, ACCEL_ACQUISITION_LATENCY, | from .constants import (ACCEL_ACQUISITION_FREQ_HZ, ACCEL_ACQUISITION_LATENCY, | ||||||
|                         ACCEL_MAX_RANGE, ACCEL_SEND_RATE, JOYCON_UPDATE_RATE, |                         ACCEL_MAX_RANGE, ACCEL_SEND_RATE, JOYCON_UPDATE_RATE, | ||||||
|                         SHORTCUT_MAPPING, UBI_APP_ID, UBI_SKU_ID, |                         SHORTCUT_MAPPING, UBI_APP_ID, UBI_SKU_ID, | ||||||
|                         WS_SUBPROTOCOL, Command, JoyConButton) |                         WS_SUBPROTOCOLS, Command, JoyConButton, | ||||||
|  |                         WsSubprotocolVersion) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class PairingState(Enum): | class PairingState(Enum): | ||||||
|  | @ -36,6 +37,7 @@ class JoyDance: | ||||||
|     def __init__( |     def __init__( | ||||||
|             self, |             self, | ||||||
|             joycon, |             joycon, | ||||||
|  |             protocol_version, | ||||||
|             pairing_code=None, |             pairing_code=None, | ||||||
|             host_ip_addr=None, |             host_ip_addr=None, | ||||||
|             console_ip_addr=None, |             console_ip_addr=None, | ||||||
|  | @ -45,6 +47,7 @@ class JoyDance: | ||||||
|             on_state_changed=None): |             on_state_changed=None): | ||||||
|         self.joycon = joycon |         self.joycon = joycon | ||||||
|         self.joycon_is_left = joycon.is_left() |         self.joycon_is_left = joycon.is_left() | ||||||
|  |         self.protocol_version = protocol_version | ||||||
| 
 | 
 | ||||||
|         if on_state_changed: |         if on_state_changed: | ||||||
|             self.on_state_changed = on_state_changed |             self.on_state_changed = on_state_changed | ||||||
|  | @ -271,7 +274,10 @@ class JoyDance: | ||||||
|             delta_time += (end - start) * 1000 |             delta_time += (end - start) * 1000 | ||||||
| 
 | 
 | ||||||
|     async def send_command(self): |     async def send_command(self): | ||||||
|         ''' Capture Joycon's input and send to console ''' |         ''' Capture Joycon's input and send to console. Only works on procol v2 ''' | ||||||
|  |         if self.protocol_version == WsSubprotocolVersion.V1: | ||||||
|  |             return | ||||||
|  | 
 | ||||||
|         while True: |         while True: | ||||||
|             try: |             try: | ||||||
|                 if self.disconnected: |                 if self.disconnected: | ||||||
|  | @ -348,6 +354,10 @@ class JoyDance: | ||||||
|                 await self.disconnect() |                 await self.disconnect() | ||||||
| 
 | 
 | ||||||
|     async def connect_ws(self): |     async def connect_ws(self): | ||||||
|  |         if self.protocol_version == WsSubprotocolVersion.V1: | ||||||
|  |             ssl_context = None | ||||||
|  |             server_hostname = None | ||||||
|  |         else: | ||||||
|             ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |             ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) | ||||||
|             ssl_context.set_ciphers('ALL') |             ssl_context.set_ciphers('ALL') | ||||||
|             ssl_context.options &= ~ssl.OP_NO_SSLv3 |             ssl_context.options &= ~ssl.OP_NO_SSLv3 | ||||||
|  | @ -355,10 +365,12 @@ class JoyDance: | ||||||
|             ssl_context.verify_mode = ssl.CERT_NONE |             ssl_context.verify_mode = ssl.CERT_NONE | ||||||
| 
 | 
 | ||||||
|             server_hostname = self.console_conn.getpeername()[0] if self.console_conn else None |             server_hostname = self.console_conn.getpeername()[0] if self.console_conn else None | ||||||
|  | 
 | ||||||
|  |         subprotocol = WS_SUBPROTOCOLS[self.protocol_version.value] | ||||||
|         try: |         try: | ||||||
|             async with websockets.connect( |             async with websockets.connect( | ||||||
|                     self.pairing_url, |                     self.pairing_url, | ||||||
|                     subprotocols=[WS_SUBPROTOCOL], |                     subprotocols=[subprotocol], | ||||||
|                     sock=self.console_conn, |                     sock=self.console_conn, | ||||||
|                     ssl=ssl_context, |                     ssl=ssl_context, | ||||||
|                     ping_timeout=None, |                     ping_timeout=None, | ||||||
|  | @ -391,6 +403,9 @@ class JoyDance: | ||||||
|         try: |         try: | ||||||
|             if self.console_ip_addr: |             if self.console_ip_addr: | ||||||
|                 await self.on_state_changed(self.joycon.serial, PairingState.CONNECTING) |                 await self.on_state_changed(self.joycon.serial, PairingState.CONNECTING) | ||||||
|  |                 if self.protocol_version == WsSubprotocolVersion.V1: | ||||||
|  |                     self.pairing_url = 'ws://{}:8080/smartphone'.format(self.console_ip_addr) | ||||||
|  |                 else: | ||||||
|                     self.pairing_url = 'wss://{}:8080/smartphone'.format(self.console_ip_addr) |                     self.pairing_url = 'wss://{}:8080/smartphone'.format(self.console_ip_addr) | ||||||
|             else: |             else: | ||||||
|                 await self.on_state_changed(self.joycon.serial, PairingState.GETTING_TOKEN) |                 await self.on_state_changed(self.joycon.serial, PairingState.GETTING_TOKEN) | ||||||
|  |  | ||||||
|  | @ -1,10 +1,18 @@ | ||||||
| from enum import Enum | from enum import Enum | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| JOYDANCE_VERSION = '0.1' | JOYDANCE_VERSION = '0.1' | ||||||
| UBI_APP_ID = '210da0fb-d6a5-4ed1-9808-01e86f0de7fb' | UBI_APP_ID = '210da0fb-d6a5-4ed1-9808-01e86f0de7fb' | ||||||
| UBI_SKU_ID = 'jdcompanion-android' | UBI_SKU_ID = 'jdcompanion-android' | ||||||
| WS_SUBPROTOCOL = 'v2.phonescoring.jd.ubisoft.com' | 
 | ||||||
|  | 
 | ||||||
|  | class WsSubprotocolVersion(Enum): | ||||||
|  |     V1 = 'v1', | ||||||
|  |     V2 = 'v2', | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | WS_SUBPROTOCOLS = {} | ||||||
|  | WS_SUBPROTOCOLS[WsSubprotocolVersion.V1.value] = 'v1.phonescoring.jd.ubisoft.com' | ||||||
|  | WS_SUBPROTOCOLS[WsSubprotocolVersion.V2.value] = 'v2.phonescoring.jd.ubisoft.com' | ||||||
| 
 | 
 | ||||||
| JOYCON_UPDATE_RATE = 0.02  # 50Hz | JOYCON_UPDATE_RATE = 0.02  # 50Hz | ||||||
| ACCEL_SEND_RATE = 40  # ms | ACCEL_SEND_RATE = 40  # ms | ||||||
|  |  | ||||||
|  | @ -138,7 +138,8 @@ class PrivateIpAddress extends Component { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     componentDidMount() { |     componentDidMount() { | ||||||
|         window.mitty.emit('update_addr', this.state.host_ip_addr) |         let addr = this.props.pairing_method == PairingMethod.DEFAULT ? this.state.host_ip_addr : this.state.console_ip_addr | ||||||
|  |         window.mitty.emit('update_addr', addr) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     render(props, state) { |     render(props, state) { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue