-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WLM control by task handler #27
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b28a472
Add dependency for `pylablib`
BECATRUE a4f890b
Not test Pylint for Python 3.13
BECATRUE 247e1c8
Open WLM connection
BECATRUE f6946f2
Handle `CLOSE` action
BECATRUE 9b63dff
Start/Stop measurement in handling connection
BECATRUE 3d531b9
Remove unnecessary Pylint comment
BECATRUE 6e1877f
Start measurement of a certain channel
BECATRUE da9110d
Stop measurement of a certain channel
BECATRUE 5e65c1e
Set period of a certain channel
BECATRUE 414aee3
Set exposure time
BECATRUE d9bd9f7
Fix import for `now()`
BECATRUE 62587a1
Apply reviews
BECATRUE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ python-decouple==3.8 | |
pylint==3.3.* | ||
pylint-django==2.6.* | ||
djangorestframework==3.15.* | ||
pylablib==1.4.* |
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,12 +1,15 @@ | ||
"""Module for task handler with WLM.""" | ||
|
||
import threading | ||
from datetime import timedelta | ||
from datetime import datetime, timedelta | ||
|
||
from django.conf import settings | ||
from pylablib.devices.HighFinesse.wlm import WLM | ||
|
||
from .message import MessageQueue | ||
from .measure import MeasureQueue | ||
from config.models import Config | ||
from setting.models import Setting | ||
from .message import ActionType, MessageQueue | ||
from .measure import MeasureInfo, MeasureQueue | ||
|
||
class TaskHandler(threading.Thread): | ||
"""Task handler for controlling and monitoring WLM. | ||
|
@@ -21,12 +24,54 @@ class TaskHandler(threading.Thread): | |
|
||
def __init__(self): | ||
super().__init__() | ||
self._wlm: WLM | ||
self._message_queue: MessageQueue = settings.MESSAGE_QUEUE | ||
self._measure_queue: MeasureQueue = MeasureQueue() | ||
self._channel_to_period: dict[int, timedelta] = {} | ||
self._open_connection() | ||
|
||
def _open_connection(self): | ||
config = Config.objects.first() | ||
self._wlm = WLM(config.wlm_version, config.wlm_dll_path, config.wlm_app_path) | ||
self._wlm.open() | ||
self._wlm.start_measurement() | ||
kangz12345 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def _close_connection(self): | ||
self._wlm.stop_measurement() | ||
self._wlm.close() | ||
|
||
def _start_channel_measurement(self, channel: int): | ||
kangz12345 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setting = Setting.objects.filter(channel__name=channel).order_by('-created_at').first() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep this in mind: the filtering might cost quite a lot in the future. |
||
period = setting.period | ||
self._channel_to_period[channel] = period | ||
measure = MeasureInfo(channel, datetime.now()) | ||
self._measure_queue.push(measure) | ||
|
||
def _stop_channel_measurement(self, channel: int): | ||
self._measure_queue.remove(channel) | ||
|
||
def _set_channel_exposure(self, channel: int, exposure: timedelta): | ||
self._wlm.set_exposure(exposure=exposure.total_seconds(), channel=channel) | ||
|
||
def run(self): | ||
while True: | ||
while (message := self._message_queue.pop()) is not None: # pylint: disable=unused-variable | ||
pass | ||
while (message := self._message_queue.pop()) is not None: | ||
channel = message.channel | ||
data = message.data | ||
match message.action: | ||
kangz12345 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case ActionType.CLOSE: | ||
self._close_connection() | ||
return | ||
case ActionType.OPERATE: | ||
on = data['on'] | ||
if on: | ||
self._start_channel_measurement(channel) | ||
else: | ||
self._stop_channel_measurement(channel) | ||
case ActionType.EXPOSURE: | ||
exposure = data['exposure'] | ||
self._set_channel_exposure(channel, exposure) | ||
case ActionType.PERIOD: | ||
period = data['period'] | ||
self._channel_to_period[channel] = period | ||
measure = self._measure_queue.pop() # pylint: disable=unused-variable |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method may cause several errors, e.g., there is no
Config
, failed to open WLM.In fact, regarding WLM, it is always possible to fail the API call as the USB connection goes wrong sometimes.
I think we should carefully handle such error cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe modeling this handler as an FSM would be helpful..?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Umm.. Indeed, I didn't consider any error on purpose.
To send the occurred error info to the request handler, we need to implement more APIs, hence I'd like to handle this after implementing main features.
What do you think about this? @kangz12345
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright then, for now the structure might vary in the future, so let's handle the errors later.