-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
748ef90
commit 200cba5
Showing
17 changed files
with
496 additions
and
359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from tornado.websocket import WebSocketHandler | ||
import tornado | ||
import json | ||
|
||
|
||
class CustomWebSocketHandler(WebSocketHandler): | ||
def open(self): | ||
self.write_message(json.dumps({"status": "connected"})) | ||
self.set_nodelay(True) | ||
# Start a periodic callback to send data every 50ms | ||
self.callback = tornado.ioloop.PeriodicCallback(self.send_data, 1000) | ||
self.callback.start() | ||
|
||
def on_message(self, message): | ||
message_data = json.loads(message) | ||
# Update the periodic callback frequency | ||
new_frequency = message_data["updateFrequency"] | ||
if hasattr(self, "callback"): | ||
self.callback.stop() | ||
self.callback = tornado.ioloop.PeriodicCallback( | ||
self.send_data, new_frequency | ||
) | ||
if not message_data["isPaused"]: | ||
self.callback.start() | ||
|
||
def on_close(self): | ||
if hasattr(self, "callback") and self.callback.is_running(): | ||
self.callback.stop() | ||
|
||
def send_data(self): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,107 @@ | ||
import { ISettingRegistry } from '@jupyterlab/settingregistry'; | ||
import { SetStateAction, useEffect } from 'react'; | ||
import { DEFAULT_UPDATE_FREQUENCY, PLUGIN_ID_CONFIG } from './constants'; | ||
import { SetStateAction, useEffect, useRef } from 'react'; | ||
import { | ||
DEFAULT_MAX_RECORDS_TIMESERIES, | ||
DEFAULT_UPDATE_FREQUENCY, | ||
PLUGIN_ID_CONFIG | ||
} from './constants'; | ||
import { connectToWebSocket } from '../handler'; | ||
|
||
function loadSettingRegistry( | ||
settingRegistry: ISettingRegistry, | ||
setUpdateFrequency: { | ||
(value: SetStateAction<number>): void; | ||
(arg0: number): void; | ||
/** | ||
* Updates the settings for update frequency and maximum records for time series charts. | ||
*/ | ||
const updateSettings = ( | ||
settings: ISettingRegistry.ISettings, | ||
setUpdateFrequency: (value: SetStateAction<number>) => void, | ||
setMaxRecords?: (value: SetStateAction<number>) => void | ||
) => { | ||
setUpdateFrequency( | ||
(settings.get('updateFrequency').composite as number) || | ||
DEFAULT_UPDATE_FREQUENCY | ||
); | ||
if (setMaxRecords) { | ||
setMaxRecords( | ||
(settings.get('maxTimeSeriesDataRecords').composite as number) || | ||
DEFAULT_MAX_RECORDS_TIMESERIES | ||
); | ||
} | ||
) { | ||
}; | ||
|
||
/** | ||
* Loads the setting registry and updates the settings accordingly. | ||
*/ | ||
export const loadSettingRegistry = ( | ||
settingRegistry: ISettingRegistry, | ||
setUpdateFrequency: (value: SetStateAction<number>) => void, | ||
setIsSettingsLoaded: (value: SetStateAction<boolean>) => void, | ||
setMaxRecords?: (value: SetStateAction<number>) => void | ||
) => { | ||
useEffect(() => { | ||
const loadSettings = async () => { | ||
try { | ||
const settings = await settingRegistry.load(PLUGIN_ID_CONFIG); | ||
const loadedUpdateFrequency = | ||
(settings.get('updateFrequency').composite as number) || | ||
DEFAULT_UPDATE_FREQUENCY; | ||
setUpdateFrequency(loadedUpdateFrequency); | ||
|
||
updateSettings(settings, setUpdateFrequency, setMaxRecords); | ||
settings.changed.connect(() => { | ||
setUpdateFrequency( | ||
(settings.get('updateFrequency').composite as number) || | ||
DEFAULT_UPDATE_FREQUENCY | ||
); | ||
updateSettings(settings, setUpdateFrequency, setMaxRecords); | ||
}); | ||
setIsSettingsLoaded(true); | ||
} catch (error) { | ||
console.error(`An error occurred while loading settings: ${error}`); | ||
} | ||
}; | ||
loadSettings(); | ||
}, []); | ||
} | ||
}; | ||
|
||
/** | ||
* Custom hook to establish a WebSocket connection and handle incoming messages. | ||
*/ | ||
export const useWebSocket = <T>( | ||
endpoint: string, | ||
isPaused: boolean, | ||
updateFrequency: number, | ||
processData: (response: T, isPaused: boolean) => void, | ||
isSettingsLoaded: boolean | ||
) => { | ||
const wsRef = useRef<WebSocket | null>(null); | ||
|
||
useEffect(() => { | ||
if (!isSettingsLoaded) { | ||
return; | ||
} | ||
|
||
wsRef.current = connectToWebSocket(endpoint); | ||
const ws = wsRef.current; | ||
|
||
ws.onopen = () => { | ||
console.log('WebSocket connected'); | ||
}; | ||
|
||
export default loadSettingRegistry; | ||
ws.onmessage = event => { | ||
const response = JSON.parse(event.data); | ||
if (response.status !== 'connected') { | ||
processData(response, isPaused); | ||
} else { | ||
ws.send(JSON.stringify({ updateFrequency, isPaused })); | ||
} | ||
}; | ||
|
||
ws.onerror = error => { | ||
console.error('WebSocket error:', error); | ||
}; | ||
|
||
ws.onclose = () => { | ||
console.log('WebSocket disconnected'); | ||
}; | ||
|
||
return () => { | ||
ws.close(); | ||
}; | ||
}, [isSettingsLoaded]); | ||
|
||
useEffect(() => { | ||
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) { | ||
wsRef.current.send(JSON.stringify({ updateFrequency, isPaused })); | ||
} | ||
}, [isPaused, updateFrequency]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.