Skip to content

Commit

Permalink
COR-5099-update-scanning-flow-python-example (#213)
Browse files Browse the repository at this point in the history
* COR-5099-update-scanning-flow

* COR-5099 update comment
  • Loading branch information
sondhemotiv authored Nov 15, 2023
1 parent 5a955a3 commit 9f9bbbd
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion python/cortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
GET_CORTEX_INFO_ID = 22
UPDATE_MARKER_REQUEST_ID = 23
UNSUB_REQUEST_ID = 24
REFRESH_HEADSET_LIST_ID = 25

#define error_code
ERR_PROFILE_ACCESS_DENIED = -32046
Expand All @@ -56,7 +57,7 @@
HEADSET_CONNECTED = 104
HEADSET_CANNOT_WORK_WITH_BTLE = 112
HEADSET_CANNOT_CONNECT_DISABLE_MOTION = 113

HEADSET_SCANNING_FINISHED = 142

class Cortex(Dispatcher):

Expand All @@ -73,6 +74,7 @@ def __init__(self, client_id, client_secret, debug_mode=False, **kwargs):
self.debug = debug_mode
self.debit = 10
self.license = ''
self.isHeadsetConnected = False

if client_id == '':
raise ValueError('Empty your_app_client_id. Please fill in your_app_client_id before running the example.')
Expand Down Expand Up @@ -160,6 +162,8 @@ def handle_result(self, recv_dic):
elif req_id == AUTHORIZE_ID:
print("Authorize successfully.")
self.auth = result_dic['cortexToken']
#After successful authorization, the app will call the API refresh headset list for the first time
self.refresh_headset_list()
# query headsets
self.query_headset()
elif req_id == QUERY_HEADSET_ID:
Expand All @@ -176,6 +180,7 @@ def handle_result(self, recv_dic):
headset_status = status

if len(self.headset_list) == 0:
self.isHeadsetConnected = False
warnings.warn("No headset available. Please turn on a headset.")
elif self.headset_id == '':
# set first headset is default headset
Expand All @@ -186,6 +191,7 @@ def handle_result(self, recv_dic):
warnings.warn("Can not found the headset " + self.headset_id + ". Please make sure the id is correct.")
elif found_headset == True:
if headset_status == 'connected':
self.isHeadsetConnected = True
# create session with the headset
self.create_session()
elif headset_status == 'discovered':
Expand Down Expand Up @@ -328,6 +334,11 @@ def handle_warning(self, warning_dic):
if session_id == self.session_id:
self.emit('warn_cortex_stop_all_sub', data=session_id)
self.session_id = ''
elif warning_code == HEADSET_SCANNING_FINISHED:
# After headset scanning finishes, if no headset is connected yet, the app should call the controlDevice("refresh") again
# We recommend the app should NOT call controlDevice("refresh") when a headset is connected, to have the best data stream quality.
if (self.isHeadsetConnected == False):
self.refresh_headset_list()

def handle_stream_data(self, result_dic):
if result_dic.get('com') != None:
Expand Down Expand Up @@ -892,6 +903,21 @@ def get_mental_command_training_threshold(self, profile_name):
print('get mental command training threshold \n', json.dumps(training_threshold_request, indent=4))
self.ws.send(json.dumps(training_threshold_request))

def refresh_headset_list(self):
print('refresh headset list --------------------------------')
refresh_request = {
"jsonrpc": "2.0",
"id": REFRESH_HEADSET_LIST_ID,
"method": "controlDevice",
"params": {
"command": "refresh"
}
}
if self.debug:
print('controlDevice refresh request \n', json.dumps(refresh_request, indent=4))

self.ws.send(json.dumps(refresh_request, indent=4))

# -------------------------------------------------------------------
# -------------------------------------------------------------------
# -------------------------------------------------------------------

0 comments on commit 9f9bbbd

Please sign in to comment.