Skip to content

Commit

Permalink
Add emulator GUI server client
Browse files Browse the repository at this point in the history
  • Loading branch information
ckaraneen committed Apr 10, 2021
1 parent 929b704 commit a92dc05
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions pybpodapi/bpod/emulator/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
"""
__author__ = "Chris Karageorgiou Kaneen"

import time
import math
import logging
import math
import queue
import time

from . import constants as const
from .state import State
from AnyQt import QtCore
from AnyQt import QtNetwork
from pybpodapi.bpod_modules.bpod_modules import BpodModules

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -60,17 +62,21 @@ def wrapper(*args, **kwargs):
class Emulator:
"""A class for emulating the Bpod device."""

GUI_PLUGIN_SERVER_NAME = 'emulator_gui_server'

_GLOBAL_TIMER_START_OFFSET = None
_GLOBAL_TIMER_END_OFFSET = None
_GLOBAL_COUNTER_OFFSET = None
_CONDITION_OFFSET = None
_JUMP_OFFSET = None
_CONDITION_EVENT_NAME = None
_SOCKET_WAIT_FOR_CONNECTED_TIMEOUT = 500

def __init__(self, hardware):
self._init_hardware(hardware)
self._state = State(self.hardware)
self._manual_override_events = queue.Queue()
self.socket = QtNetwork.QLocalSocket()

@property
def hardware(self):
Expand Down Expand Up @@ -209,6 +215,30 @@ def _set_global_timer_channel(self, channel, on):
if this_channel < FINAL_CHANNEL:
self._state.output[this_channel] = on

def _send_output_to_gui_server(self, channel_number, value):
self.socket.connectToServer(
self.GUI_PLUGIN_SERVER_NAME, QtCore.QIODevice.WriteOnly)
if self.socket.waitForConnected(
self._SOCKET_WAIT_FOR_CONNECTED_TIMEOUT):
output_channel_name = self._hardware.channels.output_channel_names[
channel_number]
message = f'{output_channel_name}:{value}'
self.socket.write(message.encode('utf-8'))
if not self.socket.waitForBytesWritten(2000):
error_message = \
f'Could not write to socket: {self.socket.errorString()}'
logger.error(error_message)
raise EmulatorError(error_message)
self.socket.disconnectFromServer()
elif self.socket.error() != \
QtNetwork.QAbstractSocket.HostNotFoundError:
error_message = \
f'Could not connect to server: {self.socket.errorString()}'
logger.error(error_message)
raise EmulatorError(error_message)
else:
logger.error('Emulator gui plugin server is down.')

def set_state_machine(self, state_machine):
self._state_machine = state_machine
logger.debug('State machine set.')
Expand Down Expand Up @@ -388,12 +418,13 @@ def mirror_state(self, state):
# TODO: Do the BpodSystem.RefreshGUI equivalent
else:
# Add outputs that have not been overridden to output state
for new_output_code, new_output_value in \
for new_output_channel, new_output_value in \
self._state_machine.output_matrix[state]:
if new_output_code not in self._state.output:
self._state.output[
new_output_code] = new_output_value
logger.debug('State mirrored.')
if new_output_channel not in self._state.output:
self._state.output[new_output_channel] = new_output_value
self._send_output_to_gui_server(
new_output_channel, new_output_value)
logger.debug('State mirrored.')

def mirror_events(self, events):
for event in events:
Expand Down

0 comments on commit a92dc05

Please sign in to comment.