Skip to content

Commit d34df02

Browse files
COR-5953: syncWithHeadsetClock API call before inject markers (#240)
* COR-5953: syncWithHeadsetClock API call before inject markers * COR-5953: fix comments by Copilot
1 parent 34179bb commit d34df02

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

python/cortex.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
REFRESH_HEADSET_LIST_ID = 25
5757
QUERY_RECORDS_ID = 26
5858
REQUEST_DOWNLOAD_RECORDS_ID = 27
59+
SYNC_WITH_HEADSET_CLOCK_ID = 28
5960

6061
#define error_code
6162
ERR_PROFILE_ACCESS_DENIED = -32046
@@ -89,7 +90,7 @@ class Cortex(Dispatcher):
8990
'warn_record_post_processing_done', 'query_records_done', 'request_download_records_done',
9091
'inject_marker_done', 'update_marker_done', 'export_record_done', 'new_data_labels',
9192
'new_com_data', 'new_fe_data', 'new_eeg_data', 'new_mot_data', 'new_dev_data',
92-
'new_met_data', 'new_pow_data', 'new_sys_data']
93+
'new_met_data', 'new_pow_data', 'new_sys_data', 'sync_with_headset_clock_done']
9394
def __init__(self, client_id, client_secret, debug_mode=False, **kwargs):
9495
"""
9596
Initialize a Cortex instance with authentication and configuration options.
@@ -220,6 +221,7 @@ def _get_result_handler(self, req_id):
220221
EXPORT_RECORD_ID: self._handle_export_record,
221222
INJECT_MARKER_REQUEST_ID: self._handle_inject_marker_request,
222223
UPDATE_MARKER_REQUEST_ID: self._handle_update_marker_request,
224+
SYNC_WITH_HEADSET_CLOCK_ID: self._handle_sync_with_headset_clock
223225
}
224226
return handlers.get(req_id)
225227

@@ -420,6 +422,9 @@ def _handle_inject_marker_request(self, result_dic):
420422
def _handle_update_marker_request(self, result_dic):
421423
self.emit('update_marker_done', data=result_dic['marker'])
422424

425+
def _handle_sync_with_headset_clock(self, result_dic):
426+
self.emit('sync_with_headset_clock_done', data=result_dic)
427+
423428
def handle_error(self, recv_dic):
424429
req_id = recv_dic['id']
425430
print('handle_error: request Id ' + str(req_id))
@@ -1091,6 +1096,29 @@ def refresh_headset_list(self):
10911096

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

1099+
def sync_with_headset_clock(self, headset_id=None):
1100+
print('sync with headset clock --------------------------------')
1101+
1102+
# Use instance headset_id if not provided
1103+
if headset_id is None:
1104+
headset_id = self.headset_id
1105+
1106+
sync_request = {
1107+
"jsonrpc": "2.0",
1108+
"id": SYNC_WITH_HEADSET_CLOCK_ID, # Using next available ID
1109+
"method": "syncWithHeadsetClock",
1110+
"params": {
1111+
"headset": headset_id,
1112+
"systemTime": time.time(),
1113+
"monotonicTime": time.monotonic()
1114+
}
1115+
}
1116+
1117+
if self.debug:
1118+
print('sync with headset clock request \n', json.dumps(sync_request, indent=4))
1119+
1120+
self.ws.send(json.dumps(sync_request))
1121+
10941122
# -------------------------------------------------------------------
10951123
# -------------------------------------------------------------------
10961124
# -------------------------------------------------------------------

python/marker.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def __init__(self, app_client_id, app_client_secret, **kwargs):
1212
self.c.bind(export_record_done=self.on_export_record_done)
1313
self.c.bind(inform_error=self.on_inform_error)
1414
self.c.bind(warn_record_post_processing_done=self.on_warn_record_post_processing_done)
15+
self.c.bind(sync_with_headset_clock_done=self.on_sync_with_headset_clock_done)
16+
self.headset_clock_adjustment = 0
1517

1618
def start(self, number_markers=10, headset_id=''):
1719
"""
@@ -73,7 +75,7 @@ def export_record(self, folder, stream_types, export_format, record_ids,
7375
def add_markers(self):
7476
print('add_markers: ' + str(self.number_markers) + ' markers will be injected each second automatically.')
7577
for m in range(self.number_markers):
76-
marker_time = time.time()*1000
78+
marker_time = (time.monotonic() + self.headset_clock_adjustment) * 1000
7779
print('add marker at : ', marker_time)
7880

7981
# marker_value = "test marker value"
@@ -112,6 +114,14 @@ def update_marker(self, marker_id, time, **kwargs):
112114
def on_create_session_done(self, *args, **kwargs):
113115
print('on_create_session_done')
114116

117+
# sync with headset clock before creating record
118+
self.c.sync_with_headset_clock()
119+
120+
def on_sync_with_headset_clock_done(self, *args, **kwargs):
121+
print('on_sync_with_headset_clock_done')
122+
data = kwargs.get('data') or {}
123+
self.headset_clock_adjustment = data.get('adjustment', 0)
124+
print(f'Headset clock adjustment: {self.headset_clock_adjustment} s')
115125
# create a record
116126
self.create_record(self.record_title, description=self.record_description)
117127

0 commit comments

Comments
 (0)