Skip to content

Commit

Permalink
WLM control by task handler (#27)
Browse files Browse the repository at this point in the history
This resolves #6.

I couldn't test these features since it needs a WLM device, so please
check the codes carefully.
  • Loading branch information
BECATRUE authored Nov 20, 2024
2 parents b096497 + 62587a1 commit 9437393
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13"]
python-version: ["3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ python-decouple==3.8
pylint==3.3.*
pylint-django==2.6.*
djangorestframework==3.15.*
pylablib==1.4.*
55 changes: 50 additions & 5 deletions wlm_server/task/handler.py
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.
Expand All @@ -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()

def _close_connection(self):
self._wlm.stop_measurement()
self._wlm.close()

def _start_channel_measurement(self, channel: int):
setting = Setting.objects.filter(channel__name=channel).order_by('-created_at').first()
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:
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
9 changes: 9 additions & 0 deletions wlm_server/task/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ def pop(self) -> MeasureInfo | None:
except IndexError:
return None
return item[1]

def remove(self, channel: int):
"""Removes the measurement of the given channel from the measurement queue.
Args:
channel: Target channel.
"""
self._queue = [item for item in self._queue if item[1].channel != channel]
heapq.heapify(self._queue)

0 comments on commit 9437393

Please sign in to comment.